C++ Object Access

cpp Germany
  • 18 years ago

    Hello!


    I have a basic question about OO-Programming and C++:


    asume the following classes:


    class C_tyres {
    public:
    .... *)
    }


    class Ccar {
    public:
    int fuel;
    C
    tyres tyres;
    }


    is there a possibility to access to Ccar-Object's "fuel" from within *) the Ctyres Object ?


    thanks in advance,
    Philipp

  • 18 years ago

    Basically no, at least not as far as I'm aware.


    valkyr

  • 18 years ago

    so the way to access the values is to put them in the methods like this:


    class C_tyres {
    ...
      void drive(int fuel) {
       ....
      }
    }


    ...
    C_tyres tyres;
    tyres.drive(fuel);
    ...


    ?

  • 18 years ago

    Yup, thats the way to do it. The CCar object should be able to call methods of the CTyres object, but not the other way around.


    valkyr

  • 18 years ago

    I have a VERY basic knowledge of C++, so maybe what I'm going to say is useless, but here it goes:  Why don't you set a pointer to the parent class in the child class?  Then this pointer can be directed to point the parent by the child's constructor.

  • 18 years ago

    Yes, I tried it with pointers and in some cases it works, but there is a lot of to take care about, when using pointers. After all I found it doesn't seem to look like OO-Programming when using pointers here. But for my example with the car and the tyres an OO-strict programming would say: the tyres don't need to know anything about the fuel of the car If it needs to, I'll have to use another design of my Objects ... ?


    regards

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup