How to use class object as private member in another class?

hi, could someone explain how to implement constructors, copy constructors, and overloaded assignment operators for a class that has another class object as a private member?

for example, in my Vehicle class, one of the private members is Person owner, and Person is a different class..

for the default constructor, i don't need to do anything with the owner right?
for non-default constructors, would i do something like:
owner = Person(name);

for copy constructors, i am not sure. is it like:
owner = theObject.owner;
and same problem for overloaded assignment operators, i am not sure what to do with the "owner" member..
have a read through http://cplusplus.com/doc/tutorial/ on the various topics you just mentioned (it's a very short concise tutorial), and have a go at writing up some code then posting here with specific questions and some code samples in [c ode] ..blocks.. [/c ode] because as it is, I'm unsure whether you constructor actually passes name in arguments, or whether you are just trying to figure out how to initialize it... or what...
Last edited on
for example, if i have:

1
2
3
4
5
6
7
8
class Car{
      public:
                Car(const Car& object);
                Car& operator=(const Car& rightSide);
      private:
                 string manufacturer
                 Person owner;
};

and

1
2
3
4
5
6
class Person{
      public:  
                //code here
      private:
                string name;
};


I don't know how to put the implementation of the copy constructors and overloaded assignment operators for the Car class.. because there is a Person owner private variable, I don't know what to do with this for copy constructor and overloaded assignment operator in Car class.. I need help fast please
Last edited on
You would just create assignment/copy stuff for Person, then just call them in Car's copy/assignment functions.
oh, so it's exactly like inheritance then?
so would i do

1
2
Car::Car(const Car& object) : Person(object),
{   }


and

1
2
3
4
Car::Car& operator=(const Car& rightSide)
{
      Person::operator=(rightSide);
}
Last edited on
That looks fine, except the second part you would probably want owner. instead of Person::
why would i put owner.? wouldn't Person work if it only has one private variable "name"?
umm..i got a lot of errors and changed it to this and it compiled.. is this correct?

1
2
Car::Car(const Car& object) : owner(object.owner),
{   }


1
2
3
4
Car::Car& operator=(const Car& rightSide)
{
      owner.operator=(rightSide.owner);
}
The reason Person:: doesn't work is because that's trying to call a static function of the Person class, rather than call the assignment on the instance of Person in the Car (called owner).

Also, I didn't notice before, but it looks like your Person constructor takes a string for the name as a parameter, so you don't want to be passing object.owner; instead you want to pass a name string.
owner = rightSide.owner;

is preferred to

owner.operator=( rightSide.owner );

although for classes that have "normal" (member-wise) copy semantics, such as this one, you don't even need
to write an assignment operator or a copy constructor... the default member-wise versions provided by the
compiler will suffice.
Maybe my question is a little off topic, but why the Car class contains a Person object? Shouldn't it reference a Person object instead?

If it's just a poorly chosen example then feel free to ignore me:)
Topic archived. No new replies allowed.