Object Oriented
Object Oriented
Lecture # 02 & 03
Constructor in C++ is a special method that is invoked automatically at the time of object
creation.
Constructor is a special member function of a class that initializes the object of the class.
whose name is the same as the class name.
Constructors do not return value, hence they do not have a return type.
Constructors can be overloaded.
How to use constructor in C++
#include <iostream>
int main(){
using namespace std;
/* This is how we create the object
class constructorDemo{
of class, * I have given the object
public:
name as obj, you can * give any
int num;
name, just remember the syntax: *
char ch;
class_name object_name; */
/* This is a default
constructorDemo obj;
constructor of the * class,
/* This is how we access data
do note that it's name is
members using object * we are just
same as * class name and it
checking that the value we have *
doesn't have return type. */
initialized in constructor are
constructorDemo() {
reflecting or not. */
num = 100;
cout<<"num: "<<obj.num<<endl;
ch = 'A';
cout<<"ch: "<<obj.ch;
}
return 0;
};
}
Constructor vs Member function
Now that we know what is constructor, lets discuss how a constructor is different from member
function of the class.
1) Constructor doesn’t have a return type. Member function has a return type.
2) Constructor is automatically called when we create the object of the class. Member
function needs to be called explicitly using object of class.
3) When we do not create any constructor in our class, C++ compiler generates a default
constructor and insert it into our code. The same does not apply to member functions.
This is how a compiler generated default
constructor looks:
#include <iostream>
using namespace std;
class Website{
public:
//Default constructor
Website() {
cout<<"Welcome to City University"<<endl;
} };
int main(void){
/*creating two objects of class Website. *
This means that the default constructor *
should have been invoked twice. */
Website obj1;
Website obj1;
return 0;
}
Parameterized Constructor
XYZ(int a, int b)
{
} ...
XYZ obj(10, 20);
Parameterized Constructor
#include <iostream>
using namespace std;
class Studentdata{
public: int main(){
int ID; Studentdata std1;
string name; Studentdata std2(1607, “zahid”,50);
float marks; return 0;
public: }
Studentdata() {
ID=1606;
Name=“fayaz khan”;
Marks= 70;
}
Studentdata(int num1, string
name, float marks) {
}
};
What is a destructor?
A destructor is also a special member function like a constructor. Destructor destroys the
class objects created by the constructor.
Destructor has the same name as their class name preceded by a tilde (~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created by the constructor. Hence
destructor can-not be overloaded.
Destructor release memory space occupied by the objects created by the constructor.
// C++ program to demonstrate the execution of constructor
// and destructor
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed"; }
// User-Defined Destructor
~Test() { cout << "\n Destructor executed"; }
};
main()
{
Test t;
return 0;
}
// C++ program to demonstrate the execution of constructor
// and destructor when multiple objects are created
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed"; }
// User-Defined Destructor
~Test() { cout << "\n Destructor executed"; }
};
main()
{
// Create multiple objects of the Test class
Test t, t1, t2, t3;
return 0;
}
// C++ program to demonstrate the number of times constructor and destructors are called
#include <iostream>
using namespace std;
static int Count = 0; //It is static so that every class object has the same value
class Test {
public:
Test(){
// Number of times constructor is called
Count++;
cout << "No. of Object created: " << Count<< endl;}
~Test(){
cout << "No. of Object destroyed: " << Count //It will print count in << endl;
// Number of times destructor is called
Count--; } };
int main(){
Test t, t1, t2, t3;
return 0;
}