Constructor Notes
Constructor Notes
} 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++.
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;
{
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.
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 A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
} ;
#include<iostream>
using namespace std;
class BBA
{
public:
int rollno;
}
BBA(BBA &y) // create copy constructor
{
rollno=y.rollno;
}
}
};
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(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)