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...
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
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.
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.