Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A tricky OOP problem I never got my head around

Say I have two .cpp files: oranges.cpp and basket.cpp. They have the classes orange and basket, respectively. My main program generates many baskets which in turn generate many oranges. So basically, main will have many objects of Baskets; and baskets will have many objects of Oranges. If I have a function in orange that needs to know the color of my basket, how would I go about finding the color of the basket?

orangle.cpp

class oranges
{
    void whichColorBasket()
    {
        // get the color of the basket the orange is in...?
    }
}

basket.cpp

class basket
{
    int color;

    void basket()
    {
        for(int i = 0; i < 10; i++)              
            oranges *o = new oranges;
    }
}

I know my syntax may not be perfect but how would I access the datamember of basket from a function in orange (orange is an object created by basket).

Sending the color a parameter isn't an option as there are too many oranges and the color of the basket may change during runtime.

I read somewhere that static functions would do the trick, but they only work if they are in the same .cpp file.

So, what do I do?

like image 379
sleeping.ninja Avatar asked May 19 '11 14:05

sleeping.ninja


People also ask

Why is OOP so hard?

As a beginner, OOP is also more difficult to read for several non-code related reasons. First, it's near impossible to understand why a piece of code exists if you're unfamiliar with the domain being modeled with classes. Secondly, OOP is a craft and is inherently opinionated.

What OOP means?

Object-oriented programming (OOP) is a style of programming characterized by the identification of classes of objects closely linked with the methods (functions) with which they are associated. It also includes ideas of inheritance of attributes and methods.

When should you not use OOP?

These include: design patterns, abstraction, encapsulation, modularity, polymorphism, and inheritance. When not to use OOP: Putting square pegs in round holes: Don't wrap everything in classes when they don't need to be. Sometimes there is no need and the extra overhead just makes your code slower and more complex.

Is object-oriented programming dying?

Object-oriented programming has fulfilled many of its promises. Software systems today are longer-lived and more amenable to change and extension than ever. Nevertheless we observe that object orientation is slowly dying, with the introduction of ever more complex and heterogeneous systems.


2 Answers

Static functions are almost certainly not the answer here.

You would probably need to pass a reference to the "parent" Basket to the Oranges object, which it can then interrogate to find the colour.

For example:

class Basket
{
public:
    void Basket() {
        Oranges *o = new Oranges(*this);
    }

    color getColor() const { ... }
};


class Oranges
{
private:
    const Basket &myBasket;

public:
    Oranges(const Basket &b) : myBasket(b) {} // Store reference

    color getColor() const { return myBasket.getColor(); }  // Interrogate my basket

};

Whether you use a pointer or a reference will depend on whether the Oranges can ever move between Baskets.

like image 81
Oliver Charlesworth Avatar answered Nov 07 '22 15:11

Oliver Charlesworth


you'll need a reference in orange pointing to the basket the orange is in

class oranges{
    basket* b;
    int whichColorBasket() {
        return b->color;
    }
}


class basket{
     int color;

     void basket(){
         for(int i=0;i<10;i++){         
              oranges *o=new oranges;
              o->b = &this;
         }
     } 
}

I'm ignoring memory leaks here

like image 44
ratchet freak Avatar answered Nov 07 '22 17:11

ratchet freak