position: fixed; top: auto !important; margin-left: 112px;

What is Constructor?

Whenever you create an instance or object of a class, by default one function is being called. And that member function is called as Constructors. They are special functions. There are three types of Constructors in C++, 1) Default Constructor 2) Parameterised Constructor 3) Copy Constructor Why Constructors are special

1) Default Constructor
2) Parameterised Constructor 
3) Copy Constructor 

Why Constructors are special functions?

Normal Functions returns something like an integer value, float value or anything or nothing void. Right?

But,
  • Constructors return nothing not even void.

  • Constructors have the same name as class name.

  • Constructors are used for initialization of the objects.

Default Constructors before scenario:

In this case, we will create an object of a class but we don't write any Default Constructor to catch it. Still, it compiles properly.

#include <iostream>
using namespace std;
class Game
{
public:

}; 

int main()
{
 Game sankeObject;            // Creatig Game class's object
}
Compiles Properly. But didn't

Default Constructors after scenario :-

In this case , we will create an object of a class and now we create Defualt Constructor to catch it. Still it compiles properly. Good.

#include <iostream>
using namespace std;
class Game
{
public:
// Default Constructor
   Game()
   {
    out <<"Constructor is being initialised here with no parameters";
   }

}; 

int main()
{
 Game sankeObject;            // Creatig Game class's object
}
Constructor is being initialised here with no parameters

Firstly, main function is being called. Then we created an object of Game class named as sankeObject . When this sankeObject is created by default compiler calls a Constructor which is also called as default constructor. That's why output is "Constructor is being initialised here with no parameters"

Parameterised Constructor

When we create an object of a class by defualt "Default Constructor" is called. You can also pass some data as a parameter to these constructors like any other functions . Let's see this in action with two cases so that concept gets cleared in depth:

Parameterised Constructor before scenario :-

You will get Compilation error here because you created an parameterised object but there is no Constructor defined to catch it.

#include <iostream>
using namespace std;
class Game
{
public:

}; 

int main()
{
 Game sankeObject(2);            // Creatig Game class's object with parameter 2
                                 // Parameterised Constructor is being called
}

Output :-

Compilation Error

Here you created a snakeObject with parameter value 2. But there is no Parameterised Constructor is defined. In case of default constructor it worked because there were no parameters present to send.

Parameterised Constructor after scenario :-

#include <iostream>
using namespace std;
class Game
{
public:
// Parameterised Constructor
   Game(int a)
   {
    cout <<a;
   }

}; 

int main()
{
 Game sankeObject(2);            // Creatig Game class's object
}

Output :-

2

What we did is simply created an object snakeObject and passed a value 2 which will then catched by a Parameterised Constructor and then printed the passed value which is 2. That's why output is 2.

Remember :-

1) If object is created using Defualt Constructorand you did not defined defualt Constructor to catch the object then it works fine but.

2) If object is created using Parameterised Constructor and you did not defined Parameterised Constructor then it gives Compilation Error.

What are Destructors ?

Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope.

The syntax for destructor is same as that for the constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it.

Example :-

#include <iostream>
using namespace std;
class Game
{
public:
   Game(int a )
   {
    cout <<a;
   }

   ~Game()
   {
    cout<<"\nDestructor Function is called and deleted an Object.";
   }

}; 

int main()
{
 Game sankeObject(2);            // Creatig Game class's object
}

Output :-

2
Destructor Function is called and deleted an Object.

When main function completes all the execution of statements include into it and reaches at the end of curly brace which is also called as End of Scope of a function then Destrutor function is being called.

Solve the Quiz of Article

1) Is C++ is a "multi-paradigm" programming language?
Yes
No

2) Can C++ Identifiers be used as a keyword?
Yes
No


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.