Let’s consider the following example:
#include <iostream> using namespace std; class Example{ public: //default constructor Example(){cout<<"Constructor called."<<endl;} //function to print message void display(){ cout<<"display function called."<<endl; } //Destructor ~Example(){cout<<"Destructor called."<<endl;} }; int main(){ //object creation Example objE; objE.display(); return 0; }
Constructor called. display function called. Destructorcalled.
#include <iostream> using namespace std; class Example{ public: //default constructor Example(); //function to print message void display(); //Destructor ~Example(); }; //function definitions Example::Example(){ cout<<"Constructor called."<<endl; } void Example::display(){ cout<<"display function called."<<endl; } Example::~Example(){ cout<<"Destructor called."<<endl; } int main(){ //object creation Example objE; objE.display(); return 0; }
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
#1 – Definitions of Constructor and Destructor inside class definition
Let’s consider the following example:
Output
#2 – Definitions of Constructor and Destructor outside class definition
Output