introduction to programming lecture 40. class class is a user defined data type

28
Introduction to Introduction to Programming Programming Lecture 40 Lecture 40

Upload: helena-whitehead

Post on 06-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Data Hiding

TRANSCRIPT

Page 1: Introduction to Programming Lecture 40. Class Class is a user defined data type

Introduction to Introduction to ProgrammingProgramming

Lecture 40Lecture 40

Page 2: Introduction to Programming Lecture 40. Class Class is a user defined data type

ClassClass

Class is a userClass is a userdefined data type. defined data type.

Page 3: Introduction to Programming Lecture 40. Class Class is a user defined data type

Data Data HidingHiding

Page 4: Introduction to Programming Lecture 40. Class Class is a user defined data type

Public Public InterfaceInterface

Page 5: Introduction to Programming Lecture 40. Class Class is a user defined data type

Yes you can use userYes you can use userdefined data type as a defined data type as a member of the classmember of the classClass can contain Class can contain objects.objects.

Page 6: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Dateclass Date{{private :private : int month , day , year ;int month , day , year ;public :public : // member functions// member functions

} ;} ;

Page 7: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Person class Person {{ private :private : char * name ;char * name ; char * address ;char * address ; Date dateOfBirth ; // member Date dateOfBirth ; // member

objectobject public :public : // public member functions . . .// public member functions . . .} ;} ;

Page 8: Introduction to Programming Lecture 40. Class Class is a user defined data type

Person :: Person ( char * name , char * address , int day , int month , int Person :: Person ( char * name , char * address , int day , int month , int year )year ){{ dateOfBirth.setDay ( dy ) ; dateOfBirth.setDay ( dy ) ;

dateOfBirth.setMonth ( mn ) ; dateOfBirth.setMonth ( mn ) ; dateOfBirth.setYear ( yr ) ; dateOfBirth.setYear ( yr ) ; // statement ( s ) ;// statement ( s ) ;}}

Page 9: Introduction to Programming Lecture 40. Class Class is a user defined data type

InitializeInitializer Listr List

Page 10: Introduction to Programming Lecture 40. Class Class is a user defined data type

Person :: Person ( char * name , Person :: Person ( char * name , char * address , int day, int month, int year ) char * address , int day, int month, int year ) : Date ( int month , int day , int year ): Date ( int month , int day , int year )

Page 11: Introduction to Programming Lecture 40. Class Class is a user defined data type

ConstructorConstructorDate :: Date ( int day , int month , int year )Date :: Date ( int day , int month , int year ){{

cout << “Inside Date constructor ” ;cout << “Inside Date constructor ” ;}}

Person :: Person ( char * name , char * address ,int Person :: Person ( char * name , char * address ,int day , day ,

int month , int year ) : Date ( month , day , year )int month , int year ) : Date ( month , day , year ){{

cout << “Inside Person constructor” ;cout << “Inside Person constructor” ;}}

Page 12: Introduction to Programming Lecture 40. Class Class is a user defined data type

DestructorDestructor~ Date ( )~ Date ( ){{

cout << “Inside Date destructor ” ;cout << “Inside Date destructor ” ;}}~ Person ( )~ Person ( ){{

cout << “Inside Person destructor” ;cout << “Inside Person destructor” ;}}

Page 13: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Columnclass Column{{ private :private : int size ;int size ; public :public : Column ( )Column ( ) {{ cout << "Column Created" << endl ;cout << "Column Created" << endl ; }} void display ( ) ;void display ( ) ; void set ( int ) ;void set ( int ) ;

~ Column ( )~ Column ( ) {{ cout << "Column destroyed" << endl ;cout << "Column destroyed" << endl ; }}} ;} ;

Page 14: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Rowclass Row{{ private :private : int size ;int size ; Column col ;Column col ; public :public : void set ( int ) ;void set ( int ) ;

Row ( )Row ( ) {{ cout << "Inside Constructor for object Row" << cout << "Inside Constructor for object Row" <<

endl ;endl ; }}

~ Row ( )~ Row ( ) {{ cout << "Row destroyed" << endl ;cout << "Row destroyed" << endl ; }} void display ( ) ; void display ( ) ; } ;} ;

Page 15: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Matrixclass Matrix{{ private :private : Row row ;Row row ;

int size ;int size ; public :public : Matrix ( )Matrix ( ) {{ cout << "Matrix Created" << endl << cout << "Matrix Created" << endl <<

endl ;endl ; }} ~ Matrix ( )~ Matrix ( ) {{ cout << "Matrix destroyed" << endl << cout << "Matrix destroyed" << endl <<

endl ;endl ; }} void display ( ) ;void display ( ) ;} ;} ;

Page 16: Introduction to Programming Lecture 40. Class Class is a user defined data type

main( )main( ){{

Matrix m ;Matrix m ;m.display ( ) ;m.display ( ) ;

}}

ExampleExample

Page 17: Introduction to Programming Lecture 40. Class Class is a user defined data type

Column CreatedColumn CreatedInside Constructor for object Row Inside Constructor for object Row Matrix CreatedMatrix Created……..

Matrix DestroyedMatrix DestroyedRow DestroyedRow DestroyedColumn DestroyedColumn Destroyed

OutputOutput

Page 18: Introduction to Programming Lecture 40. Class Class is a user defined data type

Code Code ReuseReuse

Page 19: Introduction to Programming Lecture 40. Class Class is a user defined data type

const Date dateOfBirth ;const Date dateOfBirth ;

Page 20: Introduction to Programming Lecture 40. Class Class is a user defined data type

Structure Structure inside a inside a

structurestructure

Page 21: Introduction to Programming Lecture 40. Class Class is a user defined data type

Class inside Class inside a classa class

Page 22: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Outerclass Outer{ {

…………. . class Inner1 class Inner1 { { private :private :// Data members// Data members//data functions//data functionspublic :public :// Data members// Data members//data functions//data functions} ;} ;//data members//data members//data functions//data functions

} ; } ;

Page 23: Introduction to Programming Lecture 40. Class Class is a user defined data type

Class inside a Class inside a classclass

Outer

Inner1

Inner2

Page 24: Introduction to Programming Lecture 40. Class Class is a user defined data type

FrienFriendd

Page 25: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Outerclass Outer{ {

public : public : class Inner ;class Inner ;friend class Inner ; friend class Inner ; class Inner class Inner { {

friend class outer ;friend class outer ;// Data members// Data members

};};// Data members// Data members// Member functions // Member functions

} ; } ;

Page 26: Introduction to Programming Lecture 40. Class Class is a user defined data type

ReturnType className :: ReturnType className :: functionName ( )functionName ( )

{{// Statement ( s ) ; // Statement ( s ) ;

definitiondefinition}}

}

Page 27: Introduction to Programming Lecture 40. Class Class is a user defined data type

Outer :: Inner :: Inner ( )Outer :: Inner :: Inner ( ){{// definition// definition

}}

Page 28: Introduction to Programming Lecture 40. Class Class is a user defined data type

ExampleExampleclass Outerclass Outer{ {

class Inner1 class Inner1 { {

class Inner2class Inner2{{

class Inner3class Inner3 { {

// Data members and functions// Data members and functions // and perhaps more inner // and perhaps more inner

classesclasses } ;} ;

// Data members and functions// Data members and functions} ; } ; // Data members and functions// Data members and functions

} ;} ;} ; } ;

48: