void Satellite::Draw(void)
{
cout << "oh hell";
}
void Satellite::Assign(void)
{
cout << "what do you want";
}
==============
void highlight(Displayed* a)
{
a->Draw();
}
void suspend(newTask* b)
{
}
void g (Satellite*p)
{
highlight(p);
suspend(p);
}
int main()
{
Satellite t ;
g(&t);
cout << "Hello world!" << endl;
return 0;
}
==========================
i had expected it to either give an error or give some random value of variable a in Satellite. but even if i put the variable int a in satellite as public and change its value to 12. when i use the function highlight, it prints garbage value.
and it still doesn not print what is given in satellite::draw and prints what is given in display::draw.
Because you are switching pointer types while calling the functions.
Here are the steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void highlight(Displayed* a)
{
a->Draw(); //3. call the draw function - Draw function is not virtual so Displayed::Draw() function is used.
}
void g (Satellite*p)
{
highlight(p); //<<2. Call Highlight function with a DISPLAYED pointer
suspend(p);
}
int main()
{
Satellite t ;
g(&t); //<< 1. OK call function g with a SATELLITE pointer
cout << "Hello world!" << endl;
return 0;
}