Back engineering simple code with classes

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 his program looks something like this:
1
2
3
4
5
6
7
8
9
10
11
#include "Computer.h"

using namespace std;

int main(){

Computer Dell ("XPS 13");

system("pause");
return 0;
}


and the output of this program would look like this:

Computer name -> XPS 13
RAM -> 4 GB
SSD -> 128 GB
Price -> 999.99 USD

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.

Thank you for your help.
Last edited on
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?
Hello wirelesskill,

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.

Hope that helps,

Andy

Edit:
Last edited on
Thanks for this.

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?

Heres what I have:

Computer.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef COMPUTER_H
#define COMPUTER_H
#include "Computer.cpp"


class Computer{
    public:
        void Computer(string x);
        void SetComputer(string x);
        getComputer();
        PrintComputer();

    private:
        int RAM();
        double Price();
        int SSD();

};

#endif // COMPUTER_H 


This is obviously just a quick sketch of what I think I will need for this part, but yeah. Let me know how many mistakes there are..

Computer.cpp is empty at the moment.
Last edited on
1) You must not include .cpp files, ever.
2) VVV
1
2
3
4
5
6
7
8
9
10
    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 
Hello wirelesskill,

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?

Also you never put using namespace std; in a header file. It is best to read this http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

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:
1
2
3
4
Computer::Computer (std::string modNum)
{
    modelNumber = modNum;
}

This is one possibility. There is another that I am not use to using, so I do not want to give you the wrong information.

Then as Grime pointed out you can use the set function to set your other variables of the class.

Just to be fair the ctor and dtor have no return values. You could think of them as a special case for a function.

Hope that helps,

Andy
Thank you both!

All of this has been really helpful, Im editing it now, and will see how it goes based on this feedback.
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:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Computer.h"
using namespace std;

int main(){

Computer Asus;
Asus.Computer = "Asus One";
cout << Asus.PrintComputer();


system ("pause");
return 0;
}


Computer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef COMPUTER_H
#define COMPUTER_H
#include <string>

class Computer{
    public:
        Computer(string x);
        void SetComputer(string x);
        int getRAM();
        double getPrice();
        int getSSD();
        void PrintComputer();

    private:
        string Name;
        int RAM;
        float Price;
        int SSD;

};

#endif // COMPUTER_H 


and Computer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Computer.h"

Computer::Computer(string x){
    Name = x;
}

void Computer::SetComputer(int RAM, double Price, int SSD){
    RAM = 4;
    SSD = 128;
    Price = 595.99;
}

Computer::getComputer(){
}

int Computer::getRAM(){
    return RAM;
}

int Computer::getSSD(){
    return SSD;
}

float Computer::getPrice(){
    return Price;
}

void Computer::PrintComputer(){
    cout << Name <<endl;
    cout << RAM <<endl;
    cout << SSD <<endl;
    cout << Price <<endl;
}


Sorry its a bit messy, and Im still using namespace std for now, I promise I will get in the habit of not using it later on.

Thanks for any help.
computer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef COMPUTER_H
#define COMPUTER_H

#include <string>

class Computer {

    public:

        // constructor
        Computer( std::string name, int ram, double price ) ;

        // get (for each field) note const
        // https://isocpp.org/wiki/faq/const-correctness#const-member-fns
        std::string name() const ;
        int ram() const ;
        double price() const;

        // print Computer
        void print() const ;

        // set Computer (assigns field values)
        void set( std::string name, int ram, double price ) ;

    private:

        // 2 computer parameters of your choice
        std::string its_name;
        int ram_gb;

        double cost_price; // price
};


computer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "computer.h"
#include <iostream>

// https://en.cppreference.com/w/cpp/language/initializer_list
Computer::Computer( std::string name, int ram, double price )
    : its_name(name), ram_gb(ram), cost_price(price) // initialise members
{
    // add sanity checks, correct invalid data
    if( its_name.empty() ) its_name = "unknown" ;
    if( ram_gb < 1 ) ram_gb = 4 ;
    if( cost_price < 0 ) cost_price = 0 ;
}

std::string Computer::name() const { return its_name ; }

int Computer::ram() const { return ram_gb ; }

double Computer::price() const { return cost_price ; }

void Computer::print() const
{
    std::cout << "computer{ " << name() << ", " << ram() << " gb, $" << price() << " }\n" ;
}

void Computer::set( std::string name, int ram, double price )
{
    // simple: construct a computer with the given info and assign it to this computer
    *this = Computer( name, ram, price ) ;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "computer.h"

int main ()
{
    Computer asus( "Asus One", 8, 123.45 ) ;
    asus.print() ;

    asus.set( "Asus Two", 16, 234.56 ) ;
    asus.print() ;
}
Last edited on
Topic archived. No new replies allowed.