Connect with us

C programming

Constructors And Destructors | Object & Classes

Published

on

Introduction:

A constructor is a special member function whose task is to initialize the objects of it’s class.

Some key points of constructor:

  • A constructor has the same name as that of class name.
  • Constructors are invoke automatically and create objects.
  • A constructor can not be abstract.
  • similar to normal functions , constructors may be overloaded.
  • constructors are normally use to initialize variables and allocate memory.

A constructor is declare and define as follows :

// constructor
class code
 
   int j, k;
   public:
          code(void);         //constructor declared;
  //code here;
 ;
code::code(void)             //constructor defined;
 
   j=0, k=0;
 

In the above program we define constructor and name it as “code” .

An object of Code:

code c1; //object created;

Different Types Of Constructors :

1.DEFAULT CONSTRUCTOR

2.PARAMETERIZED CONSTRUCTOR

3.COPY CONSTRUCTOR

1.Default constructor:

Default constructor do not take any value/ parameter.

2.Parameterized constructors :

Parameterized constructor that means it can accept any number of arguments.

code(int x, int y);

3.Copy constructor :

A copy constructor takes a reference to an object of same class as itself as an argument.

A Constructor Function Have Some Special Characteristics :

  • Constructors are declare “Public” section.
  • A constructors are invoke automatically and objects are create.
  • Constructors do not have “return type” and not even “void”.
  • Constructor can’t be “inherit”.
  • They can not be “virtual”.

The post Constructors And Destructors | Object & Classes appeared first on GeeksGod.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Newsletter

Trending

Copyright © 2019 - 2020