class function names (set and get)

Jan 7, 2012 at 10:00am
Hi guys.

Do you have to put set in front of the class function names if you set value in the function of a class to an object, or get to get a value from an object in class function. Please explain?

Here is an example of a class, class functions, and it's names that i made.
And if i do anything wrong please explain.
The class in about messaging.

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

class message
{
public:
	message()
	{
		numberOfOutbox=0;
		numberOfInbox=0;
		a=0;
	}

	void setNewMessage()	// sendding newmessage and storing it
	{
		cout << "Sending New Message\n";
		cout << "===========\n";
		cout << "\nAddress : ";
		getline(cin,addressOutbox[numberOfOutbox]);
		cout << "\nMessage : ";
		getline(cin,outbox[numberOfOutbox]);
		numberOfOutbox++;
	}

	void getOutbox()		// Getting the outbox, in other words history of the send message
	{
		cout << "Outbox\n";
		cout << "======\n";
		for(a=0;a<numberOfOutbox;a++)
		{
			cout << "\nAddress : " << addressOutbox[a];
			cout << "\nMessage : " << outbox[a];
		}
		a=0;
	}

	void setNewInbox(string tempAddress, string tempInbox)	// to view new receive message
	{
		cout << "Receive New Message\n";
		cout << "=====\n";
		addressInbox[numberOfInbox]=tempAddress;
		inbox[numberOfInbox]=tempInbox;
		cout << "\nAddress : " << tempAddress;
		cout << "\nMessage : " << tempInbox;
		numberOfInbox++;
	}

	void getInbox()		// to view all receive message
	{
		cout << "Inbox\n";
		cout << "=====\n";
		for(a=0;a<numberOfInbox;a++)
		{
			cout << "\nAddress : " << addressInbox[a];
			cout << "\nMessage : " << inbox[a];
		}
		a=0;
	}

private:
	string outbox[1000],inbox[1000],addressOutbox[1000],addressInbox[1000];
	int numberOfOutbox,numberOfInbox,a;
};


Thank you.
Jan 7, 2012 at 10:21am
No, you can call your function names whatever you like.

They must start with a letter and contain only letters, numbers and _ character.

Don't use get/set. Think up descriptive names for the functions

e.g.

appendMessage, printOutbox etc.

this way you are clear about what the functions are doing
Jan 7, 2012 at 10:33am
@mik2718, thank you for the explanation. Appreciate it.

If there any other comments, please do write them.

Thank you.
Last edited on Jan 7, 2012 at 10:35am
Jan 7, 2012 at 11:28am
Maybe use vector instead of fixed size arrays for your strings

1
2
3
4
5
// create vector of strings for inbox
vector<string> inbox;

// append message to inbox
inbox.push_back(message);


then if there are more than 1000 messages your program wont crash


Jan 8, 2012 at 5:26am
@mik2718, that is new to me. But, thank you for your input.
Topic archived. No new replies allowed.