KEMBAR78
Constructor and Types of Constructors | PPTX
Topic
Constructor and Types of Constructor
Contain
• Overview of Constructor
• Properties of Constructor
• Types of Constructor
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Constructor
• A constructor is a “special” member function which initializes the
objects of class.
Properties of Constructor
• Constructor is invoked automatically whenever an object of class is created.
• Constructor name must be same as class name.
• Constructors should be declared in the public section because private
constructor cannot be invoked
• outside the class so they are useless.
• Constructors do not have return types and they cannot return values, not
even void.
• Constructors cannot be inherited, even though a derived class can call the
base class constructor.
• Constructors cannot be virtual.
• An object with a constructor cannot be used as a member of a union.
• They make implicit calls to the operators new and delete when memory
allocation is required.
Types of Constructors
• There are mainly three types of constructors as follows:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
• Default constructor is the one which invokes by default when
object of the class is created.
• It is generally used to initialize the value of the data members.
• It is also called no argument constructor.
Example
class integar
{
int m,n;
public:
integer() // Default constructor
{
m=n=0;
}
};
Parameterized Constructor
• Constructors that can take arguments are called parameterized
constructors.
• Sometimes it is necessary to initialize the various data elements of
different objects with different values when they are created.
• We can achieve this objective by passing arguments to the
constructor function when the objects are created.
Example
class integer
{
int m,n;
public:
integer(int x,int y) // Parameterized constructor
{
m =x;
n=y;
}
};
Copy Constructor
• A copy constructor is used to declare and initialize an object from
another object.
• For example, integer(integer &i); OR integer I2(I1);
• Constructor which accepts a reference to its own class as a
parameter is called copy constructor.
Example
class integer
{
int m, n;
public:
integer(rectangle &x) // Copy constructor
{
m = x.m;
n = x.n;
}
};
References
Thank You
#include<iostream>
using namespace std;
int main()
{
cout << “Thank You”;
return 0;
}

Constructor and Types of Constructors

  • 2.
  • 3.
    Contain • Overview ofConstructor • Properties of Constructor • Types of Constructor 1. Default Constructor 2. Parameterized Constructor 3. Copy Constructor
  • 4.
    Constructor • A constructoris a “special” member function which initializes the objects of class.
  • 5.
    Properties of Constructor •Constructor is invoked automatically whenever an object of class is created. • Constructor name must be same as class name. • Constructors should be declared in the public section because private constructor cannot be invoked • outside the class so they are useless. • Constructors do not have return types and they cannot return values, not even void. • Constructors cannot be inherited, even though a derived class can call the base class constructor. • Constructors cannot be virtual. • An object with a constructor cannot be used as a member of a union. • They make implicit calls to the operators new and delete when memory allocation is required.
  • 6.
    Types of Constructors •There are mainly three types of constructors as follows: 1. Default Constructor 2. Parameterized Constructor 3. Copy Constructor
  • 7.
    Default Constructor • Defaultconstructor is the one which invokes by default when object of the class is created. • It is generally used to initialize the value of the data members. • It is also called no argument constructor.
  • 8.
    Example class integar { int m,n; public: integer()// Default constructor { m=n=0; } };
  • 9.
    Parameterized Constructor • Constructorsthat can take arguments are called parameterized constructors. • Sometimes it is necessary to initialize the various data elements of different objects with different values when they are created. • We can achieve this objective by passing arguments to the constructor function when the objects are created.
  • 10.
    Example class integer { int m,n; public: integer(intx,int y) // Parameterized constructor { m =x; n=y; } };
  • 11.
    Copy Constructor • Acopy constructor is used to declare and initialize an object from another object. • For example, integer(integer &i); OR integer I2(I1); • Constructor which accepts a reference to its own class as a parameter is called copy constructor.
  • 12.
    Example class integer { int m,n; public: integer(rectangle &x) // Copy constructor { m = x.m; n = x.n; } };
  • 13.
  • 14.
    Thank You #include<iostream> using namespacestd; int main() { cout << “Thank You”; return 0; }