Polimorfizm :)
#include <iostream>
class Base
{
protected:
int Value;
public:
void SetValue(int NewValue)
{
Value=NewValue;
}
virtual int GetValue()
{
return Value;
}
};
class Div_A : public Base
{
int GetValue()
{
return Value+1;
}
void Write()
{
std::cout<<"Hallo world!\n";
}
};
class Div_B : public Base
{
int GetValue()
{
return Value+Value;
}
};
class Div_C : public Base
{
int GetValue()
{
return Value*Value;
}
};
int main()
{
Base* Tab[3];
Tab[0] = new Div_A;
Tab[1] = new Div_B;
Tab[2] = new Div_C;
(*Tab[0]).SetValue(10);
Tab[1]->SetValue(10);
Tab[2]->SetValue(10);
for(int i=0; i<3; ++i)
{
std::cout<<Tab[i]->GetValue()<<' ';
}
for(int i=0; i<3; ++i) delete Tab[i];
}
|
run
| edit
| history
| help
|
0
|
|
|