Array of class objects





Array of class objects and single inheritance

A program consisting of base class with data member name, roll number and gender. The derived class data members are height and weight. The derived class has been declared as an array of class objects. The member functions are used to get information on the derived class and display the content of the array of a class objects.

A  sample program with single inheritance, using array of objects.

 

#include<iostream.h>

#include<conio.h>

Class student

{

Private:

Char name [25];

Int roll;

Char gender [10];

Public;

Void getdata ()

{

Cout<<”\n Enter name”;

Cin>>name;

Cout<<”\n Enter the roll number”;

Cin>>roll;

Cout<<”\ enter the gender”;

Cin>>gender;

}

Void display ()

{

Cout<<”\n Name =”<<name;

Cout<<”\n Roll number= “<<roll;

Cout<<”\n Gender=”<<gender;

}

};

Class studentinfo: public student

{

Private:

Float height, weight;

Public:

Void getdata ()

{

Student:: getdata ();

Cout<<”\n Enter weight”;

Cin>>weight;

Cout<<”\n Enter height”:

Cin>>height;

}

Void display ()

{

Student:: display ();

Cout<<”\n Height=”<<height;

Cout<<”\n weight=”<<weight;

}

};

Void main ()

{

Studentinfo obj [100];

Int I, n;

Cout<<”\n How many students? “;

Cin>>n;

Cout<<”\n Enter the following information”;

For (i=0; i<n; i++)

{

Obj [i].getdata ();

}

For (i=0; i<n; i++)

{

Obj [i].display ();

}

getch ();

}



Related posts:

  1. Array Of Class Object Array of class objects Array is a collection of similar...
  2. Inheritance in C++ Inheritance in C++ Inheritance is one of the most important...
  3. Array of structure Array of structure It is well known that array is...
  4. Defining Member Function In C++ Class methods definitions  (Defining the member functions) Member functions can...
  5. Class In C++ Class in c++ Class is a group of similar object....