polymorp.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. class Base 
  4. {
  5.  public:
  6.    virtual int add(int a, int b) { return(a + b); };
  7.    virtual int sub(int a, int b) { return(a - b); };
  8.    virtual int mult(int a, int b) { return(a * b); };
  9. };
  10. class ShowMath : public Base 
  11. {
  12.    virtual int mult(int a, int b) 
  13.     { 
  14.       cout << a * b << endl; 
  15.       return(a * b); };
  16. };
  17. class PositiveSubt : public Base 
  18. {
  19.    virtual int sub(int a, int b) { return(abs(a - b)); };
  20. };
  21. void main(void)
  22.  {
  23.    Base *poly = new ShowMath;
  24.    cout << poly->add(562, 531) << ' ' << poly->sub(1500, 407) << endl; 
  25.    poly->mult(1093, 1);
  26.    poly = new PositiveSubt;
  27.    cout << poly->add(892, 201) << ' ' << poly->sub(0, 1093) << endl;
  28.    cout << poly->mult(1, 1093);
  29.  }