Question

C++ problem

a 6. 9. Read the following program carefully and answer questions. Rinclude <iostream using namespace std; class Grand Parent public Grand Parent cout Grandparent constructor! <<endl. class Father public GrandParent public: Father Grandparent cout Father Constructor Kendl. void cout In Father functiob <<endl y virtual void cout In Father, y function <<endl i class Mother public Grand Parent public Motber() Grandparent() cout Mother Constructor <<endl void cout In Nother, x function <<endli yo void y() cout <<In Mother, y function... <<endl class Child public Father, public Motber public: Child() Notber Father cout Child Constructor <<endl void y() cout <<In child, y function <<endl i int main() Q6. What do you expect? Child c Q7. What do you expect? c. Father x() Mother amom norm->x(); Q8. What do you expect? return 0 A 3 ft 5 a

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

6)In the main function, what do expect by the line "Child c;"?Explain Why.

Explaination:

a)This is the best example of Hybrid Inheritance.
So Child class is derived class and which is at the last level so in inheritance we need to create
an object of Derived class which is situated at the end of structure of inheritace (ie Child is derived class of Mother & Father class which is situated at last level).
b)as we have created an object of Child with name c it implicitly call constructor of Mother & Father ,through that GrandFather Constructor gets call twice (Through Mother & Father Constructor)
So it will show an output which is mentioned as below
  
Grand Parent Constructor
Father Constructor
Grand Parent Constructor
Mother Constructor
Child Constructor

7)In the main function, what do expect by the line "c.Father::x();"?Explain Why.

   Explaination :

   a)c.Father::x() this statement is stating that object of Child class is accessing Father class & its method x().so to get direct access of father we use scope resolution operator to get access of x()
   method of father class.
   b)as x() function of father class gets call from child class it shows an output "In father ,x function " & as control goes on next line it calls the y() function of Child class .so output would be like this
   "In child, y function "
   Complete output of statment c.Father::x() is
  
   In Father,x function
   In Child, y function

8)In the main function, what will you have outputs by the line "mom->x();"?Explain Why.
         
   Explaination :
  
   a)Outputs by the line "mom->x()"
      
   In Mother , x function
   In Mother, y function

   b)This is the best example of virtual function. If we try to call x() function of mother with object of child then it will create an ambiguity because child class has same function as Mother class with same name as x() function
   so it will create an ambiguity. So to resolve this problem we have created an pointer object to store reference of Child object.
   we cant use (.) operator to call x() function because we have created pointer object so to call x() function with an object of Mother class we have to use (->) operator.
   first mom->x() it will give call to x() function of Mother class and display an output "In Mother, x function" & as control goes down it will call y() function of mother class.

9)Explain Polymorphism with the above program.

   Explaination:
  
   Above example is the best example of Runtime Polymorphism ie It shows the Function Overriding .
   Multiple classes have same function with same name & same signature.here we can see y() function is used in all the classes except GrandParent even x() function is used in Father & Mother classes.
   All overrided functions have same name and signature in all classes. this is all about polymorphism with above program.
             
  
   

Add a comment
Know the answer?
Add Answer to:
C++ problem a 6. 9. Read the following program carefully and answer questions. Rinclude <iostream using...
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
  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • C++ output 6) What is the exact output of the following program? #include <iostream> using namespace...

    C++ output 6) What is the exact output of the following program? #include <iostream> using namespace stdi void zolo(int &a, int sb) int main int x = 5, y =8; zolo(x,y)i cout << "x " << x << endl ; cout << "y = "" << y << endl ; return o: void zolo(int &a, int &b) int v1 = b + a; ' int v2 = b-a; 3 a=v1;13

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • 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...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

  • What is the output of this program? #include <iostream>   using namespace std;   class rect   {       int...

    What is the output of this program? #include <iostream>   using namespace std;   class rect   {       int x, y;       public:       void val (int, int);       int area ()       {           return (x * y);       }   };   void rect::val (int a, int b)   {       x = a;       y = b;   }   int main ()   {       rect rect;       rect.val (3, 4);       cout << "rect area: " << rect.area();       return 0; } a) rect area:7 b) rect area: 12 c) rect area:24 d) none of the...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

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