2. Destructor Function
• This member function is called when you
are done with the object.
• It eliminates any dynamically allocated
variables created by the object so that the
memory space allocated becomes free.
• It has no parameters and no return type.
• No overloading is allowed.
3. Syntax
#include <iostream>
using namespace std;
class Employee{
int empNo;
public:
int getNo(){return empNo;}
void setNo(int number){
empNo=number;
}
Employee(){
cout << "constructor is called"<<endl;
empNo = 0;}
/* This is called automatically when the object is no more needed e.g. when
main() is finished*/
~Employee() {cout << "destructor is called"<<endl;} // Keep screen on to
see this text
};
constructor is called
4
Press any key to continue . . .
destructor is called
#include "employee.h"
main(){
Employee myEmp;
myEmp.setNo(4);
cout<<myEmp.getNo()<<endl;
system("pause");
}
4. Syntax
#include <iostream>
using namespace std;
class Employee{
int empNo;
public:
int getNo(){return empNo;}
void setNo(int number){
empNo=number;
}
Employee(){
cout << "constructor is called"<<endl;
empNo = 0;}
~Employee();
};
Employee::~Employee() {
cout << "destructor is called"<<endl;
}
•You can write the destructor
outside, as well.
•The compiler should provide
you default constructor and
destructor in case you do not
write them.
5. Array as data member
• A class may have arrays as data members
#include <iostream>
using namespace std;
class person{
int f[10];
public:
person (int=0);
void printF3(){cout<<f[2]<<endl;
}
};
person :: person(int n){
for (int i=0;i<10;i++)
f[i]=n;
}
main(){
person a(5);
person b;
a.printF3();
b.printF3();
system("pause");
}
What is the output?
6. Memberwise Initialization
• When a class object is assigned to another
object of the same class, all members are
automatically assigned.
class Example{
private:
int x;
int y;
public:
Example (int a, int b){x=a; y=b;}
};
main(){
Example ex1(3,5);
Example ex2=ex1; // x and y of ex2 are now 3 and 5, respectively.
}
7. Memberwise Initialization
• Memberwise initialization occurs when:
– Initializing a class object with another
– Passing of a class object as a parameter to a
function e.g. void myFunction1(Example s)
– Returning a class object from a function e.g.
Example myFunction2(int) {
return ex(int);
}
9. Constant Objects
• When an object is created as a constant
object, its data members cannot be
changed.
• Only constant functions can be called for
constant objects.
• Constant Member Function
– A function, which can be called for constant
objects but still cannot change the value of its
data members.
10. Example
main(){
const Example ex2(20,30);
ex2.setX(100); // ERROR
cout<<ex2.getX()<<endl; // ERROR
cout<<ex2.getMeX()<<endl;
system("pause");
}
#include <iostream>
using namespace std;
class Example{
private:
int x;
int y;
public:
Example (int a, int b){x=a; y=b;}
int getX(){
return x;
}
void setX(int myX){
x=myX;
}
int getMeX() const{
x=x+1; // ERROR
return x;
}
};
11. Example
#include <iostream>
using namespace std;
class myIntegerClass{
private:
int number;
public:
void store(int) ;
int get() ;
int incrementNumber();
myIntegerClass(int n){
number=n;}
};
int myIntegerClass::incrementNumber(){
return number++;
}
void myIntegerClass::store(int n){
number=n;
}
int myIntegerClass::get() {
return number;
}
main(){
myIntegerClass in(10);
in.store(20);
cout<<in.get()<<endl;
cout<<in.incrementNumber()<<endl;
cout<<in.get()<<endl;
system("pause");
}
What is the output?
12. Example
#include <iostream>
using namespace std;
class myIntegerClass{
private:
int number;
public:
void store(int) const;
int get() ;
int incrementNumber();
myIntegerClass(int n){
number=n;}
};
int myIntegerClass::incrementNumber(){
return number++;
}
void myIntegerClass::store(int n) const{
number=n;
}
int myIntegerClass::get() {
return number;
}
main(){
const myIntegerClass in(10);
in.store(20);
cout<<in.get()<<endl;
cout<<in.incrementNumber()<<endl;
cout<<in.get()<<endl;
system("pause");
}
What is the output?
13. Member Initialization
• This is to initialize members without using
assignment.
• For constant, reference and object class
members, you need to initialize instead of
assigning.
14. #include <iostream>
using namespace std;
class Simple{
private:
int i;
float f;
public:
Simple(int, float);
int getFirst(){return i;}
};
Simple::Simple(int x, float y){
i=x;
f=y;
}
/* OR */
Simple::Simple(int x, float y): i(x), f(y){}
main(){
Simple s(2, 2.3);
cout<<s.getFirst()<<endl;
system("pause");
}
Example
15. Example
#include <iostream>
using namespace std;
class ConR{
public:
ConR(int);
private:
int i;
const int ci;
};
ConR::ConR(int ii){
i=ii;
ci=ii; // Constant ci cannot make assignment
}
main(){
ConR myC(3);
system("pause");
}
#include <iostream>
using namespace std;
class ConR{
public:
ConR(int);
private:
int i;
const int ci;
};
/* ci is in a member initialization
list.*/
ConR::ConR(int ii): ci(ii){
i=ii;
}
16. Example
#include<iostream>
using namespace std;
class Date{
private:
int day, month, year;
public:
Date(int d, int m, int y):day(d), month(m), year(y){}
void display(){
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
};
main(){
Date today(10,3,2010);
today.display();
system("pause");
}
17. Class objects as members of
classes (composition)
• An object of a class can be a member of
another class.
• Recall “Example” and “AnotherExample”
classes in section Memberwise
Initialization.
18. Date and Student classes
#include<iostream>
using namespace std;
class Date{
private:
int day, month, year;
public:
Date(int d, int m, int y):day(d), month(m), year(y){}
void display(){cout<<day<<"/"<<month<<"/"<<year<<endl;}
};
class Student{
private:
Date birthdate;
int stId;
public:
Student(int, int, int, int);
void print(){cout<<"Student No: "<<stId<<endl<<"Birthdate: "; birthdate.display();}
};
Student::Student(int id, int a, int b, int c):stId(id), birthdate(a,b,c){}
main(){
Student ali(505, 3,7,1980);
ali.print();
system("pause");
}
19. Nested Classes
• Recall that you can create nested loops and if
statements; likewise, you can define classes within other
classes.
• By this way, you can embed a class definition inside
another class and you cover both definitions in one
include.
For example:
/************Already defined in StudentDateNested.h
#include "date.h"
#include "student.h"
*****************************************************/
#include "StudentDateNested.h"
20. Example
/*StudentDateNested.h*/
#include<iostream>
using namespace std;
class Student{
private:
class Date{
private:
int day, month, year;
public:
Date(int d, int m, int y):day(d), month(m), year(y){}
void display(){cout<<day<<"/"<<month<<"/"<<year<<endl;}
};
Date birthdate;
int stId;
public:
Student(int, int, int, int);
void print(){cout<<"Student No: "<<stId<<endl<<"Birthdate: "; birthdate.display();}
};
Student::Student(int id, int a, int b, int c):stId(id), birthdate(a,b,c){}
#include "StudentDateNested.h"
main(){
Student ali(505, 3,7,1980);
ali.print();
system("pause");
}