Inheritance In C++

#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Calculator{
public :
  Calculator() { cout<<"Calculator created "<<endl;}
  ~Calculator() { cout<<"Calculator expired "<<endl;}
  int add(int a,int b) {
  int c;
  c = a+b;
  return c;
  }
  int sub(int a,int b) {
  int c;
  c = a+b;
  return c;
  }

};

class ScCalculator:public Calculator{// public inheritance
//class ScCalculator:private Calculator{ // private inheritance

public :
  ScCalculator() { cout<<"ScCalculator created "<<endl;}
  ~ScCalculator() { cout<<"ScCalculator expired "<<endl;}
  int sq(int a) { return a*a;}
  int power(int a,int b) { 
  int c;
  for(int i=0;i<b;i++)
  c = c*a;
  return c;
  }

};
int main () {
ScCalculator ob;
cout<<"\n45+87 = "<<ob.add(45,87);//not accessible in main if made private
cout<<"\n45+87 = "<<ob.sub(45,87);//not accessible in main if made private
cout<<"\n23^2 = "<<ob.sq(23);
cout<<"\n3^6 = "<<ob.power(3,6);

}

Post a Comment

 
Top