C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Constructors

Constructors

shape Description

As soon as the class with objects is executed, a member function called "Constructor" is generated.The name of constructor will be same as the name of class. The constructor does not have any return type(void also).

Characteristics of Constructor


  • Constructor have to be declared in public section and cannot be virtual.
  • They are invoked automatically as soon as objects are created.
  • Even though derived class calls base class constructor, they cannot be inherited.

Advantage of constructor is to generate immediately after the object initialization.

Default constructor

shape Description

The default constructor is the constructor which doesn't take any argument.It has no parameters and is called for any declared objects of its class to which arguments are not passed. It is also called as zero-argument constructor.

shape Example

[c] #include <iostream> using namespace std; class Triangle { public: void setBase( double bas ); double getBase( void ); Triangle(); // This is the constructor private: double base; }; // Member functions definitions including constructor Triangle::Triangle(void) { cout << "Object is being created" << endl; } void Triangle::setBase( double bas) { base = bas; } double Triangle::getBase( void ) { return base; } // Main function for the program int main( ) { Triangle triangle; // set line base triangle.setBase(25.0); cout << "Base of Triangle : " << triangle.getBase() <<endl; return 0; }[/c] Output [c] Object is being created Base of Triangle : 25[/c]

Parameterized constructor

shape Description

Parameterized constructor helps in initializing the values for the executed objects automatically.

shape Example

[c] #include <iostream> using namespace std; class Triangle { public: void setBase( double bas ); double getBase( void ); Triangle(double bas); // This is the constructor private: double base; }; // Member functions definitions including constructor Triangle::Triangle( double bas) { cout << "Object is being created, base = " << bas << endl; base = bas; } void Triangle::setBase( double bas ) { base = bas; } double Triangle::getBase( void ) { return base; } // Main function for the program int main( ) { Triangle triangle(25.0); // get initially set Base. cout << "Base of triangle is : " << triangle.getBase() <<endl; // set riangle Base again triangle.setBase(50.0); cout << "Base of triangle is : " << triangle.getBase() <<endl; return 0; }[/c] Output [c] Object is being created, base = 25 Base of triangle is : 25 Base of triangle is : 50[/c]

Destructors

shape Description

Destructors are exactly opposite to the constructors. These are generated when the objects are out of their scope. It helps in deletion of objects once the execution is completed. The destructor is represented with negation~ in the front of constructor.

shape Example

[c]class Triangle { public: void setBase( double len ); double getBase( void ); Triangle(); // constructor declaration ~Triangle(); // destructor declaration private: double base; };[/c]

C++ - Related Information
C++ Tutorial
C++ Syntax
C++ Classes
C++ Object
C++ Data Types

Join us on Telegram Channel