0% found this document useful (0 votes)
20 views

Object Oriented

The document discusses constructors and destructors in C++. Constructors are special methods that initialize objects and are called automatically upon object creation. Destructors destroy objects and are called when objects go out of scope. The document covers default constructors, parameterized constructors, constructor overloading, and how constructors and destructors are used.

Uploaded by

farhan.mukhtiar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Object Oriented

The document discusses constructors and destructors in C++. Constructors are special methods that initialize objects and are called automatically upon object creation. Destructors destroy objects and are called when objects go out of scope. The document covers default constructors, parameterized constructors, constructor overloading, and how constructors and destructors are used.

Uploaded by

farhan.mukhtiar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming(Oop)

Lecture # 02 & 03

Lab Engineer: Fayaz Khan


Department of Computer Science,
City University of Sciences & Information Technology(CUSIT),
Peshawar
What is Constructor ?

 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:

class XYZ { XYZ() {


.... } ....
XYZ() { XYZ obj; ....
//Empty no code
}
};
Types of Constructor
Default Constructor

 A default constructor doesn’t have any arguments (or parameters)

#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

 Constructors with parameters are known as Parameterized constructors. These type of


constructor allows us to pass arguments while object creation.

XYZ(int a, int b)
{

} ...
XYZ obj(10, 20);
Parameterized Constructor

#include <iostream> int main(){


using namespace std; /* One way of creating object. Also *
class Add{ known as implicit call to the *
public: constructor */
//Parameterized constructor Add obj1(10, 20);
Add(int num1, int num2) /* Another way of creating object.
{ cout<<(num1+num2)<<endl; This * is known as explicit calling
} the * constructor. */
}; Add obj2 = Add(50, 60);
return 0;
}
Constructor overloading
 Constructor overloading is a concept of having more than one constructor with different
parameters list, in such a way so that each constructor performs a different task.
Constructor Overloading

#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;
}

You might also like