-
Classes Exercise (again)
Ok.. yet another homework.. hope I'm not bugging you guys out too much.
"Make a program that processes computer orders. There are two ways computers can be ordered. Either in 3 packages (A, B, C) or custom configured computers. The custom computers can be modified in 3 ways: (CPU Speed, Hard Drive, RAM)
Package A: 75 (Mhz) 1(GB) 16(MB of RAM)
Package B: 100 (Mhz) 1.5(GB) 16(MB of RAM)
Package C: 133 (Mhz) 2(GB) 32(MB of RAM)
"
And heres my program:
Code:
#include<iostream.h>
class Computer
{
public:
void display(char a)
{
if(a=='A')
{
cpuSpeed=75;
hardDrive=1;
memory=16;
cout<<"A "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
}
if(a=='B')
{
cpuSpeed=100;
hardDrive=1.5;
memory=16;
cout<<"B "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
}
if(a=='C')
{
cpuSpeed=133;
hardDrive=2;
memory=32;
cout<<"C "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
}
}
void display(int b,double c,int d)
{
cpuSpeed=b;
hardDrive=c;
memory=d;
cout<<"Custom "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
}
private:
int cpuSpeed, memory;
double hardDrive;
};
int main()
{
Computer compaq(50,0.5,8), toshiba('A'), gateway('C');
compaq.show();
toshiba.show();
gateway.show();
return ;
}
The int main() is a given, so that can't be modified.
When I compile it, errors are given, such as "Cannot convert int to Computer.."
Can anyone point out my mistakes?
-
First, the function call is named show() not display as you defined in the class:
Code:
void display(int b,double c,int d)
{
cpuSpeed=b;
hardDrive=c;
memory=d;
cout<<"Custom "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
}
Also, why is display have a function arguement as char when you have not defined one.
Look at your book- These are easy assignments your instructor is giving you.
Mr. C
-
Well, first, you need some constructors for your Computer class if you want to pass arguments to them. Without the proper constructors, the following line will cause errors:
Computer compaq(50,0.5,8), toshiba('A'), gateway('C');
Second, there is no show method defined in Computer, you define display, but it doesn't take zero arguments.
Finally, return ; should be return 0;. :)
Code:
#include<iostream>
using namespace std;
class Computer
{
public:
Computer ( char a );
Computer ( int b,double c,int d ) :
cpuSpeed ( b ), hardDrive ( c ), memory ( d )
{}
void show();
private:
int cpuSpeed, memory;
double hardDrive;
};
Computer::Computer ( char a )
{
switch ( a ) {
case 'A':
cpuSpeed = 75;
hardDrive = 1;
memory = 16;
break;
case 'B':
cpuSpeed = 100;
hardDrive = 1.5;
memory = 16;
break;
case 'C':
cpuSpeed = 133;
hardDrive = 2;
memory = 32;
}
}
void Computer::show()
{
cout<<"CPU: "<< cpuSpeed
<<"\nHD: "<< hardDrive
<<"\nMem: "<< memory
<<'\n'<<endl;
}
int main()
{
Computer compaq ( 50, 0.5, 8 ), toshiba ( 'A' ), gateway ( 'C' );
compaq.show();
toshiba.show();
gateway.show();
return 0;
}
>These are easy assignments your instructor is giving you.
Maybe for you, but these kind of problems tend to give obscure error messages.
-Prelude
-
Thanks guys.. I'm still really bad at Classes, since I learned them about 5 days ago.
And my book is useless... the author has a hard time explaining C++ in "simple" terms. Thats why we never use them.
And you're right, the program is really simple. But the error messages gave me no clue on what was wrong.
-
>And my book is useless...
I would recommend Accelerated C++ by Andrew Koenig and Barbara Moo. It is by far the best introductory text on C++, and it actually uses the language as it should be from the start. :)
-Prelude
-
A book that I can agree with!!
Mr. C.