Question

Need help with a multiple inheritance work Question Compile and run the file. If you find...

Need help with a multiple inheritance work

Question

Compile and run the file. If you find any compilation errors,explain their reason as comments in the code. Then fix the errors such that the program compiles and runs. Explain the motivation of your modifications in the code and their effects on the execution

inheritance.cpp file

#include<iostream>
using namespace std;

class A
{
int x;
public:
void setX(int i) {x = i;}
void print() { cout << x; }
};

class B: public A
{
public:
B() { setX(10); }
};

class C: public A
{
public:
C() { setX(20); }
};

class D: public B, public C {
};

int main()
{
D d;
d.print();
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per requirement submitted above kindly find below solution.

Compile Error :

  • The line in main() d.print(); causing compile time error as
    request for member ‘print’ is ambiguous
  • Because class D is inheriting class B and class C
  • hence need to change the line so that either print() from B or Print from C is called
  • So for print() from B call as d.B::print();
  • or for class c call as d.C::print();

Demonstration :

Here new c++ program with name "main.cpp" is created which contains below code.

main.cpp

#include<iostream>//header files
using namespace std;
class A //Class A
{
int x;
public:
void setX(int i) {x = i;}
void print() { cout << x; }
};
//Class B inheriting class A
class B: public A
{
public:
B() { setX(10); }
};
//Class C inheriting class A
class C: public A
{
public:
C() { setX(20); }
};
//class D inheriting class C
class D: public B, public C {
};

int main()
{
D d;
d.B::print();//change this line
return 0;
}

==================================

Output :Compile and Run main.cpp to get the screen as shown below

Screen 1:For  d.B::print(); output is

Screen 2:For  d.C::print(); output is

NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Need help with a multiple inheritance work Question Compile and run the file. If you find...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10. Does the following code compile? Does it run? Is there any problem with the code?...

    10. Does the following code compile? Does it run? Is there any problem with the code? If yes, how do you fix it? 1. #include < iostream > 2. using namespace std; 4 class Computer int Id; 7. public: Computer(int id) this -Id- id; ) void process() cout << "Computer::process()"; 9 10. H; 12. class Employee 13. 14. 15. public: 16 Computer* c; Employee )cnew Computer (123); 17.Employee() C 18. 19. 20 21. 22. ^; 23. 24. int main(){ 25....

  • Question 4: If the line does not compile explain why: (10Pts) B objB: // Linel: Linel...

    Question 4: If the line does not compile explain why: (10Pts) B objB: // Linel: Linel compiles: Yes/No class Af (Circle right answer) public: If no, Explain: A(O virtual int output () 0 c objc: Line3 compiles: Yes/No (Cirele right answer) If no, Explain: Line3: private: int i elass B: public A private: D objD: // Line4: Line4 Compiles: Yes/No (Circle right answer) If no, Explain: int j class C ( objc.setx (2) Line5 Compiles: Yes/No (Circle right answer) //...

  • Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are...

    Please answer in C++ ONLY, and please fill all comments elaboratively. Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are required for this question. Run and provide a single screenshot showing the output. Complete the missing comments to explain the action performed by each statement highlighted in red. The first comment is given as an example. Copy and paste the source code with the comments filled out in your...

  • KINDLY HELP SOLVE THE C++ SECURE SHELL DRILLS, RUN AND COMPILE THEM TO PERFORM THE TASK PERFECTLY...

    KINDLY HELP SOLVE THE C++ SECURE SHELL DRILLS, RUN AND COMPILE THEM TO PERFORM THE TASK PERFECTLY. THANK YOU. /1 Function: Complete the body of the Read _Kth Num function. #include <iostream> using namespace std; // Read the k'th input value. DO NOT ISSUE A PROMPT // Example: when 3 and inputs are 4 6 8 10 12 the desired value is 8 void Read Kth Num (int k, int & value) tinclude "mainpt60300a.cpp" //Function: Complete the MaxVal function #include...

  • Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible...

    Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible would be really grateful to a quick solution. would give a thumbs up. Thank you! Q3: Question # 3 [20] Will the following code compile? If it does not, state the errors. If it does compile, write the output. //Function.cpp #include <iostream> using namespace std; void printData (long i) cout<<"In long print Data "«<i<<endl; } void printData(int i) cout<<"In int printData "<<i<<endl; ) void...

  • C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...

    C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your code in the source code where the comment “// your code” locates. After you finish the implementation, please also provide the output of your program. #include <iostream> using namespace std; class Shape { protected: // your code public: void setWidth (int w) { // your code } void setHeight (int h) { // your code } }; class Rectangle: public Shape { public: int...

  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • I need help with this question. Please answer it in full sentences and neat for me...

    I need help with this question. Please answer it in full sentences and neat for me to understand. Thank you. 1. What does the following code print out? Give the exact answer. #include <iostream> using namespace std; class Cellphone { public:                 virtual void features() {                                 cout << "Loading cellphone features.\n";                 } }; class App: public Cellphone{ public:                 void features() {                                 cout << "Loading app features.\n";                 } }; class Driver: public Cellphone{ public:                 void...

  • n this programming assignment, you need to create 3 files. 1. DateType.h 2. DateType.cpp 3. A...

    n this programming assignment, you need to create 3 files. 1. DateType.h 2. DateType.cpp 3. A test driver for testing the class defined in the other 2 files. You can name your file in the way you like. Remember it must be a .cpp file. In DateType.h file, type these lines: // To declare a class for the Date ADT // This is the header file DateType.h class DateType { public: void Initialize(int newMonth, int newDay, int newYear); int GetYear()...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT