{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS201-40 - Introduction to Programming Lecture 40 Class...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 40 Class Class is a user defined data type. Data Hiding Public Interface Yes you can use user defined data type as a member of the class Class can contain objects. class Date { private : int month , day , year ; public : // member functions } ; Example class Person { private : char * name ; char * address ; Date dateOfBirth ; // member object public : // public member functions . . . } ; Example Person :: Person ( char * name , char * address , int day , int month , int year ) { dateOfBirth.setDay ( dy ) ; dateOfBirth.setMonth ( mn ) ; dateOfBirth.setYear ( yr ) ; // statement ( s ) ; } Initializer List Person :: Person ( char * name , char * address , int day, int month, int year ) : Date ( int month , int day , int year ) Constructor Date :: Date ( int day , int month , int year ) { cout << “Inside Date constructor ” ; } Person :: Person ( char * name , char * address ,int day , int month , int year ) : Date ( month , day , year ) { cout << “Inside Person constructor” ; } ~ Date ( ) { cout << “Inside Date destructor ” ; } ~ Person ( ) { cout << “Inside Person destructor” ; } Destructor class Column { private : int size ; public : Column ( ) { cout << "Column Created" << endl ; } void display ( ) ; void set ( int ) ; Example ~ Column ( ) { cout << "Column destroyed" << endl ; } } ; class Row { private : int size ; Column col ; public : void set ( int ) ; Row ( ) { cout << "Inside Constructor for object Row" << endl ; } ~ Row ( ) { cout << "Row destroyed" << endl ; } void display ( ) ; } ; Example class Matrix { private : Row row ; int size ; public : Matrix ( ) { cout << "Matrix Created" << endl << endl ; } ~ Matrix ( ) { cout << "Matrix destroyed" << endl << endl ; } void display ( ) ; } ; Example main( ) { Matrix m ; m.display ( ) ; } Example Column Created Output Inside Constructor for object Row Matrix Created …. Matrix Destroyed Row Destroyed Column Destroyed Code Reuse const Date dateOfBirth ; Structure inside a structure Class inside a class class Outer { ……. class Inner1 { private : // Data members //data functions public : // Data members //data functions } ; //data members //data functions } ; Example Class inside a class Inner2 Inner1 Outer Friend class Outer { public : class Inner ; friend class Inner ; class Inner { Example }; // Data members // Member functions } ; friend class outer ; // Data members ReturnType className :: functionName ( ) { // Statement ( s ) ; definition } } Outer :: Inner :: Inner ( ) { // definition } class Outer { class Inner1 { class Inner2 { class Inner3 { // Data members and functions // and perhaps more inner classes } ; // Data members and functions } ; // Data members and functions 48: } ; } ; Example ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern