6 Classes & Objects -III
Jyoti Pareek
In the previous modules we have understood the basic anatomy of the objects. In this module we will understand
· How to assign object to another object
· How to define and use array of objects
· How to define and use pointer to object
· How to define pointer to members of class
· Memory allocation of objects
Assigning Objects
Objects of similar type can be assigned in a similar manner as other variables.
class Time
{
int hours;
int minutes;
int seconds;
public :
voidreadTime();
voiddisplayTime();
timedifferenceTime(Time t);
timeaddTime(Time t);
};
void main()
{
Time time1, time2;
time1.readdata();
time2=time1;
}
Array of Objects
Object of one class can store information of one entity only. For example object of student class can store information of one student only. What if we want to store information of many
students? Creating several student objects to store information of different students would make writing and managing application very cumbersome. What is required and preferred would be to create array of objects. C++ allows us to have an array of objects. If we wish to store information of all the students of class, we can declare array of object as follows
student MCA[35];
Let us understand this with the help of the following program.
class student
{
int rollno;
char name[20];
int marks_obtained;
static int total_marks;
Time dob;
piblic :
void readdata();
void display();
int computePercentage();
static int update_totalmarks(int mks)
{ total_marks = mks; }
};
int student : : total_marks = 0;
void main()
{
// array of 30 student objects student MCA[30];
……………………………
…………………………..
…………………………..
}
This will make it easy to manipulate list of students. One can observe, here we would need to perform operations on list of students also, such as add student, delete student, modify student information, search for a student, sort the student list etc. If we carefully see this, we would realize that list of student in itself is a potential candidate of being ADT to be implemented as class. Let us define Student_List ADT.
Student_List is ADT. Where the
• Value – list of students of some finite length
• Operations on data – insert student, delete student, search student, sort student list etc. It is possible to define a student_list as a class and MCA can be defined as instance of that class.
Let us have a look at the following program.
#include<iostream>
#include<iomanip>
usingnamespace std;
class student
{
int rollno;
char name[20];
int marks;
public :
void readdata()
{
cout<<“Enter Rollno : “;
cin>>rollno;
cout<<“Enter Name : “;
cin>> name;
cout<<“Enter Marks : “;
cin>> marks;
}
static void displayHeader()
{
cout<< endl << setw(5) <<“RollNo”;
cout<< setw(18) <<“Name”;
cout<< setw(15) <<“Marks”<<endl;
}
void display()
{
cout << endl;
cout << setw(4) << rollno;
cout << setw(20) << name;
cout << setw(13) << marks;
}
int getrollno()
{
return rollno;
}
};
class Student_List
{
int size;
student list[30];
public :
void readdata()
{
cout<<“Enter the number of student records you want to enter”<<endl;
cin>> size;
cout<<endl<<“Enter student information “<<endl; for (int i=0; i<size; i++)
list[i].readdata();
}
void addStudent(student S)
{
list[size] = S;
size++;
}
bool searchStudent(student S)
{
int i;
bool found = false;
for ( i=0; i<size && !found; i++)
{
if (list[i].getrollno() == S.getrollno())
found = true;
}
return found;
}
void display()
{
for (int i=0; i<size; i++)
list[i].display();
}
};
void main()
{
Student_List MCA;
student s;
MCA.readdata();
cout <<“Enter the details of student who has to be added to the list”
<< endl;
s.readdata();
MCA.addStudent(s);
student :: displayHeader();
MCA.display();
}
The advantage here is that once we define student-list, to store the information of students of MCA we simply have to create object of student_list as follows
Student_List MCA
To store the information of students of M. Tech we simply have to create object of student_list as follows
Student_List M. tech
If we also wish to store information about PGDCSA, again we can simply create another object as follows
Student_List PGDCSA;
For performing various operations on any of these objects, we simply need to invoke appropriate function defined in Student_List class.
Pointer to Object
As we can define pointer to inbuilt data types, we can also define pointer to objects.
Time t;
Time *time_ptr;
Here time_ptr can store address of any Time object. We can make it to point to any Time objects as
time_ptr =& t;
or
time_ptr = new Time;
Similarly we can define pointer to Student object as follows :
Student s;
Student *student_ptr;
Here student_ptr can store address of any Student object. We can make it to point to any Student objects as
student_ptr =&s;
or
student_ptr = new student;
Dereferencing Pointer to Object
We can dereference the pointer to access object being pointed as follows
(*time_ptr).hours = 3;
// if hours is public
or
time_ptr->hours =3;
// if hours is public
(*time.ptr).readdata();
or
time_ptr->readdata();
Pointer to Class Member
We not only can define pointer to an object, we can also define pointer to the data members and member function of the classes. For example for our student class, we can define pointer to point to data members such as rollno, name etc. We can also define pointer to member functions readdata(), display() etc.
Pointer to data member of the class
int student :: *rn_ptr = &student :: rollno;
student s;
s.*rn_ptr = 10;
s.rollno = 10; // will work if rollno is public
Pointer to the member of class does not contain absolute address of the member. It only contains the offset of that member within the object of the class.In the similar fashion we can declare pointer to member functions of the class.
Const Objects
Constant object is an object which cannot modify itself. Which means the value held in the data members of constant object cannot be modified. In most cases data members of a class are private members of the class, which can be modified only by the non-constant member functions of the class only. As the data members of constant object are not to be modified, we can invoke only non-constant functions of the class on the const objects. Let us understand this with the help of the following program :
class Time
{
int hours;
int minutes;
int seconds;
public :
void readTime();
void displayTime(); const
time differenceTime(Time t);
time addTime(Time t);
};
void main()
{
const Time time;
time.readTime(); // will not work
time.displayTime();
}
As we can see, we can declare const object for only that class which has at least one constant function.
Memory allocation of classes and Objects
In C++ memory allocation of objects is done in two phases.
When class is defined memory to member function and static data members is allocated.
They remain in existence during the entire execution of the program.
Whenever declaration of objects is encountered memory is allocated for all non static data members for the object. Lifetime of this memory is the time for which the block (in which this object is declared) executed.
Summary
- Classes are powerful tool to create your own data type.
- The classes help us achieve object oriented concepts like abstraction and encapsulation and data hiding.
- C++ provides many features using which we can make our classes behave similar to built-in data type. We will discuss them in successive modules.
you can view video on Classes & Objects -III |
Additional References
1) Stanley Lippmann, “C++ Primer”, Pearson Education.
2) Bjarne Stroustrup, “The C++ Programming Language”, Pearson Education.
3) Scott Mayer, “Effective C++”, Addison Wesley.
4) Bhushan Trivedi , Programming with ANSI C++, 2/e , Oxford University Press.
5) Yashavant P. Kanetkar “Let Us C++” , Bpb Publications.
6) Abhiram G. Ranade, “An Introduction to Programming through C++” , McGraw Hill
7) Ellis and B. Stroustrup “Annotated C++ Reference Manual”, http://www.stroustrup.com/arm.html
8) Herbert Schildt, “Complete Reference C++”, McGraw Hill Publications.
9) Ashok Kamthane, “Object Oriented Programming with ANSI and Turbo C++”, Pearson Education
10) E Balaguruswami, “Object Oriented Programming With C++”, Tata McGraw Hill
11) “C++ FAQs”, Pearson Education.