Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.

Similar presentations


Presentation on theme: "Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data."— Presentation transcript:

1

2 Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data and functions// public data and functions };};

3 Example class Date {class Date { private :private : int day, month, year ;int day, month, year ; public :public : setMonth ( ) ;setMonth ( ) ; print ( ) ;print ( ) ; };};

4 Example int main ( ) {int main ( ) { Date mydate ;Date mydate ; mydate.setMonth ( ) ;mydate.setMonth ( ) ; mydate.print ( ) ;mydate.print ( ) ; }

5 Example class Date {class 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 ; } ;} ;

6 Class Functions void Date :: display ( )void Date :: display ( ) { cout << day << / " << month << / " << year ;cout << day << / " << month << / " << year ; } } Scope Resolution OperatorScope Resolution Operator

7 Example 3 int main ( ) { int main ( ) { Date mydate ;Date mydate ; mydate.display ( ) ;mydate.display ( ) ; } }

8 Example 3 class Date {class Date { public :public : Date ( int month, int day, int year ) ;Date ( int month, int day, int 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 ; } ;} ;

9 Example 3 void Date :: setDay ( int i )void Date :: setDay ( int i ) { { day = i ;day = i ; }

10 Example 3 int main ( ) { int main ( ) { Date mydate ;Date mydate ; mydate.setDay ( 10 ) ;mydate.setDay ( 10 ) ; }

11 Constructor class Date {class Date { public :public : Date ( ) ;Date ( ) ; void display ( ) ;void display ( ) ; private :private : int month, day, year ; int month, day, year ; };}; Date :: Date ( ) { // Body of constructor }Date :: Date ( ) { // Body of constructor }

12 Constructor A constructor is a class member function with same name as the class This function is invoked (called) automatically each time when the object of that class is created(instantiated) Constructor must be in pubic area of that class

13 class Date {class Date { public :public : Date ( ) ;Date ( ) ; void display ( ) ;void display ( ) ; private :private : int month, day, year ; int month, day, year ; };}; Date :: Date ( ) { // Body of constructor }Date :: Date ( ) { // Body of constructor } int main ( ) {int main ( ) { Date d1,d2,d3;Date d1,d2,d3; mydate.display ( ) ;mydate.display ( ) ; } }

14 Constructor When a class object is created,its member variables can be initialized in that classs constructor Date :: Date ( ) { month=day=year=0; }Date :: Date ( ) { month=day=year=0; } Constructor can have argumentsConstructor can have arguments Constructor with no argument is called default constructorConstructor with no argument is called default constructor

15 Constructor class Date {class 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 ) { // Body of the function }Date :: Date ( int month, int day, int year ) { // Body of the function }

16 Example 3 int main ( ) { int main ( ) { Date mydate ( 1, 1,2002 ) ;Date mydate ( 1, 1,2002 ) ; mydate.display ( ) ;mydate.display ( ) ; } } We can set default argument for constructorWe can set default argument for constructor Date :: Date ( int day, int month=9, int year = 2002 )Date :: Date ( int day, int month=9, int year = 2002 )

17 Example 3 main ( ) {main ( ) { Date mydate ( 1, 1,2006 ) ;Date mydate ( 1, 1,2006 ) ; Date mydate ( 1, 1 ) ;Date mydate ( 1, 1 ) ; Date mydate ( 1 ) ;Date mydate ( 1 ) ;} Constructors can be overloadedConstructors can be overloaded

18 Rules of function overloading Whenever we overload a function, the name of the function remain the same but argument list changes.Whenever we overload a function, the name of the function remain the same but argument list changes. The argument list can:The argument list can: Either vary in the number of argumentsEither vary in the number of arguments Or vary in the typeOr vary in the type

19 #include class Date { public : Date ( ) ; Date ( int, int, int=2005 ) ; void show(); private: int month, day, year ; } ; Date::Date( ){ cout<<"\nDefault constructure is called"; }

20 Date::Date(int d,int m,int y ){ cout<<"\nArgument constructure is called"; month=m; day=d; year=y; } void Date::show(){ cout<

21 The main Function void main(){ Date a,b(10,26,2004),c(10,26); a.show(); b.show(); c.show(); getch(); }

22 Destructor(~) A destructor is called when the object of that class is destroyed Object is destroyed when program execution leaves the the scope in which that object is created The destructor is written, tilde(~) character followed by class name ~Date ( ) ;~Date ( ) ;

23 Rules of Destructor Destructors cannot be overloadedDestructors cannot be overloaded Destructors take no argumentsDestructors take no arguments They dont return a valueThey dont return a value

24 class Date {class 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 ) ; setDate (int day, int month, int year ) ;setDate (int day, int month, int year ) ; private:private: int month, day, year ;int month, day, year ; } ;} ;

25 class CreateAndDestroy { public : CreateAndDestroy(int) ; ~CreateAndDestroy( ) ; private: int data ; } ; CreateAndDestroy::CreateAndDestroy(int value){ data=value; cout<<"Object "<

26 void create(); CreateAndDestroy first(1); int main(){ cout<<" (object created outside main)"<

27


Download ppt "Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data."

Similar presentations


Ads by Google