0% found this document useful (0 votes)
28 views25 pages

Constructor Notes

The document discusses three types of constructors in C++ - default constructor, parameterized constructor, and copy constructor. It provides examples and explanations of each type of constructor. It also discusses constructor overloading, destructor, and when a user-defined copy constructor is needed.

Uploaded by

vishalpalv43004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views25 pages

Constructor Notes

The document discusses three types of constructors in C++ - default constructor, parameterized constructor, and copy constructor. It provides examples and explanations of each type of constructor. It also discusses constructor overloading, destructor, and when a user-defined copy constructor is needed.

Uploaded by

vishalpalv43004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Constructors: types of constructors

• A special method which is used to initialize the


the data members of objects .
• It is automatically called when an object of a
class is created.
• it has the same name as the class name.
• it is always public
• it does not have any return type
Types of constructor:
• Default constructor
• Parameterized constructor
• Copy constructor
Default constructor

A constructor which has no argument is known as default


constructor. It is automatically invoked at the time of
creating object.
class Employee
{
public:
Employee()
{
cout<<"Default Constructor"<<endl;
}
};
// WAP without constructor // WAP using construtor
#include<iostream>
#include<iostream>
using namespace std;
using namespace std;
class BCOM
{ class BCOM
public:
void display() //method /memberfunction
{
{ public: // Access specifier
cout<<"hello seema"; BCOM() // Constructor
}
{
};
cout<<"hello seema";
int main() }
{ };
BCOM obj; // Create an object of MyClass

} int main()
{
BCOM obj; // Create an object of
MyClass (this will call the constructor)

}
class Employee
{
public:
Employee()
{
cout<<"Default Constructor"<<endl;
}
};

Int main()
{

//REMAINING CODE ?

}
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};

int main()
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;

}
Three types of constructors in C++.

Default constructor/Constructors with Default Arguments


Parameterized constructor
Copy constructor

Default Constructor (Constructors with Default Arguments)

A constructor which has no argument is known as default constructor.


OR

A default constructor is one that does not have function parameters. It is used to
initialize data members with a value. The default constructor is called when the object is
created.
#include<iostream>
using namespace std;

class BCOM
{
public: // Access specifier
BCOM() // Constructor
{
cout<<"hello seema";
}
};

int main()
{
BCOM obj; // Create an object of MyClass (this will call the
constructor)

}
Parameterized Constructor
Constructors can also take parameters (just like regular functions), which can be
useful for setting initial values for attributes are called parametized constructor .

OR
parameterized constructor does have the constructor arguments and the value
passed in the argument is initialized to its data members.
EXAMPLE 1 :

#include <iostream>
using namespace std;
class mca {
public:
mca(string x) // this is parameterized constructor
{
cout<<x;
}
};

int main()
{
mca obj1("seema");
//no need to write obj1.mca(“seema”)

}
EXAMPLE 2 :
#include<iostream>
using namespace std;
class BBA
{
public:
int rollno;
string name;

BBA(int x ,string y) // this is parameterized constructor

{
rollno=x;
name=y;
}
void display()
{
cout<<"rollno of student is : "<<rollno <<endl;
cout<<"name of studen is : "<<name<<endl;
}
};

int main()
{
BBA obj1(1,"seema");
//BBA obj1=BBA(1,"seema");
// BBA obj2(2,"kiran");
obj1.display();
//obj2.display();
}
copy constructor
 A copy constructor is a member function that initializes an object using another
object of the same class

 Copy Constructor creates a new object, which is exact copy of the existing object.

Copy Constructor is of two types:

Default Copy constructor: The compiler defines the default copy constructor. If
the user defines no copy constructor, compiler supplies its constructor.
User Defined constructor: The programmer defines the user-defined
constructor.
Syntax Of User-defined Copy Constructor:

class-name (class-name &){}


Consider the following situation:

class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
} ;
#include<iostream>
using namespace std;
class BBA
{
public:
int rollno;

BBA(int x) // create parameterzed constructor


{
rollno=x;

}
BBA(BBA &y) // create copy constructor
{
rollno=y.rollno;
}

void display() //member function


{
cout<<"rollno of student is : "<<rollno <<endl;

}
};

int main()
{
BBA obj1(1);
// BBA obj2(obj1);
BBA obj2=obj1; //
obj2.display();

obj1.display();

}
When is a user-defined copy constructor needed?

We need to define our own copy constructor only if an object has pointers or any
runtime allocation of the resource like filehandle, a network connection..etc

46 27 10 4 41 11 7
Constructor overloading / Multiple Constructor in a Class

We can have more than one constructor in a class with same name, as long as each
has a different list of arguments.This concept is known as Constructor Overloading and
is quite similar to function overloading.

 Overloaded constructors essentially have the same name (exact name of the class)
and differ by number and type of arguments.

 A constructor is called depending upon the number and type of arguments passed.

 While creating the object, arguments must be passed to let compiler know, which
constructor needs to be called.
#include<iostream>
using namespace std;

class BBA
{
public:
int rollno;
string name;

BBA() // no argument constructor


{
cout<<"hello ";
}

BBA(int x ) // one parameterized constructor


{
rollno=x;

BBA(int x ,string y)
{
rollno=x;
name=y;
}
void display()
{
cout<<"rollno of student is : "<<rollno <<endl;
cout<<"name of studen is : "<<name<<endl;
}
};

int main()
{
BBA obj1;
BBA obj2(1);
BBA obj3(1,"seema");
obj2.display();
obj3.display();
}
Destructor
• Destructor is a special member function which destructs
or deletes an object.
• A destructor is called automatically when object goes out
of scope.
• Destructors have same name as the class preceded by a
tilde (~)
• Destructor should not have any parameter
• There can only one destructor in a class
• When a class contains a pointer to memory allocated in
class, we should write a destructor to release memory
syntax
~classname()
{
//code
}
#include<iostream>
using namespace std;

class BCOM
{
public: // Access specifier
BCOM() // Constructor
{
cout<<"hello";
}

~BCOM() //destructor
{
cout<<"DESTRUCTOR EXECUTE OR MEMORY RELEASE";
}
};

int main()
{
BCOM obj; // Create an object of MyClass (this will call the
constructor)

You might also like