OOPortal OOPortal


Inheritance/Polymorphism  «Prev 

C++ Virtual Function - Exercise

Adding a virtual function

Objective: Add a virtual function to the shape class that returns the name of a shape as a char* value.

Background:

Here is the Shape class hierarchy for reference:
In Java, all classes becuse with a capital letter.
class Shape {
public:
 virtual double  area() const { return 0; }
 //virtual double area is default behavior 
 protected:
  double  x, y;
};

class rectangle : public shape {
 public:
  double  area() const { 
   return (height * width); 
  } 
 private:
  double  height, width;
};

class circle : public shape {
public:
 double  area() const{ 
  return (PI * radius * radius);
 }
private:
 double   radius;
};

This code is also available in a file named shape.cpp, which can be found in the compressed course download file available on the Resources page.
Paste your code below and click the Submit button when you are ready to submit this exercise.