Classes w/in Classes- Need Help

Started by
13 comments, last by Leanna 19 years, 11 months ago
Ok, your laptop class (updated from Cocalus) has 3 PRIVATE members

CPU myCPU;
Screen myScreen;
Modem myModem;

to set these, the constructor of Laptop will need to have them passed in

class Laptop{private:   CPU myCPU;   Screen myScreen;   Modem myModem;public:   Laptop(CPU cpu, Screen screen, Modem modem);/* Other members here */};


NOTE the parameters to your Laptop constructor, there are three.
cpu which is a type of CPU
screen which is a type of Screen
modem whcih is a type of Modem

(Not the case differences)

now, In your main function

//Create a CPUCPU cpu(0.0,0.0,0.0); //I'm not worrying about the actual numbers//Create a ScreenScreen screen(0.0,0.0,0.0);//Create a modemModem modem(0.0,0.0,0.0);//Now, create your laptop instance and tell it about the 3 created instances aboveLaptop myLaptop(cpu, screen, modem);


Now you have an instance of Laptop that has been constructed properly.

Inside your Laptop constructor you can now access the modem, screen and cpu costs, weights and speed etc..


Laptop::Laptop(CPU cpu, Screen screen, Modem modem){    //etc..    myCPU = cpu;    myScreen = screen;    myModem = modem;    //etc. obvisouly you'll actually want to use the results    //of these i.e.    double total_cost = myCPU.GetCost() + myScreen.GetCost() + myModem.GetCost();}


Hope this helps.

Keep up with this example as it also has more to learn from it that your not using.

Two things to look out for later are
1/ Already mentioned, passing my const reference in the constructors of Laptop.
2/ Using inheritence or polymorphism for Modem ,Screen, CPU. Maybe have a class called Component instead and Modem, screen, cpu can be different instances of Component or can be subclasses of component

Keep posting and asking until you get it, don't give up



[edited by - gommo on May 29, 2004 10:15:13 PM]
------------------------------------------------------
50% of me is a huge nerd! How about you?
Advertisement
First nowhere in your Compute function is GetCost or the like called.

void Laptop::Compute()
{
total_cost = Modem.cost + CPU.cost + Screen.cost;
total_weight = Modem.weight + CPU.weight + Screen.weight;
}

Ok the way to call a member function is
instance.functionname();
Modem is a typename not an instance
and cost is a varible name not a function name.

Ok so we're in a member function of a Laptop and we want to know the cost of the Laptop's modem. So what's the the name of the Laptop's modem. Let's check the Laptop definition.

class Laptop
{
private:
CPU myCPU;
Screen myScreen;
Modem myModem;

public:
double total_cost;
double total_weight;
Laptop(CPU CPU, Screen Screen, Modem Modem);
void Compute();
void Showspecs();
};

So a Laptop has an instance of a Modem called myModem. So now now when ask a Laptop to compute() (which you don't do which is another bug, but that's for latter). The Laptop is thinking
"To find my total_cost I need to get the cost of myCPU, myScreen,and myModem. So I need to ask each of them for their cost and add the costs together." Hopefully that's enough of a hint.


In my opionion it looks like your either trying to learn several different conepts at once (extremely hard at begining level), or need help geting a grasp of program flow (seems easy once you get it, but a lot of beginers have trouble getting a hold of it). I don't know how much time you have to spend on this, but I can install ICQ, AIM or what not and give a crash course on programming if you want.

Also a good place to get the basics of c++ is here. http://cplus.about.com/library/blcplustut.htm
I would suggest going through them in order and make sure you understand the one your on before your continue to the next.
I personally didn't go through them but they look pretty good.


[edited by - Cocalus on May 29, 2004 10:26:07 PM]
It''s almost working!!!
The last post was really helpful- I understand now that the instance needs to go in front of the function name. I''m sorry I''m such a slow learner- I guess I don''t catch on to your guys'' hints very fast.
Anyway, it says I have 1 error left, which is in this line:

Screen scr1, scr2(1.3, 10.3, 306), scr3(1.8, 14.1, 520);

and it says that it is a "parse error before numeric constant." I copied this line exactly from my book, so it should work!
I''m sorry to be such a pain in the rear, but are there any suggestions?

Thanks again! I''m going to go check out the websites mentioned (Thanks Cocalus!) and try to get a better handle on the subject- and C++ in general. (Not tonite, though, I need a break!)

Everyone''s help has been great- thank you to all. Only a little ways to go!

Thank you,
Leanna
We''ll you seem to have found an odd bug (which luckily for you my system also has). Your right it, in that it should work. But aparently iostream is doing something odd to make it so you can''t have a varible name of scr#, probally a strange macro or define. The solution is to rename them to some thing else IE
scr1 becomes sc1

Do note that wierd bugs from the compiler and standard library are extremely rare (this is the second I''ve run across one. the first was really, really evil). So don''t start assuming the compiler or library is the cause of your codes problems it rarely (and I mean RARELY) is.
Thanks for all of that. I changed it to accomodate for the bug and it works now.
Unfortunately, I don''thave any more problems with it- so now we can all go to bed!
Thank you for all of your help- I don''t know what I would do without people like you (I''d be failing!) and can''t thank you enough. I''ll be working on getting a better understanding of it- but keep an eye out for my future posts- I''m sure there''ll be one within the next few days. And being that the subject matter is only getting harder- it will probably be worse.
Thank you for your patience!
Have a great weekend!!
Leanna

This topic is closed to new replies.

Advertisement