2. Constructors
• Member functions can be used to give values to the data items in an object.
Sometimes, however, it’s convenient if an object can initialize itself when it’s first
created, without requiring a separate call to a member function.
• A type of member function that is automatically executed when an object of the
class is created is known as constructor.
• The constructor has
• no return type and
• has the same name that of class name.
• The constructor can work as normal function but it cannot return any value.
• It is normally defined in classes to initialize data members.
• It construct the object, therefore it is called constructor.
3. Syntax
• The syntax of declaring constructor is as follows:
name()
{
Body of constructor;
}
• name()
• Indicates the name of the constructor.
• The name must be same as the name of the class in which the constructor is
declared.
4. Example
#include <iostream.h>
#include <conio.h>
class Hello
{
private:
int n;
public:
Hello()
{ cout<<”Object is created”; }
};
void main()
{
Hello x,y,z;
getch();
}
In this program, three objects are
created, so the constructor will be
called three times and display
OUTPUT
Object is created
Object is created
Object is created
5. Same Name as the Class, No Return Type
• There are some unusual aspects of constructor functions.
• First, it is no accident that they have exactly the same name as the class of
which they are members.
• Second, no return type is used for constructors.
• Why not? Since the constructor is called automatically by the system, there’s no program
for it to return anything to; a return value wouldn’t make sense.
• These are two ways, the compiler knows that they are constructors.
6. Default Constructor
• An implicit no-argument constructor is built into every program
automatically by the compiler, and this constructor creates the objects,
even though we do not define it in the class. This no-argument constructor
is called the default constructor.
• If it weren’t created automatically by the compiler, we wouldn’t be able to
create objects of a class for which no constructor was defined.
• Often we want to initialize data members in the default (no-argument)
constructor as well.
• If we let the default constructor do it, we don’t really know what values the data
members may be given.
• If we care what values they may be given, we need to explicitly define the
constructor.
7. Example
#include <iostream.h>
#include <conio.h>
class Number
{
private:
int n,m;
public:
Number() //Default Constructor Explicit Definition
{ n = m = 100; }
Avg()
{ cout<<“Average of two numbers=”<<(n+m)/2; }
};
void main()
{
Number x;
x.avg();
getch();
}
8. Passing Parameters to Constructor
• Constructor can have parameters as normal functions do.
• The method of passing the parameters to a constructor is same as passing parameters to a
normal function.
• The only difference is that parameters are passed to the constructor when the object is declared.
• The parameters are written in parenthesis along with the object name in declaration statement.
• Syntax (parameterized constructor):
Constructor_Name(parameter_list)
{
Constructor body;
}
• Syntax (creating object):
Class_Name object_name(parameter_list);
9. Example: Write a class TV that contains attributes of Brand Name, Model and Retail Price.
Write a method to display all attributes and a method to change the attributes. Also write constructor
to initialize all the attributes.
#include<conio.h>
#include<iostream.h>
#include<string.h>
class TV {
private:
char BrandName[20], Model[10];
float Price;
public:
TV(char Brand[], char mod[], float pr)
{
// strcpy() is used to copy one string to another.
strcpy(BrandName,Brand);
strcpy(Model,mod);
Price=pr;
}
void Change(char Brand[], char mod[], float pr)
{
strcpy(BrandName,Brand);
strcpy(Model,mod);
Price=pr;
}
void Display()
{
cout<<"nBrand Name="<<BrandName<<endl;
cout<<"Model Name="<<Model<<endl;
cout<<"Retail Price="<<Price<<endl;
}
};
void main()
{
TV test("Sony","2010", 2000);
test.Display();
test.Change("Toshiba","2014",15000);
cout<<"nAfter Changing Values of Object";
test.Display();
getch();
}
10. Default Copy Constructor
• A type of constructor that is used to initialize an object with another object of the same type is known as
default copy constructor or copy constructor.
• Its name is “default copy constructor” because it is available by default in all classes. The user does not need
to write this constructor.
• It accepts a single object of the same type as parameter.
• The parameter for default copy constructor can be given in parenthesis or using assignment operator.
• Syntax
class_name object_name (parameter); OR
class_name object_name = parameter;
• class_name
• is the name of class and indicates the type of object to be created.
• Object_name
• indicates the name of object to be created.
• Parameter
• indicates the name of parameter that is passed to default copy constructor. The values of data members of parameter object
are copied to the data members of the new object. The type of new object and parameter object must be same.
11. Constructor Overloading
• The process of declaring multiple constructors with same name but
different parameters is known as constructor overloading.
• The constructors with same name must differ in one of the following
ways:
• Number of parameters
• Type of parameter
• Sequence of parameters
• It’s convenient to be able to give values to member variables of a
class when the object is created.
12. Example
#include<iostream.h>
#include<conio.h>
class Over
{
private:
int num;
char ch;
public:
Over() //default constructor
{ num=0; ch=‘x’; }
Over(int n, char c)//Overloaded constructor
{ num=n; ch=c; }
void Show()
{
cout<<“num=”<<num<<endl<<“ch=”<<ch;
}
};
void main()
{
Over first,second(100,’p’);
cout<<“The Content of First”<<endl;
first.Show();
cout<<“The Content of Second”<<endl;
second.Show();
getch();
}
13. Destructors
• We’ve seen that a special member function—the constructor—is called automatically when an
object is first created.
• You might guess that another function is called automatically when an object is destroyed. Such a
function is called a destructor.
• A destructor has
• same name as the constructor or class name
• No return type
• Not accept any parameters.
• preceded by a tilde sign (~).
• Syntax
~ name()
{
Destructor body
}