Constructors & Destructors
Course Title: Programming Language 2
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 4 Week No: 4 Semester: Fall 25-26
Lecturer: Farhan Faisal; [Link]@[Link] | Room: DN0223
Constructor Functions
• A constructor is a special member function that initializes a
new object.
• It ALWAYS runs automatically when an object is created.
• Its name is same as the class name
• It has no return type (not even void).
• You can overload constructors (different parameter lists).
Default Constructors
▪ Default constructor is the constructor which doesn’t
take any argument. It has no parameters.
class construct{
public:
int a, b;
// Default Constructor
construct(){
cout << "Constructing” << endl;
a = 10;
b = 20;
}
};
int main(){
/* Default constructor called automatically
Constructing…
when the object is created a: 10
*/
construct c; b: 20
cout << "a: " << c.a << endl << "b: " << c.b;
return 0;
}
Parameterized Constructors
▪ It is possible to pass arguments to constructors.
▪ Typically, these arguments help initialize
attributes of an object when it is created.
▪ Simply add parameters to it the way you would to
any other function.
▪ When you define the constructor’s body, use the
parameters to initialize the object.
Parameterized Constructors
class Point{
int x, y;
public:
Point(int x1, int y1){
x = x1;
y = y1;
}
int getX() { int main(){
return x;
} // Constructor called
Point p1(10, 15);
int getY() {
return y; // Access values
} cout << "p1.x = " << [Link]() << endl;
}; cout << "p1.y = " << [Link]() << endl;
return 0;
}
Parameterized Constructors
Once you declare any constructor (e.g., a parameterized one), the
compiler won’t implicitly declare a no-arg (default) constructor
for you.
int main(){
// Constructor called
Point p1;
// Error because the object attributes are not initialized
// Access values assigned by constructor
cout << "p1.x = " << [Link]() << ", p1.y = " << [Link]();
return 0;
}
Destructor
▪ The complement of a constructor is called destructor.
▪ Destructor is a member function which destructs or deletes an
object.
▪ When is destructor called?
▪ A destructor function is called automatically when the object
goes out of scope:
▪ the function ends
▪ the program ends
▪ a block containing local variables ends
▪ How destructors are different from a normal member function?
▪ Destructors have same name as the class preceded by a tilde
(~)
▪ Destructors don’t take any argument and don’t return anything
Constructors and Destructors
class Line{
double length;
public:
double getLength();
Line(double len); // This is the constructor declaration
~Line(); // This is the destructor: declaration
};
Line::Line(double len) {
length = len;
cout << "Object is being created" << endl;
}
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
double Line::getLength() { return length; }
Constructors and Destructors
int main(){
Line line(6.0);
cout << "Length of line : " << [Link]();
return 0;
}
Output:
Object is being created
Length of line : 6
Object is being deleted
What happens when an object is passed to a
function?
• Passing an object by value creates a new object and calls
its copy constructor (or move constructor for rvalues)—not
the default/parameterized constructor.
• Passing by (const) reference (const T&) does not make a
copy, so no copy/move constructor is called.
• Constructors initialize brand-new objects; copy/move
constructors initialize a new object from an existing one
without changing the original.
• The by-value parameter (the copy) is destroyed when the
function ends, so its destructor runs; the caller’s original
object is unaffected.
What happens when an object is passed to a
function?
class student{ class student{
int id; int id;
public: public:
int setId(int i) { id = i; } student(int i){
int getId() { return id; } cout <<“Constructor…”<< endl;
}; id = i;
}
void show(student st){
cout << [Link]() << endl;
} int getId() { return id; }
};
int main(){
student ob; int main(){
[Link](50); student ob(3);
show(ob); show(ob);
} }
What happens when an object is passed to a
function?
void show(student st){
class student{ cout << [Link]() << endl;
int id; }
public:
int main(){
student ob(3);
show(ob);
}
student(int i){
cout << "Constructing.." << endl;
id = i;
}
Output:
int getId() { return id; }
Constructing..
~student(){ 3
cout << "Destructing.." << endl;
} Destructing..
}; Destructing..
Overloading Constructors
class Point
{
int x, y;
public:
//overloading constructors
Point() { x = 0; y = 0; }
Point(int x1, int y1)
{ x = x1; y = y1; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p0;
Point p1(10, 20);
cout << "p0.x:" << [Link]() << " " << "p0.y:" << [Link]() << endl;
cout << "p1.x:" << [Link]() << " " << "p1.y:" << [Link]() << endl;
return 0;
}
Overloading Constructors
class date{
int day, month, year;
public:
int main(){
date(char* str);
date sdate("10/10/2018");
date(int d, int m, int y);
date idate(10, 10, 2018);
};
}
date::date(char * str){
cout << str << endl;
}
date::date(int d, int m, int y){
day = d; month = m; year = y;
cout << day << " " << month << " " << year << endl;
}
Default Arguments and Constructor
You can also give constructor functions
default arguments.
Default Arguments and Constructor
class Point{
int x, y;
public:
//initialization
Point(int x1 = 0, int y1 = 0){
x = x1; y = y1;
}
int getX() { return x; }
int getY() { return y; }
};
int main(){
Point p0; //declare without initialization
Point p1(10, 20); //declare with initial value
cout << "p0.x:" << [Link]() << " " << "p0.y:" << [Link]() << endl;
cout << "p1.x:" << [Link]() << " " << "p1.y:" << [Link]() << endl;
return 0;
}
PRACTICE
Create a class Box with the following requirements:
Private data members: double length, width, height,
volume;
Constructor: Box(double l, double w, double h) that
sets the values of the attributes using the inputs and
computes volume = l * w * h (store it in volume).
Member function: void showVolume(); that prints the
box’s volume. MEMBER FUNCTION BODY SHOULD BE OUTSIDE
CLASS.
In main: create three Box objects (dimensions: [3, 3,
3], [4, 4, 4], [5, 5, 5]) and call showVolume() for
each.
References
1.[Link]
2.[Link]
3.[Link]
Books
❑ Teach Yourself C++, 3rd Edition, Herbert Schildt.
❑ The C++ Complete Reference, 4th Edition, Herbert Schildt.
❑ C++ How to Program, 4th Edition, Deitel and Deitel.
❑ The C++ Programming Language, Special 3rd Edition, Bjarne Stroustrup
❑ Thinking in C++, Volume One, 2nd Edition. Bruce Eckel.