Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
1.4k views
in JECRC University B.Tech(CSE-III Sem) Object Oriented Programming Lab by Goeduhub's Expert (7.1k points)

Constructors, object creation using constructors, and access restrictions

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence || Labeled as Highest Rated Course by Udemy

Apply Coupon

2.

Complete Machine Learning & Data Science with Python| ML A-Z Apply Coupon

3.

Complete Python Programming from scratch | Python Projects Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.1k points)
edited by
 
Best answer

Constructors in C++

A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.

Properties of Constructors

  • Constructor has same name as the class.
  • Constructors don’t have any return type.
  • A constructor is automatically called when an object of the class is created.
  • If we do not specify any constructor, C++ compiler generates a default constructor for us.It expects no parameters from user and has an empty body.

Types of Constructor

There are three types of constructors defined in C++.

1. Default Constructor

2. Parameterized Constructor

3. Copy Constructor

1. Default Constructor

Default constructor is the constructor which doesn’t take any argument. It has no parameters.

Example

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;

//Default Construction Declaration
Myclass()
{
x=10;
y=20;
}
};
void main()
{
clrscr();
//Object creation of class Myclass
Myclass My;
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
}

Output

x:10
y:20

2. Parameterized Constructor

It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.

Parameterized constructors are helpful to provide default value to class object.

Example

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;

//Parameterized Constructor Declaration
Myclass(int a,int b)
{
x=a;
y=b;
}
};
void main()
{
clrscr();

//Passing values to parameterized constructor
Myclass My(10,20);
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
}

Output

x:10
y:20

3. Copy Constructor

A copy constructor is a member function which initializes an object using another object of the same class.

Example

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y,z,w;

//Declatation of Parameterized Constructor
Myclass(int a,int b)
{
x=a;
y=b;
}

//Declaration of Copy Construction
Myclass(Myclass &m)
{
z=m.x;
w=m.y;
}
};
void main()
{
clrscr();

//Calling Parameterized Constructor
Myclass My(10,20);

//Calling Copy Constructor and passing another object as argument
Myclass My1(My);
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
cout<<"z:"<<My1.z<<"\n";
cout<<"w:"<<My1.w<<"\n";
getch();
}

Output

x:10
y:20
z:10
w:20

Constructor Definition outside the class

Constructor can also be declared outside the class body. It is done using scope resolution operator(::).

Example

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;

//Declaring Constructor inside class body
Myclass(int a,int b);
};

//Defining Constructor outside class bosy
Myclass::Myclass(int a,int b)
{
x=a;
y=b;
}
void main()
{
clrscr();
Myclass My(10,20);
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
}

Output

x:10
y:20

When class has a lot of member functions and constructors in it then defining constructors outside the class lessens class body. It makes program code easy to understand an it also increases speed of code compilation.

Difference Between Member Function and Constructor

Member FunctionConstructor
Member functions has a return type.Constructors does not have a return type.
Member function are not called automatically.Constructors are automatically called on creation of class object.
Member functions are not created itself they have to be created by programmer.When we do not create constructor compiler creates a default constructor and call itself.

Multiple constructors of different types can be declared inside a class. Multiple Constructors with same type and same name can also be declared in class which is called constructor overloading.

Destructors in C++

Destructor is a member function which destructs or deletes an object.

A destructor function is called automatically when the object goes out of scope. An object goes out of scope when

  • The function ends
  • The program ends
  • A block containing local variables ends
  • A delete operator is called


Apart from normal member functions destructors have same name as the class preceded by a tilde (~) and destructors don’t take any argument and don’t return anything.

In a class there can only be one destructor.

Why user defined destructor

If we do not write our own destructor in class, compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak.

Example

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Myclass
{
public:
int x,y;

//Default Construction Declaration
Myclass()
{
x=10;
y=20;
}

//Defining destructor inside class Myclass
~Myclass()
{
cout<<"\nDestructor is called\n";
getch();
}
};
void main()
{
clrscr();
//Object creation of class Myclass
Myclass My;
cout<<"x:"<<My.x<<"\n";
cout<<"y:"<<My.y<<"\n";
getch();
}

Output

x:10
y:20
Destructor is called


For more Object Oriented Programming Lab Experiments Click Here

3.3k questions

7.1k answers

394 comments

4.6k users

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...