Answer to Question #180762 in C++ for RAMAKRISHNA

Question #180762

Explain default constructor and parameterized constructor with the help of program.


1
Expert's answer
2021-04-14T16:00:26-0400
//C++ program to demostrate default and parameterized constructor.
#include <iostream>
using namespace std;


class MyClass
{
    //Class data members
    int age;
    string name;
    // Default constructor
public:
    MyClass()
    {
        cout<<"Default constructor called"<<endl;
    }


    //Parameterized constructor
    MyClass(int a, string n)
    {
        cout<<"Parameterized constructor called"<<endl;
        age = a;
        name = n;
        cout<<"His name is "<<name <<"and his age is "<<age<<endl;
    }


};




// Main program
int main()
{
	MyClass mc;//This will call the default constructor


	MyClass mc2(12, "Joe");//This will call the parameterized constructor


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS