Introduction to Introduction to ProgrammingProgramming
Lecture 26Lecture 26
Today’s Today’s LectureLecture
– ClassesClasses– ObjectObject
structstruct
ClassClass A class A class hashas– data data – functionsfunctions
ClassClass
A Class is a user A Class is a user defineddefined
data type.data type.
ObjectObject
The instances of the The instances of the
class are called class are called Objects.Objects.
Structure of a Structure of a classclass
class name_of_classclass name_of_class
{{
// definition of a class// definition of a class
}}
Example 1Example 1struct Datestruct Date{{
int day ;int day ;int month ;int month ;int year ;int year ;
} ;} ;
Date mydate ;Date mydate ;mydate.month = 1 ;mydate.month = 1 ;mydate.day = 21 ;mydate.day = 21 ;mydate.year = 1979 ;mydate.year = 1979 ;
class Dateclass Date
{{
int day ;int day ;
int month ;int month ;
int year ;int year ;
} ;} ;
Example 2Example 2
main ( )main ( ){{
Date mydate ;Date mydate ;/* manipulate the data members /* manipulate the data members mydate.day ;mydate.day ;mydate.month ;mydate.month ;mydate.year ;mydate.year ;*/*/
}}
Example 2Example 2
class Dateclass Date
{{
int day ;int day ;
int month ;int month ;
int year ;int year ;
} ;} ;
Example 2Example 2
main ( )main ( )
{{
Date mydate;Date mydate;
mydate.month = 10 ; mydate.month = 10 ; // Error// Error
}}
Example 2Example 2
PrivatePrivate
Default visibility ofDefault visibility of
all data and all data and functionfunction
inside a class isinside a class is
privateprivate
PublicPublic
class Dateclass Date{{
private :private :
// private data and functions // private data and functions
public :public :
// public data and functions // public data and functions };};
class Dateclass Date
{{
int day ;int day ;
int month ;int month ;
int year ;int year ;
} ;} ;
Date ClassDate Class
main ( )main ( )
{{
Date mydate ;Date mydate ;
mydate.month = 10 ; // illegalmydate.month = 10 ; // illegal
}}
class Dateclass Date{{
private : private : int day , month , year ;int day , month , year ;
public :public :setMonth ( ) ;setMonth ( ) ;print ( ) ;print ( ) ;
};};
Date Class Date Class
main ( )main ( ){{Date mydate ;Date mydate ;mydate.setMonth ( 10 ) ;mydate.setMonth ( 10 ) ;mydate.print ( ) ;mydate.print ( ) ;
}}
Separation of Interface Separation of Interface from the from the
Implementation.Implementation.
class Dateclass Date{{
public :public :void display ( ) ;void display ( ) ;Date ( int day , int month , int year ) ;Date ( int day , int month , int year ) ;
private:private:int day , month , year ;int day , month , year ;
} ;} ;
Example 3Example 3
void Date :: display ( )void Date :: display ( )
{{
cout << day << “/ " << month << “/ " << year ;cout << day << “/ " << month << “/ " << year ;
}}
Example 3Example 3
Scope Resolution Scope Resolution OperatorOperator
main ( )main ( )
{{
Date mydate ;Date mydate ;
mydate.display ( ) ;mydate.display ( ) ;
}}
Example 3Example 3
class Dateclass Date{{
public :public :Date ( int month , int day , int Date ( int month , int day , int
year ) ;year ) ;void display ( ) ;void display ( ) ;setDay ( int ) ;setDay ( int ) ;setMonth ( int ) ;setMonth ( int ) ;setYear ( int ) ;setYear ( int ) ;
private :private :int month , day , year ;int month , day , year ;
} ; } ;
Example 3: ModifiedExample 3: Modified
main ( )main ( )
{{
Date mydate ;Date mydate ;
mydate.setDay ( 10 ) ;mydate.setDay ( 10 ) ;
}}
Example 3: Example 3: ModifiedModified
void Date :: setDay ( int i )void Date :: setDay ( int i ){{
day = i ;day = i ;}}
Example 3: Example 3: ModifiedModified
main ( )main ( ){{Date date1 , date2 , date3 Date date1 , date2 , date3 ;;date1.setMonth ( 10 ) ;date1.setMonth ( 10 ) ;date2.display ( ) ;date2.display ( ) ;
}}
Example 3: Example 3: ModifiedModified
ConstructoConstructor r
class Dateclass Date{{
public :public : Date ( int month , int day , int year ) ;Date ( int month , int day , int year ) ; void display ( ) ;void display ( ) ;
private :private : int month , day , year ;int month , day , year ;
};};Date :: Date ( int month , int day , int year )Date :: Date ( int month , int day , int year ){{
// Body of the function// Body of the function
}}
Example 3: ModifiedExample 3: Modified
main ( )main ( )
{{
Date mydate ( 1 , 1 ,2002 ) ;Date mydate ( 1 , 1 ,2002 ) ;
mydate.display ( ) ;mydate.display ( ) ;
}}
Example 3: Example 3: ModifiedModified
Date :: Date ( int day , int month , int year = 2002 )Date :: Date ( int day , int month , int year = 2002 )
main ( )main ( )
{{
Date mydate ( 1 , Date mydate ( 1 , 1 ,2002 ) ;1 ,2002 ) ;
Date mydate ( 1 , 1 ) ;Date mydate ( 1 , 1 ) ;
}}
Example 3: Example 3: ModifiedModified