Hi all, so at the moment Im doing some assignments involving C++ classes, I think I understand them fairly okay now, but this is a bit challenging none the less for me.
The task is to write a similar program to the one I found online and I planned to use it as a starting point to build my own, the problem is that theres only a picture and the guy didnt include the source code, so Im trying to back engineer it to understand how he did his to do mine using just the output of the program - if that makes sense?
So essentially Im trying to understand how to do the above, using classes. The RAM, SSD, Price values would be in 'private' and would have to be set using get/set.
And the "Computer.h" would further include "Computer.cpp" for the functions.
Ive basically made no progress on this myself, and wanted to see if someone smarter than me could give me a little nudge in the right direction to start.
The program that is 'similar to your assignment' uses the constructor to set the values. But it seems like your assignment needs you to use "get/set" methods. You're better off doing what your assignment tells you, Because I don't think it makes sense to write a constructor that fills out information depending on the computer name (prices could change).
You've got a class with 4 data members and you need 2 methods that would set the values of the data members and get the values of the data members from what you've said.
I don't see what's so difficult that you had to search for similar programs, is that not all the assignment asks you?
You have a good start putting the class in a header file and the member functions in a ".cpp" file.
You can use what you have found as a guide or reference, but as Grime says what you do needs to be tailored to what the assignment needs.
The line Computer Dell ("XPS 13"); I would take as an overloaded ctor creating an object called "Dell", (should be "dell"), and initializing what might be "modleNumber" to "XPS 13". Although this works this may not be the best way to start with. You may want to collect the information in regular variables and then use the set function to put the information into the class. Or you could use the set function to collect the input and put the information directly into the class.
It would help if yo would post the code for the class's ".h" file and the class's ".cpp" file. Also the full information on the assignment helps.
The reason Im confused about this whole deal is that the assignment is so badly written, at least I think so, that its confusing as to what the prof. actually wants.
Heres what he's given us:
Class Computer, consisting of fields: at least 2 computer parameters of your choice, price. Methods to be implemented:
Constructor
setComputer (assigns field values)
get (for each field)
PrintComputer
(this is just a third of this task, but the rest is basically the same thing with different classes and values).
And that is all, which is why I tried to find something similar online to actually fully understand what this entails. Or am I being silly and this is actually really straight forward?
public:
void Computer(string x);
void SetComputer(string x);
getComputer();
PrintComputer();
private:
int RAM();
double Price();
int SSD();
should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public:
Computer(string x); // constructors have no return value
void SetComputer(int RAM, double Price, int SSD); // this is a suggestion, you could
// still use a string and parse it but then you have to worry about parsing errors and also
// it would be ambiguous to users
int getRAM();
double getPrice();
int getSSD(); // Having individual methods for every data member would be the easiest
void PrintComputer(); // you forgot void
private:
int RAM; // RAM should be a variable, not a funtion.
double Price; // Price should be a variable, not a function.
int SSD; // SSD should be a variable, not a function
The header guard is good and as Grime said do not include the ".cpp" file in the header file. This leads to the problem when the "main" file includes "Computer.h" and using an IDE to write and compile your program it will already be setup to compile the "Computer.cpp" file and then when the main file includes the header file "Computer.h" it will include the "Computer.cpp" again leading to variables and functions that have already been defined. That does not even cover the problem of "Computer.cpp", which needs to include "Computer.h", which includes "Computer.cpp". See the problem?
In your class you are missing an important variable. Something like "modelNumber". Just as an overloaded ctor takes a parameter of a model number you still need someplace to store it.
Your overloaded ctor could look something like this:
So I've done some changes, but Im stuck on some errors about "string has not been declared" and that it expects constructors destructors or type conversions.
Basically, I thought I had it, but clearly not, any pointers?
Heres the code I have so far: