34
Introduction to Introduction to Programming Programming Lecture 26 Lecture 26

CS201- Introduction to Programming- Lecture 26

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 26 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 26

Introduction to Introduction to ProgrammingProgramming

Lecture 26Lecture 26

Page 2: CS201- Introduction to Programming- Lecture 26

Today’s Today’s LectureLecture

– ClassesClasses– ObjectObject

Page 3: CS201- Introduction to Programming- Lecture 26

structstruct

Page 4: CS201- Introduction to Programming- Lecture 26

ClassClass A class A class hashas– data data – functionsfunctions

Page 5: CS201- Introduction to Programming- Lecture 26

ClassClass

A Class is a user A Class is a user defineddefined

data type.data type.

Page 6: CS201- Introduction to Programming- Lecture 26

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

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

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

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

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

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

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

PrivatePrivate

Page 14: CS201- Introduction to Programming- Lecture 26

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

PublicPublic

Page 16: CS201- Introduction to Programming- Lecture 26

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

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

main ( )main ( )

{{

Date mydate ;Date mydate ;

mydate.month = 10 ; // illegalmydate.month = 10 ; // illegal

}}

Page 19: CS201- Introduction to Programming- Lecture 26

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

main ( )main ( ){{Date mydate ;Date mydate ;mydate.setMonth ( 10 ) ;mydate.setMonth ( 10 ) ;mydate.print ( ) ;mydate.print ( ) ;

}}

Page 21: CS201- Introduction to Programming- Lecture 26

Separation of Interface Separation of Interface from the from the

Implementation.Implementation.

Page 22: CS201- Introduction to Programming- Lecture 26

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

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

Scope Resolution Scope Resolution OperatorOperator

Page 25: CS201- Introduction to Programming- Lecture 26

main ( )main ( )

{{

Date mydate ;Date mydate ;

mydate.display ( ) ;mydate.display ( ) ;

}}

Example 3Example 3

Page 26: CS201- Introduction to Programming- Lecture 26

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

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

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

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

ConstructoConstructor r

Page 31: CS201- Introduction to Programming- Lecture 26

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

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

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

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