Constructors
Constructors
Constructor
• Constructor in C++ is a special method that is invoked automatically
at the time of object creation.
• It is used to initialize the data members of new objects generally.
• The constructor in C++ has the same name as the class or structure.
• It constructs the values i.e. provides data for the object which is why it
is known as constructor.
Syntax:
<class-name>(list-of-parameters)
{ //constructor definition }
Example
#include<iostream>
void display()
using namespace std;
class student
{
{ cout<<endl<<rno<<"\
int rno; t"<<name<<"\t"<<fee;
char name[50]; }
double fee; };
public:
student() int main()
{ {
cout<<"Enter the RollNo:"; student s; //constructor gets called
cin>>rno; automatically when we create the object of the class
cout<<"Enter the Name:";
s.display();
cin>>name;
return 0;
cout<<"Enter the Fee:";
cin>>fee;
} }
Default Constructor
• A constructor without any arguments or with the default value for every argument
is said to be the Default constructor.
• A constructor that has zero parameter list or in other sense, a constructor that
accept no arguments is called a zero argument constructor or default constructor.
• If default constructor is not defined in the source code by the programmer, then
the compiler defined the default constructor implicitly during compilation.
• If the default constructor is defined explicitly in the program by the programmer,
then the compiler will not defined the constructor implicitly, but it calls the
constructor implicitly.
Copy Constructor
• A copy constructor is a member function that initializes an object using another object of the
same class. In simple terms, a constructor which creates an object by initializing it with an object
of the same class, which has been created previously is known as a copy constructor.
• Copy constructor is used to initialize the members of a newly created object by copying the
members of an already existing object.
• Copy constructor takes a reference to an object of the same class as an argument.
Sample(Sample &t)
{
id=t.id;
}
• The copy constructor can be defined explicitly by the programmer. If the
programmer does not define the copy constructor, the compiler does it for us.
Static Constructor
• C++ doesnot have static constructors.
• But we can emulate them using a static instance of a nested block
class has_static_constructor
{
friend class constructor;
struct constructor
{
constructor()
{ /* do some constructing here … */ }
};
static constructor cons;
};
Destructors
• Destructor is an instance member function that is invoked automatically whenever an
object is going to be destroyed. Meaning, a destructor is the last function that is
going to be called before an object is destroyed.
• 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 neither requires any argument nor returns any value.
• It is automatically called when an object goes out of scope.
• Destructor release memory space occupied by the objects created by the constructor.
• In destructor, objects are destroyed in the reverse of an object creation.
~ <class-name>() { // some instructions }
Constructor Overloading
• In C++, 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 different 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.
// C++ program to illustrate
// Constructor overloading void disp()
#include <iostream> {
using namespace std; cout<< area<< endl;
}
class construct };
{
int main()
public: {
float area; // Constructor Overloading
// with two different constructors
// Constructor with no parameters // of class name
construct() construct o;
{ construct o2( 10, 20);
area = 0;
} o.disp();
o2.disp();
// Constructor with two parameters return 1;
construct(int a, int b) }
{
area = a * b;
}
Method Overloading
• Method overloading is the process of overloading a method with the same name but
different parameters.
int sample(a)
{
}
int sample(int a , int b)
{
}
float sample(float a, float b)
{
}
#include <iostream>
using namespace std;
class addition
{
public:
int addMethod(int x, int y)
{
return x + y;
}
int addMethod(int x, int y, int z)
{
return x + y + z;
}
};
int main(void)
{
addition add;
cout << add.addMethod(2, 3) << endl;
cout << add.addMethod(2, 3, 6) << endl;
return 0;
}
Operator Overloading
• C++ has the ability to provide the operators with a special meaning for
a data type, this ability is known as operator overloading.
• Operator overloading is a compile-time polymorphism.
• For example, we can overload an operator ‘+’ in a class like String so
that we can concatenate two strings by just using +
Syntax:
returntype operator symbol(arguments)
Non overloadable Operators
• . (dot operator)
• :: (Scope resolution operator)
• .* (Pointer to member operator)
• ?: (Ternary operator)
// C++ Program to Demonstrate Void operator ++()
// Operator Overloading {
#include <iostream> value=value+5;
using namespace std; }