inheritance and composition

G

Gary Wessle

class Client {
public:
Client();
virtual void login()=0;
};
Client::Client(){}

class Play : public Client {
public:
Play();
void login();
};
Play::play() Client(){}
void Play::login(){ /*...*/};

class Work : public Client {
public:
Work();
void login();
};
Work::Work() Client(){}
void Work::login(){ /*...*/};

class Conn_task {
protected:
Work wo;
Play pl;
void est_con();
public:
Conn_task();
};
Conn_task::Conn_task(){}
void Conn_task::est_con(){
if( some_bool_value) wo.login(); else pl.login();
}

class Spac_task : public Conn_task {
public:
void preform();
};
void Spac_task::preform(){
est_con();
}

my question is: how can I move the definition of login to the
real-estate of Client and be sure Client knows which class type
called it when est_con() is called?

thanks
 
A

amparikh

Gary said:
class Client {
public:
Client();
virtual void login()=0;
};
Client::Client(){}

class Play : public Client {
public:
Play();
void login();
};
Play::play() Client(){}
void Play::login(){ /*...*/};

class Work : public Client {
public:
Work();
void login();
};
Work::Work() Client(){}
void Work::login(){ /*...*/};

class Conn_task {
protected:
Work wo;
Play pl;
void est_con();
public:
Conn_task();
};
Conn_task::Conn_task(){}
void Conn_task::est_con(){
if( some_bool_value) wo.login(); else pl.login();
}

class Spac_task : public Conn_task {
public:
void preform();
};
void Spac_task::preform(){
est_con();
}

my question is: how can I move the definition of login to the
real-estate of Client and be sure Client knows which class type
called it when est_con() is called?

thanks

Use Virtual inheritance for your Client class as a first because right
know you have 2 instances of client floating around. Basically when you
use virtual inheritance, the most derived class needs to call the
constructor of the virtual base class. You could use a protected member
variable in the Client class that could be set by the resp child
classes in their constructor and that could be used whenever login
function in the client is called.
There could be other solutions but this what popped up in my mind when
I saw the minimal code that was posted.
 
R

Radu

class Client {
public:
Client();
virtual void login()=0;};Client::Client(){}

class Play : public Client {
public:
Play();
void login();};Play::play() Client(){}
void Play::login(){ /*...*/};

class Work : public Client {
public:
Work();
void login();};Work::Work() Client(){}
void Work::login(){ /*...*/};

class Conn_task {
protected:
Work wo;
Play pl;
void est_con();
public:
Conn_task();};Conn_task::Conn_task(){}
void Conn_task::est_con(){
if( some_bool_value) wo.login(); else pl.login();

}class Spac_task : public Conn_task {
public:
void preform();};void Spac_task::preform(){
est_con();

}my question is: how can I move the definition of login to the
real-estate of Client and be sure Client knows which class type
called it when est_con() is called?

thanks

you can use the Visitor pattern

HTH
Radu
 
I

implicit_differentiation

Use Virtual inheritance for your Client class as a first because right
know you have 2 instances of client floating around. Basically when you
use virtual inheritance, the most derived class needs to call the
constructor of the virtual base class. You could use a protected member
variable in the Client class that could be set by the resp child
classes in their constructor and that could be used whenever login
function in the client is called.
There could be other solutions but this what popped up in my mind when
I saw the minimal code that was posted.

HUH ???????????????????????????????????????????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top