Hi everyone,

I'm trying to learn C++ and about pointers and whatnot and seem to be a bit stuck... one of the exercises we were given involves passing an object (a Driver object) by reference to another object (a Manufacturer), which stores it. The Manufacturer object can then "employ" and "release" a Driver, but I can't seem to get my head around the way the pointers and that work :S

Any help would be much appreciated, and thank you in advance!!!

#include <iostream>
#include <string>

using namespace std;

class Driver
{
    public :
        Driver(string fn, string sn) : firstname(fn), surname(sn)
        {
        }
        ~Driver()
        {
        }

    private :
        string firstname, surname;
};

class Manufacturer
{
    public :
        Manufacturer(string n) : name(n)
        {
        }
        ~Manufacturer()
        {
        }

        void Employ(Driver d)
        {
            if(driver1 == 0)
                driver1 = d;
            else if (driver2 == 0)
                driver2 = d;
        }
        void Release(Driver d)
        {
            if(driver1 == d)
                driver1 = 0;
            else if (driver2 == d)
                driver2 = 0;
        }
        void WhatIsYourTeam()
        {
        }

    private :
        string name;
        Driver driver1;
        Driver driver2;
};



int main()
{
    Driver M_Schumacher("Michael", "Schumacher");
    Driver R_Schumacher("Ralph", "Schumacher");
    Driver J_Button("Jenson", "Button");
    Driver E_Irvine("Eddie", "Irvine");
    Manufacturer ferrari("Ferrari");
    Manufacturer williams("Williams");
    ferrari.Employ(&M_Schumacher);
    ferrari.Employ(&E_Irvine);
    williams.Employ(&R_Schumacher);
    williams.Employ(&J_Button);
    ferrari.WhatIsYourTeam();
    williams.WhatIsYourTeam();
    // change teams
    ferrari.Release(&E_Irvine);
    williams.Release(&J_Button);
    ferrari.Employ(&J_Button);
    williams.Employ(&E_Irvine);
    ferrari.WhatIsYourTeam();
    williams.WhatIsYourTeam();

    return 0;
}

on lines 64 through 67 you are passing the drivers address to the functions but you defined the functions as taking a driver by value. Here is the different ways you can pass an argument to a function.

#include <iostream>

using namespace std;

int PassByValue(int value)
{
    return value += 5;
}

int PassByReference(int & value)  // & is used here to signify a reference
{
    return value += 5;
}

int PassByPointer(int * value)
{
    return *value += 5;
}

int main()
{
    int value = 5, returner;
    cout << value << endl;
    returner = PassByValue(value)
    cout << After PassByValue() value is: " << value << "\treturner is: " << returner << endl;
    returner = PassByReference(value);
    cout << After PassByReference() value is: " << value << "\treturner is: " << returner << endl;
    returner = PassByPointer(&value); // & is used here to pass an address
    cout << After PassByPointer() value is: " << value << "\treturner is: " << returner << endl;
    return 0;
}

Yeah what I was trying to do is -

since your passing the Driver objects to the manufacturer by reference, and the Manufacturer class is then storing the Driver object, for the WhatIsMyTeam function, when I try to print the Driver's names it keeps saying it cannot access private members of the Driver class :S

void WhatIsYourTeam()
{
	cout << "Driver 1 : " << driver1.firstname << " " << driver1.surname << endl;
	cout << "Driver 2 : " << driver2.firstname << " " << driver2.surname << endl;
}

Is there some way that you can access those (since its passing by reference) or is it impossible?

void Manufacturer::WhatIsYourTeam()
{
    // ...
}

Well you can make an acessor function in your driver class that will return the name like

class Driver
{
public:
    //...
    std::string GetFirstname() const { return firstname; }
    std::string GetSurname() const { return surname; }
    //...
private:
    //...
};

class Manufacturer
{
    //...
};

void Manufacturer::WhatIsYourTeam
{
    std::cout << "Driver 1: " << driver1.GetFirstname() << " " << driver1.GetSurname();
    std::cout << "Driver 2: " << driver2.GetFirstname() << " " << driver2.GetSurname();
}

Thanks for the help so far but still not getting anywhere :(

What I meant was, in the main program it passes two driver objects by reference into a Manufacturer object, and assigned to the two Driver objects in it :=

main program....

ferrari.Employ(&M_Schumacher);
ferrari.Employ(&E_Irvine);

Manufacturer class....

void Employ(const Driver* d)
{
     if(driver1 == 0)
         (*driver1) = *d;
     else if (driver2 == 0)
         (*driver2) = *d
}

private :
    Driver* driver1;
    Driver* driver2;

so I'm guessing the Driver objects inside Manufacturer are pointing at the objects created in the main program, so when I want to print out the details of each driver using the WhatIsMyTeam method from the Manufacturer class how would I go about it?

void Manufacturer::WhatIsYourTeam()
{ 
	cout << "Driver 1 : " << this->driver1->firstname << " " << this->driver1->surname << endl;
	cout << "Driver 2 : " << this->driver2->firstname << " " << this->driver2->surname << endl;
}

Just gives me constant errors!

The way you have your driver class setup right now you would need get functions like I previously posted. The reason for this is that the name variables in driver are private. The only way to access private members is either in a function of the class or to use access-or functions. this might also help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.