Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago