Archived

This topic is now archived and is closed to further replies.

Classes w/in Classes- Need Help

This topic is 4947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am having trouble getting the kinks out of my program. I am new to the whole class thing, and I can''t figure out what''s wrong! I have about 17 errors right now. In my main, there are lap1,lap2,lap3,lap4,& lap5 and my compiler says that there is no mathcing function for call. Also, in my compute function it says that there are parse errors, as well as in my showspecs function. Both of these seem to be when I am trying to call the cost & weight from my classes. Any help would be greatly appreciated! Thank you, Leanna #include <cstdlib> #include <iostream> using namespace std; class CPU { private: double weight, size, cost; public: CPU (double w, double s, double c); double GetCost() {return cost;} double GetWeight() {return weight;} double GetSize() {return size;} }; class Screen { private: double weight, size, cost; public: Screen (double w, double s, double c); double GetCost(){return cost;} double GetWeight(){return weight;} double GetSize(){return size;} }; class Modem { private: double weight, speed, cost; public: Modem (double w, double s, double c); double GetCost(){return cost;} double GetWeight(){return weight;} double GetSpeed(){return speed;} }; class Laptop { public: double total_cost; double total_weight; Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem()); void Compute(); void Showspecs(); }; CPU::CPU(double w= 2.266, double s= 300, double c= 1258): weight(w), size(s), cost(c){} Screen::Screen(double w= 3, double s= 13.3, double c= 215): weight(w), size(s), cost(c){} Modem::Modem(double w= .05, double s= 33600, double c= 119): weight(w), speed(s), cost(c){} Laptop::Laptop(CPU::CPU(), Screen::Screen(), Modem::Modem()){} void Laptop::Compute() { total_cost = Modem.cost + CPU.cost + Screen.cost; total_weight = Modem.weight + CPU.weight + Screen.weight; } void Laptop::Showspecs() { cout << "This computer has a " << CPU.size << "Mhz CPU \n"; cout << "with a" << Screen.size << "display and a" << Modem.size << "\n"; cout << "bps modem. It costs" << total_cost << "and weighs" << total_weight << "\n"; cout << "pounds. \n"; system ("pause"); } int main() { CPU cpu1,cpu2(3.4, 233, 945),cpu3(2.9, 350, 1295); Screen scr1,scr2(1.3, 10.3, 306), scr3(1.8, 14.1, 520); Modem mdm1, mdm2(.026, 56000, 160); Laptop lap1, lap2(cpu1, scr3, mdm2), lap3(cpu3, scr2), lap4(cpu2, scr2, mdm2), lap5(cpu3, scr3, mdm2); lap1.Showspecs(); lap2.Showspecs(); lap3.Showspecs(); lap4.Showspecs(); lap5.Showspecs(); return 0; }

Share this post


Link to post
Share on other sites
Your problem is the difference between classes and instances of classes.
A class is a type, it has no data storage (well it can with static but you should ignore that stuff for now).

int a; //a is an instance of int.
CPU cpu1; //cpu1 is an instance of CPU.


first couple things
1. The laptop constructor is really messed up.
2. Your laptop class has no instances of cpu, modem or screen in it. Which is not what you want.

Did you get this example from a book or something. I only say this because your using more advanced syntax but are making some basic mistakes.



[edited by - Cocalus on May 29, 2004 4:21:32 PM]

[edited by - Cocalus on May 29, 2004 4:23:49 PM]

Share this post


Link to post
Share on other sites
These lines are a source of many errors:
Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem());
Laptop::Laptop(CPU::CPU(), Screen::Screen(), Modem::Modem()){}
They should be something like this:
Laptop (CPU c, Screen s, Modem m);
Laptop::Laptop(CPU c, Screen s, Modem m){}
Note: Using const references is preferred but I don''t want to confuse the issue.


John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2

Share this post


Link to post
Share on other sites
Thanks for the help, but I am not sure I am understanding it yet- and no I didn''t get it out of a book- I wrote it As you can see, I don''t have the basics down yet!
Anyway, I am not sure what I should put in the place of these:

Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem());

Laptop::Laptop(CPU::CPU(), Screen::Screen(), Modem::Modem()){}

I understand that cpu1, cpu2, etc.are instances of cpu, so do I need to include them all in the laptop class and in the laptop constructor?

If you could give me a little more clarification I would appreciate it!

Thank you!
Leanna

Share this post


Link to post
Share on other sites
I could rewrite the program for you but then you wouldn't learn. So I'll give you some hints.

Ok let's look at your cpu class which is correct. (well there's const references that JohnBolton mentioned but that's a little advanced.)

class CPU
{
private:
double weight, size, cost;

public:
CPU (double w, double s, double c);
double GetCost() {return cost;}
double GetWeight() {return weight;}
double GetSize() {return size;}

};

So you've made a CPU class, and from this class definition a CPU has weight, size and cost. Notice I said "has", when you think about what a class has you will probally want to make that into a member.

Now a laptop has a CPU, Modem and what not, so your laptop class needs to be changed a bit.

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

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

Notice the constructor is still messed up try to fix it. Then post again when you get stuck.


When you start getting a hang for C++ I recommend you read
[url]http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html [/url]
It'll explain the reasoning behind a lot of C++ (at least the first will). It's also a free download.

[edit]Bad Grammar[/edit]

[edited by - Cocalus on May 29, 2004 5:28:23 PM]

Share this post


Link to post
Share on other sites
Okay, I''ve been working on the Laptop constructor, and the compiler isn''t flagging it as an error now. I have an example program I have been trying to follow, but it works differently than mine and so I am having trouble understanding where the cpu1, cpu2, etc, and scr 1, scr2, etc fit in.
I''ll paste it all below again.
Thanks for all your help!
Leanna

#include <cstdlib>
#include <iostream>

using namespace std;

class CPU
{
private:
double weight, size, cost;

public:
CPU (double w, double s, double c);
double GetCost() {return cost;}
double GetWeight() {return weight;}
double GetSize() {return size;}

};

class Screen
{
private:
double weight, size, cost;

public:
Screen (double w, double s, double c);
double GetCost(){return cost;}
double GetWeight(){return weight;}
double GetSize(){return size;}
};

class Modem
{
private:
double weight, speed, cost;

public:
Modem (double w, double s, double c);
double GetCost(){return cost;}
double GetWeight(){return weight;}
double GetSpeed(){return speed;}
};

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();
};

CPU::CPU(double w= 2.266, double s= 300, double c= 1258): weight(w), size(s), cost(c){}
Screen::Screen(double w= 3, double s= 13.3, double c= 215): weight(w), size(s), cost(c){}
Modem::Modem(double w= .05, double s= 33600, double c= 119): weight(w), speed(s), cost(c){}

Laptop::Laptop (CPU CPU, Screen Screen, Modem Modem)
{
myCPU = CPU;
myScreen = Screen;
myModem = Modem;
}

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

void Laptop::Showspecs()
{
cout << "This computer has a " << CPU.size << "Mhz CPU \n";
cout << "with a" << Screen.size << "display and a" << Modem.size << "\n";
cout << "bps modem It costs" << total_cost << "and weighs" << total_weight << "\n";
cout << "pounds. \n";
system ("pause");
}

int main()
{
CPU cpu1, cpu2(3.4, 233, 945), cpu3(2.9, 350, 1295);
Screen scr1, scr2(1.3, 10.3, 306), scr3(1.8, 14.1, 520);
Modem mdm1, mdm2(.026, 56000, 160);

Laptop lap1,
lap2(cpu1, scr3, mdm2),
lap3(cpu3, scr2),
lap4(cpu2, scr2, mdm2),
lap5(cpu3, scr3, mdm2);



lap1.Showspecs();
lap2.Showspecs();
lap3.Showspecs();
lap4.Showspecs();
lap5.Showspecs();

return 0;
}

Share this post


Link to post
Share on other sites
Ok the next compiler problem (on mine at least) is

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

There are two different problems repeating a couple times here.

First Modem is a class, not an instance of class, thus it doesn''t have a cost. What your asking is what''s the cost of computer not the cost of "A" computer.

Second cost and weight are private (From the whole private: thing above where you define a computer). private means that only member functions (functions defined in the class) of that class can access the private parts of that class. Luckily you''ve already written that part, you just need to use it. Remeber member functions can access private parts, and anything can access public.

Share this post


Link to post
Share on other sites
review (very basic though):

in a class, if you have any variables which are private, they can only be directly accessed from that particular class, and nowhere else.

example:

class Example {
private:
int data_blocks;
float average_speed;
string title_name;

public:
Example ( ) {
data_blocks = 0;
average_speed = 0;
title_name = "Name not set.";
}
void setData (int numOfBlocks ) {
data_blocks = numOfBlocks;
}
void printSpeed ( ) {
average_speed = data_blocks / 4;
cout << "It is:" << average_speed << endl;
}
void setName ( ) {
title_name = "Example Class";
}
void printName ( ) {
cout << title_name << endl;
}
}

In this class the private members are accessed/called directly by the class that holds them. The class itself can access those private members but not an instance of the class, for example:

class Example_2 {
private:
float pipeline_misses;
Example mistake; //an instance of class Example


public:
Example_2 () {
pipeline_misses = 2; //this is fine.

mistake.data_blocks = 16; //VERY WRONG, CAN''T DO THAT!

}

//...blah blah

}

Granted when you get more experience with C++ you''ll see there are workarounds and features that don''t make the above always true. But for now, pay attention to the guidelines above, and you''ll be fine.

Share this post


Link to post
Share on other sites
I''ve been trying a ton of different ways to fix the problem, and I can''t seem to get it. I''m not sure if I need to change the actual functions GetCost, GetWeight, & GetSize, or if I just need to call them differently.
I thought that my GetCost, GetWeight, & GetSize functions returned cost, weight, and size respectively. So, when I called them from my Compute function, I could call these, as it would now be public.
I also am not sure I understand how to call the individual costs, weights, and sizes without using modem, CPU, and screen.
Could you give me a few more hints? Please???

Thank you!
Leanna

Share this post


Link to post
Share on other sites
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 CPU

CPU cpu(0.0,0.0,0.0); //I'm not worrying about the actual numbers

//Create a Screen

Screen screen(0.0,0.0,0.0);
//Create a modem

Modem modem(0.0,0.0,0.0);

//Now, create your laptop instance and tell it about the 3 created instances above

Laptop 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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites