cs201- introduction to programming- lecture 27

34
Introduction of Programmin Introduction of Programmin Lecture 27 Lecture 27

Upload: bilal-ahmed

Post on 18-Dec-2014

13 views

Category:

Education


2 download

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

Introduction of ProgrammingIntroduction of Programming

Lecture 27Lecture 27

Page 2: CS201- Introduction to Programming- Lecture 27

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

Function Function OrientedOriented

Page 4: CS201- Introduction to Programming- Lecture 27

Object OrientedObject Oriented

Page 5: CS201- Introduction to Programming- Lecture 27

MultipleMultiple

MediaMedia

Page 6: CS201- Introduction to Programming- Lecture 27

ClassesClasses

ObjectsObjects

ConstructorsConstructors

Page 7: CS201- Introduction to Programming- Lecture 27

Data Data Hiding Hiding

Encapsulation

Page 8: CS201- Introduction to Programming- Lecture 27

ConstructorConstructor

Page 9: CS201- Introduction to Programming- Lecture 27

BugsBugs

Page 10: CS201- Introduction to Programming- Lecture 27

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

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

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

Function Function OverloadingOverloading

Page 14: CS201- Introduction to Programming- Lecture 27

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

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

main ( )main ( )

{{

Date mydate ( ) ; Date mydate ( ) ;

}}

Example Example

Page 17: CS201- Introduction to Programming- Lecture 27

main ( )main ( )

{{

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

}}

Example Example

Page 18: CS201- Introduction to Programming- Lecture 27

Memory Allocation Memory Allocation Inside a ConstructorInside a Constructor

Page 19: CS201- Introduction to Programming- Lecture 27

Utility Utility FunctionsFunctions

Page 20: CS201- Introduction to Programming- Lecture 27

Friend Friend FunctionsFunctions

Page 21: CS201- Introduction to Programming- Lecture 27

DestructorDestructor

Page 22: CS201- Introduction to Programming- Lecture 27

~~

Page 23: CS201- Introduction to Programming- Lecture 27

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

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

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

}}

Example 1Example 1

Page 26: CS201- Introduction to Programming- Lecture 27

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

Example 1Example 1

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

}}

Page 28: CS201- Introduction to Programming- Lecture 27

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

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

DestructoDestructorsrs

~className ( ) ;

Page 31: CS201- Introduction to Programming- Lecture 27

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

}}

Page 32: CS201- Introduction to Programming- Lecture 27

ConstructorConstructorss

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

Page 33: CS201- Introduction to Programming- Lecture 27

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

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