CS201-27 - Introduction of Programming Lecture 27 Today’s...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction of Programming Lecture 27 Today’s Lecture – Classes and objects – Constructors – Destructors – Member functions – Member data Function Oriented Object Oriented Multiple Media Classes Objects Constructors Data Hiding Encapsulation Constructor Bugs The majority of programming problems occur because of the use of un­initialized data. Constructor Name of the constructor is same as the name of the class It does not return any thing, not even void Example class Date { int month ; int day ; int year ; public: Date ( int day = 1 , int month = 1 , int year = 1 ) } ; Function Overloading Rules of function overloading When ever we overload a function, the name of the function remain the same but argument list changes. The argument list can: arguments – Either vary in the number of – Or vary in the type class Date { public : Date ( int month = 1 , int day = 1 , int year = 1 ) ; private : int month , day , year ; } ; Example Example main ( ) { Date mydate ( ) ; } Example main ( ) { Date mydate ( “01­Jan­2002” ) ; } Memory Allocation Inside a Constructor Utility Functions Friend Functions Destructor ~ Rules of Destructor 1. 2. 3. Destructors cannot be overloaded Destructors take no arguments They don’t return a value class Date { Example 1 public : Date ( ) ; Date ( int month , int day , int year ) ; ~Date ( ) ; setMonth ( int month ) ; setDay ( int day ) ; setYear ( int year ) ; int getDay ( ) ; int getMonth ( ) ; int getYear ( ) ; setDate (int day, int month, int year ) ; private: int month , day , year ; } ; Example 1 main ( ) { Date mydate ( 35 , 13 , 2000 ) ; } Example 1 main ( ) { Date mydate ; mydate.setDate ( 21 , 01 , 1979 ) ; } Example 1 main ( ) { Date mydate ( 21 , 01 , 1979 ) ; } What Happens in Memory Functions setMonth ( int month ) ; setDay ( int day ) ; setYear ( int year ) ; int getMonth ( ) ; int getDay ( ) ; int getYear ( ) ; int month ; int day ; int year ; int month ; int day ; int year ; int month ; int day ; int year ; main ( ) { Date date1 , date2 , date3 ; date1.setMonth ( 3 ) ; date2.setDay ( 23 ) ; } Example 2 Destructors ~className ( ) ; Destructors ~Date :: Date ( ) { cout<< “Object Destroyed” ; } Constructors Date :: Date ( ) { cout << “Date Object Created” ; } Constructors without Arguments Date :: Date ( ) { cout<< “Default constructor without arguments called ” ; } Constructors with Arguments Date :: Date ( int month , int day , int year ) { cout<< “A constructor with paramerters ” ; } ...
View Full Document

Ask a homework question - tutors are online