Introduction To Programming
Introduction To Programming
Lecture 26
Today’s Lecture
– Classes
– Object
struct
Class
A class has
– data
– functions
Class
A Class is a user defined
data type.
Object
// definition of a class
}
Example 1
struct Date
{
int day ;
int month ;
int year ;
};
Date mydate ;
mydate.month = 1 ;
mydate.day = 21 ;
mydate.year = 1979 ;
Example 2
class Date
{
int day ;
int month ;
int year ;
};
Example 2
main ( )
{
Date mydate ;
/* manipulate the data members
mydate.day ;
mydate.month ;
mydate.year ;
*/
}
Example 2
class Date
{
int day ;
int month ;
int year ;
};
Example 2
main ( )
{
Date mydate;
mydate.month = 10 ; // Error
}
Private
Default visibility of
all data and function
inside a class is
private
Public
class Date
{
private :
public :
class Date
{
public :
Date ( int month , int day , int year ) ;
void display ( ) ;
private :
int month , day , year ;
};
Date :: Date ( int month , int day , int year )
{
// Body of the function
}
Example 3: Modified
main ( )
{
Date mydate ( 1 , 1 ,2002 ) ;
mydate.display ( ) ;
}
Date :: Date ( int day , int month , int year = 2002 )
Example 3: Modified
main ( )
{
Date mydate ( 1 , 1 ,2002 ) ;
Date mydate ( 1 , 1 ) ;
}