Chapter 5 Constructor
Chapter 5 Constructor
Chapter 5
Learning outcomes:
Constructor
Types of Constructor
Default Constructor
Parameterized Constructor
Constructor overloading
Destructor
Constructors Chapter 5
What is Security?
What is constructor?
Constructor is a member function of class, whose name is same as the
class name.
A constructor is a special type of function of a class which initializes
objects of a class.
In C++, Constructor is automatically called when object of class is
created.
Constructor does not have a return value, hence they do not have a return
type.
Constructors Chapter 5
What is Security?
Syntax for creating constructor:
1. Syntax for defining the constructor within the class:
<class-name> (list-of-parameters)
{
// constructor definition
}
2. Syntax for defining the constructor outside the class:
<class-name>: :<class-name> (list-of-parameters)
{
// constructor definition
}
Default Constructor Example Chapter 5
What is Security?
#include <iostream>
using namespace std;
class Emp {
public:
Emp() {
cout<<"Constructor”;
}
};
int main() {
Emp e1; //creating an object of Emp
}
Difference between constructor and normal functions
What is Security?
Constructor Function
1. Default constructor
2. Parameterized constructor
Types of Constructors Chapter 5
What is Security?
1. Default Constructor
• A constructor which has no argument is known as default constructor.
• Default constructor is the constructor which doesn’t take any argument.
• It is invoked at the time of creating object.
Default Constructor Example Chapter 5
What is Security?
#include <iostream>
using namespace std;
class Employee {
public:
Employee() {
cout<<"Default Constructor Invoked"<<endl;
}
};
int main() {
Employee e1; //creating an object of Employee
}
Types of Constructors Chapter 5
What is Security?
2. Parameterized Constructor
• A constructor which has parameters is called parameterized constructor.
• To create a parameterized constructor, simply add parameters to it, the
way you would do it to any other function.
• When you define the constructor’s body, use the parameters to initialize
the object.
• It is used to provide different values to distinct objects.
Parameterized Constructor Example Chapter 5
What is Security?
#include <iostream> void display()
using namespace std; {
class Employee { cout<<id<<" "<<name<<endl;
public: }
int id; };
string name; int main()
Employee(int i, string n) {
{ Employee e1 =Employee(101, "Sonoo");
id = i; e1.display();
name = n; }
}
Constructors Example 3 Chapter 5
What is Security?
#include<iostream> cout<<"Enter the Name:";
using namespace std; cin>>name;
class student{ }
int rno; void student :: display()
char name[50]; {
double fee; cout << rno << "\t“ << name;
public: }
student(); int main()
void display(); {
}; student s;
student::student() { s.display();
cout<<"Enter the RollNo:"; }
cin>>rno;
Constructor Overloading Chapter 5
What is Security?
• We can have more than one constructor in a class with same name, but
different list of arguments. This concept is known as Constructor
Overloading.
• Overloaded constructors have the same name (name of the class) but the
different number of arguments.
• Depending upon the number and type of arguments passed, the
corresponding constructor is called.
Default Constructor Example Chapter 5
What is Security?
#include <iostream> construct (int a, int b) {
using namespace std; area = a * b;
class construct { cout<< area<< endl;
public: }
float area; };
construct() { int main() {
area = 0; construct ob;
cout << area<< endl; construct ob2( 10, 20);
} }
Destructor Chapter 5
What is Security?
• Destructor is also a special member function and destructor destroys the
class objects created by constructor, and release memory space occupied
by the objects.
• Destructor has the same name as their class name preceded by a tiled (~)
symbol.
• It is not possible to define more than one destructor hence it can-not be
overloaded.
• Destructor neither requires any argument nor returns any value.
Destructor Chapter 5
What is Security?
Syntax for defining the destructor within the class:
~ <class-name>()
{
}
Destructor Chapter 5
What is Security?
#include <iostream> ~Employee()
using namespace std; {
class Employee cout<<"Destructor called"<<endl;
{ }
public: };
Employee() int main(void)
{ {
cout<<"Constructor called"; Employee e1;
} }
What is Security?
End of Chapter