cs201- introduction to programming- lecture 40

28
Introduction to Introduction to Programming Programming Lecture 40 Lecture 40

Upload: bilal-ahmed

Post on 18-Dec-2014

15 views

Category:

Education


2 download

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 40 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

TRANSCRIPT

Page 1: CS201- Introduction to Programming- Lecture 40

Introduction to Introduction to ProgrammingProgramming

Lecture 40Lecture 40

Page 2: CS201- Introduction to Programming- Lecture 40

ClassClass

Class is a userClass is a user

defined data type. defined data type.

Page 3: CS201- Introduction to Programming- Lecture 40

Data Data HidingHiding

Page 4: CS201- Introduction to Programming- Lecture 40

Public Public InterfaceInterface

Page 5: CS201- Introduction to Programming- Lecture 40

Yes you can use userYes you can use userdefined data type as a defined data type as a member of the classmember of the class

Class can contain Class can contain objects.objects.

Page 6: CS201- Introduction to Programming- Lecture 40

ExampleExampleclass Dateclass Date{{private :private :

int month , day , int month , day , year ;year ;public :public :

// member // member functionsfunctions

} ;} ;

Page 7: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

InitializeInitializer Listr List

Page 10: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

DestructorDestructor~ Date ( )~ Date ( )

{{

cout << “Inside Date destructor ” ;cout << “Inside Date destructor ” ;

}}

~ Person ( )~ Person ( )

{{

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

}}

Page 13: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

main( )main( ){{

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

}}

ExampleExample

Page 17: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

Code Code ReuseReuse

Page 19: CS201- Introduction to Programming- Lecture 40

const Date dateOfBirth ;const Date dateOfBirth ;

Page 20: CS201- Introduction to Programming- Lecture 40

Structure Structure inside a inside a

structurestructure

Page 21: CS201- Introduction to Programming- Lecture 40

Class inside Class inside a classa class

Page 22: CS201- Introduction to Programming- Lecture 40

ExampleExampleclass Outerclass Outer{ {

…………. . class Inner1 class Inner1

{ { private :private :

// Data members// Data members//data functions//data functions

public :public :// Data members// Data members//data functions//data functions

} ;} ;//data members//data members//data functions//data functions

} ; } ;

Page 23: CS201- Introduction to Programming- Lecture 40

Class inside a Class inside a classclass

Outer

Inner1

Inner2

Page 24: CS201- Introduction to Programming- Lecture 40

FrienFriendd

Page 25: CS201- Introduction to Programming- Lecture 40

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: CS201- Introduction to Programming- Lecture 40

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

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

definitiondefinition}}

}

Page 27: CS201- Introduction to Programming- Lecture 40

Outer :: Inner :: Inner ( )Outer :: Inner :: Inner ( )

{{

// definition// definition

}}

Page 28: CS201- Introduction to Programming- Lecture 40

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: