Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Learn More

Article

Friend Functions in C++ Programming - Codzify.com

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()
{
  // Creating Game class's object
 Game sankeObject;          
}
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()
{
// Creating Game class's object
 Game sankeObject;            
}
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()
{
 // Creatig Game class's object with parameter 2
 Game sankeObject(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 <

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 <

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.

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor