C++ Laguage



Admission Enquiry Form

  

Array and Pointer of an Object in C++:



Code for Array of an Object.

//Array of an object
#include<iostream>
using namespace std;
class Student
{
char name[20];
int rollno;
float fee;
public:
void input()
{
cout<<"Enter name ";
cin>>name;
cout<<"Enter rollno ";
cin>>rollno;
cout<<"Enter fee ";
cin>>fee;
}
void display()
{
cout<<"\nName is "<<name;
cout<<"\nRollno is "<<rollno;
cout<<"\nEnter fee "<<fee;
}
};//end of class

int main()
{
Student st[2];//array of an object
int i;
for(i=0;i<2;i++)
{
st[i].input();
st[i].display();
printf("\n\n");
}
}//end of main


Output:

Enter name riya
Enter rollno 1
Enter fee 10000

Name is riya
Rollno is 1
Enter fee 10000

Enter name priya
Enter rollno 2
Enter fee 10000

Name is priya
Rollno is 2
Enter fee 10000



Pointer of an Object.

//Pointer of an object
#include<iostream>
using namespace std;
class Student
{
char name[20];
int rollno;
float fee;
public:
void input()
{
cout<<"Enter name ";
cin>>name;
cout<<"Enter rollno ";
cin>>rollno;
cout<<"Enter fee ";
cin>>fee;
}
void display()
{
cout<<"\nName is "<<name;
cout<<"\nRollno is "<<rollno;
cout<<"\nEnter fee "<<fee;
}
};//end of class

int main()
{
Student st,*ptr;//pointer of an object
ptr=&st;
ptr->input();
ptr->display();
}//end of main


Output:

Enter name riya
Enter rollno 20
Enter fee 10000

Name is riya
Rollno is 20
Enter fee 10000