i have this function :
void f(const A& a){a.print()};
and a class:
class A {
void print() const{cout<<"a"<<endl;}
}
in the first section i want my code to print b using inheritance without changing the print function.
i tried implementing new class B
which inherits from A
. and my code printed b.
now in the second section i need to change only this line : void f(const A& a)
and my code should print a.
how can i do so?
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class A{
protected:
string a;
public:
A(string a):a(a){}
virtual void print()const{
cout<<"a"<<endl;
}
virtual ~A(){}
};
class B:public A{
//string b;
public:
B(string b):A(b){}
virtual void print()const{
cout<<"b"<<endl;
}
virtual ~B(){}
};
void f(const A& a){
a.print();
}
int main(){
B a=B("b");
f(a);
return 0;
};