KEMBAR78
C++ L09-Classes Part2 | PDF
C++ 
L09 -Classes, P2 
Programming Language 
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14
ClassesComposition
ClassesCompositionClass has object of other classes as members“has –a ” relation
Composition 
#include<iostream> 
usingnamespacestd; 
#ifndefdate_h 
#definedate_h 
classdate 
{ 
public: 
date(int= 1, int= 1, int= 1990); 
voidprint()const; 
~date(); 
private: 
intday, month, year; 
}; 
date::date(intd, intm, inty) 
{day = d; month = m, year = y; 
cout << "Constructor runs for date class “;print();} 
voiddate::print() const 
{cout << day << "/"<< month << "/"<< year << endl; } 
date::~date() 
{cout << "destructor runs for date class, which date is:"<< endl;print(); 
} 
#endif
Composition 
#include<iostream> 
#include"date.h" 
usingnamespacestd; 
#ifndefemployee_h 
#defineemployee_h 
classemployee 
{ 
public: 
employee(); 
employee(char*First, 
char*Last, 
constdate& d1, 
constdate& d2); 
voidprint()const; 
~employee(); 
private: 
charFirstName [20]; 
charLastName [20]; 
constdate hiredate; 
constdate birthdate; 
}; 
employee::employee(char*First, char*Last,constdate& d1, constdate& d2):birthdate(d1), hiredate(d2) 
{ 
intLength = strlen(First); 
strncpy(FirstName,First,20); 
FirstName[Length]='0'; 
strncpy(LastName,Last,20); 
LastName[Length]='0'; 
cout<<“Constructor runs for employee:"<<FirstName<<' '<< LastName<<endl; 
// ' ' space char;) 
} 
voidemployee::print() const 
{ 
cout << "Employee: "<< FirstName << ' '<< LastName << endl; 
cout << "printing dates for this employee"<< endl; 
birthdate.print(); 
hiredate.print(); 
cout << "________________"<< endl; 
} 
employee::~employee() 
{ 
cout << "destructor runs for employee class, for "; 
print(); 
} 
#endif
Composition 
#include<iostream> 
#include"employee.h" 
#include"date.h" 
usingnamespacestd; 
voidmain() 
{ 
date Birth (15,12,1990); 
date Hire (28,7,2010); 
employee manager ("mee", "loo", Birth, Hire ); 
manager.print(); 
} 
Constructor runs for date class 15/12/1990 
Constructor runs for date class 28/7/2010 
Constructor runs for employee:mee Hee 
Employee: mee Hee 
printing dates for this employee 
15/12/1990 
28/7/2010 
________________ 
destructor runs for employee class, for Employee: mee Hee 
printing dates for this employee 
15/12/1990 
28/7/2010 
________________ 
destructor runs for date class, which date is: 
15/12/1990 
destructor runs for date class, which date is: 
28/7/2010 
destructor runs for date class, which date is: 
28/7/2010 
destructor runs for date class, which date is: 
15/12/1990 
Press any key to continue
Composition 
classemployee 
{ 
public: 
employee(); 
employee(char*First, 
char*Last, 
constdate& d1, 
constdate& d2); 
voidprint()const; 
~employee(); 
private: 
charFirstName [20]; 
charLastName [20]; 
constdate hiredate; 
constdate birthdate; 
}; 
classemployee 
{ 
public: 
employee(); 
employee(char*First, 
char*Last, 
constdate& d1, 
constdate& d2); 
voidprint()const; 
~employee(); 
private: 
charFirstName [20]; 
charLastName [20]; 
const date birthdate;// we change this 
const date hiredate;// we change this 
}; 
Change this to
Composition 
#include<iostream> 
#include"employee.h" 
#include"date.h" 
usingnamespacestd; 
voidmain() 
{ 
date Birth (15,12,1990); 
date Hire (28,7,2010); 
employee manager ("mee", "loo", Birth, Hire ); 
manager.print(); 
} 
Constructor runs for date class 15/12/1990 
Constructor runs for date class 28/7/2010 
constructor runs for employee:mee Hee 
Employee: mee Hee 
printing dates for this employee 
15/12/1990 
28/7/2010 
________________ 
destructor runs for employee class, for Employee: mee Hee 
printing dates for this employee 
15/12/1990 
28/7/2010 
________________ 
destructor runs for date class, which date is: 
28/7/2010 
destructor runs for date class, which date is: 
15/12/1990 
destructor runs for date class, which date is: 
28/7/2010 
destructor runs for date class, which date is: 
15/12/1990 
Press any key to continue 
So! 
Member objects are constructed in order declared and not in order of constructor’s member initializer list!
friend Functions
friendFunctions 
•friend Functions 
–A function that is defined outside the scope of the class 
–A non-member function of class 
•But has access to its private data members! 
–The world “friend ” is appears only in the function prototype (in the class definition) 
–Escaping data-hiding restriction 
–Benefits: 
•friend function can be used to access more than one class!
friendFunctions 
#include<iostream> 
usingnamespacestd; 
#ifndefCount_H 
#defineCount_H 
classCount 
{ 
public: 
Count(): x(0) {} 
voidprint()const{cout << x << endl;}; 
private: 
intx; 
}; 
voidWrong() 
{ 
Count C; 
C.x = 3; 
} 
#endif 
Compiler error Can’t access private date members
friendFunctions 
#include<iostream> 
usingnamespacestd; 
#ifndefCount_H 
#defineCount_H 
classCount 
{ 
public: 
Count(): x(0) {} 
voidprint()const{cout << x << endl;}; 
private: 
intx; 
}; 
#endif 
Compiler error Can’t access private date members 
#include<iostream> 
#include"count.h" 
usingnamespace::std; 
voidWrong() 
{ 
Count C; 
C.x = 3; 
} 
voidmain() 
{ 
Wrong(); 
}
friendFunctions 
#include<iostream> 
usingnamespacestd; 
#ifndefCount_H 
#defineCount_H 
classCount 
{ 
public: 
friendvoidPlay (); 
Count(): x(0) {}; 
voidprint()const{cout << x << endl;}; 
private: 
intx; 
}; 
voidPlay() 
{ 
Count C; 
C.x = 3; 
} 
#endif 
Compile and run 
#include<iostream> 
#include"count.h" 
usingnamespace::std; 
voidWrong() 
{ 
Count C; 
C.x = 3; 
} 
voidmain() 
{ 
Wrong(); 
}
friendFunctions 
#include<iostream> 
usingnamespacestd; 
#ifndefCount_H 
#defineCount_H 
classCount 
{ 
public: 
friendvoidPlay (); 
Count(): x(0) {}; 
voidprint()const{cout << x << endl;}; 
private: 
intx; 
}; 
#endif 
Compile and run 
#include<iostream> 
#include"count.h" 
usingnamespace::std; 
voidPlay() 
{ 
Count C; 
C.x = 3; 
} 
voidmain() 
{ 
}
friendFunctions 
#include<iostream> 
usingnamespacestd; 
#ifndefCount_H 
#defineCount_H 
classCount 
{ 
public: 
friendvoidPlay (Count &C); 
Count(): x(0) {}; 
voidprint()const{cout << x << endl;}; 
private: 
intx; 
}; 
voidPlay(Count &C) 
{ 
C.x = 3; 
} 
#endif 
0 
3 
Press any key to continue 
#include<iostream> 
#include"count.h" 
usingnamespace::std; 
voidmain() 
{ 
Count CooCoo; 
CooCoo.print(); 
Play(CooCoo); 
CooCoo.print(); 
}
#include<iostream> 
#include"count.h" 
usingnamespace::std; 
friendvoidStillWrong() 
{ 
Count C; 
C.x = 3; 
} 
voidmain() 
{ 
StillWrong(); 
} 
friendFunctions 
#include<iostream> 
usingnamespacestd; 
#ifndefCount_H 
#defineCount_H 
classCount 
{ 
public: 
friendvoidPlay (Count &C); 
Count(): x(0) {}; 
voidprint()const{cout << x << endl;}; 
private: 
intx; 
}; 
voidPlay(Count &C) 
{ 
C.x = 3; 
} 
#endif 
Compiler error we write the “friend” key word in the function defintion!
friendClasses
friend Classes 
•Properties of friendship 
–Granted not taken 
•class B is a friend of class A 
–class A must explicitly declare class B as friend 
–Not symmetric 
•class B friend of class A 
–class A not necessarily friend of class B! 
–Not transitive 
•A friend of B 
•B friend of C 
–A not necessarily friend of C!
friendClasses 
#include<iostream> 
#include"_2ndClass.h" 
usingnamespacestd; 
class_1stClass 
{ 
public: 
friendclass_2nClass; // declaring _2ntClass 
// as friend to _1stClass 
private: 
intx; 
}; 
#include<iostream> 
usingnamespacestd; 
#ifndef_2ndClass_h 
#define_2ndClass_h 
class_2ndClass 
{ 
public: 
private: 
intx; 
}; 
#endif 
In _2ndClass header 
In _1stClass header 
#include<iostream> 
usingnamespacestd; 
class_1stClass 
{ 
public: 
friendclass_2nClass; 
private: 
constintx; 
}; 
class_2ndClass 
{ 
public: 
voidPlayWith_1stClass(_1stClass C1); 
private: 
inty; 
}; 
void_2ndClass::PlayWith_1stClass(_1stClass C1) 
{ 
y = C1.x; 
} 
Now it’s all okay
thisKeyword
thisKeyword 
•“this” pointer 
–Represent the address memory of the object 
–Its values is always the memory address of the object 
–Can be used 
•Explicitly implicitly 
–What’s used for? 
•Can be used to check if a parameter passed to a member function of an object is the object itself 
•Cascading 
–Multiple function invoked in the same statement
thisKeyword 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test():x(){}; 
voidChange(int); 
voidprint() const; 
private: 
intx; 
}; 
voidTest::print() const 
{ 
cout << x << endl; 
cout << this->x << endl; 
cout << (*this).x << endl; 
} 
voidTest::Change (intnewValue) 
{ 
x = newValue; 
} 
#include<iostream> 
#include“Test.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T; 
T.print(); 
T.Change(6); 
T.print(); 
} 
0 
0 
0 
6 
6 
6 
Press any key to continue
thisKeyword 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test():x(){}; 
voidChange(int); 
voidprint() const; 
private: 
intx; 
}; 
voidTest::print() const 
{ 
cout << x << endl; 
cout << this->x << endl; 
cout << *this.x << endl; 
} 
voidTest::Change (intnewValue) 
{ 
x = newValue; 
} 
*this.x != (*this).x because the (.) has higher precedence than (*) So,Compiler error!
thisKeyword 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test():x(){}; 
voidIsSame(Test &Tempo); 
voidprint() const; 
private: 
intx; 
}; 
voidTest::print() const 
{ 
cout << x << endl; 
} 
voidTest::IsSame (Test &Tempo) 
{ 
if(this== &Tempo) 
{ 
cout << "References are the same"<< endl; 
} 
} 
0 
References are the same 
Press any key to continue 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T; 
Test *Tptr = &T; 
T.print(); 
Tptr->IsSame(T); 
}
thisKeyword 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test():x(){}; 
voidIsSame(Test &Tempo); 
voidprint() const; 
private: 
intx; 
}; 
voidTest::print() const 
{ 
cout << x << endl; 
} 
voidTest::IsSame (Test Tempo) 
{ 
if(this== &Tempo) 
{ 
cout << "References are the same"<< endl; 
} 
} 
0 
Press any key to continue 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T; 
Test *Tptr = &T; 
T.print(); 
Tptr->IsSame(T); 
} 
Cause it’s passed by value not reference
thisKeyword 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test():x(){}; 
voidIsSame(Test &Tempo); 
voidprint() const; 
private: 
intx; 
}; 
voidTest::print() const 
{ 
cout << x << endl; 
} 
voidTest::IsSame (Test &Tempo) 
{ 
if(this== &Tempo) 
{ 
cout << "References are the same"<< endl; 
} 
} 
0 
References are the same 
Press any key to continue 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T; 
Test *Tptr = &T; 
T.print(); 
T.IsSame(*Tptr); 
}
thisKeyword 
classTime 
{public: 
Time(); 
Time &SetTime(int,int,int); // set functions 
Time &SetHour(int); 
Time &SetMinute(int); 
Time &SetSecond(int); 
intGetHour(); // get functions 
intGetMinute(); 
intGetSecond(); 
voidPrintTime(); 
private: inthour;intminute;intsecond; }; 
Time::Time() 
{hour = minute = second = 0; }; 
Time &Time::SetTime(intxhour,intxminute, intxsecond) 
{SetHour(xhour);SetMinute(xminute);SetSecond(xsecond); 
return*this;// enabling cascading 
}; 
Time &Time::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; 
return*this;// enabling cascading 
}; 
Time &Time::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; 
return*this;// enabling cascading 
}; 
Time &Time::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; 
return*this;// enabling cascading 
}; 
intTime::GetHour() {returnhour;}; 
intTime::GetMinute() {returnminute;}; 
intTime::GetSecond() {returnsecond;}; 
voidTime::PrintTime() 
{cout << hour << ":"<< minute << ":"<< second << endl; } 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Time T; 
//cascade member calls 
T.SetHour(12).SetMinute(5).SetSecond(2); 
T.PrintTime(); 
}
static Class Members
static Class Members 
•Static members variables are shared among all instances of class 
–Keeps track of how many objects have been created 
•Incrementing with each constructing 
•Decrementing with each destructing 
–Must be initialized 
•Only once 
•Out side the class 
–Can be 
•public, private or protected
static Class Members 
•Accessible through public member function or friends 
–Public static variables 
•When no object of class exists 
–Private static variables 
•When no object of class exists 
•Can be accessed via public static member functions 
•Can’t access non-static data or functions, why? 
•No thispointer for staticfunctions, why?
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << SVariable << endl;SVariable--;}; 
private: 
staticintSVariable; 
}; 
// initialize just once outside the scope of the class 
intTest::SVariable = 5; 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T; 
} 
5 
6 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << SVariable << endl;--SVariable;}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T; 
cout << T.SVariable<<endl; 
} 
5 
6 
6 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << "const "<< SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << "dest "<< SVariable << endl;SVariable- -;}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
{ 
Test T; 
cout << T.SVariable << endl; 
} 
cout << Test::SVariable << endl; 
} 
const 5 
6 
dest 6 
5 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << "const "<< SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << "dest "<< SVariable << endl;SVariable- -;}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
{ 
Test T[5]; 
cout << Test::SVariable << endl; 
} 
cout << Test::SVariable << endl; 
} 
const 5 
const 6 
const 7 
const 8 
const 9 
10 
dest 10 
dest 9 
dest 8 
dest 7 
dest 6 
5 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << "const "<< SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << "dest "<< SVariable << endl;SVariable- -;}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
{ 
Test *T = newTest[5]; 
cout << Test::SVariable << endl; 
} 
cout << Test::SVariable << endl; 
} 
const 5 
const 6 
const 7 
const 8 
const 9 
10 
10 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << "const "<< SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << "dest "<< SVariable << endl;SVariable- -;}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
{ 
Test *T = newTest[5]; 
cout << Test::SVariable << endl; 
delete[] T; 
} 
cout << Test::SVariable << endl; 
cout << Test::SVariable << endl; 
} 
const 5 
const 6 
const 7 
const 8 
const 9 
10 
dest 10 
dest 9 
dest 8 
dest 7 
dest 6 
5 
5 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{cout << "const "<< SVariable << endl; SVariable++;}; 
voidprint() const; 
~Test() 
{cout << "dest "<< SVariable << endl;SVariable- -;}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
{ 
Test *T = newTest[5]; 
cout << Test::SVariable << endl; 
} 
cout << Test::SVariable << endl; 
delete[] T; 
cout << Test::SVariable << endl; 
} 
Compile error. Undeclared identifier T 
We did allocate memory and we have never been de-allocated it!
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T1; 
Test T2; 
} 
const 1 
const 2 
dest 1 
dest 0 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test *T1; 
Test *T2; 
} 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1; 
Test *T2; 
//cout << T1->SGet() << endl; 
} 
const 1 
dest 0 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = &TSource; 
Test *T2; 
} 
const 1 
dest 0 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = newTest; 
Test *T2; 
} 
const 1 
const 2 
dest 1 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = newTest; 
Test *T2; 
cout << T1->SGet() << endl; 
} 
const 1 
const 2 
2 
dest 1 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = newTest; 
Test *T2; 
cout << Test::SGet() << endl; 
} 
const 1 
const 2 
2 
dest 1 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = newTest; 
Test *T2; 
deleteT1; 
cout << Test::SGet() << endl; 
} 
const 1 
const 2 
dest 1 
1 
dest 0 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = newTest; 
Test *T2; 
deleteT1; 
cout << T1->SGet() << endl; 
} 
const 1 
const 2 
dest 1 
1 
dest 0 
Press any key to continue
static Class Members 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{++SVariable; 
cout << "const "<< SVariable << endl; 
}; 
voidprint() const; 
~Test() 
{--SVariable; 
cout << "dest "<< SVariable << endl; 
}; 
staticintSGet();// static member function 
private: 
staticintSVariable;// static date member 
}; 
intTest::SVariable = 0;// initialize just once outside the scope of the class 
voidTest::print() const 
{cout << SVariable << endl;} 
intTest::SGet() 
{ 
returnSVariable; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test TSource; 
Test *T1 = newTest; 
Test *T2; 
delete[]T1; 
cout << T1->SGet() << endl; 
} 
Runtime error coz of [] not compiler error for sure!
static Class Members 
#include <iostream> 
using namespace::std; 
class TestStaticClass 
{ 
public: 
static inti; 
static void StaticPublicMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
} 
}; 
intTestStaticClass::i= 0; 
void main() 
{ 
TestStaticClassT; 
T.StaticPublicMethodTest(); 
TestStaticClass::StaticPublicMethodTest(); 
system("pause"); 
} 
You called a static method! 
You called a static method! 
Press any key to continue
static Class Members 
#include <iostream> 
using namespace::std; 
class TestStaticClass 
{ 
public: 
void GetMyName() 
{ 
cout<< "You are in "TestStaticClass" class" << endl; 
} 
static void StaticPublicMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
} 
}; 
void main() 
{ 
TestStaticClassT; 
T.GetMyName(); 
TestStaticClass::GetMyName(); 
system("pause"); 
} 
Compiler error!, TestStaticClass::GetMyName();is not static!
static Class Members 
#include <iostream> 
using namespace::std; 
class TestStaticClass 
{ 
public: 
void GetMyName() 
{ 
cout<< "You are in "TestStaticClass" class" << endl; 
} 
static void StaticPublicMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
} 
}; 
void main() 
{ 
TestStaticClassT; 
T.GetMyName(); 
system("pause"); 
} 
You are in "TestStaticClass" class 
Press any key to continue
static Class Members 
#include <iostream> 
using namespace::std; 
class TestStaticClass 
{ 
public: 
void GetMyName() 
{ 
cout<< "You are in "TestStaticClass" class" << endl; 
StaticPublicMethodTest(); 
} 
static void StaticPublicMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
} 
}; 
void main() 
{ 
TestStaticClassT; 
T.GetMyName(); 
system("pause"); 
} 
You are in "TestStaticClass" class 
You called a static method! 
Press any key to continue 
class TestClass 
{ 
public: 
void PublicNonStaticMethodTest() 
{ 
cout<< "You called a non static method!" << endl; 
PublicStaticMethodTest(); 
} 
static void PublicStaticMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
} 
}; 
void main() 
{ 
TestClassT; 
T.PublicNonStaticMethodTest(); 
system("pause"); 
} 
You called a non static method! 
You called a static method! 
Press any key to continue
static Class Members 
class TestClass 
{ 
public: 
intiNonStatic; 
static intiStatic; 
void PublicNonStaticMethodTest() 
{ 
cout<< "You called a non static method!" << endl; 
PublicStaticMethodTest(); 
} 
static void PublicStaticMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
iStatic= 5; 
} 
}; 
intTestClass::iStatic= 0; 
void main() 
{ 
TestClassT; 
T.PublicNonStaticMethodTest(); 
cout<< TestClass::iStatic<< endl; 
system("pause"); 
} 
You called a non static method! 
You called a static method! 
5 
Press any key to continue 
class TestClass 
{ 
public: 
intiNonStatic; 
static intiStatic; 
void PublicNonStaticMethodTest() 
{ 
cout<< "You called a non static method!" << endl; 
PublicStaticMethodTest(); 
} 
static void PublicStaticMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
iStatic= 5; 
iNonStatic= 5; 
} 
}; 
intTestClass::iStatic= 0; 
void main() 
{ 
TestClassT; 
T.PublicNonStaticMethodTest(); 
cout<< TestClass::iStatic<< endl; 
cout<< T.iNonStatic<< endl; 
system("pause"); 
} 
Compile error iNonStatic= 5; 
In “PublicStaticMethodTest” 
Static method!
static Class Members 
class TestClass 
{ 
public: 
intiNonStatic; 
static intiStatic; 
void PublicNonStaticMethodTest() 
{ 
cout<< "You called a non static method!" << endl; 
iNonStatic= 5; 
PublicStaticMethodTest(); 
} 
static void PublicStaticMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
iStatic= 5; 
} 
}; 
intTestClass::iStatic= 0; 
void main() 
{ 
TestClassT; 
T.PublicNonStaticMethodTest(); 
cout<< TestClass::iStatic<< endl; 
cout<< T.iNonStatic<< endl; 
system("pause"); 
} 
You called a non static method! 
You called a static method! 
5 
5 
Press any key to continue 
class TestClass 
{ 
public: 
intiNonStatic; 
static intiStatic; 
void PublicNonStaticMethodTest() 
{ 
cout<< "You called a non static method!" << endl; 
iNonStatic= 5; 
PublicStaticMethodTest(); 
} 
static void PublicStaticMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
this.iStatic= 5; 
} 
}; 
intTestClass::iStatic= 0; 
void main() 
{ 
TestClassT; 
T.PublicNonStaticMethodTest(); 
cout<< TestClass::iStatic<< endl; 
cout<< T.iNonStatic<< endl; 
system("pause"); 
} 
This can’t be used in a static method! 
It’s logically wrong!
static Class Members
static Class Members 
class TestClass 
{ 
public: 
intiNonStatic; 
static intiStatic; 
void PublicNonStaticMethodTest() 
{ 
cout<< "You called a non static method!" << endl; 
this.iNonStatic= 5; 
PublicStaticMethodTest(); 
} 
static void PublicStaticMethodTest() 
{ 
cout<< "You called a static method!" << endl; 
iStatic= 5; 
} 
}; 
intTestClass::iStatic= 0; 
void main() 
{ 
TestClassT; 
T.PublicNonStaticMethodTest(); 
cout<< TestClass::iStatic<< endl; 
cout<< T.iNonStatic<< endl; 
system("pause"); 
} 
Compiler error
static Class Members
Important Example
Constructors and Destructors 
#include <iostream> 
using namespace::std; 
class TestClass 
{ 
public: 
char* _name; 
TestClass(char *name) 
{ 
this->_name = name; 
cout<< "Constructor for" << _name << endl; 
} 
~TestClass() 
{ 
cout<< "Destructor for" << _name << endl; 
} 
}; 
void main() 
{ 
TestClassT1("T1"); 
{ 
TestClassT2("T2"); 
static TestClassTStaticInMainInnerScope("TStaticInMainInnerScope"); 
} 
static TestClassTStaticInMainOuterScope("TStaticInMainOuterScope"); 
TestClassT4("T4"); 
} 
TestClassTGlobal("TGlobal"); 
static TestClassTStaticGlobal("TStaticGlobal"); 
Constructor forTGlobal 
Constructor forTStaticGlobal 
Constructor forT1 
Constructor forT2 
Constructor forTStaticInMainInnerScope 
Destructor forT2 
Constructor forTStaticInMainOuterScope 
Constructor forT4 
Destructor forT4 
Destructor forT1 
Destructor forTStaticInMainOuterScope 
Destructor forTStaticInMainInnerScope 
Destructor forTStaticGlobal 
Destructor forTGlobal 
Press any key to continue
Quiz
Quiz #1 
#include<iostream> 
#include"date.h" 
usingnamespacestd; 
#ifndefemployee_h 
#defineemployee_h 
classemployee 
{ 
public: 
employee(); 
employee(char*First, 
char*Last, 
constdate& d1, 
constdate& d2); 
voidprint()const; 
~employee(); 
private: 
charFirstName [20]; 
charLastName [20]; 
constdate hiredate; 
constdate birthdate; 
}; 
employee::employee(char*First, char*Last,constdate& d1, constdate& d2):birthdate(d1), hiredate(d2) 
{ 
intLength = strlen(First); 
strncpy(FirstName,First,20); 
FirstName[Length]='0'; 
strncpy(LastName,Last,20); 
LastName[Length]='0'; 
cout<<"constructor runs for employee:"<<FirstName<<' '<< LastName<<endl; 
// ' ' space char;) 
} 
voidemployee::print() const 
{ 
cout << "Employee: "<< FirstName << ' '<< LastName << endl; 
cout << "printing dates for this employee"<< endl; 
birthdate.print(); 
hiredate.print(); 
cout << "________________"<< endl; 
} 
employee::~employee() 
{ 
cout << "destructor runs for employee class, for "; 
print(); 
} 
#endif
Quiz #1, What’s the Output? 
#include<iostream> 
#include"employee.h" 
#include"date.h" 
usingnamespacestd; 
voidmain() 
{ 
date Birth1 (8,8,1990); 
date Hire1 (4,4,2010); 
date Birth2 (10,10,1990); 
date Hire2 (5,5,2010); 
employee manager1 ("mee", "loo", Birth1, Hire1 ); 
employee manager2 ("zee", "HooHoo", Birth2, Hire2 ); 
manager1.print(); 
manager2.print(); 
} 
#include<iostream> 
usingnamespacestd; 
#ifndefdate_h 
#definedate_h 
classdate 
{ 
public: 
date(int= 1, int= 1, int= 1990); 
voidprint()const; 
~date(); 
private: 
intday, month, year; 
}; 
date::date(intd, intm, inty) 
{day = d; month = m, year = y; 
cout << "Constructor runs for date class “;print();} 
voiddate::print() const 
{cout << day << "/"<< month << "/"<< year << endl; } 
date::~date() 
{cout << "destructor runs for date class, which date is:"<<endl; 
print(); 
} 
#endif
Quiz #1 
destructor runs for employee class, for Employee: zee HooHoo 
printing dates for this employee 
10/10/1990 
5/5/2010 
________________ 
destructor runs for date class, which date is: 
5/5/2010 
destructor runs for date class, which date is: 
10/10/1990 
destructor runs for employee class, for Employee: mee Hee 
printing dates for this employee 
8/8/1990 
4/4/2010 
________________ 
destructor runs for date class, which date is: 
4/4/2010 
destructor runs for date class, which date is: 
8/8/1990 
destructor runs for date class, which date is: 
5/5/2010 
destructor runs for date class, which date is: 
10/10/1990 
destructor runs for date class, which date is: 
4/4/2010 
destructor runs for date class, which date is: 
8/8/1990 
Constructor runs for date class 8/8/1990 
Constructor runs for date class 4/4/2010 
Constructor runs for date class 10/10/1990 
Constructor runs for date class 5/5/2010 
constructor runs for employee:mee Hee 
constructor runs for employee:zee HooHoo 
Employee: mee Hee 
printing dates for this employee 
8/8/1990 
4/4/2010 
________________ 
Employee: zee HooHoo 
printing dates for this employee 
10/10/1990 
5/5/2010 
________________
Quiz #2, What’s the Output? 
#include<iostream> 
usingnamespacestd; 
classTest 
{ 
public: 
Test() 
{ 
cout << "const "<< SVariable << endl; 
SVariable++; 
}; 
voidprint() const; 
~Test() 
{cout << "dest "<< SVariable << endl; 
SVariable--; 
}; 
staticintSVariable; 
private: 
}; 
intTest::SVariable = 5; 
voidTest::print() const 
{ 
cout << SVariable << endl; 
} 
#include<iostream> 
#include"MyFile.h" 
usingnamespacestd; 
voidmain() 
{ 
Test T1; 
{ 
Test T2[5]; 
Test TTemp; 
cout << TTemp.SVariable << endl; 
} 
cout << Test::SVariable << endl; 
Test *T3 = &T1; 
staticTest T4; 
cout << Test::SVariable << endl; 
Test* &T5 = T3; 
cout << Test::SVariable << endl; 
} 
const 5 
const 6 
const 7 
const 8 
const 9 
const 10 
const 11 
12 
dest 12 
dest 11 
dest 10 
dest 9 
dest 8 
dest 7 
6 
const 6 
7 
7 
dest 7 
dest 6

C++ L09-Classes Part2

  • 1.
    C++ L09 -Classes,P2 Programming Language Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14
  • 2.
  • 3.
    ClassesCompositionClass has objectof other classes as members“has –a ” relation
  • 4.
    Composition #include<iostream> usingnamespacestd; #ifndefdate_h #definedate_h classdate { public: date(int= 1, int= 1, int= 1990); voidprint()const; ~date(); private: intday, month, year; }; date::date(intd, intm, inty) {day = d; month = m, year = y; cout << "Constructor runs for date class “;print();} voiddate::print() const {cout << day << "/"<< month << "/"<< year << endl; } date::~date() {cout << "destructor runs for date class, which date is:"<< endl;print(); } #endif
  • 5.
    Composition #include<iostream> #include"date.h" usingnamespacestd; #ifndefemployee_h #defineemployee_h classemployee { public: employee(); employee(char*First, char*Last, constdate& d1, constdate& d2); voidprint()const; ~employee(); private: charFirstName [20]; charLastName [20]; constdate hiredate; constdate birthdate; }; employee::employee(char*First, char*Last,constdate& d1, constdate& d2):birthdate(d1), hiredate(d2) { intLength = strlen(First); strncpy(FirstName,First,20); FirstName[Length]='0'; strncpy(LastName,Last,20); LastName[Length]='0'; cout<<“Constructor runs for employee:"<<FirstName<<' '<< LastName<<endl; // ' ' space char;) } voidemployee::print() const { cout << "Employee: "<< FirstName << ' '<< LastName << endl; cout << "printing dates for this employee"<< endl; birthdate.print(); hiredate.print(); cout << "________________"<< endl; } employee::~employee() { cout << "destructor runs for employee class, for "; print(); } #endif
  • 6.
    Composition #include<iostream> #include"employee.h" #include"date.h" usingnamespacestd; voidmain() { date Birth (15,12,1990); date Hire (28,7,2010); employee manager ("mee", "loo", Birth, Hire ); manager.print(); } Constructor runs for date class 15/12/1990 Constructor runs for date class 28/7/2010 Constructor runs for employee:mee Hee Employee: mee Hee printing dates for this employee 15/12/1990 28/7/2010 ________________ destructor runs for employee class, for Employee: mee Hee printing dates for this employee 15/12/1990 28/7/2010 ________________ destructor runs for date class, which date is: 15/12/1990 destructor runs for date class, which date is: 28/7/2010 destructor runs for date class, which date is: 28/7/2010 destructor runs for date class, which date is: 15/12/1990 Press any key to continue
  • 7.
    Composition classemployee { public: employee(); employee(char*First, char*Last, constdate& d1, constdate& d2); voidprint()const; ~employee(); private: charFirstName [20]; charLastName [20]; constdate hiredate; constdate birthdate; }; classemployee { public: employee(); employee(char*First, char*Last, constdate& d1, constdate& d2); voidprint()const; ~employee(); private: charFirstName [20]; charLastName [20]; const date birthdate;// we change this const date hiredate;// we change this }; Change this to
  • 8.
    Composition #include<iostream> #include"employee.h" #include"date.h" usingnamespacestd; voidmain() { date Birth (15,12,1990); date Hire (28,7,2010); employee manager ("mee", "loo", Birth, Hire ); manager.print(); } Constructor runs for date class 15/12/1990 Constructor runs for date class 28/7/2010 constructor runs for employee:mee Hee Employee: mee Hee printing dates for this employee 15/12/1990 28/7/2010 ________________ destructor runs for employee class, for Employee: mee Hee printing dates for this employee 15/12/1990 28/7/2010 ________________ destructor runs for date class, which date is: 28/7/2010 destructor runs for date class, which date is: 15/12/1990 destructor runs for date class, which date is: 28/7/2010 destructor runs for date class, which date is: 15/12/1990 Press any key to continue So! Member objects are constructed in order declared and not in order of constructor’s member initializer list!
  • 9.
  • 10.
    friendFunctions •friend Functions –A function that is defined outside the scope of the class –A non-member function of class •But has access to its private data members! –The world “friend ” is appears only in the function prototype (in the class definition) –Escaping data-hiding restriction –Benefits: •friend function can be used to access more than one class!
  • 11.
    friendFunctions #include<iostream> usingnamespacestd; #ifndefCount_H #defineCount_H classCount { public: Count(): x(0) {} voidprint()const{cout << x << endl;}; private: intx; }; voidWrong() { Count C; C.x = 3; } #endif Compiler error Can’t access private date members
  • 12.
    friendFunctions #include<iostream> usingnamespacestd; #ifndefCount_H #defineCount_H classCount { public: Count(): x(0) {} voidprint()const{cout << x << endl;}; private: intx; }; #endif Compiler error Can’t access private date members #include<iostream> #include"count.h" usingnamespace::std; voidWrong() { Count C; C.x = 3; } voidmain() { Wrong(); }
  • 13.
    friendFunctions #include<iostream> usingnamespacestd; #ifndefCount_H #defineCount_H classCount { public: friendvoidPlay (); Count(): x(0) {}; voidprint()const{cout << x << endl;}; private: intx; }; voidPlay() { Count C; C.x = 3; } #endif Compile and run #include<iostream> #include"count.h" usingnamespace::std; voidWrong() { Count C; C.x = 3; } voidmain() { Wrong(); }
  • 14.
    friendFunctions #include<iostream> usingnamespacestd; #ifndefCount_H #defineCount_H classCount { public: friendvoidPlay (); Count(): x(0) {}; voidprint()const{cout << x << endl;}; private: intx; }; #endif Compile and run #include<iostream> #include"count.h" usingnamespace::std; voidPlay() { Count C; C.x = 3; } voidmain() { }
  • 15.
    friendFunctions #include<iostream> usingnamespacestd; #ifndefCount_H #defineCount_H classCount { public: friendvoidPlay (Count &C); Count(): x(0) {}; voidprint()const{cout << x << endl;}; private: intx; }; voidPlay(Count &C) { C.x = 3; } #endif 0 3 Press any key to continue #include<iostream> #include"count.h" usingnamespace::std; voidmain() { Count CooCoo; CooCoo.print(); Play(CooCoo); CooCoo.print(); }
  • 16.
    #include<iostream> #include"count.h" usingnamespace::std; friendvoidStillWrong() { Count C; C.x = 3; } voidmain() { StillWrong(); } friendFunctions #include<iostream> usingnamespacestd; #ifndefCount_H #defineCount_H classCount { public: friendvoidPlay (Count &C); Count(): x(0) {}; voidprint()const{cout << x << endl;}; private: intx; }; voidPlay(Count &C) { C.x = 3; } #endif Compiler error we write the “friend” key word in the function defintion!
  • 17.
  • 18.
    friend Classes •Propertiesof friendship –Granted not taken •class B is a friend of class A –class A must explicitly declare class B as friend –Not symmetric •class B friend of class A –class A not necessarily friend of class B! –Not transitive •A friend of B •B friend of C –A not necessarily friend of C!
  • 19.
    friendClasses #include<iostream> #include"_2ndClass.h" usingnamespacestd; class_1stClass { public: friendclass_2nClass; // declaring _2ntClass // as friend to _1stClass private: intx; }; #include<iostream> usingnamespacestd; #ifndef_2ndClass_h #define_2ndClass_h class_2ndClass { public: private: intx; }; #endif In _2ndClass header In _1stClass header #include<iostream> usingnamespacestd; class_1stClass { public: friendclass_2nClass; private: constintx; }; class_2ndClass { public: voidPlayWith_1stClass(_1stClass C1); private: inty; }; void_2ndClass::PlayWith_1stClass(_1stClass C1) { y = C1.x; } Now it’s all okay
  • 20.
  • 21.
    thisKeyword •“this” pointer –Represent the address memory of the object –Its values is always the memory address of the object –Can be used •Explicitly implicitly –What’s used for? •Can be used to check if a parameter passed to a member function of an object is the object itself •Cascading –Multiple function invoked in the same statement
  • 22.
    thisKeyword #include<iostream> usingnamespacestd; classTest { public: Test():x(){}; voidChange(int); voidprint() const; private: intx; }; voidTest::print() const { cout << x << endl; cout << this->x << endl; cout << (*this).x << endl; } voidTest::Change (intnewValue) { x = newValue; } #include<iostream> #include“Test.h" usingnamespacestd; voidmain() { Test T; T.print(); T.Change(6); T.print(); } 0 0 0 6 6 6 Press any key to continue
  • 23.
    thisKeyword #include<iostream> usingnamespacestd; classTest { public: Test():x(){}; voidChange(int); voidprint() const; private: intx; }; voidTest::print() const { cout << x << endl; cout << this->x << endl; cout << *this.x << endl; } voidTest::Change (intnewValue) { x = newValue; } *this.x != (*this).x because the (.) has higher precedence than (*) So,Compiler error!
  • 24.
    thisKeyword #include<iostream> usingnamespacestd; classTest { public: Test():x(){}; voidIsSame(Test &Tempo); voidprint() const; private: intx; }; voidTest::print() const { cout << x << endl; } voidTest::IsSame (Test &Tempo) { if(this== &Tempo) { cout << "References are the same"<< endl; } } 0 References are the same Press any key to continue #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T; Test *Tptr = &T; T.print(); Tptr->IsSame(T); }
  • 25.
    thisKeyword #include<iostream> usingnamespacestd; classTest { public: Test():x(){}; voidIsSame(Test &Tempo); voidprint() const; private: intx; }; voidTest::print() const { cout << x << endl; } voidTest::IsSame (Test Tempo) { if(this== &Tempo) { cout << "References are the same"<< endl; } } 0 Press any key to continue #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T; Test *Tptr = &T; T.print(); Tptr->IsSame(T); } Cause it’s passed by value not reference
  • 26.
    thisKeyword #include<iostream> usingnamespacestd; classTest { public: Test():x(){}; voidIsSame(Test &Tempo); voidprint() const; private: intx; }; voidTest::print() const { cout << x << endl; } voidTest::IsSame (Test &Tempo) { if(this== &Tempo) { cout << "References are the same"<< endl; } } 0 References are the same Press any key to continue #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T; Test *Tptr = &T; T.print(); T.IsSame(*Tptr); }
  • 27.
    thisKeyword classTime {public: Time(); Time &SetTime(int,int,int); // set functions Time &SetHour(int); Time &SetMinute(int); Time &SetSecond(int); intGetHour(); // get functions intGetMinute(); intGetSecond(); voidPrintTime(); private: inthour;intminute;intsecond; }; Time::Time() {hour = minute = second = 0; }; Time &Time::SetTime(intxhour,intxminute, intxsecond) {SetHour(xhour);SetMinute(xminute);SetSecond(xsecond); return*this;// enabling cascading }; Time &Time::SetHour(inth) { hour = (h>=0 && h<24)? h: 0; return*this;// enabling cascading }; Time &Time::SetMinute(intm) { minute = (m>=0 && m<60)? m: 0; return*this;// enabling cascading }; Time &Time::SetSecond(ints) { second = (s>=0 && s<60)? s: 0; return*this;// enabling cascading }; intTime::GetHour() {returnhour;}; intTime::GetMinute() {returnminute;}; intTime::GetSecond() {returnsecond;}; voidTime::PrintTime() {cout << hour << ":"<< minute << ":"<< second << endl; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Time T; //cascade member calls T.SetHour(12).SetMinute(5).SetSecond(2); T.PrintTime(); }
  • 28.
  • 29.
    static Class Members •Static members variables are shared among all instances of class –Keeps track of how many objects have been created •Incrementing with each constructing •Decrementing with each destructing –Must be initialized •Only once •Out side the class –Can be •public, private or protected
  • 30.
    static Class Members •Accessible through public member function or friends –Public static variables •When no object of class exists –Private static variables •When no object of class exists •Can be accessed via public static member functions •Can’t access non-static data or functions, why? •No thispointer for staticfunctions, why?
  • 31.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << SVariable << endl;SVariable--;}; private: staticintSVariable; }; // initialize just once outside the scope of the class intTest::SVariable = 5; voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T; } 5 6 Press any key to continue
  • 32.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << SVariable << endl;--SVariable;}; staticintSVariable; private: }; intTest::SVariable = 5;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T; cout << T.SVariable<<endl; } 5 6 6 Press any key to continue
  • 33.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << "const "<< SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << "dest "<< SVariable << endl;SVariable- -;}; staticintSVariable; private: }; intTest::SVariable = 5;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { { Test T; cout << T.SVariable << endl; } cout << Test::SVariable << endl; } const 5 6 dest 6 5 Press any key to continue
  • 34.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << "const "<< SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << "dest "<< SVariable << endl;SVariable- -;}; staticintSVariable; private: }; intTest::SVariable = 5;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { { Test T[5]; cout << Test::SVariable << endl; } cout << Test::SVariable << endl; } const 5 const 6 const 7 const 8 const 9 10 dest 10 dest 9 dest 8 dest 7 dest 6 5 Press any key to continue
  • 35.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << "const "<< SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << "dest "<< SVariable << endl;SVariable- -;}; staticintSVariable; private: }; intTest::SVariable = 5;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { { Test *T = newTest[5]; cout << Test::SVariable << endl; } cout << Test::SVariable << endl; } const 5 const 6 const 7 const 8 const 9 10 10 Press any key to continue
  • 36.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << "const "<< SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << "dest "<< SVariable << endl;SVariable- -;}; staticintSVariable; private: }; intTest::SVariable = 5;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { { Test *T = newTest[5]; cout << Test::SVariable << endl; delete[] T; } cout << Test::SVariable << endl; cout << Test::SVariable << endl; } const 5 const 6 const 7 const 8 const 9 10 dest 10 dest 9 dest 8 dest 7 dest 6 5 5 Press any key to continue
  • 37.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {cout << "const "<< SVariable << endl; SVariable++;}; voidprint() const; ~Test() {cout << "dest "<< SVariable << endl;SVariable- -;}; staticintSVariable; private: }; intTest::SVariable = 5;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { { Test *T = newTest[5]; cout << Test::SVariable << endl; } cout << Test::SVariable << endl; delete[] T; cout << Test::SVariable << endl; } Compile error. Undeclared identifier T We did allocate memory and we have never been de-allocated it!
  • 38.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T1; Test T2; } const 1 const 2 dest 1 dest 0 Press any key to continue
  • 39.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test *T1; Test *T2; } Press any key to continue
  • 40.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1; Test *T2; //cout << T1->SGet() << endl; } const 1 dest 0 Press any key to continue
  • 41.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = &TSource; Test *T2; } const 1 dest 0 Press any key to continue
  • 42.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = newTest; Test *T2; } const 1 const 2 dest 1 Press any key to continue
  • 43.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = newTest; Test *T2; cout << T1->SGet() << endl; } const 1 const 2 2 dest 1 Press any key to continue
  • 44.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = newTest; Test *T2; cout << Test::SGet() << endl; } const 1 const 2 2 dest 1 Press any key to continue
  • 45.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = newTest; Test *T2; deleteT1; cout << Test::SGet() << endl; } const 1 const 2 dest 1 1 dest 0 Press any key to continue
  • 46.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = newTest; Test *T2; deleteT1; cout << T1->SGet() << endl; } const 1 const 2 dest 1 1 dest 0 Press any key to continue
  • 47.
    static Class Members #include<iostream> usingnamespacestd; classTest { public: Test() {++SVariable; cout << "const "<< SVariable << endl; }; voidprint() const; ~Test() {--SVariable; cout << "dest "<< SVariable << endl; }; staticintSGet();// static member function private: staticintSVariable;// static date member }; intTest::SVariable = 0;// initialize just once outside the scope of the class voidTest::print() const {cout << SVariable << endl;} intTest::SGet() { returnSVariable; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test TSource; Test *T1 = newTest; Test *T2; delete[]T1; cout << T1->SGet() << endl; } Runtime error coz of [] not compiler error for sure!
  • 48.
    static Class Members #include <iostream> using namespace::std; class TestStaticClass { public: static inti; static void StaticPublicMethodTest() { cout<< "You called a static method!" << endl; } }; intTestStaticClass::i= 0; void main() { TestStaticClassT; T.StaticPublicMethodTest(); TestStaticClass::StaticPublicMethodTest(); system("pause"); } You called a static method! You called a static method! Press any key to continue
  • 49.
    static Class Members #include <iostream> using namespace::std; class TestStaticClass { public: void GetMyName() { cout<< "You are in "TestStaticClass" class" << endl; } static void StaticPublicMethodTest() { cout<< "You called a static method!" << endl; } }; void main() { TestStaticClassT; T.GetMyName(); TestStaticClass::GetMyName(); system("pause"); } Compiler error!, TestStaticClass::GetMyName();is not static!
  • 50.
    static Class Members #include <iostream> using namespace::std; class TestStaticClass { public: void GetMyName() { cout<< "You are in "TestStaticClass" class" << endl; } static void StaticPublicMethodTest() { cout<< "You called a static method!" << endl; } }; void main() { TestStaticClassT; T.GetMyName(); system("pause"); } You are in "TestStaticClass" class Press any key to continue
  • 51.
    static Class Members #include <iostream> using namespace::std; class TestStaticClass { public: void GetMyName() { cout<< "You are in "TestStaticClass" class" << endl; StaticPublicMethodTest(); } static void StaticPublicMethodTest() { cout<< "You called a static method!" << endl; } }; void main() { TestStaticClassT; T.GetMyName(); system("pause"); } You are in "TestStaticClass" class You called a static method! Press any key to continue class TestClass { public: void PublicNonStaticMethodTest() { cout<< "You called a non static method!" << endl; PublicStaticMethodTest(); } static void PublicStaticMethodTest() { cout<< "You called a static method!" << endl; } }; void main() { TestClassT; T.PublicNonStaticMethodTest(); system("pause"); } You called a non static method! You called a static method! Press any key to continue
  • 52.
    static Class Members class TestClass { public: intiNonStatic; static intiStatic; void PublicNonStaticMethodTest() { cout<< "You called a non static method!" << endl; PublicStaticMethodTest(); } static void PublicStaticMethodTest() { cout<< "You called a static method!" << endl; iStatic= 5; } }; intTestClass::iStatic= 0; void main() { TestClassT; T.PublicNonStaticMethodTest(); cout<< TestClass::iStatic<< endl; system("pause"); } You called a non static method! You called a static method! 5 Press any key to continue class TestClass { public: intiNonStatic; static intiStatic; void PublicNonStaticMethodTest() { cout<< "You called a non static method!" << endl; PublicStaticMethodTest(); } static void PublicStaticMethodTest() { cout<< "You called a static method!" << endl; iStatic= 5; iNonStatic= 5; } }; intTestClass::iStatic= 0; void main() { TestClassT; T.PublicNonStaticMethodTest(); cout<< TestClass::iStatic<< endl; cout<< T.iNonStatic<< endl; system("pause"); } Compile error iNonStatic= 5; In “PublicStaticMethodTest” Static method!
  • 53.
    static Class Members class TestClass { public: intiNonStatic; static intiStatic; void PublicNonStaticMethodTest() { cout<< "You called a non static method!" << endl; iNonStatic= 5; PublicStaticMethodTest(); } static void PublicStaticMethodTest() { cout<< "You called a static method!" << endl; iStatic= 5; } }; intTestClass::iStatic= 0; void main() { TestClassT; T.PublicNonStaticMethodTest(); cout<< TestClass::iStatic<< endl; cout<< T.iNonStatic<< endl; system("pause"); } You called a non static method! You called a static method! 5 5 Press any key to continue class TestClass { public: intiNonStatic; static intiStatic; void PublicNonStaticMethodTest() { cout<< "You called a non static method!" << endl; iNonStatic= 5; PublicStaticMethodTest(); } static void PublicStaticMethodTest() { cout<< "You called a static method!" << endl; this.iStatic= 5; } }; intTestClass::iStatic= 0; void main() { TestClassT; T.PublicNonStaticMethodTest(); cout<< TestClass::iStatic<< endl; cout<< T.iNonStatic<< endl; system("pause"); } This can’t be used in a static method! It’s logically wrong!
  • 54.
  • 55.
    static Class Members class TestClass { public: intiNonStatic; static intiStatic; void PublicNonStaticMethodTest() { cout<< "You called a non static method!" << endl; this.iNonStatic= 5; PublicStaticMethodTest(); } static void PublicStaticMethodTest() { cout<< "You called a static method!" << endl; iStatic= 5; } }; intTestClass::iStatic= 0; void main() { TestClassT; T.PublicNonStaticMethodTest(); cout<< TestClass::iStatic<< endl; cout<< T.iNonStatic<< endl; system("pause"); } Compiler error
  • 56.
  • 57.
  • 58.
    Constructors and Destructors #include <iostream> using namespace::std; class TestClass { public: char* _name; TestClass(char *name) { this->_name = name; cout<< "Constructor for" << _name << endl; } ~TestClass() { cout<< "Destructor for" << _name << endl; } }; void main() { TestClassT1("T1"); { TestClassT2("T2"); static TestClassTStaticInMainInnerScope("TStaticInMainInnerScope"); } static TestClassTStaticInMainOuterScope("TStaticInMainOuterScope"); TestClassT4("T4"); } TestClassTGlobal("TGlobal"); static TestClassTStaticGlobal("TStaticGlobal"); Constructor forTGlobal Constructor forTStaticGlobal Constructor forT1 Constructor forT2 Constructor forTStaticInMainInnerScope Destructor forT2 Constructor forTStaticInMainOuterScope Constructor forT4 Destructor forT4 Destructor forT1 Destructor forTStaticInMainOuterScope Destructor forTStaticInMainInnerScope Destructor forTStaticGlobal Destructor forTGlobal Press any key to continue
  • 59.
  • 60.
    Quiz #1 #include<iostream> #include"date.h" usingnamespacestd; #ifndefemployee_h #defineemployee_h classemployee { public: employee(); employee(char*First, char*Last, constdate& d1, constdate& d2); voidprint()const; ~employee(); private: charFirstName [20]; charLastName [20]; constdate hiredate; constdate birthdate; }; employee::employee(char*First, char*Last,constdate& d1, constdate& d2):birthdate(d1), hiredate(d2) { intLength = strlen(First); strncpy(FirstName,First,20); FirstName[Length]='0'; strncpy(LastName,Last,20); LastName[Length]='0'; cout<<"constructor runs for employee:"<<FirstName<<' '<< LastName<<endl; // ' ' space char;) } voidemployee::print() const { cout << "Employee: "<< FirstName << ' '<< LastName << endl; cout << "printing dates for this employee"<< endl; birthdate.print(); hiredate.print(); cout << "________________"<< endl; } employee::~employee() { cout << "destructor runs for employee class, for "; print(); } #endif
  • 61.
    Quiz #1, What’sthe Output? #include<iostream> #include"employee.h" #include"date.h" usingnamespacestd; voidmain() { date Birth1 (8,8,1990); date Hire1 (4,4,2010); date Birth2 (10,10,1990); date Hire2 (5,5,2010); employee manager1 ("mee", "loo", Birth1, Hire1 ); employee manager2 ("zee", "HooHoo", Birth2, Hire2 ); manager1.print(); manager2.print(); } #include<iostream> usingnamespacestd; #ifndefdate_h #definedate_h classdate { public: date(int= 1, int= 1, int= 1990); voidprint()const; ~date(); private: intday, month, year; }; date::date(intd, intm, inty) {day = d; month = m, year = y; cout << "Constructor runs for date class “;print();} voiddate::print() const {cout << day << "/"<< month << "/"<< year << endl; } date::~date() {cout << "destructor runs for date class, which date is:"<<endl; print(); } #endif
  • 62.
    Quiz #1 destructorruns for employee class, for Employee: zee HooHoo printing dates for this employee 10/10/1990 5/5/2010 ________________ destructor runs for date class, which date is: 5/5/2010 destructor runs for date class, which date is: 10/10/1990 destructor runs for employee class, for Employee: mee Hee printing dates for this employee 8/8/1990 4/4/2010 ________________ destructor runs for date class, which date is: 4/4/2010 destructor runs for date class, which date is: 8/8/1990 destructor runs for date class, which date is: 5/5/2010 destructor runs for date class, which date is: 10/10/1990 destructor runs for date class, which date is: 4/4/2010 destructor runs for date class, which date is: 8/8/1990 Constructor runs for date class 8/8/1990 Constructor runs for date class 4/4/2010 Constructor runs for date class 10/10/1990 Constructor runs for date class 5/5/2010 constructor runs for employee:mee Hee constructor runs for employee:zee HooHoo Employee: mee Hee printing dates for this employee 8/8/1990 4/4/2010 ________________ Employee: zee HooHoo printing dates for this employee 10/10/1990 5/5/2010 ________________
  • 63.
    Quiz #2, What’sthe Output? #include<iostream> usingnamespacestd; classTest { public: Test() { cout << "const "<< SVariable << endl; SVariable++; }; voidprint() const; ~Test() {cout << "dest "<< SVariable << endl; SVariable--; }; staticintSVariable; private: }; intTest::SVariable = 5; voidTest::print() const { cout << SVariable << endl; } #include<iostream> #include"MyFile.h" usingnamespacestd; voidmain() { Test T1; { Test T2[5]; Test TTemp; cout << TTemp.SVariable << endl; } cout << Test::SVariable << endl; Test *T3 = &T1; staticTest T4; cout << Test::SVariable << endl; Test* &T5 = T3; cout << Test::SVariable << endl; } const 5 const 6 const 7 const 8 const 9 const 10 const 11 12 dest 12 dest 11 dest 10 dest 9 dest 8 dest 7 6 const 6 7 7 dest 7 dest 6