{"id":48,"date":"2018-07-09T11:45:32","date_gmt":"2018-07-09T11:45:32","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=48"},"modified":"2019-05-14T11:10:00","modified_gmt":"2019-05-14T11:10:00","slug":"classes-objects-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/classes-objects-ii\/","title":{"rendered":"Classes &amp; Objects &#8211; II"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/zrLqIJg0xPM\" 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<p style=\"text-align: justify\">In the previous module we have seen that the Classes are basic building blocks of object oriented applications. A well-crafted class can create powerful user defined data type. Whenever we need to store information of that data type, we create variable of that data type or object of that data type. Data members can be private or public. Private members can be accessed only by member functions. Public members can be accessed outside the class through object of the class using dot operator. Whenever we create an object of any class, each object is allocated memory for each data member. For example have a look at the following program.<\/p>\r\n&nbsp;\r\n\r\n<strong>Need of Static Data Members<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-49 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5.png\" alt=\"\" width=\"834\" height=\"660\" \/>\r\n<p style=\"text-align: justify\">When we read data from the user in these objects, objects will be holding data (assumed to be given by the user) as follows<\/p>\r\n<img class=\"size-full wp-image-50 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6.png\" alt=\"\" width=\"786\" height=\"348\" \/>\r\n<p style=\"text-align: justify\">You will notice here that rollno, name and marks_obtained will have different values in each object as each student has different rollno and name and each will obtain different marks. But total_marks remain same for each student. You would notice that even when total_marks are same for all, they are stored in each student object. This results in memory wastage. Also this unnecessary repetition of total_marks has other problems. If we want to change the value of total_marks, even though it is common value for all, we will have to modify them separately for all objects as follows :<\/p>\r\n<img class=\"size-full wp-image-51 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7.png\" alt=\"\" width=\"802\" height=\"382\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Also if we forget to change value of total_marks in one of the objects, it will result into data inconsistency as shown below. If we attempt to compute percentage of marks obtained by the students.The students whose total_marks did not get modified and remained 50, will be at disadvantage as correct percentage will not be calculated.<\/p>\r\n<img class=\"size-full wp-image-52 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8.png\" alt=\"\" width=\"854\" height=\"454\" \/>\r\n\r\n<strong>Static Data Members<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can define data member of a class as static using static keyword static.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A static data member is shared by all objects of the class. There is only one copy of the static member. All static data is initialized to zero when the first object is created, if no other initialization is present. We cannot initialize in the class definition but it can be initialized outside the class.<\/p>\r\n&nbsp;\r\n\r\nclass\u00a0 student\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint rollno;\r\n\r\nchar\u00a0 name[20];\r\n\r\n&nbsp;\r\n\r\nint marks_obtained;\r\n\r\nstatic int total_marks;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\n&nbsp;\r\n\r\nvoid readdata();\r\n\r\nvoid displaydata();\r\n\r\nint\u00a0\u00a0 computePercentage();\r\n\r\nint update_totalmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\n\/\/ Initialize static data member int student :: total_marks = 0;\r\n\r\n<img class=\"size-full wp-image-53 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9.png\" alt=\"\" width=\"814\" height=\"839\" \/>\r\n\r\n&nbsp;\r\n\r\nThe advantage of having one copy is that whenever the value of static data member needs to be modified, we need to modify it at one place only as there is only one copy of the same.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-54 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10.png\" alt=\"\" width=\"830\" height=\"395\" \/>\r\n<div>\r\n<p style=\"text-align: justify\">As we can see in the above program that the modification is done only once as there is only one common copy of the same. We need to invoke update_totalmarks() function on any of the objects. In the above program we have invoked this function on object stud1. We do not wish to update total marks for object 1 only, but we have invoked it on stud1 object as being a member function, it needs any object for its invocation. We could have invoked it on stud2 or stud3 as well. Invokation of update_totalmarks() on stud1 doesn\u2019t look logical, and gives impression that we wish to modify total marks of stud1. What can we do to make function invocation look more logical and convincing?<\/p>\r\n&nbsp;\r\n\r\nWell as we can define data members as static, we can also define member functions static. Defining update_totalmarks() as static can remove the need of calling update_totalmarks() on any object.\r\n\r\n&nbsp;\r\n\r\n<strong>Static Members Function<\/strong>\r\n\r\n&nbsp;\r\n\r\nA member function in a class can be declared as static. A static member function has certain special characteristics. These are:\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A static function can have access to only other static members.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A static member function can be accessed not only by the object name but also with the class name.\r\n\r\n&nbsp;\r\n\r\nFollowing program illustrates the use of static data member:\r\n\r\n&nbsp;\r\n\r\nclass\u00a0 student\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint rollno;\r\n\r\nchar\u00a0 name[20];\r\n\r\nint marks_obtained;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">static int total_marks;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public :<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\nvoid readdata();\r\n\r\nvoid displaydata();\r\n\r\nint\u00a0\u00a0 computePercentage();\r\n\r\nstatic int update_totalmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>\/\/ Initialize static data member int student :: total_marks = 0;<\/li>\r\n<\/ul>\r\nAs in the above program, we have declared update_totalmarks() as static, we need not invoke it on any object. It can be invoked by qualifying its call with class as follows :\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-55 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11.png\" alt=\"\" width=\"871\" height=\"409\" \/>\r\n\r\n<strong>Public Static Data Members<\/strong>\r\n\r\n&nbsp;\r\n\r\nStatic data member can also be declared as public.\r\n\r\n&nbsp;\r\n\r\nclass\u00a0 student\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint rollno;\r\n\r\nchar\u00a0 name[20];\r\n\r\nint marks_obtained;\r\n<div>\r\n\r\npublic :\r\n\r\n<\/div>\r\n<div>\r\n\r\nstatic int total_marks;\r\n\r\n&nbsp;\r\n\r\n\/\/ public static member\r\n\r\n<\/div>\r\n<div>\r\n\r\nvoid readdata();\r\n\r\nvoid displaydata();\r\n\r\nint\u00a0\u00a0 computePercentage();\r\n\r\nstatic int update_totalmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n};\r\n\r\n\/\/ Initialize static data member int student :: total_marks = 0;\r\n\r\n&nbsp;\r\n\r\nIn this case static member can be accessed outside the class without object as follows :\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-56 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12.png\" alt=\"\" width=\"797\" height=\"356\" \/>\r\n\r\n<strong>Advantage of Static Data Members<\/strong>\r\n\r\n&nbsp;\r\n\r\nFollowing are the advantages of using Static Data members\r\n\r\n&nbsp;\r\n<ol>\r\n \t<li>Only one copy of the data which is common for all the objects of that class is made<\/li>\r\n \t<li>As there is only one copy of the data , Updation needs to be done at one place only once<\/li>\r\n \t<li>Removes the possibility of data inconsistency<\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\n<strong>Nested Classed<\/strong>\r\n\r\n&nbsp;\r\n\r\nA nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.\r\n\r\n&nbsp;\r\n\r\nFollowing program illustrates defining nested classes :\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">class<\/td>\r\n<td style=\"width: 226.063px\">student<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">{<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">int rollno;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">char\u00a0 name[20];<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">int marks_obtained;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">static int total_marks;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">class Time<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">{<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">int hours;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">int minute;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">int seconds;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">public :<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">void readTime();<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">void displayTime();<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">time differenceTime(Time t);<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">time addTime(Time t);<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">} dob;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">public :<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">void readdata();<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">{<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cin &gt;&gt; rollno;<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cin &gt;&gt; name;<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cin &gt;&gt; marks_obtained;<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cin &gt;&gt; dob.hours;<\/td>\r\n<td style=\"width: 144.063px\">\/\/\u00a0 will not work<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">dob.readTime();<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">}<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">void displaydata();<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">{<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cout &lt;&lt; rollno;<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cout &lt;&lt; name;<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cout &lt;&lt; marks_obtained;<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">cout &lt;&lt; dob.hours;<\/td>\r\n<td style=\"width: 144.063px\">\/\/ will not work<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\"><\/td>\r\n<td style=\"width: 226.063px\">dob.displayTime();<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">}<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 12.0625px\"><\/td>\r\n<td style=\"width: 250.063px\">int<\/td>\r\n<td style=\"width: 226.063px\">computePercentage();<\/td>\r\n<td style=\"width: 144.063px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nstatic int update_totalmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nint student :: total_marks = 0;\r\n\r\nIn the above program class is declared inside the student Class.\r\n\r\n&nbsp;\r\n\r\n<strong>Limitation of Nested Classed<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe limitation of the nested class is that ists object can only be created inside the enclosing class. Its definition is not available outside the class. The same is illustrated using following example\r\n\r\n&nbsp;\r\n\r\nclass\u00a0 student\r\n\r\n{\r\n\r\nint rollno;\r\n\r\nchar\u00a0 name[20];\r\n\r\nint marks_obtained;\r\n\r\n&nbsp;\r\n\r\nstatic int total_marks;\r\n\r\nclass Time\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minute;\r\n\r\nint seconds;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\n&nbsp;\r\n\r\nvoid readTime();\r\n\r\nvoid displayTime();\r\n\r\ntime differenceTime(Time t);\r\n\r\ntime addTime(Time t);\r\n\r\n&nbsp;\r\n\r\n} dob;\r\n\r\n&nbsp;\r\n\r\nvoid readdata();\r\n\r\n&nbsp;\r\n\r\nvoid displaydata();\r\n\r\nint\u00a0\u00a0 computePercentage();\r\n\r\nstatic int update_totalmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n};\r\n\r\nint student :: total_marks = 0;\r\n\r\nvoid main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 213px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 15px\"><\/td>\r\n<td style=\"height: 28px;width: 361px\">Time\u00a0 current_time;\u00a0\u00a0 \/\/<\/td>\r\n<td style=\"height: 28px;width: 210px\">will not work;<\/td>\r\n<td style=\"height: 28px;width: 16px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 15px\"><\/td>\r\n<td style=\"height: 28px;width: 361px\">\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/td>\r\n<td style=\"height: 28px;width: 210px\"><\/td>\r\n<td style=\"width: 16px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 15px\"><\/td>\r\n<td style=\"height: 28px;width: 361px\">\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026..<\/td>\r\n<td style=\"height: 28px;width: 210px\"><\/td>\r\n<td style=\"height: 28px;width: 16px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 15px\"><\/td>\r\n<td style=\"height: 28px;width: 361px\"><\/td>\r\n<td style=\"height: 28px;width: 210px\"><\/td>\r\n<td style=\"height: 28px;width: 16px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 101px\">\r\n<td style=\"height: 101px;width: 15px\"><\/td>\r\n<td style=\"height: 101px;width: 361px\">\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.<\/td>\r\n<td style=\"height: 101px;width: 210px\"><\/td>\r\n<td style=\"height: 101px;width: 16px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can see in the above program that the object of time class cannot be created in main() function as its definition is local to the student class and is not available outside the student class.<\/p>\r\n&nbsp;\r\n\r\n<strong>Object as a Data Member<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A class can also have object of another class as data members. Limitation of nested class can be overcome by defining Time class outside the student class, and declare the object of Time class as one of the data members of student class. Following program illustrates the same:<\/p>\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minute;\r\n\r\nint seconds;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\n&nbsp;\r\n\r\nvoid readTime();\r\n\r\nvoid displayTime();\r\n\r\n&nbsp;\r\n\r\ntime differenceTime(Time t);\r\n\r\ntime addTime(Time t);\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nclass\u00a0 student\r\n\r\n{\r\n\r\nint rollno;th\r\n\r\n&nbsp;\r\n\r\nchar\u00a0 name[20];\r\n\r\nint marks_obtained;\r\n\r\nstatic int total_marks;\r\n\r\nTime dob;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\n&nbsp;\r\n\r\nvoid readdata();\r\n\r\n{\r\n\r\ncin &gt;&gt; rollno;\r\n\r\n&nbsp;\r\n\r\ncin &gt;&gt; name;\r\n\r\ncin &gt;&gt; marks_obtained;\r\n\r\ndob.readTime();\r\n\r\n}\r\n\r\nvoid displaydata();\r\n\r\n{\r\n\r\ncout &lt;&lt; rollno;\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; name;\r\n\r\ncout &lt;&lt; marks_obtained;\r\n<div>\r\n\r\ndob.displayTime();\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nint\u00a0\u00a0 computePercentage();\r\n\r\nstatic int update_totalmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nint student : : total_marks = 0;\r\n\r\n<\/div>\r\n<div>\r\n\r\nvoid function demo()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nTime\u00a0 t; \/\/\u00a0 will\u00a0 Work;\r\n\r\n..\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n\r\n<\/div>\r\n<div>\r\n\r\n}\r\n\r\n<\/div>\r\n<strong>Local Classes<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can also define Class inside any function. But when we do so, such class becomes local to that function. The definition of such class will only be available to the class in which it has been defined and object of this class cannot be defined outside this function. Following program defines class time inside main() function.<\/p>\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n&nbsp;\r\n\r\n{\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\n&nbsp;\r\n\r\nvoid readTime();\r\n\r\nvoid displayTime();\r\n\r\ntime differenceTime(time t);\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nTime current_time;\r\n\r\ncurrent_time.hours = 3;\r\n\r\ncurrent_time.minutes= 20;\r\n\r\ncurrent_time.seconds = 35;\r\n\r\n&nbsp;\r\n\r\ncurrent_time.displayTime();\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nclass Time\r\n\r\n{\r\n\r\nint hours;\r\n\r\n&nbsp;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\n&nbsp;\r\n\r\nvoid readTime();\r\n\r\n&nbsp;\r\n\r\nvoid displayTime();\r\n\r\ntime differenceTime(time t);\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nTime current_time;\r\n\r\n&nbsp;\r\n\r\ncurrent_time.hours = 3;\r\n\r\ncurrent_time.minutes= 20;\r\n\r\n&nbsp;\r\n\r\ncurrent_time.seconds = 35;\r\n\r\n&nbsp;\r\n\r\ncurrent_time.displayTime();\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid function demo()\r\n\r\n{\r\n\r\nTime\u00a0 t;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ will not Work;\r\n\r\n..\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n\r\n}\r\n<div>\r\n<p style=\"text-align: justify\">You can see in above program, class Time is defined inside the main() function. Therefore it becomes local to the main() function, and its definition is not available outside the main() function. Hence its object cannot be created in another function demo(). It is advisable that the datatype implemented through class should be defined globally and objects of that class must be defined locally.<\/p>\r\n&nbsp;\r\n\r\n<strong>Defining Class Globally<\/strong>\r\n\r\n&nbsp;\r\n\r\nFollowing program defines the class Time globally, rather than declaring as local to main().\r\n\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\n&nbsp;\r\n\r\nvoid readTime();\r\n\r\nvoid displayTime();\r\n\r\ntime differenceTime(time t);\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nTime current_time;\r\n\r\n<\/div>\r\ncurrent_time.hours = 3;\r\n\r\n&nbsp;\r\n\r\ncurrent_time.minutes= 20;\r\n\r\ncurrent_time.seconds = 35;\r\n\r\n&nbsp;\r\n\r\ncurrent_time.displayTime();\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\nvoid function demo()\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nTime\u00a0 t;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ will Work;\r\n\r\n..\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>We can define class members static using <strong>static<\/strong> keyword. A static member is shared by all objects of the class. There is only one copy of the static member.<\/li>\r\n \t<li>Defining common data members as static help save memory, makes modification simple and consistant.<\/li>\r\n \t<li>We can define classes inside another class(nested class) and inside function(local class). But it is better to define class globally.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Classes & Objects -II<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/zrLqIJg0xPM\" 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\r\n&nbsp;\r\n\r\n&nbsp;","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/zrLqIJg0xPM\" 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<p style=\"text-align: justify\">In the previous module we have seen that the Classes are basic building blocks of object oriented applications. A well-crafted class can create powerful user defined data type. Whenever we need to store information of that data type, we create variable of that data type or object of that data type. Data members can be private or public. Private members can be accessed only by member functions. Public members can be accessed outside the class through object of the class using dot operator. Whenever we create an object of any class, each object is allocated memory for each data member. For example have a look at the following program.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Need of Static Data Members<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-49 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5.png\" alt=\"\" width=\"834\" height=\"660\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5.png 834w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5-300x237.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5-768x608.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5-225x178.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-5-350x277.png 350w\" sizes=\"auto, (max-width: 834px) 100vw, 834px\" \/><\/p>\n<p style=\"text-align: justify\">When we read data from the user in these objects, objects will be holding data (assumed to be given by the user) as follows<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-50 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6.png\" alt=\"\" width=\"786\" height=\"348\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6.png 786w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6-300x133.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6-768x340.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6-65x29.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6-225x100.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-6-350x155.png 350w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/p>\n<p style=\"text-align: justify\">You will notice here that rollno, name and marks_obtained will have different values in each object as each student has different rollno and name and each will obtain different marks. But total_marks remain same for each student. You would notice that even when total_marks are same for all, they are stored in each student object. This results in memory wastage. Also this unnecessary repetition of total_marks has other problems. If we want to change the value of total_marks, even though it is common value for all, we will have to modify them separately for all objects as follows :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-51 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7.png\" alt=\"\" width=\"802\" height=\"382\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7.png 802w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7-300x143.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7-768x366.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7-225x107.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-7-350x167.png 350w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Also if we forget to change value of total_marks in one of the objects, it will result into data inconsistency as shown below. If we attempt to compute percentage of marks obtained by the students.The students whose total_marks did not get modified and remained 50, will be at disadvantage as correct percentage will not be calculated.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-52 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8.png\" alt=\"\" width=\"854\" height=\"454\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8.png 854w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8-300x159.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8-768x408.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8-225x120.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-8-350x186.png 350w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/p>\n<p><strong>Static Data Members<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can define data member of a class as static using static keyword static.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A static data member is shared by all objects of the class. There is only one copy of the static member. All static data is initialized to zero when the first object is created, if no other initialization is present. We cannot initialize in the class definition but it can be initialized outside the class.<\/p>\n<p>&nbsp;<\/p>\n<p>class\u00a0 student<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int rollno;<\/p>\n<p>char\u00a0 name[20];<\/p>\n<p>&nbsp;<\/p>\n<p>int marks_obtained;<\/p>\n<p>static int total_marks;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>&nbsp;<\/p>\n<p>void readdata();<\/p>\n<p>void displaydata();<\/p>\n<p>int\u00a0\u00a0 computePercentage();<\/p>\n<p>int update_totalmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Initialize static data member int student :: total_marks = 0;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-53 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9.png\" alt=\"\" width=\"814\" height=\"839\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9.png 814w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9-291x300.png 291w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9-768x792.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9-65x67.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9-225x232.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-9-350x361.png 350w\" sizes=\"auto, (max-width: 814px) 100vw, 814px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The advantage of having one copy is that whenever the value of static data member needs to be modified, we need to modify it at one place only as there is only one copy of the same.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-54 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10.png\" alt=\"\" width=\"830\" height=\"395\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10.png 830w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10-300x143.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10-768x365.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10-225x107.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-10-350x167.png 350w\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\">As we can see in the above program that the modification is done only once as there is only one common copy of the same. We need to invoke update_totalmarks() function on any of the objects. In the above program we have invoked this function on object stud1. We do not wish to update total marks for object 1 only, but we have invoked it on stud1 object as being a member function, it needs any object for its invocation. We could have invoked it on stud2 or stud3 as well. Invokation of update_totalmarks() on stud1 doesn\u2019t look logical, and gives impression that we wish to modify total marks of stud1. What can we do to make function invocation look more logical and convincing?<\/p>\n<p>&nbsp;<\/p>\n<p>Well as we can define data members as static, we can also define member functions static. Defining update_totalmarks() as static can remove the need of calling update_totalmarks() on any object.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Static Members Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A member function in a class can be declared as static. A static member function has certain special characteristics. These are:<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A static function can have access to only other static members.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A static member function can be accessed not only by the object name but also with the class name.<\/p>\n<p>&nbsp;<\/p>\n<p>Following program illustrates the use of static data member:<\/p>\n<p>&nbsp;<\/p>\n<p>class\u00a0 student<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int rollno;<\/p>\n<p>char\u00a0 name[20];<\/p>\n<p>int marks_obtained;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">static int total_marks;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public :<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>void readdata();<\/p>\n<p>void displaydata();<\/p>\n<p>int\u00a0\u00a0 computePercentage();<\/p>\n<p>static int update_totalmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>\/\/ Initialize static data member int student :: total_marks = 0;<\/li>\n<\/ul>\n<p>As in the above program, we have declared update_totalmarks() as static, we need not invoke it on any object. It can be invoked by qualifying its call with class as follows :<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-55 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11.png\" alt=\"\" width=\"871\" height=\"409\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11.png 871w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11-300x141.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11-768x361.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11-225x106.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-11-350x164.png 350w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/p>\n<p><strong>Public Static Data Members<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Static data member can also be declared as public.<\/p>\n<p>&nbsp;<\/p>\n<p>class\u00a0 student<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int rollno;<\/p>\n<p>char\u00a0 name[20];<\/p>\n<p>int marks_obtained;<\/p>\n<div>\n<p>public :<\/p>\n<\/div>\n<div>\n<p>static int total_marks;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ public static member<\/p>\n<\/div>\n<div>\n<p>void readdata();<\/p>\n<p>void displaydata();<\/p>\n<p>int\u00a0\u00a0 computePercentage();<\/p>\n<p>static int update_totalmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>\/\/ Initialize static data member int student :: total_marks = 0;<\/p>\n<p>&nbsp;<\/p>\n<p>In this case static member can be accessed outside the class without object as follows :<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-56 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12.png\" alt=\"\" width=\"797\" height=\"356\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12.png 797w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12-300x134.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12-768x343.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12-65x29.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12-225x101.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-12-350x156.png 350w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/p>\n<p><strong>Advantage of Static Data Members<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Following are the advantages of using Static Data members<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li>Only one copy of the data which is common for all the objects of that class is made<\/li>\n<li>As there is only one copy of the data , Updation needs to be done at one place only once<\/li>\n<li>Removes the possibility of data inconsistency<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>Nested Classed<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.<\/p>\n<p>&nbsp;<\/p>\n<p>Following program illustrates defining nested classes :<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">class<\/td>\n<td style=\"width: 226.063px\">student<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">{<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">int rollno;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">char\u00a0 name[20];<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">int marks_obtained;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">static int total_marks;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">class Time<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">{<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">int hours;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">int minute;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">int seconds;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">public :<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">void readTime();<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">void displayTime();<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">time differenceTime(Time t);<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">time addTime(Time t);<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">} dob;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">public :<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">void readdata();<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">{<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cin &gt;&gt; rollno;<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cin &gt;&gt; name;<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cin &gt;&gt; marks_obtained;<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cin &gt;&gt; dob.hours;<\/td>\n<td style=\"width: 144.063px\">\/\/\u00a0 will not work<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">dob.readTime();<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">}<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">void displaydata();<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">{<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cout &lt;&lt; rollno;<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cout &lt;&lt; name;<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cout &lt;&lt; marks_obtained;<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">cout &lt;&lt; dob.hours;<\/td>\n<td style=\"width: 144.063px\">\/\/ will not work<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\"><\/td>\n<td style=\"width: 226.063px\">dob.displayTime();<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">}<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 12.0625px\"><\/td>\n<td style=\"width: 250.063px\">int<\/td>\n<td style=\"width: 226.063px\">computePercentage();<\/td>\n<td style=\"width: 144.063px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>static int update_totalmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>int student :: total_marks = 0;<\/p>\n<p>In the above program class is declared inside the student Class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Limitation of Nested Classed<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The limitation of the nested class is that ists object can only be created inside the enclosing class. Its definition is not available outside the class. The same is illustrated using following example<\/p>\n<p>&nbsp;<\/p>\n<p>class\u00a0 student<\/p>\n<p>{<\/p>\n<p>int rollno;<\/p>\n<p>char\u00a0 name[20];<\/p>\n<p>int marks_obtained;<\/p>\n<p>&nbsp;<\/p>\n<p>static int total_marks;<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minute;<\/p>\n<p>int seconds;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>&nbsp;<\/p>\n<p>void readTime();<\/p>\n<p>void displayTime();<\/p>\n<p>time differenceTime(Time t);<\/p>\n<p>time addTime(Time t);<\/p>\n<p>&nbsp;<\/p>\n<p>} dob;<\/p>\n<p>&nbsp;<\/p>\n<p>void readdata();<\/p>\n<p>&nbsp;<\/p>\n<p>void displaydata();<\/p>\n<p>int\u00a0\u00a0 computePercentage();<\/p>\n<p>static int update_totalmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<p>};<\/p>\n<p>int student :: total_marks = 0;<\/p>\n<p>void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 213px\">\n<tbody>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 15px\"><\/td>\n<td style=\"height: 28px;width: 361px\">Time\u00a0 current_time;\u00a0\u00a0 \/\/<\/td>\n<td style=\"height: 28px;width: 210px\">will not work;<\/td>\n<td style=\"height: 28px;width: 16px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 15px\"><\/td>\n<td style=\"height: 28px;width: 361px\">\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/td>\n<td style=\"height: 28px;width: 210px\"><\/td>\n<td style=\"width: 16px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 15px\"><\/td>\n<td style=\"height: 28px;width: 361px\">\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026..<\/td>\n<td style=\"height: 28px;width: 210px\"><\/td>\n<td style=\"height: 28px;width: 16px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 15px\"><\/td>\n<td style=\"height: 28px;width: 361px\"><\/td>\n<td style=\"height: 28px;width: 210px\"><\/td>\n<td style=\"height: 28px;width: 16px\"><\/td>\n<\/tr>\n<tr style=\"height: 101px\">\n<td style=\"height: 101px;width: 15px\"><\/td>\n<td style=\"height: 101px;width: 361px\">\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.<\/td>\n<td style=\"height: 101px;width: 210px\"><\/td>\n<td style=\"height: 101px;width: 16px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can see in the above program that the object of time class cannot be created in main() function as its definition is local to the student class and is not available outside the student class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Object as a Data Member<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A class can also have object of another class as data members. Limitation of nested class can be overcome by defining Time class outside the student class, and declare the object of Time class as one of the data members of student class. Following program illustrates the same:<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minute;<\/p>\n<p>int seconds;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>&nbsp;<\/p>\n<p>void readTime();<\/p>\n<p>void displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>time differenceTime(Time t);<\/p>\n<p>time addTime(Time t);<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>class\u00a0 student<\/p>\n<p>{<\/p>\n<p>int rollno;th<\/p>\n<p>&nbsp;<\/p>\n<p>char\u00a0 name[20];<\/p>\n<p>int marks_obtained;<\/p>\n<p>static int total_marks;<\/p>\n<p>Time dob;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>&nbsp;<\/p>\n<p>void readdata();<\/p>\n<p>{<\/p>\n<p>cin &gt;&gt; rollno;<\/p>\n<p>&nbsp;<\/p>\n<p>cin &gt;&gt; name;<\/p>\n<p>cin &gt;&gt; marks_obtained;<\/p>\n<p>dob.readTime();<\/p>\n<p>}<\/p>\n<p>void displaydata();<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; rollno;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; name;<\/p>\n<p>cout &lt;&lt; marks_obtained;<\/p>\n<div>\n<p>dob.displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>int\u00a0\u00a0 computePercentage();<\/p>\n<p>static int update_totalmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>int student : : total_marks = 0;<\/p>\n<\/div>\n<div>\n<p>void function demo()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>Time\u00a0 t; \/\/\u00a0 will\u00a0 Work;<\/p>\n<p>..\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<\/div>\n<div>\n<p>}<\/p>\n<\/div>\n<p><strong>Local Classes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can also define Class inside any function. But when we do so, such class becomes local to that function. The definition of such class will only be available to the class in which it has been defined and object of this class cannot be defined outside this function. Following program defines class time inside main() function.<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/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>&nbsp;<\/p>\n<p>void readTime();<\/p>\n<p>void displayTime();<\/p>\n<p>time differenceTime(time t);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Time current_time;<\/p>\n<p>current_time.hours = 3;<\/p>\n<p>current_time.minutes= 20;<\/p>\n<p>current_time.seconds = 35;<\/p>\n<p>&nbsp;<\/p>\n<p>current_time.displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>&nbsp;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>&nbsp;<\/p>\n<p>void readTime();<\/p>\n<p>&nbsp;<\/p>\n<p>void displayTime();<\/p>\n<p>time differenceTime(time t);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Time current_time;<\/p>\n<p>&nbsp;<\/p>\n<p>current_time.hours = 3;<\/p>\n<p>current_time.minutes= 20;<\/p>\n<p>&nbsp;<\/p>\n<p>current_time.seconds = 35;<\/p>\n<p>&nbsp;<\/p>\n<p>current_time.displayTime();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void function demo()<\/p>\n<p>{<\/p>\n<p>Time\u00a0 t;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ will not Work;<\/p>\n<p>..\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<p>}<\/p>\n<div>\n<p style=\"text-align: justify\">You can see in above program, class Time is defined inside the main() function. Therefore it becomes local to the main() function, and its definition is not available outside the main() function. Hence its object cannot be created in another function demo(). It is advisable that the datatype implemented through class should be defined globally and objects of that class must be defined locally.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Defining Class Globally<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Following program defines the class Time globally, rather than declaring as local to main().<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>&nbsp;<\/p>\n<p>void readTime();<\/p>\n<p>void displayTime();<\/p>\n<p>time differenceTime(time t);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>Time current_time;<\/p>\n<\/div>\n<p>current_time.hours = 3;<\/p>\n<p>&nbsp;<\/p>\n<p>current_time.minutes= 20;<\/p>\n<p>current_time.seconds = 35;<\/p>\n<p>&nbsp;<\/p>\n<p>current_time.displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>void function demo()<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Time\u00a0 t;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ will Work;<\/p>\n<p>..\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>We can define class members static using <strong>static<\/strong> keyword. A static member is shared by all objects of the class. There is only one copy of the static member.<\/li>\n<li>Defining common data members as static help save memory, makes modification simple and consistant.<\/li>\n<li>We can define classes inside another class(nested class) and inside function(local class). But it is better to define class globally.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Classes &#38; Objects -II<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/zrLqIJg0xPM\" 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<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":5,"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-48","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\/48","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":9,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/48\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/48\/revisions\/387"}],"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\/48\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=48"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=48"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}