473,698 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help on 3 questions

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!





Jul 22 '05 #1
3 1234

"Birt" <bi***@imp.co m> wrote in message
news:GZ******** **********@bgtn sc05-news.ops.worldn et.att.net...
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
Jul 22 '05 #2
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_4l itre);

RallyCar.e.Star t();
}
//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
Jul 22 '05 #3
Sounds like homework to me.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2372
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta http-equiv="Content-Type" content="text/html;
1
1425
by: Mark | last post by:
I must say that I'm quite the newb with XML/XSLT. I kind of stubmled upon using it for a report I'm trying to make. If someone would be so kind as to help out with the following I would be grateful. I have the following XML output from SQL server 2000 and would like to transform it such that teh questions are grouped by QID. ---------------------------------------------------------------- <Personal> <MCRegID>946</MCRegID>...
15
5875
by: Anton Gavrilov | last post by:
Hi all, I seek your advice on where to start if I want to write a compiler for a toy C-like language I invented (or, rather, am in the process of inventing). Yes, yes, I know I'm crazy and the very idea is brain-damaged and all that. Chances are I will lose interest long before I reach break-even, but it doesn't hurt trying, does it? I'm a C addict reluctant to switch to C++, partly because I can't fully grok (and accept) the OOP...
28
1521
by: Jon Skeet [C# MVP] | last post by:
A while ago (too long ago) I agreed to start up a FAQ for this newsgroup. Today, I've finally got it into a sufficiently reasonable form that it's worth putting up. It's at http://www.pobox.com/~skeet/csharp/faq/ Please feel free to criticise, suggest more questions (preferrably with answers!) etc. Also feel *more* than free to point people at it when
1
9634
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
1
54514
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and we instruct our experts to ignore any such PMs completely Be sure to give the version of Access that you are working with and the Platform and OS if applicable.
0
2153
MMcCarthy
by: MMcCarthy | last post by:
I've decided to replace the announcement with a sticky thread. Again I would like to welcome all experts to this forum. We are gaining some great experts who are contributing significantly to the growth of this forum and I know based on posts both here and in the lounges that those posting questions are in the main very pleased and satisfied with the help they receive. There are just a couple of points I would like to make which I feel...
3
1637
by: iKiLL | last post by:
Hi all I am building an Windows Mobile 5 forms control in C#, for a Windows Mobile 5 application. I am using CF2.0 and SQL Mobile 2005. The control is a Questions and answer control.
4
1382
seshu
by: seshu | last post by:
Hi everybody Presently Iam write three select statements with diff where and limt conditions but the select stmt in them is same insted of that can i write only one direct stmt and the queries are like this 1)select question,opt1,opt2,opt que_level from questions where que_level=1 limit 4-------here i want only 4 questions whose level is 1 among 100 questions 2)select question,opt1,opt2,opt que_level from questions...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.