Polymorphism 


Contents of this section:

  1. Pointer to derived objects
  2. Virtual functions and polymorphism

1. Pointer to derived objects

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Poly/PtrDerived.cpp
// Purpose: Using base pointers on derived class objects.
#include <iostream>
#include <cstring>
using namespace std;

class B_class {
  char author[80];
public:
  void put_author(char *s) { strcpy(author, s); }
  void show_author() { cout << author << "\n"; }
} ;

class D_class : public B_class {
  char title[80];
public:
  void put_title(char *num) {
    strcpy(title, num);
  }
  void show_title() {
    cout << "Title: ";
    cout <<  title << "\n";
  }
};

int main()
{
  B_class *p;
  B_class B_ob;

  D_class *dp;
  D_class D_ob;

  p = &B_ob;  // address of base

  // Access B_class via pointer.
  p->put_author("Tom Clancy");

  // Access D_class via base pointer.
  p = &D_ob;
  p->put_author("William Shakespeare");

  // Show that each author went into proper object.
  B_ob.show_author();
  D_ob.show_author();
  cout << "\n";

  /* Since put_title() and show_title() are not part
     of the base class, they are not accessible via
     the base pointer p and must be accessed either
     directly, or, as shown here, through a pointer to the
     derived type.
  */
  dp = &D_ob;
  dp->put_title("The Tempest");
  p->show_author(); // either p or dp can be used here.
  dp->show_title( );

  return 0;
}

    Running session:

@9:35am merc-hu[76]% CC -LANG:std PtrDerived.cpp -o PtrDerived
@9:35am merc-hu[77]% PtrDerived
Tom Clancy
William Shakespeare

William Shakespeare
Title: The Tempest
@9:35am merc-hu[78]% 

2. Virtual functions and polymorphism

Code listing:

// File:    ~ftp/pub/class/cplusplus/Poly/VirtualPoly.cpp
// Purpose: Vitual function and polymorphism

#include <iostream>
using namespace std;

class figure {
protected:
  double x, y;
public:
  void set_dim(double i, double j=0) {
    x = i;
    y = j;
  }
  virtual void show_area() {
    cout << "No area computation defined ";
    cout << "for this class.\n";
  }
} ;

class triangle : public figure {
  public:
    void show_area() {
      cout << "Triangle with height ";
      cout << x << " and base " << y;
      cout << " has an area of ";
      cout << x * 0.5 * y << ".\n";
    }
};

class square : public figure {
  public:
    void show_area() {
      cout << "Square with dimensions ";
      cout << x << "x" << y;
      cout << " has an area of ";
      cout << x *  y << ".\n";
    }
};
class circle : public figure {
  public:
    void show_area() {
      cout << "Circle with radius ";
      cout << x;
      cout << " has an area of ";
      cout << 3.14 * x * x << ".\n";
    }
} ;

int main()
{
  figure *p; // create a pointer to base type

  triangle t; // create objects of derived types
  square s;
  circle c;

  p = &t;
  p->set_dim(10.0, 5.0);
  p->show_area();

  p = &s;
  p->set_dim(10.0, 5.0);
  p->show_area();

  p = &c;
  p->set_dim(9.0);
  p->show_area();

  return 0;
}

Running session:

@9:41am merc-hu[82]% CC -LANG:std VirtualPoly.cpp -o VirtualPoly
@9:41am merc-hu[83]% VirtualPoly
Triangle with height 10 and base 5 has an area of 25.
Square with dimensions 10x5 has an area of 50.
Circle with radius 9 has an area of 254.34.
@9:41am merc-hu[84]% 


Last modified: Friday, 21-Aug-2020 15:28:14 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page