KEMBAR78
C++ L08-Classes Part1 | PDF
C++ 
L08-CLASSES, P1 
Programming Language 
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14
OOP
Object Oriented Programming
Float, double, long double 
C++ data types 
Structured 
Simple 
Address 
Pointer 
Reference 
enum 
Floating 
Array 
Struct 
Union 
Class 
Char, Short, int, long, bool 
Integral
aclass
aClass 
•class 
–Concept 
•Information hiding 
•Prevent access to their inner workings 
•Encapsulation (private, public) 
–code 
•class 
–Is an extension of the idea of “struct” 
•class 
–User defined (programmer defined) type 
•Once class is defined it can be used as a type 
•Object of that class can be defined 
•No memory is allocated (like struct)
a Class 
•Class package data declarations with function declarations 
–Thus, coupling data with behavior 
•Used to implement “ADT” 
•Examples: 
–Time, Complex numbers,
Class 
•Type: 
–Time 
•Domain: 
–Any time value is a time in: Hour, minute, second 
•Operations: 
–Set the time 
–Print the time 
–Incrementing / decrementing time 
–Compare times 
•=, <, >
Class 
•Members 
–They are the components of the class 
•Attributes 
•Behaviors methods 
•Class instance 
–Object
Class 
•OOP and classes 
–Encapsulation 
–Composition 
–Inheritance 
–Reusable
Class 
#include<iostream> 
usingnamespacestd; 
classMyClass 
{ 
}; 
voidmain() 
{ 
} 
Compile & run 
#include<iostream> 
usingnamespacestd; 
classMyClass 
{ 
} 
voidmain() 
{ 
} 
Compiler error. Missing ; 
#include<iostream> 
usingnamespacestd; 
classMyClass 
{ 
inti; 
}; 
voidmain() 
{ 
} 
Compile and run 
#include<iostream> 
usingnamespacestd; 
classMyClass 
{ 
inti = 0; 
}; 
voidmain() 
{ 
} 
Compiler error 
Rule: 
You can’t initialize a variable when you declare it in classes.
Class 
•Function in classes 
–It can directly access any member of the class (WITHOUT PASSING IT AS PARAMETER) 
•Data members 
•Function members
Class 
•private: 
–Default access mode 
–Accessible just to the functions of the class and friends 
•public: 
–Accessible wherever object of class in scope 
•protected: 
–Accessible from members of their same class and friends class and also from their derived class
Class 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time()// constructor 
{ 
minute = second = hour = 0; 
} 
voidChangeTime(int,int,int) 
{ 
hour = 12; 
second = 0; 
minute = 0; 
} 
private: 
inthour; 
intminute; 
intsecond; 
}; 
voidmain() 
{} 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time();// constructor 
voidChangeTime(int,int,int); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
voidmain() 
{ 
}
Class 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{ 
minute = second = hour = 0; 
} 
voidTime::ChangeTime(int,int,int) 
{ 
hour = 12; 
second = 0; 
minute = 0; 
} 
voidmain() 
{} 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time::Time(); 
voidTime::ChangeTime(int,int,int); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{ 
minute = second = hour = 0; 
} 
voidTime::ChangeTime(int,int,int) 
{ 
hour = 12; 
second = 0; 
minute = 0; 
} 
voidmain() 
{}
Class 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":" 
<< second << endl; 
} 
voidmain() 
{Time T; 
T.PrintTime(); 
T.ChangeTime(11,1,5); 
T.PrintTime(); 
} 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":" 
<< second << endl; 
} 
voidmain() 
{Time T; 
T.hour = 12; 
T.PrintTime();T.ChangeTime(11,1,5); T.PrintTime(); 
} 
0:0:0 
11:5:1 
Compiler error 
Attempting to access private data members
Class 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
Time::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl;} 
voidmain() 
{Time T; 
T.PrintTime(); 
T.ChangeTime(11,1,5); 
T.PrintTime();} 
Compiler error 
Missing return type of the PrintTime() function
Class 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
voidChangeHourFromOutside(int); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl;} 
voidTime::ChangeHourFromOutside(intx) 
{hour = x;} 
voidmain() 
{ 
intTempHour = 0;Time T; 
cout << "Enter The Hour: "<< endl; 
cin >> TempHour;//5 
T.PrintTime(); 
T.ChangeHourFromOutside(TempHour); 
T.PrintTime(); 
T.ChangeTime(11,1,5); 
T.PrintTime(); 
} 
Enter The Hour: 
5 
0:0:0 
5:0:0 
11:5:1 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
voidChangeHourFromOutside(int); 
voidClear (); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl;} 
voidTime::ChangeHourFromOutside(intx) 
{hour = x;} 
voidTime::Clear () 
{minute = second = hour = 0; } 
voidmain() 
{ 
intTempHour = 0;Time T; 
cout << "Enter The Hour: "<< endl; 
cin >> TempHour;//5 
T.PrintTime(); T.ChangeHourFromOutside(TempHour); 
T.Clear(); 
T.PrintTime(); 
T.ChangeTime(11,1,5); 
T.PrintTime(); 
} 
Enter The Hour: 
5 
0:0:0 
0:0:0 
11:5:1
Class 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
voidChangeHourFromOutside(int); 
voidClear (); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl;} 
voidTime::ChangeHourFromOutside(intx) 
{hour = x;} 
voidTime::Clear () 
{minute = second = hour = 0; } 
voidmain() 
{ 
Time T1;Time *T2 = &T1; Time* &T3 = T2; 
T1.ChangeHourFromOutside(5); 
T1.PrintTime(); 
T2->PrintTime(); 
T3->PrintTime(); 
} 
5:0:0 
5:0:0 
5:0:0 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
voidChangeHourFromOutside(int); 
voidClear (); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl;} 
voidTime::ChangeHourFromOutside(intx) 
{hour = x;} 
voidTime::Clear () 
{minute = second = hour = 0; } 
voidmain() 
{ 
Time T1;Time *T2 = &T1; Time &T3 = *T2; 
T1.ChangeHourFromOutside(5); 
T1.PrintTime(); 
T2->PrintTime(); 
T3.PrintTime(); 
} 
5:0:0 
5:0:0 
5:0:0
Class 
classTime 
{ 
public: 
Time(); 
voidChangeTime(int,int,int); 
voidPrintTime(); 
voidChangeHourFromOutside(int); 
voidClear (); 
private: 
inthour = 9; 
intminute; 
intsecond; 
}; 
Time::Time()// constructor 
{minute = second = hour = 0; } 
voidTime::ChangeTime(intx1,intx2,intx3) 
{hour = x1; second = x2; minute = x3;} 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl;} 
voidTime::ChangeHourFromOutside(intx) 
{hour = x;} 
voidTime::Clear () 
{minute = second = hour = 0; } 
voidmain() 
{ 
Time T1;Time *T2 = &T1; Time &T3 = *T2; 
T1.ChangeHourFromOutside(5); 
T1.PrintTime(); 
T2->PrintTime(); 
T3.PrintTime(); 
} 
Compiler error, can’t initialize when declaring no matter what private or public
Class 
•A Getter, Get function: 
–Reads private data 
–“Query” functions 
•A Setter, Set function: 
–Modify private data 
–Perform a validity check before modifying private data 
–Notify if invalid values
Class 
classTime 
{public: 
Time(); 
voidSetTime(int,int,int);// set functions 
voidSetHour(int); 
voidSetMinute(int); 
voidSetSecond(int); 
intGetHour();// get functions 
intGetMinute() 
;intGetSecond(); 
voidPrintTime(); 
private: inthour;intminute;intsecond; }; 
Time::Time() 
{hour = minute = second = 0; }; 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; 
intTime::GetHour() {returnhour;}; 
intTime::GetMinute() {returnminute;}; 
intTime::GetSecond() {returnsecond;}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
voidmain() 
{ 
Time T; 
T.PrintTime(); 
T.SetHour(17); 
T.SetMinute(45); 
T.SetSecond(22); 
T.PrintTime(); 
T.SetHour(77); 
T.SetMinute(565); 
T.SetSecond(-1); 
T.PrintTime(); 
} 
0:0:0 
17:45:22 
0:0:0 
Press any key to continue
Class 
•Utility functions (Helper functions) 
–private 
•Support operations of public member functions 
•Not intended for direct client use 
–Examples
Organizing Your Classes
Organizing Your Classes 
•Implementing classes 
•Separating Interface from implementation 
•Headers (in header file) –if needed- 
–# ifndef Time_H 
–# define Time_H 
–// your code goes here 
–# endif
Organizing Your Classes 
In MyMain.cpp 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T; 
T.PrintTime(); 
T.SetHour(17); 
T.SetMinute(45); 
T.SetSecond(22); 
T.PrintTime(); 
T.SetHour(77); 
T.SetMinute(565); 
T.SetSecond(-1); 
T.PrintTime(); 
} 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time(); 
voidSetTime(int,int,int); 
voidSetHour(int); 
voidSetMinute(int); 
voidSetSecond(int); 
intGetHour(); 
intGetMinute(); 
intGetSecond(); 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
In TimeClass.h 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
Time::Time() 
{hour = minute = second = 0; }; 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; 
intTime::GetHour() {returnhour;}; 
intTime::GetMinute() {returnminute;}; 
intTime::GetSecond() {returnsecond;}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
In TimeClass.cpp
Live Demo
Class Constructor
Class 
•Constructor 
–What’s it for? 
•Used to guarantee the initialization of data members of class 
–The same name as class 
–Called implicitly when object is created 
•Whether calling it is statically or dynamically 
•(new operator) * 
–NO return type 
–Can be overloaded 
–Must be declared in public section 
________________________________________ 
* That means that we don’t need to call the constructor explicitly
Class 
•Constructor 
–Two types: 
•Without parameters (Default constructor) 
•With parameters 
–When creating a class object, C++ provides its own constructor for the object 
•Compiler implicitly creates default constructor 
•The programmer should explicitly define it, why? 
–Coz the data otherwise wouldn’t be guaranteed to be in consistent state 
–And that doesn’t perform any initialization of fundamentals variables
Class Constructor 
classTime 
{ 
public: 
voidSetTime(int,int,int); 
voidSetHour(int);voidSetMinute(int); 
voidSetSecond(int); 
intGetHour();intGetMinute(); 
intGetSecond(); 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond); 
}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; 
intTime::GetHour() {returnhour;}; 
intTime::GetMinute() {returnminute;}; 
intTime::GetSecond() {returnsecond;}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T; 
T.PrintTime(); 
T.SetHour(17); 
T.SetMinute(45); 
T.SetSecond(22); 
T.PrintTime(); 
T.SetHour(77); 
T.SetMinute(565); 
T.SetSecond(-1); 
T.PrintTime(); 
} 
2486832:1588607792:2486968 
17:45:22 
0:0:0 
Press any key to continue 
Cauze we didn’t write the constructor ourself so that the variables of the class hasn’t been initialized properly
Class Constructor 
classTime 
{public: 
Time(); 
voidSetTime(int,int,int);// set functions 
voidSetHour(int); 
voidSetMinute(int); 
voidSetSecond(int); 
intGetHour();// get functions 
intGetMinute() 
;intGetSecond(); 
voidPrintTime(); 
private: inthour;intminute;intsecond; }; 
Time::Time() 
{hour = minute = second = 0; }; 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; 
intTime::GetHour() {returnhour;}; 
intTime::GetMinute() {returnminute;}; 
intTime::GetSecond() {returnsecond;}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
voidmain() 
{ 
Time T; 
T.PrintTime(); 
T.SetHour(17); 
T.SetMinute(45); 
T.SetSecond(22); 
T.PrintTime(); 
T.SetHour(77); 
T.SetMinute(565); 
T.SetSecond(-1); 
T.PrintTime(); 
} 
0:0:0 
17:45:22 
0:0:0 
Press any key to continue
Overloading Constructors
Overloading Constructors 
•Constructors can be overloaded 
–must all have the same name as class 
•Overloading function in General allows you to use the same name for separate functions that have different argument lists
Overloading Constructors 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time(); 
Time(intTimeHour); 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
Time::Time() 
{hour = minute = second = 0; }; 
Time::Time(intTimeHour) 
{hour = TimeHour; } 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T1; 
T1.PrintTime(); 
Time T2(2); 
T2.PrintTime(); 
} 
0:0:0 
2:1588607792:3011112 
Press any key to continue
Class –Member initializing list 
•Two ways to initialize member data by constructor 
–Code inserted in the constructor 
–A member initialization list 
•Given in the constructor definition 
–Not the prototype! 
•Follows the arguments list 
•Tells which member data to initialize with which arguments 
•So, how is the code? 
–Consists of 
»A colon followed by a comma separated list of member name argument pairs
Overloading Constructors 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time::Time():hour(),minute(),second(){}; 
Time(intTimeHour):hour(TimeHour),minute(0),second(0){};; 
Time(intTimeHour, intTimeMinute):hour(TimeHour),minute(TimeMinute),second(0){}; 
Time(intTimeHour, intTimeMinute, intTimeSecond):hour(TimeHour),minute(TimeMinute),second(TimeSecond){}; 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T1; 
T1.PrintTime(); 
Time T2(2); 
T2.PrintTime(); 
Time T3(2,3,06); 
T3.PrintTime(); 
} 
0:0:0 
2:0:0 
2:3:6
Overloading Constructors 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time::Time():hour(),minute(),second(); 
Time(intTimeHour):hour(TimeHour),minute(0),second(0){}; 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
Compiler error, missing {}
Overloading Constructors 
#include<iostream> 
usingnamespacestd; 
classTime 
{ 
public: 
Time::Time():hour(),minute(),second(){}; 
Time(intTimeHour):hour(TimeHour),minute(0),second(0){}; 
voidPrintTime(); 
private: 
inthour; 
intminute; 
intsecond; 
}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T1; 
T1.PrintTime(); 
Time T2(2); 
T2.PrintTime(); 
Time T3(2,3,06); 
T3.PrintTime(); 
} 
Compiler error, no oveloaded function which takes 3 arguments
Classes Destructors
Classes Destructors 
•Same name as class 
•Preceded by tilde (~) 
•No arguments 
•No return value 
•Can’t be overloaded 
–Once only 
•When is it called? 
–When the object is destroyed 
•Termination housekeeping 
•new delete 
•If no explicit destructor was declared, 
–Compiler creates “Empty destructor”
Constructors VS Destructors 
•Orders of constructor destructor calls 
–Depends on scope 
–Generally, destructor are called in reverse order of constructor call
Constructors VS Destructors 
•Global scope object: 
–Constructors: 
•Before any other function (including main!) 
–Destructors: 
•When 
–main terminates 
–exit function is called 
•Not called with abort
Constructors VS Destructors 
•Automatic local object: 
–Constructors: 
•When object is defined 
–Each time entering scope 
–Destructors: 
•When 
–Objects leave scope 
•Not called with abort or exit
Constructors VS Destructors 
•static local object: 
–Constructors: 
•Exactly once 
•When the execution reaches point where the object is defined 
–Destructors: 
•When 
–main terminates 
–exit function is called 
•Not called with abort
Class Constructor 
#include<iostream> 
usingnamespacestd; 
classobject 
{ 
public: 
object(intId, char*MsgPtr); 
~object(); 
private: 
intobjectId; 
char*message; 
}; 
object::object(intId, char*MsgPtr) 
{ 
objectId = Id; 
message = MsgPtr; 
cout << objectId << " constructor runs for "<< message << endl; 
} 
object::~object() 
{ 
cout << objectId << " destructor runs for "<< message << endl; 
} 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
object first(1,"Global before main before CreateCrazy"); 
voidCreateCrazy(); 
voidmain() 
{ 
staticobject fourth(4, "static in main"); 
CreateCrazy(); 
} 
voidCreateCrazy() 
{ 
object fifth(5, "In CreateCrazy"); 
} 
1 constructor runs for Global before main before CreateCrazy 
4 constructor runs for static in main 
5 constructor runs for In CreateCrazy 
5 destructor runs for In CreateCrazy 
4 destructor runs for static in main 
1 destructor runs for Global before main before CreateCrazy 
Press any key to continue
Copy Constructors
Copy Constructor 
•Special member functions 
–That’s implicitly called in the following 3 situations 
•Object used to initialize another object 
–obj1(obj2) 
•Passing an object arguments by value to another object 
•Returning a temporary object as the return value of a function
Copy Constructor 
•C++ provides its own implicitly copy constructor 
–Overloaded constructor 
–Has no return value 
–Take only arguments as a reference to an object of the same class 
–The compiler provides a copy whose signature is: 
–Class_Name::Class_Name (const Class_Name&) 
–When an object is passed to a function, a simple bitwise (exact) copy of that object is made and given to the function
Copy Constructor 
•Assignment “=” 
–Assign object to object 
–Is done by 
•Each member in the first object is copied individually to the same member in the second one 
•Done to the object that are value arguments
Copy Constructor 
#include<iostream> 
usingnamespacestd; 
classobject 
{ 
public: object(); 
object(constobject& ob); 
~object(); 
voiddisplay(); 
private: intnum, den;}; 
object::object() 
{cout << "constructing"<< endl; num = 0; den = 1;} 
object::object(constobject& ob) 
{cout << "copy constructing"<< endl; num = ob.num; den = ob.den;} 
object::~object() 
{cout << "destructing"<< endl;} 
voidobject::display() 
{cout << "num = "<< num << ", den = "<< den << endl;} 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidPlay(object x) 
{ 
cout << "In function Play"<< endl; 
x.display(); 
} 
voidmain() 
{ 
object ob1; object ob2; 
cout << "In main, before Play"<< endl; 
ob1.display(); 
ob2.display(); 
Play (ob1); 
cout << "In main, after calling Play "<< endl; 
ob1.display(); 
ob2.display(); 
ob2 = ob1; 
cout << "In main, after assignemet "<< endl; 
ob1.display(); 
ob2.display(); 
}
Copy Constructor 
#include<iostream> 
usingnamespacestd; 
classobject 
{ 
public: object(); 
object(constobject& ob); 
~object(); 
voiddisplay(); 
private: intnum, den;}; 
object::object() 
{cout << "constructing"<< endl; num = 0; den = 1;} 
object::object(constobject& ob) 
{cout << "copy constructing"<< endl; num = ob.num; den = ob.den;} 
object::~object() 
{cout << "destructing"<< endl;} 
voidobject::display() 
{cout << "num = "<< num << ", den = "<< den << endl;} 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidPlay(object x) 
{ 
cout << "In function Play"<< endl; 
x.display(); 
} 
voidmain() 
{ 
object ob1; object ob2; 
cout << "In main, before Play"<< endl; 
ob1.display(); 
ob2.display(); 
Play (ob1); 
cout << "In main, after calling Play "<< endl; 
ob1.display(); 
ob2.display(); 
ob2 = ob1; 
cout << "In main, after assignemet "<< endl; 
ob1.display(); 
ob2.display(); 
} 
constructing 
constructing 
In main, before Play 
num = 0, den = 1 
num = 0, den = 1 
copy constructing 
In function Play 
num = 0, den = 1 
destructing 
In main, after calling Play 
num = 0, den = 1 
num = 0, den = 1 
In main, after assignemet 
num = 0, den = 1 
num = 0, den = 1 
destructing 
destructing 
Press any key to continue
Cloning Objects
Shallow copy VS Deep Copy 
•C++,by default, use shallow copy for initializations and assignments 
•With shallow copy, if the object contains a pointer to allocated memory 
–The copy will also point to the memory as does the original object 
–Deleting the original object may cause the copied object to incorrectly disappear!
Shallow copy VS Deep Copy 
•If the class data points to a dynamic data 
–You should write your own copy constructor to create a “deep copy” of the dynamic data 
–A deep copy copies not only the class data members, but also makes a separate stored copy of any pointed to data 
–The copy constructor is implicitly called in initialization situations and makes a deep copy of the dynamic data in a different memory location
constData Members
constData Members 
•Specify object not modifiable 
•Compiler error if attempting to modify const object 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
constTime T1; // dealcaring cost with default constructor 
constTime T2(11,0,0); // decalring const with another constructor 
}
constData Members 
•Member function for constobject must be const 
–Can’t modify object 
•Specify constin both 
–Prototype: after parameter list 
–Definition: Before beginning left brace 
•Note! 
–Constructors and destructors can’t be const 
•Must be able to modify
constData Members 
classTime 
{public: 
Time(); 
voidSetTime(int,int,int); // set functions 
voidSetHour(int); 
voidSetMinute(inat); 
voidSetSecond(int); 
intGetHour() const; // get functions 
intGetMinute() const; 
intGetSecond() const; 
voidPrintTime1stform() const; 
voidPrintTime2ndform(); 
private: 
inthour;intminute;intsecond; 
}; 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
Time::Time() 
{hour = minute = second = 0; }; 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; 
intTime::GetHour() const{returnhour;}; 
intTime::GetMinute() const{returnminute;}; 
intTime::GetSecond() const{returnsecond;}; 
voidTime::PrintTime1stform ()const 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
voidTime::PrintTime2ndform () 
{cout << hour << ":"<< minute << ":"<< second << endl; }
constData Members 
0:20:0 
0:20:0 
0:0:0 
Press any key to continue 
voidmain() 
{ 
constTime T1;// delcaring cost with default constructor 
Time T2; 
T2.SetMinute(20);// non const -non constworking 
T2.GetHour();// non const -constworking 
// T1.SetHour(20);// const -non consterror 
T1.GetHour();// const -constworking 
T2.PrintTime1stform();// non const -constworking 
T2.PrintTime2ndform();// non const -non const working 
T1.PrintTime1stform();// const -constworking 
//T1.PrintTime2ndform();// const -non consterror 
} 
voidmain() 
{// delcaring const 
//with default constructor 
constTime T1; 
Time T2; 
} 
Everything is okay till now
constData Members 
•Initializing with member initializer list 
–Can be used for 
•all data members 
–Must be used for 
•constdata members 
•Data members that are references
constData Members 
classTime 
{public: 
Time(); 
voidSetTime(int,int,int); // set functions 
voidSetHour(int); 
voidSetMinute(int); 
voidSetSecond(int); 
intGetHour() const; // get functions 
intGetMinute() const; 
intGetSecond() const; 
voidPrintTime1stform() const; 
voidPrintTime2ndform(); 
private: 
inthour;intminute;constintsecond; 
}; 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
Time::Time() 
{hour = second = minute = 0;} 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
intTime::GetHour() const{returnhour;}; 
intTime::GetMinute() const{returnminute;}; 
intTime::GetSecond() const{returnsecond;}; 
voidTime::PrintTime1stform ()const 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
voidTime::PrintTime2ndform () 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
Compiler error, const member “second” must be initialize with member initializer list
constData Members 
classTime 
{public: 
Time(); 
voidSetTime(int,int,int); // set functions 
voidSetHour(int); 
voidSetMinute(int); 
voidSetSecond(int); 
intGetHour() const; // get functions 
intGetMinute() const; 
intGetSecond() const; 
voidPrintTime1stform() const; 
voidPrintTime2ndform(); 
private: 
inthour;intminute;constintsecond; 
}; 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
Time::Time():hour(0),minute(0),second(0){}; 
voidTime::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; 
intTime::GetHour() const{returnhour;}; 
intTime::GetMinute() const{returnminute;}; 
intTime::GetSecond() const{returnsecond;}; 
voidTime::PrintTime1stform ()const 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
voidTime::PrintTime2ndform () 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
Compile error, function SetSecond change the const second
constData Members 
classTime 
{public: 
Time(); 
voidSetTime(int,int); // set functions 
voidSetHour(int); 
voidSetMinute(int); 
intGetHour() const; // get functions 
intGetMinute() const; 
intGetSecond() const; 
voidPrintTime1stform() const; 
voidPrintTime2ndform(); 
private: inthour;intminute;constintsecond; }; 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
Time::Time():hour(0),minute(0),second(5){}; 
voidTime::SetTime(intxhour,intxminute ) 
{SetHour(xhour);SetMinute(xminute);}; 
voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; 
voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; 
intTime::GetHour() const{returnhour;}; 
intTime::GetMinute() const{returnminute;}; 
intTime::GetSecond() const{returnsecond;}; 
voidTime::PrintTime1stform ()const 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
voidTime::PrintTime2ndform () 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
2:3:5 
1:4:5 
Press any key to continue 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T1; 
T1.SetTime(2,3); 
T1.PrintTime1stform(); 
T1.SetTime(1,4); 
T1.PrintTime1stform(); 
}
Quiz
Quiz #1 
#include<iostream> 
usingnamespacestd; 
classobject 
{ 
public: object(); 
object(constobject& ob); 
~object(); 
voiddisplay(); 
voidChangeden(int); 
private: intnum, den;}; 
object::object() 
{cout << "constructing"<< endl;num = 0;den = 1;} 
object::object(constobject& ob) 
{cout << "copy constructing"<< endl;num = ob.num;den = ob.den;} 
object::~object() 
{cout << "destructing"<< endl; } 
voidobject::display() 
{cout << "num = "<< num << ", den = "<< den << endl;} 
voidobject::Changeden(intx ) 
{den = x; } 
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidPlayAgain(object x) 
{cout << "In function PlayAgain"<< endl;x.display();} 
voidPlay(object x) 
{cout << "In function Play"<< endl;PlayAgain(x); x.display();} 
voidmain() 
{object ob1; object ob2; 
cout << "In main, before Play"<< endl; 
ob1.display(); 
ob2.display(); 
Play (ob1); 
cout << "In main, after calling Play "<< endl; 
ob1.display(); 
ob2.display(); 
ob1.Changeden(3); 
ob2 = ob1; 
cout << "In main, after assignemet "<< endl; 
ob1.display(); 
ob2.Changeden(5); 
ob2.display(); 
}
#include<iostream> 
#include"TimeClass.h" 
usingnamespacestd; 
voidPlayAgain(object x) 
{cout << "In function PlayAgain"<< endl;x.display();} 
voidPlay(object x) 
{cout << "In function Play"<< endl;PlayAgain(x); x.display();} 
voidmain() 
{object ob1; object ob2; 
cout << "In main, before Play"<< endl; 
ob1.display(); 
ob2.display(); 
Play (ob1); 
cout << "In main, after calling Play "<< endl; 
ob1.display(); 
ob2.display(); 
ob1.Changeden(3); 
ob2 = ob1; 
cout << "In main, after assignemet "<< endl; 
ob1.display(); 
ob2.Changeden(5); 
ob2.display(); 
} 
Quiz #1 
#include<iostream> 
usingnamespacestd; 
classobject 
{ 
public: object(); 
object(constobject& ob); 
~object(); 
voiddisplay(); 
voidChangeden(int); 
private: intnum, den;}; 
object::object() 
{cout << "constructing"<< endl;num = 0;den = 1;} 
object::object(constobject& ob) 
{cout << "copy constructing"<< endl;num = ob.num;den = ob.den;} 
object::~object() 
{cout << "destructing"<< endl; } 
voidobject::display() 
{cout << "num = "<< num << ", den = "<< den << endl;} 
voidobject::Changeden(intx ) 
{den = x; } 
constructing 
constructing 
In main, before Play 
num = 0, den = 1 
num = 0, den = 1 
copy constructing 
In function Play 
copy constructing 
In function PlayAgain 
num = 0, den = 1 
destructing 
num = 0, den = 1 
destructing 
In main, after calling Play 
num = 0, den = 1 
num = 0, den = 1 
In main, after assignemet 
num = 0, den = 3 
num = 0, den = 5 
destructing 
destructing 
Press any key to continue
#include <iostream> 
#include "Testing.h" 
using namespace std; 
object first(1,"Global before main before CreateCrazy"); 
void CreateCrazy(); 
static object second(2,"Static Global before main after CreateCrazy"); 
void main() 
{ 
object bar(100, "in main"); 
{ 
object third (3,"in main"); 
} 
static object fourth(4, "static in main"); 
CreateCrazy(); 
object eigth(8,"after CreateCrazyin main"); 
} 
void CreateCrazy() 
{ 
object fifth(5, "In CreateCrazy"); 
static object sixth(6, "Local static in CreateCrazy"); 
object seventh(7, "In CreateCrazy"); 
} 
Quiz #2 
#include<iostream> 
usingnamespacestd; 
classobject 
{ 
public: 
object(intId, char*MsgPtr); 
~object(); 
private: 
intobjectId; 
char*message; 
}; 
object::object(intId, char*MsgPtr) 
{ 
objectId = Id; 
message = MsgPtr; 
cout << objectId << " constructor runs for "<< message << endl; 
} 
object::~object() 
{cout << objectId << " destructor runs for "<< message << endl;}
Quiz #2 
1 constructor runs for Global before main before CreateCrazy 
2 constructor runs for Static Global before main after CreateCrazy 
100 constructor runs for in main 
3 constructor runs for in main 
3 destructor runs for in main 
4 constructor runs for static in main 
5 constructor runs for In CreateCrazy 
6 constructor runs for Local static in CreateCrazy 
7 constructor runs for In CreateCrazy 
7 destructor runs for In CreateCrazy 
5 destructor runs for In CreateCrazy 
8 constructor runs for after CreateCrazy in main 
8 destructor runs for after CreateCrazy in main 
100 destructor runs for in main 
6 destructor runs for Local static in CreateCrazy 
4 destructor runs for static in main 
2 destructor runs for Static Global before main after CreateCrazy 
1 destructor runs for Global before main before CreateCrazy 
Press any key to continue 
Local Static is the last one of all time. Just the Global is destruct after it!

C++ L08-Classes Part1

  • 1.
    C++ L08-CLASSES, P1 Programming Language Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14
  • 2.
  • 3.
  • 4.
    Float, double, longdouble C++ data types Structured Simple Address Pointer Reference enum Floating Array Struct Union Class Char, Short, int, long, bool Integral
  • 5.
  • 6.
    aClass •class –Concept •Information hiding •Prevent access to their inner workings •Encapsulation (private, public) –code •class –Is an extension of the idea of “struct” •class –User defined (programmer defined) type •Once class is defined it can be used as a type •Object of that class can be defined •No memory is allocated (like struct)
  • 7.
    a Class •Classpackage data declarations with function declarations –Thus, coupling data with behavior •Used to implement “ADT” •Examples: –Time, Complex numbers,
  • 8.
    Class •Type: –Time •Domain: –Any time value is a time in: Hour, minute, second •Operations: –Set the time –Print the time –Incrementing / decrementing time –Compare times •=, <, >
  • 9.
    Class •Members –Theyare the components of the class •Attributes •Behaviors methods •Class instance –Object
  • 10.
    Class •OOP andclasses –Encapsulation –Composition –Inheritance –Reusable
  • 11.
    Class #include<iostream> usingnamespacestd; classMyClass { }; voidmain() { } Compile & run #include<iostream> usingnamespacestd; classMyClass { } voidmain() { } Compiler error. Missing ; #include<iostream> usingnamespacestd; classMyClass { inti; }; voidmain() { } Compile and run #include<iostream> usingnamespacestd; classMyClass { inti = 0; }; voidmain() { } Compiler error Rule: You can’t initialize a variable when you declare it in classes.
  • 12.
    Class •Function inclasses –It can directly access any member of the class (WITHOUT PASSING IT AS PARAMETER) •Data members •Function members
  • 13.
    Class •private: –Defaultaccess mode –Accessible just to the functions of the class and friends •public: –Accessible wherever object of class in scope •protected: –Accessible from members of their same class and friends class and also from their derived class
  • 14.
    Class #include<iostream> usingnamespacestd; classTime { public: Time()// constructor { minute = second = hour = 0; } voidChangeTime(int,int,int) { hour = 12; second = 0; minute = 0; } private: inthour; intminute; intsecond; }; voidmain() {} #include<iostream> usingnamespacestd; classTime { public: Time();// constructor voidChangeTime(int,int,int); private: inthour; intminute; intsecond; }; voidmain() { }
  • 15.
    Class #include<iostream> usingnamespacestd; classTime { public: Time(); voidChangeTime(int,int,int); private: inthour; intminute; intsecond; }; Time::Time()// constructor { minute = second = hour = 0; } voidTime::ChangeTime(int,int,int) { hour = 12; second = 0; minute = 0; } voidmain() {} #include<iostream> usingnamespacestd; classTime { public: Time::Time(); voidTime::ChangeTime(int,int,int); private: inthour; intminute; intsecond; }; Time::Time()// constructor { minute = second = hour = 0; } voidTime::ChangeTime(int,int,int) { hour = 12; second = 0; minute = 0; } voidmain() {}
  • 16.
    Class #include<iostream> usingnamespacestd; classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":" << second << endl; } voidmain() {Time T; T.PrintTime(); T.ChangeTime(11,1,5); T.PrintTime(); } #include<iostream> usingnamespacestd; classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":" << second << endl; } voidmain() {Time T; T.hour = 12; T.PrintTime();T.ChangeTime(11,1,5); T.PrintTime(); } 0:0:0 11:5:1 Compiler error Attempting to access private data members
  • 17.
    Class #include<iostream> usingnamespacestd; classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} Time::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl;} voidmain() {Time T; T.PrintTime(); T.ChangeTime(11,1,5); T.PrintTime();} Compiler error Missing return type of the PrintTime() function
  • 18.
    Class classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); voidChangeHourFromOutside(int); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl;} voidTime::ChangeHourFromOutside(intx) {hour = x;} voidmain() { intTempHour = 0;Time T; cout << "Enter The Hour: "<< endl; cin >> TempHour;//5 T.PrintTime(); T.ChangeHourFromOutside(TempHour); T.PrintTime(); T.ChangeTime(11,1,5); T.PrintTime(); } Enter The Hour: 5 0:0:0 5:0:0 11:5:1 classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); voidChangeHourFromOutside(int); voidClear (); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl;} voidTime::ChangeHourFromOutside(intx) {hour = x;} voidTime::Clear () {minute = second = hour = 0; } voidmain() { intTempHour = 0;Time T; cout << "Enter The Hour: "<< endl; cin >> TempHour;//5 T.PrintTime(); T.ChangeHourFromOutside(TempHour); T.Clear(); T.PrintTime(); T.ChangeTime(11,1,5); T.PrintTime(); } Enter The Hour: 5 0:0:0 0:0:0 11:5:1
  • 19.
    Class classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); voidChangeHourFromOutside(int); voidClear (); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl;} voidTime::ChangeHourFromOutside(intx) {hour = x;} voidTime::Clear () {minute = second = hour = 0; } voidmain() { Time T1;Time *T2 = &T1; Time* &T3 = T2; T1.ChangeHourFromOutside(5); T1.PrintTime(); T2->PrintTime(); T3->PrintTime(); } 5:0:0 5:0:0 5:0:0 classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); voidChangeHourFromOutside(int); voidClear (); private: inthour; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl;} voidTime::ChangeHourFromOutside(intx) {hour = x;} voidTime::Clear () {minute = second = hour = 0; } voidmain() { Time T1;Time *T2 = &T1; Time &T3 = *T2; T1.ChangeHourFromOutside(5); T1.PrintTime(); T2->PrintTime(); T3.PrintTime(); } 5:0:0 5:0:0 5:0:0
  • 20.
    Class classTime { public: Time(); voidChangeTime(int,int,int); voidPrintTime(); voidChangeHourFromOutside(int); voidClear (); private: inthour = 9; intminute; intsecond; }; Time::Time()// constructor {minute = second = hour = 0; } voidTime::ChangeTime(intx1,intx2,intx3) {hour = x1; second = x2; minute = x3;} voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl;} voidTime::ChangeHourFromOutside(intx) {hour = x;} voidTime::Clear () {minute = second = hour = 0; } voidmain() { Time T1;Time *T2 = &T1; Time &T3 = *T2; T1.ChangeHourFromOutside(5); T1.PrintTime(); T2->PrintTime(); T3.PrintTime(); } Compiler error, can’t initialize when declaring no matter what private or public
  • 21.
    Class •A Getter,Get function: –Reads private data –“Query” functions •A Setter, Set function: –Modify private data –Perform a validity check before modifying private data –Notify if invalid values
  • 22.
    Class classTime {public: Time(); voidSetTime(int,int,int);// set functions voidSetHour(int); voidSetMinute(int); voidSetSecond(int); intGetHour();// get functions intGetMinute() ;intGetSecond(); voidPrintTime(); private: inthour;intminute;intsecond; }; Time::Time() {hour = minute = second = 0; }; voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; intTime::GetHour() {returnhour;}; intTime::GetMinute() {returnminute;}; intTime::GetSecond() {returnsecond;}; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } voidmain() { Time T; T.PrintTime(); T.SetHour(17); T.SetMinute(45); T.SetSecond(22); T.PrintTime(); T.SetHour(77); T.SetMinute(565); T.SetSecond(-1); T.PrintTime(); } 0:0:0 17:45:22 0:0:0 Press any key to continue
  • 23.
    Class •Utility functions(Helper functions) –private •Support operations of public member functions •Not intended for direct client use –Examples
  • 24.
  • 25.
    Organizing Your Classes •Implementing classes •Separating Interface from implementation •Headers (in header file) –if needed- –# ifndef Time_H –# define Time_H –// your code goes here –# endif
  • 26.
    Organizing Your Classes In MyMain.cpp #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { Time T; T.PrintTime(); T.SetHour(17); T.SetMinute(45); T.SetSecond(22); T.PrintTime(); T.SetHour(77); T.SetMinute(565); T.SetSecond(-1); T.PrintTime(); } #include<iostream> usingnamespacestd; classTime { public: Time(); voidSetTime(int,int,int); voidSetHour(int); voidSetMinute(int); voidSetSecond(int); intGetHour(); intGetMinute(); intGetSecond(); voidPrintTime(); private: inthour; intminute; intsecond; }; In TimeClass.h #include<iostream> #include"TimeClass.h" usingnamespacestd; Time::Time() {hour = minute = second = 0; }; voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; intTime::GetHour() {returnhour;}; intTime::GetMinute() {returnminute;}; intTime::GetSecond() {returnsecond;}; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } In TimeClass.cpp
  • 27.
  • 28.
  • 29.
    Class •Constructor –What’sit for? •Used to guarantee the initialization of data members of class –The same name as class –Called implicitly when object is created •Whether calling it is statically or dynamically •(new operator) * –NO return type –Can be overloaded –Must be declared in public section ________________________________________ * That means that we don’t need to call the constructor explicitly
  • 30.
    Class •Constructor –Twotypes: •Without parameters (Default constructor) •With parameters –When creating a class object, C++ provides its own constructor for the object •Compiler implicitly creates default constructor •The programmer should explicitly define it, why? –Coz the data otherwise wouldn’t be guaranteed to be in consistent state –And that doesn’t perform any initialization of fundamentals variables
  • 31.
    Class Constructor classTime { public: voidSetTime(int,int,int); voidSetHour(int);voidSetMinute(int); voidSetSecond(int); intGetHour();intGetMinute(); intGetSecond(); voidPrintTime(); private: inthour; intminute; intsecond; }; #include<iostream> #include"TimeClass.h" usingnamespacestd; voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond); }; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; intTime::GetHour() {returnhour;}; intTime::GetMinute() {returnminute;}; intTime::GetSecond() {returnsecond;}; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { Time T; T.PrintTime(); T.SetHour(17); T.SetMinute(45); T.SetSecond(22); T.PrintTime(); T.SetHour(77); T.SetMinute(565); T.SetSecond(-1); T.PrintTime(); } 2486832:1588607792:2486968 17:45:22 0:0:0 Press any key to continue Cauze we didn’t write the constructor ourself so that the variables of the class hasn’t been initialized properly
  • 32.
    Class Constructor classTime {public: Time(); voidSetTime(int,int,int);// set functions voidSetHour(int); voidSetMinute(int); voidSetSecond(int); intGetHour();// get functions intGetMinute() ;intGetSecond(); voidPrintTime(); private: inthour;intminute;intsecond; }; Time::Time() {hour = minute = second = 0; }; voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; intTime::GetHour() {returnhour;}; intTime::GetMinute() {returnminute;}; intTime::GetSecond() {returnsecond;}; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } voidmain() { Time T; T.PrintTime(); T.SetHour(17); T.SetMinute(45); T.SetSecond(22); T.PrintTime(); T.SetHour(77); T.SetMinute(565); T.SetSecond(-1); T.PrintTime(); } 0:0:0 17:45:22 0:0:0 Press any key to continue
  • 33.
  • 34.
    Overloading Constructors •Constructorscan be overloaded –must all have the same name as class •Overloading function in General allows you to use the same name for separate functions that have different argument lists
  • 35.
    Overloading Constructors #include<iostream> usingnamespacestd; classTime { public: Time(); Time(intTimeHour); voidPrintTime(); private: inthour; intminute; intsecond; }; #include<iostream> #include"TimeClass.h" usingnamespacestd; Time::Time() {hour = minute = second = 0; }; Time::Time(intTimeHour) {hour = TimeHour; } voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { Time T1; T1.PrintTime(); Time T2(2); T2.PrintTime(); } 0:0:0 2:1588607792:3011112 Press any key to continue
  • 36.
    Class –Member initializinglist •Two ways to initialize member data by constructor –Code inserted in the constructor –A member initialization list •Given in the constructor definition –Not the prototype! •Follows the arguments list •Tells which member data to initialize with which arguments •So, how is the code? –Consists of »A colon followed by a comma separated list of member name argument pairs
  • 37.
    Overloading Constructors #include<iostream> usingnamespacestd; classTime { public: Time::Time():hour(),minute(),second(){}; Time(intTimeHour):hour(TimeHour),minute(0),second(0){};; Time(intTimeHour, intTimeMinute):hour(TimeHour),minute(TimeMinute),second(0){}; Time(intTimeHour, intTimeMinute, intTimeSecond):hour(TimeHour),minute(TimeMinute),second(TimeSecond){}; voidPrintTime(); private: inthour; intminute; intsecond; }; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { Time T1; T1.PrintTime(); Time T2(2); T2.PrintTime(); Time T3(2,3,06); T3.PrintTime(); } 0:0:0 2:0:0 2:3:6
  • 38.
    Overloading Constructors #include<iostream> usingnamespacestd; classTime { public: Time::Time():hour(),minute(),second(); Time(intTimeHour):hour(TimeHour),minute(0),second(0){}; voidPrintTime(); private: inthour; intminute; intsecond; }; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } Compiler error, missing {}
  • 39.
    Overloading Constructors #include<iostream> usingnamespacestd; classTime { public: Time::Time():hour(),minute(),second(){}; Time(intTimeHour):hour(TimeHour),minute(0),second(0){}; voidPrintTime(); private: inthour; intminute; intsecond; }; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { Time T1; T1.PrintTime(); Time T2(2); T2.PrintTime(); Time T3(2,3,06); T3.PrintTime(); } Compiler error, no oveloaded function which takes 3 arguments
  • 40.
  • 41.
    Classes Destructors •Samename as class •Preceded by tilde (~) •No arguments •No return value •Can’t be overloaded –Once only •When is it called? –When the object is destroyed •Termination housekeeping •new delete •If no explicit destructor was declared, –Compiler creates “Empty destructor”
  • 42.
    Constructors VS Destructors •Orders of constructor destructor calls –Depends on scope –Generally, destructor are called in reverse order of constructor call
  • 43.
    Constructors VS Destructors •Global scope object: –Constructors: •Before any other function (including main!) –Destructors: •When –main terminates –exit function is called •Not called with abort
  • 44.
    Constructors VS Destructors •Automatic local object: –Constructors: •When object is defined –Each time entering scope –Destructors: •When –Objects leave scope •Not called with abort or exit
  • 45.
    Constructors VS Destructors •static local object: –Constructors: •Exactly once •When the execution reaches point where the object is defined –Destructors: •When –main terminates –exit function is called •Not called with abort
  • 46.
    Class Constructor #include<iostream> usingnamespacestd; classobject { public: object(intId, char*MsgPtr); ~object(); private: intobjectId; char*message; }; object::object(intId, char*MsgPtr) { objectId = Id; message = MsgPtr; cout << objectId << " constructor runs for "<< message << endl; } object::~object() { cout << objectId << " destructor runs for "<< message << endl; } #include<iostream> #include"TimeClass.h" usingnamespacestd; object first(1,"Global before main before CreateCrazy"); voidCreateCrazy(); voidmain() { staticobject fourth(4, "static in main"); CreateCrazy(); } voidCreateCrazy() { object fifth(5, "In CreateCrazy"); } 1 constructor runs for Global before main before CreateCrazy 4 constructor runs for static in main 5 constructor runs for In CreateCrazy 5 destructor runs for In CreateCrazy 4 destructor runs for static in main 1 destructor runs for Global before main before CreateCrazy Press any key to continue
  • 47.
  • 48.
    Copy Constructor •Specialmember functions –That’s implicitly called in the following 3 situations •Object used to initialize another object –obj1(obj2) •Passing an object arguments by value to another object •Returning a temporary object as the return value of a function
  • 49.
    Copy Constructor •C++provides its own implicitly copy constructor –Overloaded constructor –Has no return value –Take only arguments as a reference to an object of the same class –The compiler provides a copy whose signature is: –Class_Name::Class_Name (const Class_Name&) –When an object is passed to a function, a simple bitwise (exact) copy of that object is made and given to the function
  • 50.
    Copy Constructor •Assignment“=” –Assign object to object –Is done by •Each member in the first object is copied individually to the same member in the second one •Done to the object that are value arguments
  • 51.
    Copy Constructor #include<iostream> usingnamespacestd; classobject { public: object(); object(constobject& ob); ~object(); voiddisplay(); private: intnum, den;}; object::object() {cout << "constructing"<< endl; num = 0; den = 1;} object::object(constobject& ob) {cout << "copy constructing"<< endl; num = ob.num; den = ob.den;} object::~object() {cout << "destructing"<< endl;} voidobject::display() {cout << "num = "<< num << ", den = "<< den << endl;} #include<iostream> #include"TimeClass.h" usingnamespacestd; voidPlay(object x) { cout << "In function Play"<< endl; x.display(); } voidmain() { object ob1; object ob2; cout << "In main, before Play"<< endl; ob1.display(); ob2.display(); Play (ob1); cout << "In main, after calling Play "<< endl; ob1.display(); ob2.display(); ob2 = ob1; cout << "In main, after assignemet "<< endl; ob1.display(); ob2.display(); }
  • 52.
    Copy Constructor #include<iostream> usingnamespacestd; classobject { public: object(); object(constobject& ob); ~object(); voiddisplay(); private: intnum, den;}; object::object() {cout << "constructing"<< endl; num = 0; den = 1;} object::object(constobject& ob) {cout << "copy constructing"<< endl; num = ob.num; den = ob.den;} object::~object() {cout << "destructing"<< endl;} voidobject::display() {cout << "num = "<< num << ", den = "<< den << endl;} #include<iostream> #include"TimeClass.h" usingnamespacestd; voidPlay(object x) { cout << "In function Play"<< endl; x.display(); } voidmain() { object ob1; object ob2; cout << "In main, before Play"<< endl; ob1.display(); ob2.display(); Play (ob1); cout << "In main, after calling Play "<< endl; ob1.display(); ob2.display(); ob2 = ob1; cout << "In main, after assignemet "<< endl; ob1.display(); ob2.display(); } constructing constructing In main, before Play num = 0, den = 1 num = 0, den = 1 copy constructing In function Play num = 0, den = 1 destructing In main, after calling Play num = 0, den = 1 num = 0, den = 1 In main, after assignemet num = 0, den = 1 num = 0, den = 1 destructing destructing Press any key to continue
  • 53.
  • 54.
    Shallow copy VSDeep Copy •C++,by default, use shallow copy for initializations and assignments •With shallow copy, if the object contains a pointer to allocated memory –The copy will also point to the memory as does the original object –Deleting the original object may cause the copied object to incorrectly disappear!
  • 55.
    Shallow copy VSDeep Copy •If the class data points to a dynamic data –You should write your own copy constructor to create a “deep copy” of the dynamic data –A deep copy copies not only the class data members, but also makes a separate stored copy of any pointed to data –The copy constructor is implicitly called in initialization situations and makes a deep copy of the dynamic data in a different memory location
  • 56.
  • 57.
    constData Members •Specifyobject not modifiable •Compiler error if attempting to modify const object #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { constTime T1; // dealcaring cost with default constructor constTime T2(11,0,0); // decalring const with another constructor }
  • 58.
    constData Members •Memberfunction for constobject must be const –Can’t modify object •Specify constin both –Prototype: after parameter list –Definition: Before beginning left brace •Note! –Constructors and destructors can’t be const •Must be able to modify
  • 59.
    constData Members classTime {public: Time(); voidSetTime(int,int,int); // set functions voidSetHour(int); voidSetMinute(inat); voidSetSecond(int); intGetHour() const; // get functions intGetMinute() const; intGetSecond() const; voidPrintTime1stform() const; voidPrintTime2ndform(); private: inthour;intminute;intsecond; }; #include<iostream> #include"TimeClass.h" usingnamespacestd; Time::Time() {hour = minute = second = 0; }; voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; intTime::GetHour() const{returnhour;}; intTime::GetMinute() const{returnminute;}; intTime::GetSecond() const{returnsecond;}; voidTime::PrintTime1stform ()const {cout << hour << ":"<< minute << ":"<< second << endl; } voidTime::PrintTime2ndform () {cout << hour << ":"<< minute << ":"<< second << endl; }
  • 60.
    constData Members 0:20:0 0:20:0 0:0:0 Press any key to continue voidmain() { constTime T1;// delcaring cost with default constructor Time T2; T2.SetMinute(20);// non const -non constworking T2.GetHour();// non const -constworking // T1.SetHour(20);// const -non consterror T1.GetHour();// const -constworking T2.PrintTime1stform();// non const -constworking T2.PrintTime2ndform();// non const -non const working T1.PrintTime1stform();// const -constworking //T1.PrintTime2ndform();// const -non consterror } voidmain() {// delcaring const //with default constructor constTime T1; Time T2; } Everything is okay till now
  • 61.
    constData Members •Initializingwith member initializer list –Can be used for •all data members –Must be used for •constdata members •Data members that are references
  • 62.
    constData Members classTime {public: Time(); voidSetTime(int,int,int); // set functions voidSetHour(int); voidSetMinute(int); voidSetSecond(int); intGetHour() const; // get functions intGetMinute() const; intGetSecond() const; voidPrintTime1stform() const; voidPrintTime2ndform(); private: inthour;intminute;constintsecond; }; #include<iostream> #include"TimeClass.h" usingnamespacestd; Time::Time() {hour = second = minute = 0;} voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; intTime::GetHour() const{returnhour;}; intTime::GetMinute() const{returnminute;}; intTime::GetSecond() const{returnsecond;}; voidTime::PrintTime1stform ()const {cout << hour << ":"<< minute << ":"<< second << endl; } voidTime::PrintTime2ndform () {cout << hour << ":"<< minute << ":"<< second << endl; } Compiler error, const member “second” must be initialize with member initializer list
  • 63.
    constData Members classTime {public: Time(); voidSetTime(int,int,int); // set functions voidSetHour(int); voidSetMinute(int); voidSetSecond(int); intGetHour() const; // get functions intGetMinute() const; intGetSecond() const; voidPrintTime1stform() const; voidPrintTime2ndform(); private: inthour;intminute;constintsecond; }; #include<iostream> #include"TimeClass.h" usingnamespacestd; Time::Time():hour(0),minute(0),second(0){}; voidTime::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; voidTime::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; }; intTime::GetHour() const{returnhour;}; intTime::GetMinute() const{returnminute;}; intTime::GetSecond() const{returnsecond;}; voidTime::PrintTime1stform ()const {cout << hour << ":"<< minute << ":"<< second << endl; } voidTime::PrintTime2ndform () {cout << hour << ":"<< minute << ":"<< second << endl; } Compile error, function SetSecond change the const second
  • 64.
    constData Members classTime {public: Time(); voidSetTime(int,int); // set functions voidSetHour(int); voidSetMinute(int); intGetHour() const; // get functions intGetMinute() const; intGetSecond() const; voidPrintTime1stform() const; voidPrintTime2ndform(); private: inthour;intminute;constintsecond; }; #include<iostream> #include"TimeClass.h" usingnamespacestd; Time::Time():hour(0),minute(0),second(5){}; voidTime::SetTime(intxhour,intxminute ) {SetHour(xhour);SetMinute(xminute);}; voidTime::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; }; voidTime::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; }; intTime::GetHour() const{returnhour;}; intTime::GetMinute() const{returnminute;}; intTime::GetSecond() const{returnsecond;}; voidTime::PrintTime1stform ()const {cout << hour << ":"<< minute << ":"<< second << endl; } voidTime::PrintTime2ndform () {cout << hour << ":"<< minute << ":"<< second << endl; } 2:3:5 1:4:5 Press any key to continue #include<iostream> #include"TimeClass.h" usingnamespacestd; voidmain() { Time T1; T1.SetTime(2,3); T1.PrintTime1stform(); T1.SetTime(1,4); T1.PrintTime1stform(); }
  • 65.
  • 66.
    Quiz #1 #include<iostream> usingnamespacestd; classobject { public: object(); object(constobject& ob); ~object(); voiddisplay(); voidChangeden(int); private: intnum, den;}; object::object() {cout << "constructing"<< endl;num = 0;den = 1;} object::object(constobject& ob) {cout << "copy constructing"<< endl;num = ob.num;den = ob.den;} object::~object() {cout << "destructing"<< endl; } voidobject::display() {cout << "num = "<< num << ", den = "<< den << endl;} voidobject::Changeden(intx ) {den = x; } #include<iostream> #include"TimeClass.h" usingnamespacestd; voidPlayAgain(object x) {cout << "In function PlayAgain"<< endl;x.display();} voidPlay(object x) {cout << "In function Play"<< endl;PlayAgain(x); x.display();} voidmain() {object ob1; object ob2; cout << "In main, before Play"<< endl; ob1.display(); ob2.display(); Play (ob1); cout << "In main, after calling Play "<< endl; ob1.display(); ob2.display(); ob1.Changeden(3); ob2 = ob1; cout << "In main, after assignemet "<< endl; ob1.display(); ob2.Changeden(5); ob2.display(); }
  • 67.
    #include<iostream> #include"TimeClass.h" usingnamespacestd; voidPlayAgain(object x) {cout << "In function PlayAgain"<< endl;x.display();} voidPlay(object x) {cout << "In function Play"<< endl;PlayAgain(x); x.display();} voidmain() {object ob1; object ob2; cout << "In main, before Play"<< endl; ob1.display(); ob2.display(); Play (ob1); cout << "In main, after calling Play "<< endl; ob1.display(); ob2.display(); ob1.Changeden(3); ob2 = ob1; cout << "In main, after assignemet "<< endl; ob1.display(); ob2.Changeden(5); ob2.display(); } Quiz #1 #include<iostream> usingnamespacestd; classobject { public: object(); object(constobject& ob); ~object(); voiddisplay(); voidChangeden(int); private: intnum, den;}; object::object() {cout << "constructing"<< endl;num = 0;den = 1;} object::object(constobject& ob) {cout << "copy constructing"<< endl;num = ob.num;den = ob.den;} object::~object() {cout << "destructing"<< endl; } voidobject::display() {cout << "num = "<< num << ", den = "<< den << endl;} voidobject::Changeden(intx ) {den = x; } constructing constructing In main, before Play num = 0, den = 1 num = 0, den = 1 copy constructing In function Play copy constructing In function PlayAgain num = 0, den = 1 destructing num = 0, den = 1 destructing In main, after calling Play num = 0, den = 1 num = 0, den = 1 In main, after assignemet num = 0, den = 3 num = 0, den = 5 destructing destructing Press any key to continue
  • 68.
    #include <iostream> #include"Testing.h" using namespace std; object first(1,"Global before main before CreateCrazy"); void CreateCrazy(); static object second(2,"Static Global before main after CreateCrazy"); void main() { object bar(100, "in main"); { object third (3,"in main"); } static object fourth(4, "static in main"); CreateCrazy(); object eigth(8,"after CreateCrazyin main"); } void CreateCrazy() { object fifth(5, "In CreateCrazy"); static object sixth(6, "Local static in CreateCrazy"); object seventh(7, "In CreateCrazy"); } Quiz #2 #include<iostream> usingnamespacestd; classobject { public: object(intId, char*MsgPtr); ~object(); private: intobjectId; char*message; }; object::object(intId, char*MsgPtr) { objectId = Id; message = MsgPtr; cout << objectId << " constructor runs for "<< message << endl; } object::~object() {cout << objectId << " destructor runs for "<< message << endl;}
  • 69.
    Quiz #2 1constructor runs for Global before main before CreateCrazy 2 constructor runs for Static Global before main after CreateCrazy 100 constructor runs for in main 3 constructor runs for in main 3 destructor runs for in main 4 constructor runs for static in main 5 constructor runs for In CreateCrazy 6 constructor runs for Local static in CreateCrazy 7 constructor runs for In CreateCrazy 7 destructor runs for In CreateCrazy 5 destructor runs for In CreateCrazy 8 constructor runs for after CreateCrazy in main 8 destructor runs for after CreateCrazy in main 100 destructor runs for in main 6 destructor runs for Local static in CreateCrazy 4 destructor runs for static in main 2 destructor runs for Static Global before main after CreateCrazy 1 destructor runs for Global before main before CreateCrazy Press any key to continue Local Static is the last one of all time. Just the Global is destruct after it!