myCar.acUnit.SwitchOn();

G

gccntn

I know the subject of the post is a bit cryptic, but here is what I'd
like my code to look like:

int main()
{
Car myCar;
myCar.Start();
myCar.acUnit.SwitchOn(); // invoke TurnOn(), method of class
AC, which is a member of class Car
myCar.acUnit.SetTemp(75); // invoking SetTemp(), method of class
AC, which is a member of class Car
...etc...
myCar.Stop();
return 0;
}

How should I declare classes Car and AC?

The way I see things, this is what the declarations would look like:

class AC
{
public:
SwitchOn();
SwitchOff();
SetTemp();
GetTemp();
private:
...etc...
}

class Car
{
public:
Start();
Stop();
...etc...
<access_specifier>:
AC acUnit;
Engine engine;
...etc...
}

What should <access_specifier> be so that I can invoke AC's methods
from a Car as outlined in the code above?

Thanks.
 
J

Jim Langston

gccntn said:
I know the subject of the post is a bit cryptic, but here is what I'd
like my code to look like:

int main()
{
Car myCar;
myCar.Start();
myCar.acUnit.SwitchOn(); // invoke TurnOn(), method of class
AC, which is a member of class Car
myCar.acUnit.SetTemp(75); // invoking SetTemp(), method of class
AC, which is a member of class Car
...etc...
myCar.Stop();
return 0;
}

How should I declare classes Car and AC?

The way I see things, this is what the declarations would look like:

class AC
{
public:
SwitchOn();
SwitchOff();
SetTemp();
GetTemp();
private:
...etc...
}

class Car
{
public:
Start();
Stop();
...etc...
<access_specifier>:
AC acUnit;
Engine engine;
...etc...
}

What should <access_specifier> be so that I can invoke AC's methods
from a Car as outlined in the code above?

public.
 
G

gccntn

public.- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -


What if I wanted to grant Car's methods access to AC's methods, BUT I
did NOT want users of the Car class to have access to all the methods
in AC?

For instance, while I would a Car to initialize the AC:

bool Car::Start()
{
acUnit.Initialize(); // I WANT TO ALLOW THIS
...etc...
}


I would NOT want someone to initialize acUnit via the Car class:

int main()
{
Car myCar;
myCar.acUnit.Initialize(); // NO!!!
...etc...
}


Any suggestions? Thanks again for your help.
 
G

Guest

What if I wanted to grant Car's methods access to AC's methods, BUT I
did NOT want users of the Car class to have access to all the methods
in AC?

For instance, while I would a Car to initialize the AC:

bool Car::Start()
{
acUnit.Initialize(); // I WANT TO ALLOW THIS
...etc...
}


I would NOT want someone to initialize acUnit via the Car class:

int main()
{
Car myCar;
myCar.acUnit.Initialize(); // NO!!!
...etc...
}


Any suggestions? Thanks again for your help.

Make it a private member (or use private inheritance) and provide access
methods for the things you want to allow.
 
J

Jim Langston

gccntn said:
What if I wanted to grant Car's methods access to AC's methods, BUT I
did NOT want users of the Car class to have access to all the methods
in AC?

For instance, while I would a Car to initialize the AC:

bool Car::Start()
{
acUnit.Initialize(); // I WANT TO ALLOW THIS
...etc...
}

I would NOT want someone to initialize acUnit via the Car class:

int main()
{
Car myCar;
myCar.acUnit.Initialize(); // NO!!!
...etc...
}

Any suggestions? Thanks again for your help.

Encapsulation.

class Car
{
public:
void SwitchOnAC() { acUnit.switchon(); }
private:
AC acUnit;
};
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top