Public/Private Classes, explanation please in context!

I'm making a car sale program, and we are asked to have many different components in the calculation of the final price. the public stuff make sense, but can someone explain to me when I should use the private variables and how I should use them and why I should use them. it doesn't really make sense, and we really wern't given an explanation. Thank you very much. Here's a snippet of my code to help you. Could someone give me an example of the cost function that is in the private class, and how I can get it from the user? Thanks again!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Cars
{
 public:
      string get_color();
      string get_make();
      int get_year();
      int number_of_speeds();
      double cost_car();
      void set(int the_year, string the_color, int the_speeds);
 private:
      int speeds;
      string color;
      string make;
      int cost;
};
Last edited on
private members are accessible only by the class and its friends.
In your case, you cannot change the color after initialization
Eg:
1
2
3
Cars MyCar(1234,"blue",567);
MyCar.get_color();//OK, public. Let's you know which is the color
MyCar.color = "yellow"; //wrong, you can't have access to Cars' private members 

your cost function (cost_car) is public, if it was private the user couldn't use it directly
but in our assignment, we are suppoesd to have cost as a private:

Add the private variable cost and appropriate member functions to get the cost (accessor function) and set the cost (mutator function).


so how would i go about doing this?! Thanks for the help bazzy
Use setters and getters:
1
2
int getCost();
void setCost(int);
this is the error i get when I do this:
double Cars::get_cost()' is private
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
class Cars
{
 public:
    //...
    double getCost();
    void getCost(double cost);
      
 private:
    //...
    double cost;
};
ok, @ grey wolf, well i tried that, and it still gave me the same error message as before. here is the code that is before I started making changes to try to get cost to a private. I have worked everything neccessary into public classes and the program compiles correctly. I need to make cost a private function, could someone manipulate this for me so it works in private class. This is the instruction:

Add the private variable cost and appropriate member functions to get the cost (accessor function) and set the cost (mutator function).


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
using namespace std;

class Cars
{
 public:
      string get_color();
      string get_make();
      int get_year();
      int number_of_speeds();
      double get_cost();
      void set(int the_year, string the_color, int the_speeds);
 private:
      int speeds;
      string color;
      string make;
      double cost;
};
string Cars::get_color()
{
        string the_color;
        cout << "What color is the car? ";
        cin >> the_color;
        return the_color;
}
int Cars::number_of_speeds()
{
    int num_speeds;
    cout << "How many speeds does your car have? ";
    cin >> num_speeds;
    return num_speeds;
}
string Cars::get_make()
{
     string make;
     cout << "What is the make of the car? ";
     cin >> make;
     return make;
}
int Cars::get_year()
{
    int year;
    cout << "What is the year of the car? ";
    cin >> year;
    return year;
}
double Cars::get_cost()
{
    double cost;
    cout << "How much should the car cost? ";
    cin >> cost;
    return cost;
}
void Cars::set(int the_year, string the_color, int the_speeds)
{
   int year = the_year;
   string color = the_color;
   int speeds = the_speeds;
}
int main()
{
    Cars car1, car2;
    char answer;
    double discount, edit_discount;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
   
    cout << "Information for Car 1: " << endl;
    int year1 = car1.get_year();
    string make1 = car1.get_make();
    string color1 = car1.get_color();
    int num_speeds1 = car1.number_of_speeds();
    double cost_car1 = car1.get_cost();
   
    cout << "Would you like to give a discount? <y/n> ";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    {
       cout << "How much of a discount would you like to give? ";
       cin >> discount;  
           edit_discount = 1 - (discount / 100);
    }
    else
       edit_discount = 1;
    cout << "The final price of your car, the " << year1 << " " << color1 << " " << make1 << ", will be $" << edit_discount * cost_car1 << endl;
   
    cout << endl << "Information for Car 2: " << endl;
    int year2 = car2.get_year();
    string make2 = car2.get_make();
    string color2 = car2.get_color();
        int num_speeds2 = car2.number_of_speeds();
        double cost_car2 = car2.get_cost();
   
    cout << "Would you like to give a discount? <y/n> ";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    {
       cout << "How much of a discount would you like to give? ";
       cin >> discount;  
           edit_discount = 1 - (discount / 100);
    }
    else
       edit_discount = 1;
    cout << "The final price of your car, the " << year2 << " " << color2 << " " << make2 << ", will be $" << edit_discount * cost_car2 << endl;
   
    return 0;
} 
I need to make cost a private function
You may be confusing something: cost -variable- should be private and get/set cost functions should be public
In the code you posted, you are using get_... functions in a wrong way, this is an example of the way they should work:
1
2
3
4
5
6
7
class Cars
{
         double cost;
     public:
         double get_cost () const { return cost; }
         void set_cost ( double newCost ) { cost = newCost; }
};
Last edited on
small suggestion, your code says if input is y, do the yes stuff, otherwise do the no stuff, so no matter what you enter, if it isnt y, its counted as no, so if you typo and miss y and hit u instead, too bad. It should be more like, if input is y, do the yes stuff, else if input is n, do the no stuff, else error message, and go back and try again.
Topic archived. No new replies allowed.