CPP > Random > Beginner Next/Skip this question

What will be the output of following C++ program using copy constructor?

#include <iostream>
using namespace std;

class MyClass
{
    //constructor with zero parameter, also known as default contructor for a class
    public: MyClass(MyClass& m)
    {
        this->a = m.a + m.b;
        this->b = m.b;
    }
    public: MyClass(int a)
    {
        this->a = a;
        b = 3;
    }
    public: int a;
    public: int b;
    public: int add()
    {
        return this->a + this->b;
    }
};

int main () {
    MyClass m(10);
    MyClass m1(m);
    m.a = 9;
    cout << m.add();
    cout << m1.add();
    return 0;
}
Answers :
social share share on facebook share on twitter share on google plus share on linkedin
Add Comments :

Login

Register

Next Question