0
#include <iostream>
#include <string>
#include <string.h>


using namespace std;

class game_object
{
public:
    int set_id_num(int);
    int get_id_num(int);
    string set_name(string);
    string get_name(string);
    int set_type(int);
    string get_type(string, int);
    float set_buy_value(float);
    float get_buy_value(float);
    float set_market_value(float);
    float get_market_value(float);
    int set_year(int);
    int get_year(int);

private:
    int id_num;//identifier number for the game
    string name;//the name of the game
    int type;//whether the game is cartridge, CD, DVD, BR, download
    string type_name;//type of game
    float buy_value;//price of game
    float market_value;//value of game
    int game_year;//year the game was made
};

int main()
{
    game_object test;

    test.set_id_num(id_num);
    cout << test.get_id_num(id_num) << endl;

    return 0;

}

//sets id num for the game
int game_object::set_id_num(int num)
{
    cout << "Please enter a four digit id number for the game: ";
    cin >> id_num;

    id_num = num;

    return 0;
}

//displays the id num for the game
int game_object::get_id_num(int num)
{
    return(set_id_num(id_num));
}

//sets desired name for game
string game_object::set_name(string name)
{

    cout << "Please enter a name for the game: ";
    cin.ignore();
    getline(cin, name);

    return 0;
}

//displays the name of the game
string game_object::get_name(string name)
{

    cout << "The name is ";
    return(name);

}

//presents a menu to choose type of game
int game_object::set_type(int type_of_game)
{

    cout << "There are four types of games." << endl;
    cout << "     0. Cartridge " << endl;
    cout << "     1. CD " << endl;
    cout << "     2. DVD " << endl;
    cout << "     3. BR " << endl;
    cout << "     4. Download " << endl;

    cout << "Which type do you want to set for the game (enter number)? ";
    cin >> type_of_game;

    return 0;
}

//prints the type of game chosen
string game_object::get_type(string type, int type_of_game)
{

    if (type_of_game == 0)
    {
        type = "cartridge";
        cout << "The type is " << type << endl;
    }
    else if (type_of_game == 1)
    {
        type = "CD";
        cout << "The type is " << type << endl;
    }
    else if (type_of_game == 2)
    {
        type = "DVD";
        cout << "The type is " << type << endl;
    }
    else if (type_of_game == 3)
    {
        type = "BR";
        cout << "The type is " << type << endl;
    }
    else if (type_of_game == 4)
    {
        type = "download";
        cout << "The type is " << type << endl;
    }

    return 0;
}

//sets the buying value of game
float game_object::set_buy_value(float buy_value)
{
    cout << "Please set a buying value for the game: ";
    cin >> buy_value;
    return 0;
}

//displays the buying value for game
float game_object::get_buy_value(float buy_value)
{
    cout << "The game costs ";
    return(buy_value);
}

//sets market value
float game_object::set_market_value(float market_value)
{
    cout << "Please set the market value of the game: ";
    cin >> market_value;
    return 0;
}

//displays market value
float game_object::get_market_value(float market_value)
{
    cout << "The market value is ";
    return(market_value);
}

//sets model year of the game
int game_object::set_year(int year)
{
    cout << "What is the model year of the game? ";
    cin >> year;
    return 0;
}

//displays model year
int game_object::get_year(int year)
{
    cout << "The year of the game is ";
    return(year);
}

So I need some help to check if I defined the class correctly? Also if you could help me test each function. I don't understand how to call the function within the class. And I get errors with the way I have tried. Please help, thanks.

Kara
  • 6,115
  • 16
  • 50
  • 57
agenise1
  • 21
  • 1
  • 6

1 Answers1

1

I think you might be misunderstanding the concept of getters and setters.

A getter is a public function on a class that returns the value of a private member variable.

int get_id_num() const {
    return id_num;
}

Typically, you expect it to take no parameters and have no side effects. You certainly don't expect a getter to get input from the user or output anything to the user.

A setter on the other hand sets the value of a private member variable.

void set_id_num(int num) {
    id_num = num;
} 

It typically takes one parameter (the value you wish to set) and returns nothing. Again, you don't expect a setter to get input from the user or output anything to the user.

And you don't expect a getter to call a setter or vice versa.

Usually you would separate the user interface from game object classes. For example you could get the value from the user in the main function and pass it to a setter:

int main(){
    game_object test;

    int id_num = 0;
    cout << "Please enter a four digit id number for the game: ";
    cin >> id_num;  // get id_num from the user

    test.set_id_num(id_num);  // pass id_num to the setter by value
}

If the class must have responsibilities to get input from the user then getters and setters are not the place to do it.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • what is the purpose of the test variable? sorry I am just really struggling with this concept – agenise1 Aug 06 '15 at 16:57
  • Okay i have tried this now and I continue to get errors saying I have not defined a variable in the set_id_num scope. However I thought if I passed the value I wouldnt need to – agenise1 Aug 06 '15 at 17:08