Question
What is the output of the following main()? Suppose the code segments will be assembled and embedded correctly in a C++ program. CODE: class small
What is the output of the following main()? Suppose the code segments will be assembled and embedded correctly in a C++ program.
CODE:
class small
{
public:
void print() const { std::cout << "small: x " << x << ", y " << y << " "; }
int add() { return x + y; }
small(int a = 0, int b = 0) : x(a), y(b) {}
private:
int x;
int y;
};
class notSmall : public small
{
public:
void print() const { small::print(); std::cout << "notSmall: z " << z << " "; }
int add() { return small::add() + z; }
notSmall(int a = 0, int b = 0, int c = 0) : small(a, b), z(c) {}
private:
int z;
};
int main()
{
small * ptrSmall;
small objSmall(2, 3);
notSmall objBig(3, 5, 7);
ptrSmall = &objSmall;
std::cout << ptrSmall->add() << " ";
ptrSmall->print();
ptrSmall = &objBig;
std::cout << ptrSmall->add() << " ";
ptrSmall->print();
return 0;
} // end main
What would be the output of the main()in a, if the print() and add() of class small are virtual functions?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started