Could someone teach me the proper access specifier class thing

Could someone tell me the proper way of doing this? And explain it well. 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
36
37
38
39
40
41
  #include <iostream>
#include <string>
using namespace std;

class MyClass
{
public:
	void setName(string name)
	{
		myName = name;
	}

	string getName()
	{
		return myName;
	}

	void displayName(string myName)
	{
		cout << "The name you have entered is: " << myName << endl;
	}



private:
	string name;
};

int main()
{
	string theName;
	cout << "Please enter your name: \n" << endl;
	cin >> theName;

	MyClass mc;
	mc.displayName(theName);

	cin.get();
	system("PAUSE")
		return 0;
}
To me when you're creating a class, it's best to have your class do all the work and keep your main as short as possible so that the next programmer that reads it, can easily pinpoint where everything is happening. Feel free to ask me to explain anything you see here that you think looks weird.

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

class MyClass
{
public:
	void setName(string Myname)
	{
	    cout << "Enter your name. ";
	    getline(cin, Myname, '\n');
	    
		name = Myname;
	}

	void displayName()
	{
		cout << "The name you have entered is: " << name << endl;
	}



private:
	string name;
};

int main()
{
	string theName;

	MyClass mc;
	
	mc.setName(theName);
	mc.displayName();

	
   return 0;
}
Last edited on
I'll offer an alternate to deathslice: rather than having the class do all the work, I prefer write classes that make doing the work easy:
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 <iostream>
#include <string>
using namespace std;

class MyClass
{
public:
	void setName(const string &prompt)
	{
		cout << prompt << flush;
		cin >> name;
	}

	string getName()
	{
		return myName;
	}

private:
	string name;
};

int main()
{
	MyClass mc;

	mc.setName("Please enter your name: \n");
	cout << "The name you have entered is: " << mc.getName() << endl

	cin.get();
	system("PAUSE")
	return 0;
}


If you do it this way then it's much easier to modify the code to meet future requirements. For example: "we want to use diffferent prompts to depending on whether the program is running in 'formal' or 'informal' mode. Also, we want the output to go to an external stream called 'myOut." If you do it my way, then this change is easy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	MyClass mc;

	string inprompt, outPrompt;
	if (mode == "formal") {
		inprompt = "Please enter your name: \n";
		outPrompt = "The name you have entered is: ";
	} else {
		inprompt = "What's your name?\n";
		outPrompt = "Hey!  You're ";
	}
	mc.setName(imprompt);
	myOut << outPrompt << mc.getName() << endl

	cin.get();
	system("PAUSE")
	return 0;
}
The way that you described it dhayden is better because you take into account other possible future uses which is always nice.
In regards to member access specifiers, without any more information, I will say first of all that all members in a class are private by default. If you say public:, then every MEMBER after can be ACCESSED by anything outside the class PUBLICLY (such as main()), until it gets to if and when you say private:, after which every MEMBER can only be accessed PRIVATELY, meaning only within that class's function definitions. I should also add that there is a "protected" as well as public and private access specifier, but it is really for a little more advanced topics (regarding friends, class inheritance, etc.), and I have never really had to use protected myself.

So in your example, all the functions of the class can be accessed by the main function, and the private member can only be accessed by the functions within the class. I think you also may have meant for the private string name to be myName (given that the function getName() returns myName).

Oh, and I am also told that we really shouldn't use system("pause"), but that cin.get() specifically waits for us to press enter, we just need a line to tell us that beforehand. If you are using Visual Studio, there is an option in the project properties under Linker. Set the SubSystem value to "Console" and that will automatically display the "Press any key to continue..." and hold the window open for review before exiting.
Last edited on
I'd like to offer an even better way (in my opinion) to do the above as I feel that it increases the encapsulation-ness of it. This way will have no traditional "getters" or "setters". Everything that needs to be done is done through the stream insertion and extraction operators.

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

class MyClass
{
public:
	friend std::ostream& operator<<(std::ostream &os, const MyClass &myClass)
	{
		os << "Name is " << myClass.m_name << '\n';

		return os;
	}

	friend std::istream& operator>>(std::istream &is, MyClass &myClass)
	{
		std::cout << "Enter name: ";

		is >> myClass.m_name;

		return is;
	}

private:
	std::string m_name;
};

int main()
{
	MyClass myClass;

	std::cin >> myClass;

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::cout << myClass;

	std::cin.ignore();
	std::cin.ignore();
}
Topic archived. No new replies allowed.