Please help on 3 questions

B

Birt

1.
class Engine;

class Car
{
Engine& e;
....
};

How to initialize a reference member, e?


2.

Class Birthdate
{
int month;
int day;
int year;
..
};

class Date
{
int month, day, year;
..
};

Are they the same?


3.
class Date
{
int month;
int day;
int year;
public:
Date(int m, int d, int y);
..
};

class PersonInfo
{
Date birthday;
..
};

PersonInfo:: PersonInfo(int m, int d, int y) : birthday(m, d, y)
{
....
}

"birthday" in class PersonInfo looks like calling the default constructor
that have been replaced by Date(int m, int d, int y);

Could someone explain how this still works?

Thank you very much!
 
J

John Harrison

Birt said:
1.
class Engine;

class Car
{
Engine& e;
...
};

How to initialize a reference member, e?

In a ctor initaliser list

class Car
{
Car(Engine& ee) : e(ee) {}
};
2.

Class Birthdate
{
int month;
int day;
int year;
.
};

class Date
{
int month, day, year;
.
};

Are they the same?

Not under any definition of same that I would use. They are not layout
compitible for a start, in Birthdate month is before day is before year in
memory. That is not necessarily true of Date.
3.
class Date
{
int month;
int day;
int year;
public:
Date(int m, int d, int y);
.
};

class PersonInfo
{
Date birthday;
.
};

PersonInfo:: PersonInfo(int m, int d, int y) : birthday(m, d, y)
{
...
}

"birthday" in class PersonInfo looks like calling the default constructor

that have been replaced by Date(int m, int d, int y);

Could someone explain how this still works?

When the Personinfo constructor is called, the three parameters m, d, and y
are used to call the Date constructor (which also has three parameters.
There is no default constructor for either class and no default constructor
is called.

class PersonInfo
{
Date birthday;

The above line declares a Date object to be part of a Personinfo object. It
does not call any constructor at all. Class member variables are constructed
in constructor initialiser lists, nowhere else.

john
 
J

JKop

Birt posted:
1.
class Engine;

class Car
{
Engine& e;
...
};

How to initialize a reference member, e?

First I want you to consider something. Do you want the engine to be A PART
OF the car, or do you just want to associate an engine with a car.

What you're doing is associating an engine with a car. I would pressume that
this is how you'll use it:

class Engine;

class Car
{
Engine& e;
};

int main(void)
{
Engine V12_4litre;

Car RallyCar(V12_4litre);

RallyCar.e.Start();
}
//After main, the destructor for V12_4litre is called,
//followed by the destructor for RallyCar


Here, all that's happening is that my car object, RallyCar, is keeping a
pointer to an engine, (in the form of a reference), but it hasn't actually
assimilated the engine into itself.

You may also consider the following:


class Engine;

class Car
{
Engine& e;
};



Car* pPorscheTurbo;

int main(void)
{
Engine& FastEngine = *(new Engine);

pPorscheTurbo = new Car(FastEngine);


NB Note how I don't delete FastEngine
}


Car::Car(Engine& in_engine) : e(in_engine)
{

}

Car::~Car(void)
{
delete &e;

//This is where FastEngine gets deleted!
}



In the above, the car object is responsible for freeing the memory allocated
for the engine. All the user does is make and engine and pass it onto the
Car.


Another alternative would be to actually assimilate the engine into the car,
ie. there is an actual engine inside the car.

class Engine;

class Car
{
Engine e; //Note how this is an actual engine object!
};


Car::Car(Engine& in_engine)
{
e = in_engine;
}


Here, the car actually makes a copy of the engine and stores it inside
itself. You could use it as so:


Car* pMerc;


void Chocolate(void)
{
Engine& engine = *(new Engine);

pMerc = new Car(engine);

delete &engine;
}

int main(void)
{
Chocolate();

//Although the original Engine object has been deleted,
//the car object has retained a copy of it. Therefore:

pMerc->e.CheckOil();

//*will* work! And the destructor for e is called automatically
//upon destruction of the car!
}


2.

Class Birthdate
{
int month;
int day;
int year;
.
};

class Date
{
int month, day, year;
.
};

Are they the same?


int month, day, year;


is simply an abbreviation of:


int month;
int day;
int year;

No more, no less. So Yes, they are the same.

(What's the story with the fullstop before the closing brace?)
3.
class Date
{
int month;
int day;
int year;
public:
Date(int m, int d, int y);
.
};

class PersonInfo
{
Date birthday;
.
};

PersonInfo:: PersonInfo(int m, int d, int y) : birthday(m, d, y)
{
...
}

"birthday" in class PersonInfo looks like calling the default constructor
that have been replaced by Date(int m, int d, int y);

Could someone explain how this still works?

First of all, you don't have a default constructor for "Date". You could
consider the following:

Date::Date(int m = 1, int d = 1, int y = 1950) {};

Once you supply any constructor at all, the default one disappears.

Read the following sentence slowly: You are correct that the object
"birthday" of the class "Date", which is an object of an object of the class
"PersonInfo", that it's constructor is called with the parameters passed to
the constructor of PersonInfo. You have achieved this perfectly.
That is, the constructor for birthday is supplied with the data given
to the constructor of a PersonInfo object.



Hope all that helps.

-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top