Question

C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following co

4.b) A class Point is declared as follows: class Point { public: Point (int = 0, int = 0); // default constructor void setPoi

4.a)
4.b>
4.c)

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

#include <iostream>
#include <string>
using namespace std;

class Vehicle{
   public:
       void print(){
           cout << "Print: I am a vehicle.\n";
       }
   void display(){
           cout << "Display: I am a vehicle.\n";
       }
};

class Car: public Vehicle{
   public:
       void print(){
           cout << "Print: I am a car.\n";
       }
   void display(){
           cout << "Display: I am a car.\n";
       }
};

int main(){
   Vehicle *v;
   Car c;
   v = &c;
   c.print(); //C is object of car so will print Car
   c.display();//C is object of car so will display Car
   v->print(); //v is pointer to car object, it will print vehicle, as superclass will be called [since virtual is not used]
   v->display(); //v is pointer to car object, it will print vehicle, as superclass will be called [since virtual is not used]
   return 0;
}

----output----------
Print: I am a car.
Display: I am a car.
Print: I am a vehicle.
Display: I am a vehicle.
----------------------------------------------------


---------------Change using Virtual----------------------------------
#include <iostream>
#include <string>
using namespace std;

class Vehicle{
   public:
       virtual void print(){
           cout << "Print: I am a vehicle.\n";
       }
   void display(){
           cout << "Display: I am a vehicle.\n";
       }
};

class Car: public Vehicle{
   public:
       void print(){
           cout << "Print: I am a car.\n";
       }
   void display(){
           cout << "Display: I am a car.\n";
       }
};

int main(){
   Vehicle *v;
   Car c;
   v = &c;
   c.print(); //C is object of car so will print Car
   c.display(); //C is object of car so will print Car
   v->print(); //v is pointer to car object, it will print vehicle, as superclass will be called [since virtual is not used]
   v->display();//v is pointer to car object, it will print Car, as derivedclass will be called [since virtual is used]
   return 0;
}

--------output-------------
Print: I am a car.
Display: I am a car.
Print: I am a car.
Display: I am a vehicle.
---------------------------------------------------------------


#include <iostream>
#include <string>
using namespace std;

class Point{
   public:
   Point(int=0, int=0);
   void setPoint(int, int);
   int getX() const{return x;}
   int getY() const{return y;}
   void printPoint() ;
private:
   int x, y;
};

Point::Point(int x, int y){
   this->x = x;
   this->y = y;  
}

void Point::setPoint(int x , int y){
   this->x = x;
   this->y = y;      
}

void Point::printPoint() {
  
   cout << "(" << getX() << ", " << getY() << ")" ;
}

class Circle: public Point{
   public:
   void setRadius(int r = 1);
       int getRadius();
       double area();
       Circle(int=0, int=0, int =1);
   private:
       int radius;  
};

Circle::Circle(int x, int y , int r){
  
   setPoint(x, y); //indirectly calling Point member function
   if( r <= 0) {
       this->radius = 1;
   }
   else{
       this->radius = r;
   }
  
}

void Circle::setRadius(int r){
   if( r <= 0) {
       this->radius = 1;
   }
   else{
       this->radius = r;
   }
}

int Circle::getRadius(){
   return radius;
}

double Circle::area(){
   return 3.142 * radius * radius;
}

int main(){
  
   int x, y, r;
   cout << "Enter x: " ;
   cin >> x;
   cout << "Enter y: " ;
   cin >> y;
  
   cout << "Enter radius:";
   cin >> r;
  
   Circle c =Circle(x, y, r);
   cout << "Circle Center is " ;
   c.printPoint();
   cout << endl;
  
   cout << "Radius is "<< c.getRadius() << endl;
   cout << "Area is "<< c.area() << endl;
return 0;
  
}

---output--------------------------------
Enter x: 2
Enter y: 4
Enter radius:3
Circle Center is (2, 4)   
Radius is 3   
Area is 28.278
-----------------------------


-----------------code change as assigning Protected members
#include <iostream>
#include <string>
using namespace std;

class Point{
   public:
   Point(int=0, int=0);
   void setPoint(int, int);
   int getX() const{return x;}
   int getY() const{return y;}
   void printPoint() ;
Protected:
   int x, y;
};

Point::Point(int x, int y){
   this->x = x;
   this->y = y;  
}

void Point::setPoint(int x , int y){
   this->x = x;
   this->y = y;  
}

void Point::printPoint() {
  
   cout << "(" << getX() << ", " << getY() << ")" ;
}

class Circle: public Point{
   public:
   void setRadius(int r = 1);
       int getRadius();
       double area();
       Circle(int=0, int=0, int =1);
   private:
       int radius;  
};

Circle::Circle(int x, int y , int r){  
   this->x = x; //directly calling Point member fields
   this->y = y;
   if( r <= 0) {
       this->radius = 1;
   }
   else{
       this->radius = r;
   }
  
}

void Circle::setRadius(int r){
   if( r <= 0) {
       this->radius = 1;
   }
   else{
       this->radius = r;
   }
}

int Circle::getRadius(){
   return radius;
}

double Circle::area(){
   return 3.142 * radius * radius;
}

int main(){
  
   int x, y, r;
   cout << "Enter x: " ;
   cin >> x;
   cout << "Enter y: " ;
   cin >> y;
  
   cout << "Enter radius:";
   cin >> r;
  
   Circle c =Circle(x, y, r);
   cout << "Circle Center is " ;
   c.printPoint();
   cout << endl;
  
   cout << "Radius is "<< c.getRadius() << endl;
   cout << "Area is "<< c.area() << endl;
return 0;
  
}


-------output------------------------
Enter x: 2
Enter y: 4
Enter radius:3
Circle Center is (2, 4)   
Radius is 3   
Area is 28.278
---------------------

Add a comment
Know the answer?
Add Answer to:
4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...
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
  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • Question 1 10 pts Inheritance is a capability of C++ to represent increased specialization that is...

    Question 1 10 pts Inheritance is a capability of C++ to represent increased specialization that is often found in real world relationships. True False Question 2 10 pts Inheritance between a child and parent object exhibits an 'relationship. Question 3 10 pts As you move down an inheritance relationship from parent to child, you typically move from a general to specific description. True False Question 4 10 pts The syntax for declaring DerivedClass to publicly inherit from BaseClass is given...

  • // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...

    // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello virtual function (hint this will be done in Person class // 2) Change main to create two Person pointer variables // 3) Set each pointer to one of the two objects already instantiated below // 4) Use the pointer variable to invoke the two sayHello() methods #include <iostream > #include <string > using namespace std ; class Person { protected : string name; int...

  • C++ Programming QUESTION 10 Based on the following C++ code, what is the output of "pvari->print("...

    C++ Programming QUESTION 10 Based on the following C++ code, what is the output of "pvari->print(" function call? class A public void print cout << "Hello from A": 1 virtual void print2() {cout << "Hello from A2";} class B public A public void print) { cout << "Hello from B:1 virtual void print2() {cout << "Hello from B2": 1 int main() Avari Apvarl-new B; return 0; 1 o a Hello from A2 ob Hello from B O Hello from A...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is...

    C++ PROGRAMMING Hi! My assignment prompt is below. What I'm having the most trouble understanding is where the shapes are being stored. I'm assuming an array, but I'm not sure how sizing would work. Any help is appreciated, thanks! I have also attached the .h file we must use. Prompt: The goal of HW2 is to implement classes representing shapes. A given program will use this class to create shapes at arbitrary locations in the x-y plane and move them....

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

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

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

  • D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime...

    D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime (int, int, int): void get Time (inte, int&, int&) const: void printTime() const: void increment seconds(): int +incrementMinutes(): int +increment Hours(): int -equalTime (const clockType.) const: bool Consider the UML class diagram shown in the accompanying figure. According to the UML class diagram, how many private members are in the class? none zero two three D Question 4 4 pts Consider the following class...

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