cs201- introduction to programming- lecture 26
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 26 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 26Lecture 26
![Page 2: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/2.jpg)
Today’s Today’s LectureLecture
– ClassesClasses– ObjectObject
![Page 3: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/3.jpg)
structstruct
![Page 4: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/4.jpg)
ClassClass A class A class hashas– data data – functionsfunctions
![Page 5: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/5.jpg)
ClassClass
A Class is a user A Class is a user defineddefined
data type.data type.
![Page 6: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/6.jpg)
ObjectObject
The instances of the The instances of the
class are called class are called Objects.Objects.
![Page 7: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/7.jpg)
Structure of a Structure of a classclass
class name_of_classclass name_of_class
{{
// definition of a class// definition of a class
}}
![Page 8: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/8.jpg)
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 ;
![Page 9: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/9.jpg)
class Dateclass Date
{{
int day ;int day ;
int month ;int month ;
int year ;int year ;
} ;} ;
Example 2Example 2
![Page 10: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/10.jpg)
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
![Page 11: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/11.jpg)
class Dateclass Date
{{
int day ;int day ;
int month ;int month ;
int year ;int year ;
} ;} ;
Example 2Example 2
![Page 12: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/12.jpg)
main ( )main ( )
{{
Date mydate;Date mydate;
mydate.month = 10 ; mydate.month = 10 ; // Error// Error
}}
Example 2Example 2
![Page 13: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/13.jpg)
PrivatePrivate
![Page 14: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/14.jpg)
Default visibility ofDefault visibility of
all data and all data and functionfunction
inside a class isinside a class is
privateprivate
![Page 15: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/15.jpg)
PublicPublic
![Page 16: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/16.jpg)
class Dateclass Date{{
private :private :
// private data and functions // private data and functions
public :public :
// public data and functions // public data and functions };};
![Page 17: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/17.jpg)
class Dateclass Date
{{
int day ;int day ;
int month ;int month ;
int year ;int year ;
} ;} ;
Date ClassDate Class
![Page 18: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/18.jpg)
main ( )main ( )
{{
Date mydate ;Date mydate ;
mydate.month = 10 ; // illegalmydate.month = 10 ; // illegal
}}
![Page 19: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/19.jpg)
class Dateclass Date{{
private : private : int day , month , year ;int day , month , year ;
public :public :setMonth ( ) ;setMonth ( ) ;print ( ) ;print ( ) ;
};};
Date Class Date Class
![Page 20: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/20.jpg)
main ( )main ( ){{Date mydate ;Date mydate ;mydate.setMonth ( 10 ) ;mydate.setMonth ( 10 ) ;mydate.print ( ) ;mydate.print ( ) ;
}}
![Page 21: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/21.jpg)
Separation of Interface Separation of Interface from the from the
Implementation.Implementation.
![Page 22: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/22.jpg)
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
![Page 23: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/23.jpg)
void Date :: display ( )void Date :: display ( )
{{
cout << day << “/ " << month << “/ " << year ;cout << day << “/ " << month << “/ " << year ;
}}
Example 3Example 3
![Page 24: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/24.jpg)
Scope Resolution Scope Resolution OperatorOperator
![Page 25: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/25.jpg)
main ( )main ( )
{{
Date mydate ;Date mydate ;
mydate.display ( ) ;mydate.display ( ) ;
}}
Example 3Example 3
![Page 26: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/26.jpg)
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
![Page 27: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/27.jpg)
main ( )main ( )
{{
Date mydate ;Date mydate ;
mydate.setDay ( 10 ) ;mydate.setDay ( 10 ) ;
}}
Example 3: Example 3: ModifiedModified
![Page 28: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/28.jpg)
void Date :: setDay ( int i )void Date :: setDay ( int i ){{
day = i ;day = i ;}}
Example 3: Example 3: ModifiedModified
![Page 29: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/29.jpg)
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
![Page 30: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/30.jpg)
ConstructoConstructor r
![Page 31: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/31.jpg)
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
![Page 32: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/32.jpg)
main ( )main ( )
{{
Date mydate ( 1 , 1 ,2002 ) ;Date mydate ( 1 , 1 ,2002 ) ;
mydate.display ( ) ;mydate.display ( ) ;
}}
Example 3: Example 3: ModifiedModified
![Page 33: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/33.jpg)
Date :: Date ( int day , int month , int year = 2002 )Date :: Date ( int day , int month , int year = 2002 )
![Page 34: CS201- Introduction to Programming- Lecture 26](https://reader033.vdocuments.site/reader033/viewer/2022061303/5492e75cb47959ad258b47f8/html5/thumbnails/34.jpg)
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