Class does not accept private for variables- WHY

Hello!
Please, why is here giving an error?
It CAN be public, but why can't it be PRIVATE???
Many thanks!!!

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
  #include<iostream>
using namespace std;


class product {
   
  private:
    int ID;
    string name;
    string price;
    void set_values(int, string, string);
};

void product::set_values (int a, string b, string c) {
  ID = a;
  name = b;
  price=c;
}


int main(){    

  product toy;
  product book;
  product dress;

  toy.set_values(1, "Toy", "11.22€");
  book.set_values(2, "Book", "22.33€");
  dress.set_values(3, "Dress", "33.44€");

     cout<<toy.ID<<endl<<toy.name<<endl<<toy.price<<endl<<endl<<book.ID<<endl<<book.name<<endl<<book.price<<endl<<endl<<dress.ID<<endl<<dress.name<<endl<<dress.price<<endl;
return 0;

}
MANY THANKS!!
if you declare it in private scope, then it will not be accessible outside of the class (it can only be accessed by member and methods of the same class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class X
{
private :
    int data_; // private member

public : //< the methods below are declared public, they can be accesible outside the class
    // to access a private member, we should use
    // a so called "getter/setter" method
    void setData( int data ) { data_ = data; } // setter (can access data_ since this method is a member)
    int getData( ) const { return data_; }     // getter
};

int main()
{
    X x;
    x.data = 10; // <!! Illegal
    x.setData( 10 ); // < Legal
}
Last edited on
closed account (18hRX9L8)
If you want to keep the variables private, you will need to define a getter... Something like this:

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
#include<iostream>

struct product_values {
	int ID;
	std::string name, price;
};

class product {
private:
	product_values vals;
public:
	void set_values(int, std::string, std::string);
	product_values get_values(void);
};

void product::set_values (int a, std::string b, std::string c) {
	this->vals.ID = a;
	this->vals.name = b;
	this->vals.price=c;
}

product_values product::get_values(void) {
	return this->vals;
}

int main(void) {
	product toy;
	product book;
	product dress;

	toy.set_values(1, "Toy", "11.22€");
	book.set_values(2, "Book", "22.33€");
	dress.set_values(3, "Dress", "33.44€");

	std::cout << toy.get_values().ID << std::endl
			  << toy.get_values().name << std::endl
			  << toy.get_values().price << std::endl << std::endl
			  << book.get_values().ID << std::endl
			  << book.get_values().name << std::endl
			  << book.get_values().price << std::endl << std::endl
			  << dress.get_values().ID << std::endl
			  << dress.get_values().name << std::endl
			  << dress.get_values().price << std::endl;

	return 0;
}
Last edited on
Is it ment as a mistake, if I leave it as public?
closed account (18hRX9L8)
Read up when you should use private variables: http://programmers.stackexchange.com/questions/143736/why-do-we-need-private-variables.
Last edited on
usandfriends: you accidentally linked to info on private inheritance instead of private data members.
closed account (18hRX9L8)
Ah, sorry about that. Edited my post.
Topic archived. No new replies allowed.