Constructors are special function named after the class and without a return type, and are used to construct objects. Constructors, like function, can take input parameters. Constructors are used to initialize objects.
Constructors are different from function:
Types of constructor :
The program defines a class named math which has private data members a, b, and c, and a public member function add(). The math() function is a default constructor that initializes the private data members a and b to 0.
The add() function calculates the sum of the private data members a and b, and stores it in the private data member c. It then outputs the total sum to the standard output using cout.
In the main() function, an object of the math class is created using the default constructor. The add() member function of themath class is then called on the object to calculate and display the sum of a and b.
Overall, this program provides an example of using a default constructor in C++ to initialize the private data members of a class object. The program demonstrates the use of a default constructor as a special member function of a class which is executed automatically whenever an object of the class is created.
#include<iostream> using namespace std; /* Default Constructor Parameterized Constructor Copy Constructor */ //Default Constructor in C++ Programming /* A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its class which is used to initialize some valid values to the data members of an object. It is executed automatically whenever an object of a class is created. */ class math { private: int a,b,c; public: math() { a=0; b=0; } void add() { c=a+b; cout<<"Total : "<<c; } }; int main() { math o; o.add(); return 0; }
Total : 0To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions