{"id":62,"date":"2018-07-09T11:58:54","date_gmt":"2018-07-09T11:58:54","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=62"},"modified":"2019-05-14T11:13:04","modified_gmt":"2019-05-14T11:13:04","slug":"classes-objects-iii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/classes-objects-iii\/","title":{"rendered":"Classes &amp; Objects -III"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/mqkgBnzniTA\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n\r\n<div>\r\n\r\nIn the previous modules we have understood the basic anatomy of the objects. In this module we will understand\r\n\r\n&nbsp;\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to assign object to another object\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to define and use array of objects\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to define and use pointer to object\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to define pointer to members of class\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Memory allocation of objects\r\n\r\n&nbsp;\r\n\r\n<strong>Assigning Objects<\/strong>\r\n\r\n&nbsp;\r\n\r\nObjects of similar type can be assigned in a similar manner as other variables.\r\n\r\nclass Time\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\npublic :\r\n\r\nvoidreadTime();\r\n\r\nvoiddisplayTime();\r\n\r\ntimedifferenceTime(Time t);\r\n\r\ntimeaddTime(Time t);\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nvoid\u00a0 main()\r\n\r\n{\r\n\r\nTime time1, time2;\r\n\r\n&nbsp;\r\n\r\ntime1.readdata();\r\n\r\ntime2=time1;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Array of Objects<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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<\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">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<\/p>\r\n&nbsp;\r\n\r\n<strong>student MCA[35];<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Let us understand this with the help of the following program.<\/strong>\r\n\r\n&nbsp;\r\n\r\nclass student\r\n\r\n{\r\n\r\nint rollno;\r\n\r\nchar name[20];\r\n\r\nint marks_obtained;\r\n\r\n&nbsp;\r\n\r\nstatic int total_marks;\r\n\r\nTime dob;\r\n\r\npiblic :\r\n\r\nvoid readdata();\r\n\r\nvoid display();\r\n\r\nint\u00a0\u00a0\u00a0 computePercentage();\r\n\r\nstatic int update_totalmarks(int\u00a0 mks)\r\n\r\n{ total_marks = mks;\u00a0 }\r\n\r\n};\r\n\r\nint student : : total_marks = 0;\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0\u00a0 array of 30 student objects student MCA[30];\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026..\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026..\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nStudent_List\u00a0 is ADT. Where the\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Value - list of students of some finite length\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 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<strong>.<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\nLet us have a look at the following program.\r\n\r\n&nbsp;\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;iomanip&gt;\r\n\r\n&nbsp;\r\n\r\nusingnamespace std;\r\n\r\n&nbsp;\r\n\r\nclass student\r\n\r\n{\r\n\r\nint rollno;\r\n\r\nchar name[20];\r\n\r\nint marks;\r\n\r\npublic :\r\n\r\n&nbsp;\r\n\r\nvoid readdata()\r\n\r\n{\r\n\r\ncout&lt;&lt;\"Enter Rollno : \";\r\n\r\ncin&gt;&gt;rollno;\r\n\r\ncout&lt;&lt;\"Enter Name : \";\r\n\r\ncin&gt;&gt; name;\r\n\r\ncout&lt;&lt;\"Enter Marks : \";\r\n\r\ncin&gt;&gt; marks;\r\n\r\n}\r\n\r\nstatic void displayHeader()\r\n\r\n{\r\n\r\ncout&lt;&lt; endl &lt;&lt; setw(5) &lt;&lt;\"RollNo\";\r\n\r\ncout&lt;&lt; setw(18) &lt;&lt;\"Name\";\r\n\r\ncout&lt;&lt; setw(15) &lt;&lt;\"Marks\"&lt;&lt;endl;\r\n\r\n}\r\n\r\nvoid display()\r\n\r\n{\r\n\r\ncout &lt;&lt; endl;\r\n\r\ncout &lt;&lt; setw(4) &lt;&lt; rollno;\r\n\r\ncout &lt;&lt; setw(20) &lt;&lt; name;\r\n\r\ncout &lt;&lt; setw(13) &lt;&lt; marks;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nint getrollno()\r\n\r\n{\r\n\r\nreturn rollno;\r\n\r\n}\r\n\r\n};\r\n\r\nclass Student_List\r\n\r\n{\r\n\r\nint size;\r\n\r\nstudent list[30];\r\n\r\n<\/div>\r\n<div>\r\n\r\npublic :\r\n\r\nvoid readdata()\r\n\r\n{\r\n\r\ncout&lt;&lt;\"Enter the number of student records you want to enter\"&lt;&lt;endl;\r\n\r\ncin&gt;&gt; size;\r\n\r\ncout&lt;&lt;endl&lt;&lt;\"Enter student information \"&lt;&lt;endl; for (int i=0; i&lt;size; i++)\r\n\r\nlist[i].readdata();\r\n\r\n}\r\n\r\nvoid addStudent(student S)\r\n\r\n{\r\n\r\nlist[size] = S;\r\n\r\nsize++;\r\n\r\n}\r\n\r\nbool searchStudent(student S)\r\n\r\n{\r\n\r\nint i;\r\n\r\nbool found = false;\r\n\r\nfor ( i=0; i&lt;size &amp;&amp; !found; i++)\r\n\r\n{\r\n\r\nif (list[i].getrollno() == S.getrollno())\r\n\r\nfound = true;\r\n\r\n}\r\n\r\nreturn found;\r\n\r\n}\r\n\r\nvoid display()\r\n\r\n{\r\n\r\nfor (int i=0; i&lt;size; i++)\r\n\r\nlist[i].display();\r\n\r\n}\r\n\r\n};\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nStudent_List MCA;\r\n\r\nstudent s;\r\n\r\nMCA.readdata();\r\n\r\ncout &lt;&lt;\"Enter the details of student who has to be added to the list\"\r\n\r\n&lt;&lt;\u00a0 endl;\r\n\r\ns.readdata();\r\n\r\n<\/div>\r\n<div>\r\n\r\nMCA.addStudent(s);\r\n\r\nstudent :: displayHeader();\r\n\r\nMCA.display();\r\n\r\n}\r\n\r\n<img class=\"size-full wp-image-64 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14.png\" alt=\"\" width=\"619\" height=\"388\" \/>\r\n\r\n&nbsp;\r\n\r\nThe 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\r\n\r\nStudent_List\u00a0\u00a0 MCA\r\n\r\nTo store the information of students of M. Tech we simply have to create object of student_list as follows\r\n\r\nStudent_List\u00a0\u00a0 M. tech\r\n\r\nIf we also wish to store information about PGDCSA, again we can simply create another object as follows\r\n\r\nStudent_List PGDCSA;\r\n\r\nFor performing various operations on any of these objects, we simply need to invoke appropriate function defined in Student_List class.\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Pointer to Object<\/strong>\r\n\r\n&nbsp;\r\n\r\nAs we can define pointer to inbuilt data types, we can also define pointer to objects.\r\n\r\nTime t;\r\n\r\nTime\u00a0\u00a0\u00a0 *time_ptr;\r\n\r\nHere time_ptr can store address of any Time object. We can make it to point to any Time objects as\r\n\r\ntime_ptr =&amp; t;\r\n\r\nor\r\n\r\ntime_ptr = new Time;\r\n\r\nSimilarly we can define pointer to Student object as follows :\r\n\r\nStudent s;\r\n\r\nStudent *student_ptr;\r\n\r\nHere student_ptr can store address of any Student object. We can make it to point to any Student objects as\r\n\r\nstudent_ptr =&amp;s;\r\n\r\nor\r\n\r\nstudent_ptr =\u00a0\u00a0\u00a0 new student;\r\n\r\n&nbsp;\r\n\r\n<strong>Dereferencing Pointer to Object<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can dereference the pointer to access object being pointed as follows\r\n\r\n<\/div>\r\n<div>\r\n\r\n(*time_ptr).hours = 3;\r\n\r\n\/\/ if hours is public\r\n\r\n<\/div>\r\n<div>\r\n\r\nor\r\n\r\n<\/div>\r\n<div>\r\n\r\ntime_ptr-&gt;hours =3;\r\n\r\n\/\/\u00a0 if hours is public\r\n\r\n<\/div>\r\n<div>\r\n\r\n(*time.ptr).readdata();\r\n\r\nor\r\n\r\ntime_ptr-&gt;readdata();\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Pointer to Class Member<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Pointer to data member of the class<\/strong>\r\n\r\n&nbsp;\r\n\r\nint\u00a0\u00a0 student :: *rn_ptr = &amp;student :: rollno;\r\n\r\nstudent\u00a0\u00a0 s;\r\n\r\ns.*rn_ptr = 10;\r\n\r\ns.rollno\u00a0\u00a0\u00a0 = 10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ will work if rollno is public\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\n<strong>Const Objects<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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 :<\/p>\r\nclass Time\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\npublic :\r\n\r\nvoid readTime();\r\n\r\nvoid displayTime();\u00a0 const\r\n\r\ntime differenceTime(Time t);\r\n\r\ntime addTime(Time t);\r\n\r\n};\r\n\r\nvoid\u00a0 main()\r\n\r\n{\r\n\r\nconst Time time;\r\n\r\n<\/div>\r\n<div>\r\n\r\ntime.readTime();\u00a0 \u00a0\/\/ will not work\r\n\r\ntime.displayTime();\r\n\r\n<\/div>\r\n<div>\r\n\r\n}\r\n\r\n<\/div>\r\n<div>\r\n\r\nAs we can see, we can declare const object for only that class which has at least one constant function.\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<strong>Memory allocation of classes and Objects<\/strong>\r\n\r\n&nbsp;\r\n\r\nIn C++ memory allocation of objects is done in two phases.\r\n\r\n&nbsp;\r\n\r\nWhen class is defined memory to member function and static data members is allocated.\r\n\r\nThey remain in existence during the entire execution of the program.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Classes are powerful tool to create your own data type.<\/li>\r\n \t<li>The classes help us achieve object oriented<span style=\"text-align: initial;font-size: 1em\"> concepts like abstraction and encapsulation and data hiding.<\/span><\/li>\r\n \t<li>C++ provides many features using which we can make our classes behave similar<span style=\"text-align: initial;font-size: 1em\"> to built-in data type. We will discuss them in successive modules.<\/span><\/li>\r\n<\/ul>\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Classes & Objects -III<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/mqkgBnzniTA\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>Additional References<\/strong>\r\n\r\n&nbsp;\r\n\r\n1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.\r\n\r\n2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.\r\n\r\n3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.\r\n\r\n4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.\r\n\r\n5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.\r\n\r\n6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill\r\n\r\n7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html\r\n\r\n8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.\r\n\r\n9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education\r\n\r\n10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill\r\n\r\n11) \u201cC++ FAQs\u201d, Pearson Education.\r\n","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/mqkgBnzniTA\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>In the previous modules we have understood the basic anatomy of the objects. In this module we will understand<\/p>\n<p>&nbsp;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to assign object to another object<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to define and use array of objects<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to define and use pointer to object<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 How to define pointer to members of class<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Memory allocation of objects<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Assigning Objects<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Objects of similar type can be assigned in a similar manner as other variables.<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>public :<\/p>\n<p>voidreadTime();<\/p>\n<p>voiddisplayTime();<\/p>\n<p>timedifferenceTime(Time t);<\/p>\n<p>timeaddTime(Time t);<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>void\u00a0 main()<\/p>\n<p>{<\/p>\n<p>Time time1, time2;<\/p>\n<p>&nbsp;<\/p>\n<p>time1.readdata();<\/p>\n<p>time2=time1;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Array of Objects<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">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<\/p>\n<p>&nbsp;<\/p>\n<p><strong>student MCA[35];<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Let us understand this with the help of the following program.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>class student<\/p>\n<p>{<\/p>\n<p>int rollno;<\/p>\n<p>char name[20];<\/p>\n<p>int marks_obtained;<\/p>\n<p>&nbsp;<\/p>\n<p>static int total_marks;<\/p>\n<p>Time dob;<\/p>\n<p>piblic :<\/p>\n<p>void readdata();<\/p>\n<p>void display();<\/p>\n<p>int\u00a0\u00a0\u00a0 computePercentage();<\/p>\n<p>static int update_totalmarks(int\u00a0 mks)<\/p>\n<p>{ total_marks = mks;\u00a0 }<\/p>\n<p>};<\/p>\n<p>int student : : total_marks = 0;<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0\u00a0 array of 30 student objects student MCA[30];<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026..<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026..<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>Student_List\u00a0 is ADT. Where the<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Value &#8211; list of students of some finite length<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Operations on data &#8211; 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<strong>.<\/strong><\/p>\n<\/div>\n<div>\n<p>Let us have a look at the following program.<\/p>\n<p>&nbsp;<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;iomanip&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>usingnamespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>class student<\/p>\n<p>{<\/p>\n<p>int rollno;<\/p>\n<p>char name[20];<\/p>\n<p>int marks;<\/p>\n<p>public :<\/p>\n<p>&nbsp;<\/p>\n<p>void readdata()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;&#8220;Enter Rollno : &#8220;;<\/p>\n<p>cin&gt;&gt;rollno;<\/p>\n<p>cout&lt;&lt;&#8220;Enter Name : &#8220;;<\/p>\n<p>cin&gt;&gt; name;<\/p>\n<p>cout&lt;&lt;&#8220;Enter Marks : &#8220;;<\/p>\n<p>cin&gt;&gt; marks;<\/p>\n<p>}<\/p>\n<p>static void displayHeader()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt; endl &lt;&lt; setw(5) &lt;&lt;&#8220;RollNo&#8221;;<\/p>\n<p>cout&lt;&lt; setw(18) &lt;&lt;&#8220;Name&#8221;;<\/p>\n<p>cout&lt;&lt; setw(15) &lt;&lt;&#8220;Marks&#8221;&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>void display()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; setw(4) &lt;&lt; rollno;<\/p>\n<p>cout &lt;&lt; setw(20) &lt;&lt; name;<\/p>\n<p>cout &lt;&lt; setw(13) &lt;&lt; marks;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>int getrollno()<\/p>\n<p>{<\/p>\n<p>return rollno;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>class Student_List<\/p>\n<p>{<\/p>\n<p>int size;<\/p>\n<p>student list[30];<\/p>\n<\/div>\n<div>\n<p>public :<\/p>\n<p>void readdata()<\/p>\n<p>{<\/p>\n<p>cout&lt;&lt;&#8220;Enter the number of student records you want to enter&#8221;&lt;&lt;endl;<\/p>\n<p>cin&gt;&gt; size;<\/p>\n<p>cout&lt;&lt;endl&lt;&lt;&#8220;Enter student information &#8220;&lt;&lt;endl; for (int i=0; i&lt;size; i++)<\/p>\n<p>list[i].readdata();<\/p>\n<p>}<\/p>\n<p>void addStudent(student S)<\/p>\n<p>{<\/p>\n<p>list[size] = S;<\/p>\n<p>size++;<\/p>\n<p>}<\/p>\n<p>bool searchStudent(student S)<\/p>\n<p>{<\/p>\n<p>int i;<\/p>\n<p>bool found = false;<\/p>\n<p>for ( i=0; i&lt;size &amp;&amp; !found; i++)<\/p>\n<p>{<\/p>\n<p>if (list[i].getrollno() == S.getrollno())<\/p>\n<p>found = true;<\/p>\n<p>}<\/p>\n<p>return found;<\/p>\n<p>}<\/p>\n<p>void display()<\/p>\n<p>{<\/p>\n<p>for (int i=0; i&lt;size; i++)<\/p>\n<p>list[i].display();<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>Student_List MCA;<\/p>\n<p>student s;<\/p>\n<p>MCA.readdata();<\/p>\n<p>cout &lt;&lt;&#8220;Enter the details of student who has to be added to the list&#8221;<\/p>\n<p>&lt;&lt;\u00a0 endl;<\/p>\n<p>s.readdata();<\/p>\n<\/div>\n<div>\n<p>MCA.addStudent(s);<\/p>\n<p>student :: displayHeader();<\/p>\n<p>MCA.display();<\/p>\n<p>}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-64 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14.png\" alt=\"\" width=\"619\" height=\"388\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14.png 619w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14-300x188.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14-225x141.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-14-350x219.png 350w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>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<\/p>\n<p>Student_List\u00a0\u00a0 MCA<\/p>\n<p>To store the information of students of M. Tech we simply have to create object of student_list as follows<\/p>\n<p>Student_List\u00a0\u00a0 M. tech<\/p>\n<p>If we also wish to store information about PGDCSA, again we can simply create another object as follows<\/p>\n<p>Student_List PGDCSA;<\/p>\n<p>For performing various operations on any of these objects, we simply need to invoke appropriate function defined in Student_List class.<\/p>\n<\/div>\n<div>\n<p><strong>Pointer to Object<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>As we can define pointer to inbuilt data types, we can also define pointer to objects.<\/p>\n<p>Time t;<\/p>\n<p>Time\u00a0\u00a0\u00a0 *time_ptr;<\/p>\n<p>Here time_ptr can store address of any Time object. We can make it to point to any Time objects as<\/p>\n<p>time_ptr =&amp; t;<\/p>\n<p>or<\/p>\n<p>time_ptr = new Time;<\/p>\n<p>Similarly we can define pointer to Student object as follows :<\/p>\n<p>Student s;<\/p>\n<p>Student *student_ptr;<\/p>\n<p>Here student_ptr can store address of any Student object. We can make it to point to any Student objects as<\/p>\n<p>student_ptr =&amp;s;<\/p>\n<p>or<\/p>\n<p>student_ptr =\u00a0\u00a0\u00a0 new student;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Dereferencing Pointer to Object<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can dereference the pointer to access object being pointed as follows<\/p>\n<\/div>\n<div>\n<p>(*time_ptr).hours = 3;<\/p>\n<p>\/\/ if hours is public<\/p>\n<\/div>\n<div>\n<p>or<\/p>\n<\/div>\n<div>\n<p>time_ptr-&gt;hours =3;<\/p>\n<p>\/\/\u00a0 if hours is public<\/p>\n<\/div>\n<div>\n<p>(*time.ptr).readdata();<\/p>\n<p>or<\/p>\n<p>time_ptr-&gt;readdata();<\/p>\n<\/div>\n<div>\n<p><strong>Pointer to Class Member<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<\/div>\n<div>\n<p><strong>Pointer to data member of the class<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>int\u00a0\u00a0 student :: *rn_ptr = &amp;student :: rollno;<\/p>\n<p>student\u00a0\u00a0 s;<\/p>\n<p>s.*rn_ptr = 10;<\/p>\n<p>s.rollno\u00a0\u00a0\u00a0 = 10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ will work if rollno is public<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Const Objects<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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 :<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>public :<\/p>\n<p>void readTime();<\/p>\n<p>void displayTime();\u00a0 const<\/p>\n<p>time differenceTime(Time t);<\/p>\n<p>time addTime(Time t);<\/p>\n<p>};<\/p>\n<p>void\u00a0 main()<\/p>\n<p>{<\/p>\n<p>const Time time;<\/p>\n<\/div>\n<div>\n<p>time.readTime();\u00a0 \u00a0\/\/ will not work<\/p>\n<p>time.displayTime();<\/p>\n<\/div>\n<div>\n<p>}<\/p>\n<\/div>\n<div>\n<p>As we can see, we can declare const object for only that class which has at least one constant function.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><strong>Memory allocation of classes and Objects<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In C++ memory allocation of objects is done in two phases.<\/p>\n<p>&nbsp;<\/p>\n<p>When class is defined memory to member function and static data members is allocated.<\/p>\n<p>They remain in existence during the entire execution of the program.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Classes are powerful tool to create your own data type.<\/li>\n<li>The classes help us achieve object oriented<span style=\"text-align: initial;font-size: 1em\"> concepts like abstraction and encapsulation and data hiding.<\/span><\/li>\n<li>C++ provides many features using which we can make our classes behave similar<span style=\"text-align: initial;font-size: 1em\"> to built-in data type. We will discuss them in successive modules.<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Classes &#38; Objects -III<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/mqkgBnzniTA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Additional References<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.<\/p>\n<p>2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.<\/p>\n<p>3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.<\/p>\n<p>4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.<\/p>\n<p>5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.<\/p>\n<p>6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill<\/p>\n<p>7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html<\/p>\n<p>8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.<\/p>\n<p>9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education<\/p>\n<p>10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill<\/p>\n<p>11) \u201cC++ FAQs\u201d, Pearson Education.<\/p>\n","protected":false},"author":4,"menu_order":6,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyoti-pareek"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-62","chapter","type-chapter","status-publish","hentry","contributor-dr-jyoti-pareek"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/62\/revisions"}],"predecessor-version":[{"id":389,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/62\/revisions\/389"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/62\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=62"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=62"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}