CS201- Introduction to Programming- Lecture 27

Preview:

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 27 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Citation preview

Introduction of ProgrammingIntroduction of Programming

Lecture 27Lecture 27

Today’s Today’s Lecture Lecture

– Classes and Classes and objectsobjects

– ConstructorsConstructors– DestructorsDestructors– Member functions Member functions – Member dataMember data

Function Function OrientedOriented

Object OrientedObject Oriented

MultipleMultiple

MediaMedia

ClassesClasses

ObjectsObjects

ConstructorsConstructors

Data Data Hiding Hiding

Encapsulation

ConstructorConstructor

BugsBugs

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.

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

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 ) } ;} ;

Function Function OverloadingOverloading

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

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

main ( )main ( )

{{

Date mydate ( ) ; Date mydate ( ) ;

}}

Example Example

main ( )main ( )

{{

Date mydate ( “01-Jan-2002” ) ;Date mydate ( “01-Jan-2002” ) ;

}}

Example Example

Memory Allocation Memory Allocation Inside a ConstructorInside a Constructor

Utility Utility FunctionsFunctions

Friend Friend FunctionsFunctions

DestructorDestructor

~~

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

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

main ( )main ( ){{Date mydate ( 35 , 13 , 2000 ) ;Date mydate ( 35 , 13 , 2000 ) ;

}}

Example 1Example 1

main ( )main ( ){{

Date mydate ;Date mydate ;mydate.setDate ( 21 , 01 , 1979 mydate.setDate ( 21 , 01 , 1979 ) ;) ;

}}

Example 1Example 1

Example 1Example 1

main ( )main ( ){{Date mydate ( 21 , 01 , 1979 ) ;Date mydate ( 21 , 01 , 1979 ) ;

}}

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 ;

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

DestructoDestructorsrs

~className ( ) ;

DestructorsDestructors~Date :: Date ( )~Date :: Date ( ){{ cout<< “Object cout<< “Object Destroyed” ;Destroyed” ;

}}

ConstructorConstructorss

Date :: Date ( )Date :: Date ( ){{ cout << “Date Object Created” ;cout << “Date Object Created” ;}}

Constructors Constructors without without

ArgumentsArgumentsDate :: Date ( )Date :: Date ( ){{ cout<< “Default constructor cout<< “Default constructor

without arguments called ” ;without arguments called ” ;}}

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 ” ;}}