Classes, objects and methods


Published on

Published in: Education
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Classes, objects and methods

  1. 1. Lecture slides by: Farhan Amjad A new paradigm of programming
  2. 2. <ul><li>What classes, objects, member functions and data members are. </li></ul><ul><li>How to define a class and use it to create an object. </li></ul><ul><li>How to define member functions in a class to implement the class's behaviors. </li></ul><ul><li>How to declare data members in a class to implement the class's attributes. </li></ul><ul><li>How to call a member function of an object to make that member function perform its task. </li></ul><ul><li>The differences between data members of a class and local variables of a function. </li></ul><ul><li>How to use a constructor to ensure that an object's data is initialized when the object is created. </li></ul><ul><li>How to engineer a class to separate its interface from its implementation and encourage reuse. </li></ul>
  3. 3. <ul><li>A CLASS is a grouping of data and functions. A class is very much like a structure type as used in C language. It is only a pattern (a template) to be used to create a variable which can be manipulated in a program. A class is a description of similar objects. </li></ul><ul><li>Classes are designed to give certain services. </li></ul><ul><li>An OBJECT is an instance of a class, which is similar to a variable defined as an instance of a type. An object is what you actually use in a program. Objects are instances of classes </li></ul><ul><li>An ATTRIBUTE is a data member of a class that can take different values for different instances (objects) of this class. Example: Name of a student. </li></ul>
  4. 4. <ul><li>A method (member function) is a function contained within the class. You will find the functions used within class often referred to as methods in programming literature. </li></ul><ul><li>A message is the same thing as a function call. In object oriented programming. We send messages instead of calling functions. For the time being you can think of them as identical. </li></ul><ul><li>Messages are sent to objects to get some services from them. </li></ul>
  5. 5. <ul><li>Similarities </li></ul><ul><ul><li>Both are derived types. </li></ul></ul><ul><ul><li>Both present similar features </li></ul></ul><ul><ul><li>Both are automatic typedefs </li></ul></ul><ul><li>Class </li></ul><ul><ul><li>private members by default </li></ul></ul><ul><li>Structure </li></ul><ul><ul><li>public members by default </li></ul></ul><ul><ul><li>The data members are thus accessible out of the structure implementation </li></ul></ul><ul><li>Classes are more advanced than structures </li></ul>
  6. 6. <ul><li>A class is a user-defined prototype from which one </li></ul><ul><li>can instantiate objects of a particular type in the </li></ul><ul><li>same way one generates integer variables using </li></ul><ul><li>the built-in data type int. </li></ul><ul><li>A class contains data (member data) and functions </li></ul><ul><li>(membership functions). </li></ul>Class data1 data2 data3 functiona() functionb() functionc()
  7. 7. <ul><li>class welcome { </li></ul><ul><li>public: // access specifier </li></ul><ul><li>void display(){ </li></ul><ul><li>cout<<“welcome to OOP class”; </li></ul><ul><li>} }; </li></ul><ul><li>void main (){ </li></ul><ul><li>welcome w1; </li></ul><ul><li>w1.display(); </li></ul><ul><li>} Output: welcome to OOP class ; </li></ul><ul><li>Forgetting the semicolon at the end of a class definition is a syntax error. </li></ul><ul><li>Defining a function inside another function is a syntax error. </li></ul>
  8. 8. <ul><li>class welcome { </li></ul><ul><li>public: </li></ul><ul><li>void display(string course){ </li></ul><ul><li>cout<<“welcome to the class of ”<<course; </li></ul><ul><li>} }; </li></ul><ul><li>void main (){ </li></ul><ul><li>String Coursename; </li></ul><ul><li>welcome w1; </li></ul><ul><li>cout << &quot;Please enter the course name:&quot; << endl; </li></ul><ul><li>Cin>>nameOfCourse ; </li></ul><ul><li>w1.display(Coursename); </li></ul><ul><li>} </li></ul>
  9. 9. <ul><li>class welcome { </li></ul><ul><li>private: string courseName; </li></ul><ul><li> public: </li></ul><ul><li>void setCourseName( string name ) </li></ul><ul><li>{ courseName = name; } </li></ul><ul><li>string getCourseName() { </li></ul><ul><li>return courseName; } </li></ul><ul><li>void display(string course){ </li></ul><ul><li>cout<<“welcome to the class of ”<<getCourseName() ; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>void main (){ </li></ul><ul><li>String Course; </li></ul><ul><li>welcome w1; </li></ul><ul><li>cout << &quot; Please enter the course name:“; </li></ul><ul><li>Cin>> course; </li></ul><ul><li>W1..setCourseName( Course ); </li></ul><ul><li>Cout<<“Course name of W1 is: “<<w1.getCourseName(); </li></ul><ul><li>w1.display(); </li></ul><ul><li>} </li></ul>
  10. 10. <ul><li>Class smallobj{ </li></ul><ul><li>private: </li></ul><ul><li>int somedata; </li></ul><ul><li>Public: </li></ul><ul><li>void setdata (int d){ </li></ul><ul><li>somedata=d; </li></ul><ul><li> } </li></ul><ul><li>Void showdata(){ </li></ul><ul><li>cout<<“Data is……”<<somedata </li></ul><ul><li>} </li></ul><ul><li>Void main() </li></ul><ul><li>{Smallobj s1, s2; </li></ul><ul><li>S1.setdata(23); S2.setdata(54); </li></ul><ul><li>S1.showdata(); S2.showdata(); </li></ul><ul><li>} </li></ul>
  11. 11. <ul><li>Class data member cannot be initialized at declaration time. </li></ul><ul><li>By default all data members have garbage value. </li></ul>
  12. 12. <ul><li>class Date // declares class name </li></ul><ul><li>{ </li></ul><ul><li>private: // not visible outside the class </li></ul><ul><li>int day, month, year; // member data </li></ul><ul><li>public: // visible interface </li></ul><ul><li>void init(int d, int m, int y); // initialize date </li></ul><ul><li>void add_year(int n); // add n years </li></ul><ul><li>void add_month(int n); // add n months </li></ul><ul><li>void add_day(int n); // add n days </li></ul><ul><li>void show_date(); // displays date </li></ul><ul><li>}; // do not forget the ; here !!! </li></ul><ul><li>In our example first data and then functions are written, it is also possible to write in reverse order. </li></ul><ul><li>Data and functions of the class are called members of the class. </li></ul><ul><li>In our class body of the function is written outside of the class, if it is written inside the class then it is called inline function (macro) . </li></ul>
  13. 13. Private Class private: public: data1 data2 functiond() functiona() functionb() functionc() private part of class can be accessed by member functions of the same class only public part of class constitutes the interface that can be used by other parts of the program typically member data is private whereas member functions are mostly public
  14. 14. <ul><li>Data hiding is mechanism to protect data from accidentally or deliberately being manipulated from </li></ul><ul><li>other parts of the program </li></ul><ul><li>The primary mechanism for data hiding is to put it </li></ul><ul><li>in a class and make it private. </li></ul><ul><li>Private data can only be accessed from within the class </li></ul><ul><li>Public data or functions are accessible from outside the class </li></ul>
  15. 15. <ul><li>Binary scope resolution operator ( :: ) </li></ul><ul><ul><li>Specifies which class owns the member function </li></ul></ul><ul><ul><li>Different classes can have the same name for member functions </li></ul></ul><ul><li>Format for definition class member functions </li></ul><ul><li>ReturnType ClassName :: MemberFunctionName( ){ </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>If member function is defined inside the class </li></ul><ul><ul><li>Scope resolution operator and class name are not needed </li></ul></ul>
  16. 16. <ul><li>:: scope resulution operator </li></ul><ul><li>void Date::init(int d, int m, int y) </li></ul><ul><li>{ </li></ul><ul><li>day=d; </li></ul><ul><li>month=m; </li></ul><ul><li>year = y; </li></ul><ul><li>} </li></ul><ul><li>void Date::add_month(int n) </li></ul><ul><li>{ </li></ul><ul><li>month+=n; </li></ul><ul><li>year+= (month)/12; </li></ul><ul><li>} </li></ul>
  17. 17. <ul><li>#include <iostream.h> </li></ul><ul><li>void Date::show_date() // Date:: specifies that show_date is a </li></ul><ul><li>// member function of class Date </li></ul><ul><li>{ </li></ul><ul><li>cout << d << ”.” << m << ”.” << y << endl; </li></ul><ul><li>} </li></ul><ul><li>void Date::add_year(int n) </li></ul><ul><li>{ </li></ul><ul><li>year+=y; </li></ul><ul><li>} </li></ul><ul><li>void Date::add_day(int n) </li></ul><ul><li>{ </li></ul><ul><li>… . </li></ul><ul><li>} </li></ul>
  18. 18. <ul><li>class Date // class definition </li></ul><ul><li>{ … }; </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>Date d1; // instantiate a variable/object d1 of class Date </li></ul><ul><li>Date d2; // instantiate a second object d2 of class Date </li></ul><ul><li>d1.init(15,3,2001); // call member function init for object d1 </li></ul><ul><li>d1.show_date(); // member function show_date for d1 </li></ul><ul><li>d2.init(10,1,2000); // call member function init for object d1 </li></ul><ul><li>d2.show_date(); // member function show_date for d1 </li></ul><ul><li>} </li></ul>
  19. 19. object d1 data: functions: d=15; m=3; y=2001; object d2 data: function: d=10; m=1; y=2000; void add_day(int n); void add_year(int n); void show_date(); class Date void add_day(int n) { … } void add_year(int n) { … } void show_date() { … } void add_day(int n); void add_year(int n); void show_date();
  20. 20. <ul><li>A class member function can only be called in </li></ul><ul><li>association with a specific object of that class using </li></ul><ul><li>the dot operator (period) </li></ul><ul><li>Date d1; // object of class d1 </li></ul><ul><li>d1.init(12,3,2001); // call member function init for d1 </li></ul><ul><li>init(13,2,2000); // illegal, no object associated to call init </li></ul><ul><li>A member function always operates on a specific </li></ul><ul><li>object not the class itself. </li></ul><ul><li>Each objects has its own data but objects of the same class share the same member function code </li></ul><ul><li>In other OO languages member function calls are also </li></ul><ul><li>called messages . </li></ul>
  21. 21. <ul><li>◆ Visibility: in structures, all members are by default visible to anyone. </li></ul><ul><li>C++ however adds the ability to make the distinction between public and non-public members. </li></ul><ul><li>◆ Levels of Visibility: </li></ul><ul><li>● Public: members are visible to any function including member, nonmember </li></ul><ul><li>and members of other ures. </li></ul><ul><li>● Private: members are only visible to other member functions of the ures. </li></ul><ul><li>Data hiding (encapsulation), can prevent unanticipated modifications to the data members. </li></ul><ul><li>● Protected: at this stage, the same as private. </li></ul><ul><li>Members are available also to members of inheriting classes. </li></ul><ul><li>◆ In structures by default visibility is public and in classes default visibility is private. </li></ul>
  22. 22. <ul><li>#inclule………………. </li></ul><ul><li>int abs(int ); </li></ul><ul><li>double abs(double ); </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>cout << abs(-10) << &quot; &quot;; </li></ul><ul><li>cout << abs(-11.0) << &quot; &quot;; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>int abs(int i) </li></ul><ul><li>{ </li></ul><ul><li>cout << &quot;Using integer abs() &quot;; </li></ul><ul><li>return i<0 ? -i : i; </li></ul><ul><li>} </li></ul>double abs(double d) { cout << &quot;Using double abs() &quot;; return d<0.0 ? -d : d; }
  23. 23. <ul><li>#include <string.h> </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>Class String { </li></ul><ul><li>public: </li></ul><ul><li>void assign (char* st) { </li></ul><ul><li>Strcpy (s, st); </li></ul><ul><li>Len = strlen(st); </li></ul><ul><li>} </li></ul><ul><li>int length () { return len; } </li></ul><ul><li>Void print() { </li></ul><ul><li>cout<<s << “ Length : “<<len <<“ ”; } </li></ul><ul><li>Private: </li></ul><ul><li>maxLen = 222; Char s[maxLen]; </li></ul><ul><li>Int len; }; </li></ul>
  24. 24. <ul><li>String s1, s2, s3,s4; </li></ul><ul><li>Cin>>s2>>s3; </li></ul><ul><li>S3=“welcome”+s2; </li></ul><ul><li>S4=s1+s2; </li></ul><ul><li>S3=s1+ “ “ + s2; </li></ul><ul><li>S4= “Hello” + “there!” //illegal </li></ul><ul><li>Cout<<s1.length(); n=s1.length(); </li></ul><ul><li>S1= “Hi dear how r u?” </li></ul><ul><li>S1.find(“hi”); s1.find(s2); </li></ul><ul><li>S4=s21.substr(0,5); </li></ul><ul><li>S2.swap(s1); </li></ul>
  25. 25. <ul><li>A constructor is a member function that is automatically invoked when a new object of a class is </li></ul><ul><li>instantiated. </li></ul><ul><li>The constructor must always have the same name as </li></ul><ul><li>the class and has no return type. </li></ul><ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>Date(); // constructor for class Date </li></ul><ul><li>}; </li></ul>
  26. 26. <ul><li>Constructor </li></ul><ul><ul><li>Special member function that initializes data members of a class object </li></ul></ul><ul><ul><li>Constructors cannot return values </li></ul></ul><ul><ul><li>Same name as the class </li></ul></ul><ul><li>Declarations </li></ul><ul><ul><li>Once class defined, can be used as a data type </li></ul></ul>Time sunset, // object of type Time arrayOfTimes[ 5 ], // array of Time objects *pointerToTime, // pointer to a Time object &dinnerTime = sunset; // reference to a Time object Note: The class name becomes the new type specifier.
  27. 27. <ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Date(); // constructor for class Date </li></ul><ul><li>private: </li></ul><ul><li>int day; </li></ul><ul><li>int month; </li></ul><ul><li>int year; </li></ul><ul><li>}; </li></ul><ul><li>Date::Date() : day(1), month(1), year(1900) </li></ul><ul><li>// initialize int day with 1, int month with 1 etc. </li></ul><ul><li>{ </li></ul><ul><li>//empty body </li></ul><ul><li>}; </li></ul>
  28. 28. <ul><li>two types of member initialization in class constructor </li></ul><ul><ul><li>by initialization : members are initialized before the constructor is executed (necessary for data members tha have no default constructor) </li></ul></ul><ul><ul><li>by assignment : members are created per default constructor first and then a value is assigned to them </li></ul></ul><ul><li>Date::Date(int d, int m, int y) : day(d), month(m), year(y) </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>Date::Date(int d, int m, int y) // assignment initialization </li></ul><ul><li>{ </li></ul><ul><li>day=d; </li></ul><ul><li>month=m; </li></ul><ul><li>year=y; </li></ul><ul><li>} </li></ul>
  29. 29. <ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Date(); // default constructor with standard date 1.1.1900 </li></ul><ul><li>Date(int d, int m, int y); // constructor with day, month, year </li></ul><ul><li>Date(string date); // constructor with string </li></ul><ul><li>private: </li></ul><ul><li>int day; </li></ul><ul><li>int month; </li></ul><ul><li>int year; </li></ul><ul><li>}; </li></ul>
  30. 30. <ul><li>#include <stdlib> </li></ul><ul><li>#include <string> </li></ul><ul><li>Date::Date(int d, int m, int y) : day(d), month(m), year(y) </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>Date::Date(string str) // constructor with string </li></ul><ul><li>{ </li></ul><ul><li>day=atoi(str.substr(0,2).c_str()); </li></ul><ul><li>month=atoi(str.substr(3,2).c_str()); </li></ul><ul><li>year=atoi(str.substr(6,4).c_str()); </li></ul><ul><li>} </li></ul>
  31. 31. <ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>Date d1; // default constructor date 1.1.1900 </li></ul><ul><li>Date d2(12,9,1999); // Date(int d, int m, int y) constructor </li></ul><ul><li>Date d3(”17.2.1998”); // Date(string date) constructor </li></ul><ul><li>} </li></ul>
  32. 32. <ul><li>Destructors are automatically called when an object is destroyed </li></ul><ul><li>The most common use for destructors is to deallocate memory </li></ul><ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Date(); // constructor </li></ul><ul><li>~Date(); // destructor </li></ul><ul><li>… </li></ul><ul><li>}; </li></ul><ul><li>if … </li></ul><ul><li>{ </li></ul><ul><li>Date d1; </li></ul><ul><li>… </li></ul><ul><li>} // destructor for d1 is invoked </li></ul>
  33. 33. <ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Date(int d=1, int m=1, int y=1900); // constructor with </li></ul><ul><li>// day, month, year and default values </li></ul><ul><li>}; </li></ul><ul><li>Date::Date(int d, int m, int y) : day(d), month(m), year(y) {} </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>Date d1; // 1.1.1900 </li></ul><ul><li>Date d2(5); // 5.1.1900 </li></ul><ul><li>Date d3(15,8); // 15.8.1900 </li></ul><ul><li>Date d4(12,10,1998); // 12.10.1998 </li></ul><ul><li>} </li></ul>
  34. 34. <ul><li>The copy constructor initializes on object with </li></ul><ul><li>another object of the same class. </li></ul><ul><li>Each class possesses a built-in default copy constructor which is a one-argument constructor </li></ul><ul><li>which argument is an object of the same class </li></ul><ul><li>The default copy constructor can be overloaded by explicitly defining a constructor Date(Date& d) </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>Date d1(12,4,1997); </li></ul><ul><li>Date d2(d1); // default copy constructor </li></ul><ul><li>Date d3=d1; // also default copy constructor </li></ul><ul><li>} </li></ul>
  35. 35. <ul><li>Class objects can become arguments to functions in </li></ul><ul><li>the same way as ordinary built-in data type parameters </li></ul><ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>int diff(Date d); // computes the number of days between two dates </li></ul><ul><li>}; </li></ul><ul><li>int Date::diff(Date d) </li></ul><ul><li>{ </li></ul><ul><li>int; // access member data day of object d </li></ul><ul><li>n+= 30 * (month – d.month); // approximately correct… </li></ul><ul><li>n+= 365 (year – d.year); </li></ul><ul><li>return n; </li></ul><ul><li>} </li></ul>
  36. 36. <ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>void add_days (Date d, int n); // computes the date d + n days </li></ul><ul><li>}; </li></ul><ul><li>void Date::add_days(Date d, int n) </li></ul><ul><li>{ </li></ul><ul><li> + n % 30; </li></ul><ul><li>month = d.month + (n % 365) / 30; </li></ul><ul><li>year = d.year + n / 365; </li></ul><ul><li>} }; </li></ul><ul><li>Int main(){ </li></ul><ul><li>Date d1(14,5,2000); </li></ul><ul><li>Date d2; </li></ul><ul><li>d2.add_days(d1,65); // d2 set to 29.7.2000 </li></ul><ul><li>Date d1(14,5,2000); </li></ul><ul><li>Date d2(10,4,2000); </li></ul><ul><li>cout << d1.diff(d2) << endl; // difference between d1 and d2 </li></ul>
  37. 37. object d2 day month year object d1 day month year d2.add_days(d1,65); day Member function of d2 refers to its own data member day directly Member function of d2 refers to data member day in object d1 using the dot operator
  38. 38. <ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>Date get_date (int n); // returns the date + n days </li></ul><ul><li>}; </li></ul><ul><li>Date Date::get_date(Date n) </li></ul><ul><li>{ </li></ul><ul><li>Date temp; </li></ul><ul><li>; </li></ul><ul><li>temp.month = month + n.month; </li></ul><ul><li>temp.year = year + n.year; </li></ul><ul><li>return temp; </li></ul><ul><li>} </li></ul><ul><li>Main(){ </li></ul><ul><li>Date d1(14,5,2000); </li></ul><ul><li>Date d3; </li></ul><ul><li>Date d2=d1.get_date(d3); // d2 set to 29.7.2000 </li></ul>
  39. 39. <ul><li>A member function that is declared as constant does </li></ul><ul><li>not modify the data of the object </li></ul><ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>int year() const; // const year() does not modify data </li></ul><ul><li>void add_year(int n); // non-const add_year() modifies data </li></ul><ul><li>}; </li></ul><ul><li>int Date::year() const // defined as const </li></ul><ul><li>{ return year; } </li></ul><ul><li>int Date::add_year(int n) </li></ul><ul><li>{ year+=n; } </li></ul>
  40. 40. <ul><li>A const member function can be invoked for const and non-const objects, whereas a non-const member </li></ul><ul><li>function can only be invoked for non-const objects </li></ul><ul><li>void f(Date &d, const Date &cd) </li></ul><ul><li>{ </li></ul><ul><li>int i=d.year(); // ok </li></ul><ul><li>d.add_year(2); // ok </li></ul><ul><li>int j=cd.year(); // ok </li></ul><ul><li>cd.add_year(3); // error cannot change const object cd </li></ul><ul><li>} </li></ul>
  41. 41. <ul><li>Normally each object possesses its own separate data members </li></ul><ul><li>A static data member is similar to a normal static variable and is shared among all objects of the same </li></ul><ul><li>class </li></ul><ul><li>There is exactly one copy of a static data member </li></ul><ul><li>rather than one copy per object as for non-static </li></ul><ul><li>variables </li></ul>
  42. 42. <ul><li>Class Date </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>static int counter=0; </li></ul><ul><li>int day, month, year; </li></ul><ul><li>public: </li></ul><ul><li>Date() { counter++; } </li></ul><ul><li>~Date() { counter--; } </li></ul><ul><li>void display() </li></ul><ul><li>{ </li></ul><ul><li>cout << ”There are ” << counter << ” Foo objects!”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>
  43. 43. <ul><li>Date f1, Date f2; </li></ul><ul><li>f1.display(); // 2 foo objects </li></ul><ul><li>Date f3; </li></ul><ul><li>f1.display(); // 3 foo objects </li></ul><ul><li>// f3 gets destroyed </li></ul><ul><li>f1.display(); // 2 foo objects </li></ul>
  44. 44. <ul><li>class Date </li></ul><ul><li>{ </li></ul><ul><li>int days_per_month(); </li></ul><ul><li>int days_per_year(); </li></ul><ul><li>bool leap_year(); </li></ul><ul><li>static int days_in_month[12] = </li></ul><ul><li>{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; </li></ul><ul><li>}; </li></ul>
  45. 45. <ul><li>bool Date::leap_year() { </li></ul><ul><li>return ((( year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); </li></ul><ul><li>} </li></ul><ul><li>int Date::days_per_month() { </li></ul><ul><li>if ((month==2) && leap_year()) </li></ul><ul><li>return 29; </li></ul><ul><li>else </li></ul><ul><li>return days_in_month[month-1]; </li></ul><ul><li>} </li></ul><ul><li>int Date::days_per_year() { </li></ul><ul><li>If leap_year() </li></ul><ul><li>return 366; </li></ul><ul><li>else </li></ul><ul><li>return 365; </li></ul><ul><li>} </li></ul>
  46. 46. <ul><li>Class student { </li></ul><ul><ul><li>String name; </li></ul></ul><ul><ul><li>int Reg; </li></ul></ul><ul><ul><li>int marks; </li></ul></ul><ul><ul><li>Student(){ } </li></ul></ul><ul><ul><li>Void print() { cout<<name<<reg<<marks;} </li></ul></ul><ul><li>}; </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>Student s[16]; </li></ul><ul><li>S[3].name= “Awae hi”; cout<< S[3].print(); } </li></ul>
  47. 47. <ul><li>Deitel & Deitel: chapters 3, 8 </li></ul><ul><li>Stroustrup: chapters 10 </li></ul><ul><li>Lafore : chapters 6 </li></ul><ul><li>Lippman : chapters 13, 14 </li></ul><ul><li>D.S.Malik: chapter 6, 8 </li></ul>