Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Polymorphism Example 2 - Classes Circle, Cylinder & Sphere
  • Make area() a virtual function in circle.h
  • In the driver program create a pointer to a base class object and have it "point" to objects of each class.
In the definition of the Circle class:

#ifndef CIRCLE_H
#define CIRCLE_H
// Circle class definition - file circle.h

class Circle
{
   friend double distance( Circle& c ); // friend function

   friend ostream& operator<<(ostream& out, Circle& cc );

   public:
     Circle();               // default constructor
     Circle(int x, int y, double r); // constructor
     void showdata(ostream& out) const;
     double get_radius()
const;       // accessor
     void set_radius( double r );    // mutator
 
     void setCircle(int x, int y, double r);
     virtual double area()
 const;
     double circumference()
 const;
     bool equal( Circle& c2 ) const
;

     
// operator functions
     bool operator==(Circle& c2) const; 
     Circle operator+(Circle& circle2 ) const;


   protected:
     int xcenter;
     int ycenter;
     double radius;
     static double PI; // static data member
};
#endif // CIRCLE_H


Create the Sphere class:

% cat sphere.h
#ifndef SPHERE_H
#define SPHERE_H
class Sphere: public Circle
{
  public:
    Sphere();
    Sphere(int x, int y, double r);
    double area() const;
};
#endif


% cat sphere.cpp
// Sphere class implementation - file sphere.cpp

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

#include "circle.h" // header file
#include "sphere.h"

// constructors

Sphere::Sphere() : Circle() {} // call base class constructor

Sphere::Sphere(int x, int y, double r)
  : Circle( x, y, r) {} // call base class constructor

double Sphere::area() const
{
  // surface area of sphere
  return 4.0*PI*radius*radius;
}

Driver program:

% cat poly_ex.cpp
#include<iostream>
using namespace std;

#include "circle.h"
#include "cylinder.h"
#include "sphere.h"

int main()
{
   cout << "Some examples using static bindings, e.g., circ1.area() " << endl;

   Circle A(1, 1, 1.0);
   cout << "Circle area " << A.area() << endl;

   Cylinder B(1, 1, 1.0, 2.0);
   cout << "Cylinder area "<< B.area() << endl;

   Sphere C(2, 2, 1.0);
   cout << "Sphere area " << C.area() << endl;

   cout << "Some attempts at polymorphism, e.g., base_ptr->area() " << endl;

   // first create a "base class pointer"

   Circle *base_ptr;

   // point to a Cylinder object

   base_ptr = &B;

   // invoke the area function

   cout << "Cylinder area = " << base_ptr->area() << endl;
 
   // point to a Sphere object

   base_ptr = &C;

   // invoke the area function

   cout << "Sphere area = " << base_ptr->area() << endl;
 
   return 0;
}


Compile and execute:

% g++ poly_ex.cpp circle.cpp cylinder.cpp sphere.cpp
% a.out
Some examples using static bindings, e.g., circ1.area()
Circle area 3.14159
Cylinder area 18.8495
Sphere area 12.5664
Some attempts at polymorphism, e.g., base_ptr->area()
Cylinder area = 18.8495
Sphere area = 12.5664







 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming