I am trying to learn about classes and have been rewritting some of my old programs but instead of using functions, I have started to use classes.
Below is my program that uses a Username class to generate a username and it is working as I intended [well at least I think it is].
Anyway I was wanting to make a few of the member functions "const" but I was unsure how to make the definition and then call them. I think that my declaration is OK though. [line 11 & line 13]
#include <iostream>
#include <string>
#include <conio.h>
#include <cstdlib>
usingnamespace std;
class Username
{
public: //begin public section
string get_user () const;//accessor member function
void set_user ();//accessor member function
string get_username () const;//accessor member function
void set_username ();//accessor member function
void print_username ();
private: //begin private section
string its_user;//member variable
string its_username;//member variable
};
//function used to access member variable "its_user"
string Username::get_user()
{
return its_user;
}
//accessor member function which gets user's name from user and assigns it to its_user
void Username::set_user ()
{
cout << "\n\nPlease enter user's forename and surname: ";
string set_user;//declaring variable of type string which holds the name which will be used to create the username
getline (cin, set_user);
its_user = set_user;//initialising member variable "its_user"
}
//function used to access member variable "its_username"
string Username::get_username()
{
return its_username;
}
//function uses "its_user" variable to create a username. The new username is assigned to "its_username".
//Username created by taking first letter of Forename and joining it with surname
void Username::set_username ()
{
string string1;//declaring variable of type string that holds the first letter of user's forename
string string2;//declaring variable of type string that holds the user's surname
string set_username;//declaring variable of type string that creates the username by adding string1 and string2
int position;//declaring variable of type int that holds the numerical value of the position of " " in the string 'user'
string1.append(its_user,0,1);//finds the first letter of the string "user" and assigns its value to the variable string1
position = its_user.find(" " , 0);//finds the position of the " " in the string 'user' and assigns its value to the variable position
position = position + 1;//adds 1 to the value of position which is then used to find the starting position of the surname is the string 'user'
string2.append(its_user.begin()+position,its_user.end());//extracts the surname from the string "user"
set_username = string1.append(string2);//adds string1 and string2 and assigns the value to variable 'create_username'
its_username = set_username;//initialising member variable "its_username"
}
//member function that prints welcome message to the user
void Username::print_username ()
{
system ("CLS");//clears screen
string message = "Welcome " + its_username + "!";
string spaces (message.size(), ' ');//used to build the 2nd and 4th line of output
string second = "* " + spaces + " *";//creates 2nd and fourth line of output
string first (second.size(), '*');//creates the 1st and 5th line of output
cout << "\n\n\n\n\n\n\n\n\n\n";//used to position message on screen
cout << "\t\t\t\t" << first << endl;//prints first line of message
cout << "\t\t\t\t" << second << endl;//prints second line of message
cout << "\t\t\t\t" << "* " << message << " *" << endl;//prints 3rd line of message
cout << "\t\t\t\t" << second << endl;//prints fourth line of message
cout << "\t\t\t\t" << first << endl;//prints fifth line of message
}
int main ()
{
Username New;//creating object of class "Username"
New.set_user();//calling function to get value which will be assigned to "its_user"
New.set_username();//calling function that creates a username and assigns it to "its_username"
New.print_username ();//calling function that prints welcome message to user
_getch();//pauses screen
return 0;
}
I wouldn't call a variable the same name as a method.
How about instead of:
1 2 3
string string1;//declaring variable of type string that holds the first letter of user's forename
string string2;//declaring variable of type string that holds the user's surname
string set_username;//declaring variable of type string that creates the username by adding string1 and string2
class Username
{
public:
string get_user () const; // declared const
void set_user (); // there is a difference between accessor and mutator, Google it :p
string get_username () const; // declared const
void set_username ();
void print_username ();
private:
string its_user;
string its_username;
};
string Username::get_user() // not defined const
{
return its_user;
}
string Username::get_username() const // should be defined const like this
{
return its_username;
}
Also, you don't need many of those comments. Things like: //prints first line of message don't add anything, they just make it harder to read the code.
You would also be better off not using things like system() or anything in conio.h (there are a few articles in the article section about what you are trying to do).
The comments in my code are more for my sake as I am still learning. I find they help me think what each line of code is doing and also helps when I look back at a program I did previously.
Another question I would like to ask about classes relates to member variables and accessor functions.
If I am using a private member variable in my member function should I use its name or should I use the accessor function that returns its value?