Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Introduction To Programming - CS201 Power Point Slides Lecture 40

These slides are introducing c++ programming

Uploaded by

Hasnain ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction To Programming - CS201 Power Point Slides Lecture 40

These slides are introducing c++ programming

Uploaded by

Hasnain ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

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.
Example
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 . . .
};
Person :: Person ( char * name , char * address , int day , int month , int
year )
{
dateOfBirth.setDay ( dy ) ;
dateOfBirth.setMonth ( mn ) ;
dateOfBirth.setYear ( yr ) ;
// statement ( s ) ;
}
Initialize
r List
Person :: Person ( char * name ,
char * address , int day, int month, int ye
: 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” ;
}
Destructor
~ Date ( )
{
cout << “Inside Date destructor ” ;
}
~ Person ( )
{
cout << “Inside Person
destructor” ;
}
class Column
Example
{
private :
int size ;
public :
Column ( )
{
cout << "Column Created" << endl ;
}

void display ( ) ;
void set ( int ) ;

~ Column ( )
{
cout << "Column destroyed" << endl ;
}
};
class Row
Example
{
private :
int size ;
Column col ;
public :
void set ( int ) ;
Row ( )
{
cout << "Inside Constructor for object Row" <<
endl ;
}
~ Row ( )
{
cout << "Row destroyed" << endl ;
}
void display ( ) ;
class Matrix
Example
{
private :
Row row ;
int size ;
public :
Matrix ( )
{
cout << "Matrix Created" << endl <<
endl ;
}
~ Matrix ( )
{
cout << "Matrix destroyed" << endl <<
endl ;
}
Example
main( )
{
Matrix m ;
m.display ( ) ;
}
Output
Column Created
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
Example
{
…….
class Inner1
{
private :
// Data members
//data functions
public :
// Data members
//data functions
};
//data members
//data functions
};
Class inside a
class
Inner2

Inner1

Outer
Frien
d
Example
class Outer
{
public :
class Inner ;
friend class Inner ;
class Inner
{

friend class outer ;


// Data members
};
// Data members
// Member functions
};
ReturnType className ::

}
functionName ( )
{
// Statement ( s ) ;
definition
}
Outer :: Inner :: Inner ( )
{
// definition
}
class Outer
Example
{
class Inner1
{
class Inner2
{
class Inner3
{
// Data members and functions
// and perhaps more inner
classes
};
// Data members and functions
};
48: members and functions
// Data
};

You might also like