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 1 - Derive class Two from class One

1. Without virtual function f1.  

cswanson@mega (~/summer05) % cat poly0.cpp
// polymorphism example 0

#include <iostream>
using namespace std;

#include <cmath>

// class declaration for the base class One

class One
{
  protected:
    double num;
  public:
    One(double n);   // constructor
    double f1();     // a member function
    double f2();     // another member function
};

// class implementation for base class One

One::One(double n) : num( n ) {}  // constructor


double One::f1() 
{
  return num/2 ;  // divide num by 2
}

double One::f2()
{
  return  pow(f1(),2) ;  // square the result of f1()
}
 

 // class definition for the derived class

class Two : public One
{
  public:
    Two( double n ); // constructor
    double f1();     // this redefines class One's f1()
};
 

// class implementation for Two

Two::Two( double n ) : One(n) {} // constructor

double Two::f1()
{
  return num/3 ; // divide num by 3
}

// driver program
int main()
{
  One object_1(12);  // object_1 is an object of the base class
  Two object_2(12);  // object_2 is an object of the derived class

  // call f2() using a base class object call

  cout << "The computed value using a base class object call is "
       <<  object_1.f2() << endl;

  // call f2() using a derived class object call

  cout << "The computed value using a derived class object call is "
       << object_2.f2() << endl;

  return 0;
}
% g++ poly0.cpp
% a.out
The computed value using a base class object call is 36
The computed value using a derived class object call is 36
         <--- INCORRECT

2. With viirtual function f1

cswanson@mega (~/summer05) % cat poly0.cpp
// polymorphism example 0

#include <iostream>
using namespace std;

#include <cmath>

// class declaration for the base class One

class One
{
  protected:
    double num;
  public:
    One(double n);   // constructor
    virtual double f1();     // a member function
    double f2();     // another member function
};

// class implementation for base class One

One::One(double n) : num( n ) {}  // constructor


double One::f1() 
{
  return num/2 ;  // divide num by 2
}

double One::f2()
{
  return  pow(f1(),2) ;  // square the result of f1()
}
 

 // class definition for the derived class

class Two : public One
{
  public:
    Two( double n ); // constructor
    double f1();     // this redefines class One's f1()
};
 

// class implementation for Two

Two::Two( double n ) : One(n) {} // constructor

double Two::f1()
{
  return num/3 ; // divide num by 3
}

// driver program
int main()
{
  One object_1(12);  // object_1 is an object of the base class
  Two object_2(12);  // object_2 is an object of the derived class

  // call f2() using a base class object call

  cout << "The computed value using a base class object call is "
       <<  object_1.f2() << endl;

  // call f2() using a derived class object call

  cout << "The computed value using a derived class object call is "
       << object_2.f2() << endl;

  return 0;
}
cswanson@mega (~/summer05) % g++ poly0.cpp
cswanson@mega (~/summer05) % a.out
The computed value using a base class object call is 36
The computed value using a derived class object call is 16
              <--- CORRECT


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