Friday, 20 March 2026

Array of Class Objects

Array of Class Objects
An array is a user-defi ned data type whose members are homogeneous and stored in contiguous memory locations. For practical applications such as designing a large size of data base, arrays are very essential. The declaration of an array of class objects is very similar to the declaration of the array of structures in C++.
The general syntax of the array of class objects is:
class user_de ned_name {
private :
// methods
public :
// methods
protected :
// methods
};
class user_de ned_name object[MAX];
where, MAX is a user defi ned size of the array of class objects.
Example 1.
A program to read students’ particulars such as roll number, age, sex, height and weight from the keyboard and display the contents of the class on the screen. The class ‘student_info’ is defi ned as an array of class objects. This program shows how to create an array of class objects and how to access these data member and member functions in C++.
// array of class objects
#include <iostream>
using namespace std;
const int MAX = 100;
class student_info
{
private :
long int rollno;
int age;
char sex;
oat height, weight;
public :
void getinfo();
void disinfo();
}; // end of class de nition
void student_info :: getinfo()
{
cout << “ Roll no :”;
cin >> rollno;
cout <<“ Age :”;
cin >> age;
cout << “ Sex : ”;
cin >> sex;
cout << “ Height : ”;
cin >> height;
cout << “ Weight : ”;
cin >> weight;
}
void student_info :: disinfo ()
{
cout << endl;
cout << “ Roll no = ” << rollno << endl;
cout << “ Age = ” << age << endl;
cout << “ Sex = ” << sex << endl;
cout << “ Height = ” << height << endl;
cout << “ Weight = ” << weight << endl;
}
int main()
{
student_info object[MAX]; // array of objects
int i,n;
cout << “ How many students ? \n” << endl;
cin >> n;
cout << “ enter the following imformation \n” << endl;
for (i = 0; i <= n-1; ++i) {
int j = i;
cout << endl;
cout << “ record = ” << j+1 << endl;
object[i].getinfo();
}
cout << “ contents of class \n”;
for (i = 0; i <= n-1; ++i) {
object[i].disinfo();
}
}
Output of the above program
How many students?
2
enter the following imformation
record = 1
Roll no : 20071
Age : 21
Sex : M
Height : 170
Weight : 56
record = 2
Roll no : 20072
Age : 20
Sex : F
Height : 160
Weight : 50
contents of class
Roll no = 20071
Age = 21
Sex = M
Height = 170
Weight = 56
Roll no = 20072
Age = 20
Sex = F
Height = 160
Weight = 50

POINTERS AND CLASSES
The pointer variable is very much used to construct complex data bases using the data structures such as linked lists, double linked lists and binary trees. So far, it has been shown that a data member and a member function of a class could be an ordinary data type such as int, fl oat, char and even a class also.
The following declaration of creating an object is valid in C++.
class sample {
private :
int x;
oat y;
char s;
public :
void getdata();
void display();
};
sample *ptr;

where ptr is a pointer variable that holds the address of the class object sample and consists of the three data members such as int x, fl oat y and char s, and also holds member functions such as getdata() and display().
The pointer to an object of class variable will be accessed and processed in one of the following ways, 
(*object name).member name = variable;
The parentheses are essential since the member of class period (.) has a higher precedence over the indirection operator (*). Or, the pointer to the member of a class can be expressed using dash (–) followed by the greater than sign (>).
object name -> member name = variable;
Following are valid declarations of using pointer to the member of a class.

Case 1. A member of class object can be accessed by the indirection operator which has been shown in the following program segment:
class student {
private :
-------
-------
public :
-------
-------
}; // end of class de nition
void main(void)
{
student *ptr;
-------
-------
(*ptr).data_member;
(*ptr).member_function();
}

Case 2. A member of class object can be accessed by the pointer of a class operator which has been shown in the following program segment:
class student {
private :
-------
-------
public :
-------
-------
}; // end of class de nition
int main()
{
student *ptr;
-------
-------
ptr->data_member;
ptr->member_function();
return 0;
}

Example 1.
A program to assign some values to the member of class objects using a pointer structure operator (–>).
// pointers and classes 1.cpp
#include <iostream>
using namespace std;
class student_info {
private :
long int rollno;
int age;
char sex;
oat height;
oat weight;
public :
void getinfo ();
void disinfo ();
}; // end of class de nition
void student_info :: getinfo()
{
cout << “ Roll no :”;
cin >> rollno;
cout <<“ Age :”;
cin >> age;
cout << “ Sex : ”;
cin >> sex;
cout << “ Height : ”;
cin >> height;
cout << “ Weight : ”;
cin >> weight;
}
void student_info :: disinfo()
{
cout << endl;
cout << “ Roll no = ” << rollno << endl;
cout << “ Age = ” << age << endl;
cout << “ Sex = ” << sex << endl;
cout << “ Height = ” << height << endl;
cout << “ Weight = ” << weight << endl;
}
int main()
{
student_info *ptr; // ptr is an object of class student
cout << “ enter the following information ” << endl;
ptr->getinfo();
cout << “ \n contents of class ” << endl;
ptr->disinfo();
return 0;
}
Output of the above program
enter the following information
Roll no : 200710
Age : 23
Sex : M
Height : 176
Weight: 67
contents of class
Roll no = 200710
Age = 23
Sex = M
Height = 176
Weight = 67

Example 2.
A program to assign some values to the member of class objects using an indirection operator.
// pointers and classes 2.cpp
#include <iostream>
using namespace std;
class student_info {
private :
long int rollno;
int age;
char sex;
oat height;
oat weight;
public :
void getinfo();
void disinfo();
}; // end of class de nition
void student_info :: getinfo()
{
cout << “ Roll no :”;
cin >> rollno;
cout <<“ Age :”;
cin >> age;
cout << “ Sex : ”;
cin >> sex;
cout << “ Height : ”;
cin >> height;
cout << “ Weight : ”;
cin >> weight;
}
void student_info :: disinfo()
{
cout << endl;
cout << “ Roll no = ” << rollno << endl;
cout << “ Age = ” << age << endl;
cout << “ Sex = ” << sex << endl;
cout << “ Height = ” << height << endl;
cout << “ Weight = ” << weight << endl;
}
int main()
{
student_info *ptr; // ptr is an object of class student
cout << “ enter the following information ” << endl;
(*ptr).getinfo();
cout << “ \n contents of class ” << endl;
(*ptr).disinfo();
return 0;
}
Output of the above program
enter the following information
Roll no : 200711
Age : 22
Sex : F
Height : 156
Weight : 71
contents of class
Roll no = 200711
Age = 22
Sex = F
Height = 156
Weight = 71

Example 3.
A program to fi nd the distance between the given two points using the pointer to class objects technique.
// classes and pointers
#include <iostream>
#include <cmath>
using namespace std;
class point {
private :
int x,y;
public :
point (int xnew,int ynew);
inline int getx() {
return(x);
}
inline int gety() {
return(y);
}
oat nddist(point a, point b);
}; // end of class de nition
point :: point (int xnew, int ynew)
{
x = xnew;
y = ynew;
}
oat point :: nddist (point a, point b)
{
oat temp;
temp = ((b.y-a.y)*(b.y-a.y)) + ((b.x-a.x)*(b.x-a.x));
return (sqrt(temp));
}
int main()
{
point aobj(4,3),bobj(0,-1);
point *aptr = &aobj;
point *bptr = &bobj;
aptr->getx();
aptr->gety();
bptr->getx();
bptr->gety();
oat value;
value = aptr-> nddist(aobj,bobj);
cout << “ distance between two points =” << value << endl;
return 0;
}
Output of the above program
distance between two points = 5.65685

UNIONS AND CLASSES
A union is also used for declaring classes in C++. The members of a union are public by default. A union allows to store its members only one at a time. A union may have member functions including constructors and destructors, but not virtual functions. A union may not have base class. An object of a class with a constructor or a destructor or a user-defi ned assignment operator cannot be a member of a union. A union can have no static data member.
The general syntax of a union declaration is:
union user_de ned_name {
private :
//methods
public :
// methods
protected :
// methods
};
user_de ned_name object;
It is possible in C++ to declare a union without a user-defi ned name or a union tag that is called as an anonymous union.
The syntax of the anonymous union declaration is:
union {
// methods
// methods
};
The names of the members of an anonymous union must be distinct from other names. A global anonymous union must be declared static. An anonymous union may not have protected or private members. An anonymous union may not have a member function also. 
For example, the following anonymous union declarations are invalid.
Case 1 An anonymous union may not have private member. Hence, the following declaration of union
gives compiler error message.
union {
private :
int x;
oat y;
public :
void getvalue();
void display();
};
Case 2 An anonymous union may not have a member function. Hence, the following declaration is invalid
and gives error message while compiling.
union {
public :
int x;
oat y;
void getdata();
void display();
};
The following declaration is valid. A union with a name or union tag may have member functions.
union sample {
public :
int a;
char name;
void display();
void sum();
};
Note that a union with name or union tag may have private members also. For example, the following declaration is valid:
union sample {
private :
int x;
oat y;
public :
void get();
void display();
};

Example 1.
A program to demonstrate how to defi ne a union as a class object data type. This program reads the values of the data members from the keyboard and displays the contents of the union on the screen.
// unions and classes
#include <iostream>
using namespace std;
union sample {
private :
int x;
oat y;
public :
void getinfo ();
void disinfo ();
}; // end of class de nition
void sample :: getinfo ()
{
cout << “ value of x ( in integer ) :”;
cin >> x;
cout <<“ value of y ( in oat) :”;
cin >> y;
}
void sample :: disinfo ()
{
cout << endl;
cout << “ x = ” << x << endl;
cout << “ y = ” << y << endl;
}
int main()
{
sample obj;
cout << “ enter the following information ” << endl;
obj.getinfo();
cout << “ \n content of union ” << endl;
obj.disinfo();
return 0;
}
Output of the above program
enter the following information
value of x ( in integer ) : 34
value of y ( in oat) : -12.45
content of union
x = 13107
y = -12.45

CLASSES WITHIN CLASSES NESTED CLASS)
C++ permits declaration of a class within another class. A class declared as a member of another class is called as a nested class or a class within another class. The name of a nested class is local to the enclosing class. The nested class is in the scope of its enclosing class.
The general syntax of the nested class declaration is shown below.
class outer_class_name {
private :
// data
protected :
//data
// methods
public :
// methods
class inner_class_name {
private :
// data of inner class
public :
// methods of inner class
}; // end of inner class declaration
}; // end of outer class declaration
outer_class_name object1;
outer_class_name :: inner_class_name object2;
Note that simply declaring a class nested in another does not mean that the enclosing class contains an object of the enclosed class. Nesting expresses scoping, not containment of such objects.

Example 1.
A program to defi ne a nested class ‘student_info’ which contains data members such as name, roll number and sex, and also consists of one more class ‘date’, whose data members are day, month and year. The values of the student_info are to be read from the keyboard and the contents of the class have to be displayed on the screen. This program shows how to create a nested class and their objects, and also how to access these data members and member functions in C++.
// classes within classes (nested classes) demonstration
#include <iostream>
#include <string>
using namespace std;
class student_info {
private :
char name[20];
long int rollno;
char sex;
public :
student_info(char *na, long int rn, char sx);
void display();
class date {
private :
int day,month,year;
public :
date (int dy,int mh, int yr);
void show_date();
}; // end of date class declaration
};// end of student_info class declaration
student_info :: student_info(char *na,long int rn,char sx)
{
strcpy (name,na);
rollno = rn;
sex = sx;
}
student_info::date :: date(int dy, int mh, int yr)
{
day = dy;
month = mh;
year = yr;
}
void student_info:: display()
{
cout << “ student’s_name Rollno sex date_of_birth(dd-mm-yr) \n”;
cout << “ ---------------------------------------------------- ” << endl;
cout << name << “ ” ;
cout << rollno << “ ”;
cout << sex << “ ”;
}
void student_info::date::show_date()
{
cout << day << ‘/’ << month << ‘/’ << year << endl;
cout << “ -------------------------------” << endl;
}
int main()
{
student_info obj1(“Sampath Reddy”,200710,‘M’);
student_info::date obj2(13,7,94);
obj1.display();
obj2.show_date();
return 0;
}
Output of the above program
student’s_name Rollno sex date_of_birth(dd-mm-yr)
---------------------------------------------
Sampath Reddy 200710 M 13/7/94
---------------------------------------------

Example 2.
A program to defi ne a nested class ‘student_info’ which contains data members such as name, roll number and sex, and also consists of one more class ‘date’, whose data members are day, month and year. Again, the class is defi ned with one more class ‘age_class’ whose data member is age. The values of the student_info are read from the keyboard and contents of the class have to be displayed onto the screen. This program shows how to create a nested class and their objects and also how to access these data members and member functions in C++.
// classes within classes (nested classes) demonstration
// program 2.cpp
#include <iostream>
#include <string>
using namespace std;
class student_info {
private :
char name[20];
long int rollno;
char sex;
public :
student_info(char *na,long int rn,char sx);
void display();
class date {
private :
int day,month,year;
public :
date (int dy, int mh, int yr);
void show_date();
class age_class {
private :
int age;
public :
age_class (int age_value);
void show_age();
}; // end of age_class;
}; // end of date class declaration
};// end of student_info class declaration
student_info :: student_info(char *na,long int rn,char sx)
{
strcpy (name,na);
rollno = rn;
sex = sx;
}
student_info::date :: date(int dy, int mh,int yr)
{
day = dy;
month = mh;
year = yr;
}
student_info::date :: age_class::age_class (int age_value)
{
age = age_value;
}
void student_info:: display()
{
cout << “ student’s name Roll_no sex date of birth age \n”;
cout << “ -----------------------------------------------------” << endl;
cout << name << “ ”;
cout << rollno << “ ”;
cout << sex << “ ”;
}
void student_info::date::show_date()
{
cout << day << ‘/’ << month << ‘/’ << year << ‘\t’;
}
void student_info::date::age_class::show_age()
{
cout << age << endl;
cout << “ --------------------------------- ” << endl;
}
int main()
{
student_info obj1(“Suhail Ahmed”,20071,‘M’);
student_info::date obj2(31,9,1990);
student_info::date::age_class obj3(17);
obj1.display();
obj2.show_date();
obj3.show_age();
return 0;
}
Output of the above program
Student’s name Roll_no sex date of birth age
------------------------------------------------
Suhail Ahmed 20071 M 31/9/1990 17
------------------------------------------------

Example 3.
A program to defi ne an array of nested class ‘student_info’ which contains data members such as name, roll number and sex, and also consists of one more class ‘date’, whose data members are day, month and year. Again, the class is defi ned with one more class ‘age_class’ whose data member is age. The values of the student_info are read from the keyboard and contents of the class have to be displayed on the screen. This program shows how to create an array of nested class and their objects, and also how to access these data members and member functions in C++.
// array of nested classes objects
#include <iostream>
#include <string>
using namespace std;
const int MAX = 100;
class student_info {
private :
char name[20];
long int rollno;
char sex;
public :
void getbase();
void display();
class date {
private :
int day,month,year;
public :
void getdate();
void show_date();
class age_class {
private :
int age;
public :
void getage ();
void show_age();
}; // end of age_class;
}; // end of date class declaration
};// end of student_info class declaration
void student_info :: getbase()
{
cout << “ enter a name : ”;
cin >> name;
cout << “ roll no :”;
cin >> rollno;
cout << “ sex :”;
cin >> sex;
}
void student_info::date :: getdate()
{
cout << “ enter a date of birth ( dd-mm-yr) ”;
cin >> day >> month >> year;
}
void student_info::date ::age_class:: getage ()
{
cout << “ enter an age :”;
cin >>age;
}
void student_info:: display()
{
cout << name << “ ” <<‘\t’;
cout << rollno << “ ”;
cout << sex << “ ”;
}
void student_info::date::show_date()
{
cout << day << ‘/’ << month << ‘/’ << year << ‘\t’;
}
void student_info::date::age_class::show_age()
{
cout << age << endl;
}
int main()
{
student_info obj1[MAX];
student_info::date obj2[MAX];
student_info::date::age_class obj3[MAX];
int n,i;
cout << “ how many students ?\n”;
cin >> n;
cout << “ enter the following inoformation \n”;
for (i=0; i<= n-1; ++i) {
int j = i+1;
cout << “ \n object : ” << j << endl;
obj1[i].getbase();
obj2[i].getdate();
obj3[i].getage();
}
cout << “ Contents of the array of nested classes \n”;
cout << “ ------------------------------------------”;
cout << endl;
cout << “ student’s name Roll_no sex date of birth age”;
cout << endl;
cout << “ ------------------------------------------------”;
cout << endl;
for (i=0; i<= n-1; ++i) {
obj1[i].display();
obj2[i].show_date();
obj3[i].show_age();
}
cout << “-------------------------------------------------”;
cout << endl;
return 0;
}
Output of the above program
how many students?
2
enter the following information
object : 1
enter a name: Suhail
roll no: 20071
sex: M
enter a date of birth ( dd-mm-yr) 12 3 1990
enter an age: 17
object: 2
enter a name: Sudheer
roll no: 20072
sex: M
enter a date of birth ( dd-mm-yr) 21 5 1991
enter an age: 16
Contents of the array of nested classes
-------------------------------------------------------------
student’s name roll_no sex date of birth age
-------------------------------------------------------------
Suhail 20071 M 12/3/1990 17
Sudheer 20072 M 21/5/1991 16
-------------------------------------------------------------

0 comments

Post a Comment