-1

In this question, I'm supposed to create 2 base classes A and B. Each of which contains a display function. There are 2 int inputs taken, the first one gets square and we find the root of the second. How do I code this in C++ ?

The question is :

  1. Create a base class A and it contains a member function display() to calculate the square of a number and display it.
  2. Create a base class B and it contains a member function display() to calculate the square root of a number and display it.
  3. Create a class named C derived from A and B. Declare the object for class C and call the display functions of Class A and B from the main function to display the result.
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Md Nawaz
  • 13
  • 1

1 Answers1

1

You can do it using scope resolution operator like this :

class A {
public:
    void display() { std::cout << "A display" << std::endl; }
};
class B {
public:
    void display() { std::cout << "B display" << std::endl; }
};
class C : public A, public B
{
public :
    C() = default;
};

int main()
{
    C c;
    c.B::display();
    c.A::display();

    return 0;
}

This way you declare manually which base class's display you want to call.

HMD
  • 2,202
  • 6
  • 24
  • 37