how does one make their class variables private but still access them?

how does one make their class variables private but still access them?
for example

1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
const int MAX_make_charactrs = 9 ;

class Auto 
{
	public:

         char make[MAX_make_charactrs]; 
         string model; 
	 double price;
};


I have tried to make functions inside the class like so


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
class Auto 
{
	// Private section
	public:
		// Public Declarations		 

		 void setAll(string make1, string model, string color , int year, double price)
		 {
		 	make = make1;
		 	color = color;
		 	price = price;
		 	
		 }

		 
		 int getPrice()
		 {
		 	return price;
		 }

		 string getMake()
		 {
		    return make;	
		 } 
 		 string getColor()
		 {
		    return color;	
		 } 
		 

	private:		
         char make[MAX_make_charactrs]; 
		 string color; 
		 double price;
	     int year;			
};


 



Am i doing this right?




Last edited on
Yes, it looks like it should work, except that you can't assign a std::string to a char array. Why is not make a std::string?
The parameter of a function shadows the member variable. I.e. line 10 assigns the parameter color to itself. Basically nothing will happen. The same applies to line 11.

In order to assign the paramter color to the member color you can use this: this->color = color;
Within member functions of your class, you can access private members, however you normally cannot from another class; IFF you really must access private members from another class or non-member function, that class or function has to be a friend. Example below, showing friend 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
#include <iostream>

using namespace std;

class Widget
{
public:
	Widget() : num(0), mem(0) {
		Reset(); // can access private function "Reset()"
	}
	
	void SetMem(int m) { 
		mem = m; // can access private data "mem"
		++num;
	}
	
	int num;
	
	friend void print(const Widget &); // friend declaration
	
private:
	int mem;
	void Reset() { num = 0; }
};

void print(const Widget &w)
{
	cout << w.mem << endl; // friend function can access private member of Widget
}

int main()
{
		Widget w; // constructor will call private member "Widget::Reset()"
		w.SetMem(3); // SetMem() will change private member mem
		
		// w.Reset(); // cannot access private function Widget::Reset()
		// w.mem = 0; //cannot access private member Widget::mem
		w.num = 0; // will change public member num
		
		print(w); // will access private member of w
}
@Peter87 make is suppose to be a c string that why its a char.
@tipaye so by making friend declarations I can access private variables from from other classes in the class im working on?
make is suppose to be a c string that why its a char

Fine, but you can't assign a C++ string to a C-string (line 9).

 
    strcpy (make, make1.c_str());  // make1 better not be longer than 8 chars 


so by making friend declarations I can access private variables from from other classes in the class im working on?

Yes, but that is a poor practice.

If you're going to do that, you might as well make the variables public. Better yet is to keep them private and have getters and setters for them.
so by making friend declarations I can access private variables from from other classes in the class im working on?


Yes you can, but just because you can, doesn't mean you should.

This is why I used the mathematical notation "IFF" (if and only if). This is not something you should be doing, and if you find yourself needing to access private data in this way, I suggest re-thinking your approach.

You've made your data private for a reason, if you need to access it, (for example) create 2 public member functions for each data member, one to get the data and the other to set it, and call these functions. I know it's a hassle at first, but it's worth it in the long run.

(Better yet) instead of getting and setting, why not create member functions that achieve your ultimate goal? That way a class manages it's own internal data and you're free to change it's implementation without necessarily having to touch your interface and upsetting the users of your class. For (contrived) example:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Door
{
public:
	Door() : shut(true), locked(true) {}
	void Open() { locked = false; shut = false; }
	void Shut() { locked = false; shut = true; }
	void Lock() { shut = true; locked = false; }
	void Unlock() { shut = true; locked = true; }
	
private:
	bool shut;
	bool locked;
};


It's a silly example, but the point I want to make is that instead of functions getting and setting the values of "shut" and "locked", I have functions that do what I really want, i.e Close() or Open() and it is up to these functions to manipulate the internal data as they see fit. This way, I can change how a door is Lock()ed or Open()ed, independently of how the calls are made.

There are arguably good uses of friend (e.g. overloading non-member operators), but many of us just abuse the facility in order to save some extra typing
Topic archived. No new replies allowed.