cs201- introduction to programming- lecture 27
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 27 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/1.jpg)
Introduction of ProgrammingIntroduction of Programming
Lecture 27Lecture 27
![Page 2: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/2.jpg)
Today’s Today’s Lecture Lecture
– Classes and Classes and objectsobjects
– ConstructorsConstructors– DestructorsDestructors– Member functions Member functions – Member dataMember data
![Page 3: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/3.jpg)
Function Function OrientedOriented
![Page 4: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/4.jpg)
Object OrientedObject Oriented
![Page 5: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/5.jpg)
MultipleMultiple
MediaMedia
![Page 6: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/6.jpg)
ClassesClasses
ObjectsObjects
ConstructorsConstructors
![Page 7: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/7.jpg)
Data Data Hiding Hiding
Encapsulation
![Page 8: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/8.jpg)
ConstructorConstructor
![Page 9: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/9.jpg)
BugsBugs
![Page 10: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/10.jpg)
The majority of programming The majority of programming
problems occur because of the problems occur because of the
use of un-initialized data.use of un-initialized data.
![Page 11: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/11.jpg)
ConstructorConstructor
Name of the constructor is same Name of the constructor is same
as the name of the classas the name of the class It does not return any thing, not It does not return any thing, not
even voideven void
![Page 12: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/12.jpg)
ExampleExampleclass Dateclass Date{{
int month ; int month ; int day ;int day ;int year ; int year ;
public:public:Date ( int day = 1 , int month = 1 , int Date ( int day = 1 , int month = 1 , int
year = 1 ) year = 1 ) } ;} ;
![Page 13: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/13.jpg)
Function Function OverloadingOverloading
![Page 14: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/14.jpg)
Rules of function Rules of function overloadingoverloading
When ever we overload a function, When ever we overload a function,
the name of the function remain the the name of the function remain the
same but argument list changes. same but argument list changes.
The argument list can:The argument list can:– Either vary in the number of Either vary in the number of
argumentsarguments– Or vary in the typeOr vary in the type
![Page 15: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/15.jpg)
class Dateclass Date
{{
public :public :
Date ( int month = 1 , int day = 1 , int year = Date ( int month = 1 , int day = 1 , int year = 1 ) ;1 ) ;
private :private :
int month , day , year ;int month , day , year ;
} ;} ;
ExampleExample
![Page 16: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/16.jpg)
main ( )main ( )
{{
Date mydate ( ) ; Date mydate ( ) ;
}}
Example Example
![Page 17: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/17.jpg)
main ( )main ( )
{{
Date mydate ( “01-Jan-2002” ) ;Date mydate ( “01-Jan-2002” ) ;
}}
Example Example
![Page 18: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/18.jpg)
Memory Allocation Memory Allocation Inside a ConstructorInside a Constructor
![Page 19: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/19.jpg)
Utility Utility FunctionsFunctions
![Page 20: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/20.jpg)
Friend Friend FunctionsFunctions
![Page 21: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/21.jpg)
DestructorDestructor
![Page 22: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/22.jpg)
~~
![Page 23: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/23.jpg)
Rules of Rules of DestructorDestructor
1.1. Destructors cannot be Destructors cannot be overloadedoverloaded
2.2. Destructors take no Destructors take no argumentsarguments
3.3. They don’t return a valueThey don’t return a value
![Page 24: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/24.jpg)
class Dateclass Date{{
public :public :
Date ( ) ; Date ( ) ; Date ( int month , int day , int year ) ;Date ( int month , int day , int year ) ;
~Date ( ) ; ~Date ( ) ; setMonth ( int month ) ; setMonth ( int month ) ; setDay ( int day ) ; setDay ( int day ) ;
setYear ( int year ) ; setYear ( int year ) ; int getDay ( ) ; int getDay ( ) ;
int getMonth ( ) ; int getMonth ( ) ; int getYear ( ) ; int getYear ( ) ;
setDate (int day, int month, int year ) ;setDate (int day, int month, int year ) ;private:private:
int month , day , year ; int month , day , year ; } ;} ;
Example Example 11
![Page 25: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/25.jpg)
main ( )main ( ){{Date mydate ( 35 , 13 , 2000 ) ;Date mydate ( 35 , 13 , 2000 ) ;
}}
Example 1Example 1
![Page 26: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/26.jpg)
main ( )main ( ){{
Date mydate ;Date mydate ;mydate.setDate ( 21 , 01 , 1979 mydate.setDate ( 21 , 01 , 1979 ) ;) ;
}}
Example 1Example 1
![Page 27: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/27.jpg)
Example 1Example 1
main ( )main ( ){{Date mydate ( 21 , 01 , 1979 ) ;Date mydate ( 21 , 01 , 1979 ) ;
}}
![Page 28: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/28.jpg)
What Happens in What Happens in MemoryMemory
setMonth ( int month ) ; setDay ( int day ) ; setYear ( int year ) ; int getMonth ( ) ; int getDay ( ) ; int getYear ( ) ;
Functions
int month ;int day ; int year ;
int month ;int day ; int year ;
int month ;int day ; int year ;
![Page 29: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/29.jpg)
main ( )main ( ){{Date date1 , date2 , date3 Date date1 , date2 , date3 ; ; date1.setMonth ( 3 ) ;date1.setMonth ( 3 ) ;date2.setDay ( 23 ) ;date2.setDay ( 23 ) ;
}}
Example 2Example 2
![Page 30: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/30.jpg)
DestructoDestructorsrs
~className ( ) ;
![Page 31: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/31.jpg)
DestructorsDestructors~Date :: Date ( )~Date :: Date ( ){{ cout<< “Object cout<< “Object Destroyed” ;Destroyed” ;
}}
![Page 32: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/32.jpg)
ConstructorConstructorss
Date :: Date ( )Date :: Date ( ){{ cout << “Date Object Created” ;cout << “Date Object Created” ;}}
![Page 33: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/33.jpg)
Constructors Constructors without without
ArgumentsArgumentsDate :: Date ( )Date :: Date ( ){{ cout<< “Default constructor cout<< “Default constructor
without arguments called ” ;without arguments called ” ;}}
![Page 34: CS201- Introduction to Programming- Lecture 27](https://reader036.vdocuments.site/reader036/viewer/2022062616/5497f4b9b479593d4d8b52fe/html5/thumbnails/34.jpg)
Constructors with Constructors with ArgumentsArguments
Date :: Date ( int month , int day , int year )Date :: Date ( int month , int day , int year ){{ cout<< “A constructor with paramerters ” ;cout<< “A constructor with paramerters ” ;}}