classes again


class newTask
{
public:

newTask();
void Assign(void);
void Draw(void);
virtual ~newTask();
protected:
private:
int a ;
};

void newTask::Assign(void)
{
cout <<"Enter the value :";
cin >> this->a ;
}


class Displayed
{
public:

Displayed();
void Assign(void);
void Draw(void);
virtual ~Displayed();
protected:
private:
int a;
};

void Displayed::Assign(void)
{
cout << endl <<"Enter the no:";
cin >> this->a;
}

void Displayed::Draw(void)
{
cout << endl << "The ;aj;fjsno is :" << this->a;
}


class Satellite : public newTask , public Displayed
{
public:

Satellite();
void Draw(void);
void Assign(void);
virtual ~Satellite();
protected:
private:
int a;
};


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.

WHY ???
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;
}



EDITED: for typos
Last edited on
Topic archived. No new replies allowed.