0% found this document useful (0 votes)
39 views3 pages

Constructor

Constructors are special member functions that are used to initialize object variables and have the same name as the class, are automatically called when an object is created, and there are three types of constructors - the default constructor that initializes variables without arguments, the parameterized constructor that initializes variables with arguments, and the copy constructor that initializes a variable with the values of another variable of the same type.

Uploaded by

Pragnya Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Constructor

Constructors are special member functions that are used to initialize object variables and have the same name as the class, are automatically called when an object is created, and there are three types of constructors - the default constructor that initializes variables without arguments, the parameterized constructor that initializes variables with arguments, and the copy constructor that initializes a variable with the values of another variable of the same type.

Uploaded by

Pragnya Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Constructor

Constructor has special member function which is used to initialize value of variable inside an object.

Major point:

Con. name is same as class

Con. is automatically involved as soon as object of its class is created

Con. Has no any written type

Con. Allow default aurgument concept

Con. Are define in side public section

Con. Can’t be virtual

3 types of constructor.

1. Default
2. Parameter
3. Copy

Default constructor:

Class demo

Int a,b;

Public;

Demo ()

A=10;

B=20;

Void putdata();

Cout <<a<<b;

};

Int main()

Demoaa;

aa.putdata;
return 0;

it is a type of constructor which name is similar to the class name, which is automatically invobed the
constructor which has no aurgument is called default constructor.

Parameter constructor.

a constructor is a special name function name as the class name and which is automatically

invobed as soon as an object as its class is created

A constructor having aurgument is called parameterised constructor.

Code;

Class demo

Int a, b;

Public ::

Demo (int m, int n)

A=m;

B=n;

Void putdata();

Cout<<A<<b;

};

Int main;

Copy constructor;

When we need to initialize the variable with the value of variable with another variable same type
then we use concept of copy constructor.

Code:

class demo

Int a;

Public:
Demo()

{
a=10;

Demo(demo & b)

You might also like