Download presentation
Presentation is loading. Please wait.
Published byHelena Whitehead Modified over 9 years ago
1
Introduction to Programming Lecture 40
2
Class Class is a user defined data type.
3
Data Hiding
4
Public Interface
5
Yes you can use user defined data type as a member of the class Class can contain objects.
6
Example class Date { private : int month, day, year ; int month, day, year ; public : // member functions // member functions } ;
7
Example class Person { private : private : char * name ; char * name ; char * address ; char * address ; Date dateOfBirth ; // member object Date dateOfBirth ; // member object public : public : // public member functions... // public member functions... } ;
8
Person :: Person ( char * name, char * address, int day, int month, int year ) { dateOfBirth.setDay ( dy ) ; dateOfBirth.setMonth ( mn ) ; dateOfBirth.setYear ( yr ) ; // statement ( s ) ; }
9
Initializer List
10
Person :: Person ( char * name, char * address, int day, int month, int year ) : Date ( int month, int day, int year )
11
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” ; }
12
Destructor ~ Date ( ) { cout << “Inside Date destructor ” ; } ~ Person ( ) { cout << “Inside Person destructor” ; }
13
Example class 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 ; } } ;
14
Example class Row { private : private : int size ; int size ; Column col ; Column col ; public : public : void set ( int ) ; void set ( int ) ; Row ( ) { cout << "Inside Constructor for object Row" << endl ; cout << "Inside Constructor for object Row" << endl ; } ~ Row ( ) ~ Row ( ) { cout << "Row destroyed" << endl ; cout << "Row destroyed" << endl ; } void display ( ) ; void display ( ) ; } ;
15
Example class Matrix { private : private : Row row ; Row row ; int size ; int size ; public : public : Matrix ( ) Matrix ( ) { { cout << "Matrix Created" << endl << endl ; cout << "Matrix Created" << endl << endl ; } ~ Matrix ( ) ~ Matrix ( ) { cout << "Matrix destroyed" << endl << endl ; cout << "Matrix destroyed" << endl << endl ; } void display ( ) ; void display ( ) ; } ;
16
main( ) { Matrix m ; m.display ( ) ; } Example
17
Column Created Inside Constructor for object Row Matrix Created …. Matrix Destroyed Row Destroyed Column Destroyed Output
18
Code Reuse
19
const Date dateOfBirth ;
20
Structure inside a structure
21
Class inside a class
22
Example class Outer {……. class Inner1 class Inner1{ private : // Data members //data functions public : // Data members //data functions } ; //data members //data functions } ;
23
Class inside a class Outer Inner1 Inner2
24
Friend
25
Example class Outer { public : class Inner ; friend class Inner ; class Inner { friend class outer ; // Data members }; // Member functions } ;
26
ReturnType className :: functionName ( ) { // Statement ( s ) ; definition } }
27
Outer :: Inner :: Inner ( ) { // definition }
28
Example class Outer { class Inner1 { class Inner2 { class Inner3 class Inner3 { // Data members and functions // Data members and functions // and perhaps more inner classes // and perhaps more inner classes } ; } ; // Data members and functions // Data members and functions } ; // Data members and functions } ; 48:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.