write a c++ program to demonstrate hierachical inheritance to get square and cube of a number. Base class must calculate square of a number whilst Base class2 caculates cube of a numberr
#include <iostream>
using namespace std;
class Base {
public:
  Base(int n) : number(n) {}
  virtual int Calculate() { return number*number; }
protected:
  int number;
};
class Base2 : public Base {
public:
  Base2(int n) : Base(n) {}
  int Calculate() { return Base::Calculate() * number; }
};
int main() {
  Base *b = new Base(3);
  cout << "Base: " << b->Calculate() << endl;
  delete b;
  b = new Base2(3);
  cout << "Base2: " << b->Calculate() << endl;
  delete b;
  return 0;
}
Comments
Leave a comment