A simple CPP program

Hello everyone. This semester I’ve been the learning CPP (C++ / CXX) programming language and I thought I’d write a simple program in order to help out some of my friends. This program has one class; person. This class contains two private variables called name and age, these have the associated accessors and mutators.

Lets start the program, first we need to include the iostream library, we will also include the string library. They handle the input and output and give us the ability to use strings respectively. I personally thing using strings is easier and neater than using char arrays. We also have to declare our namespace.

[sourcecode language=”cpp” gutter=”false”]
#include <iostream>
#include <string>

using namespace std;
[/sourcecode]

Now we can create the person class. Private and Public are the protection level, variables should always be private. If you want to change a variable you use a mutator that can filter the new input to make sure you don’t set the age as four, because this is a string and not an interger the program would crash. In our basic program the mutators do not filter so the program would crash.

Below is the class definition of variables, constructors, mutators and accessors. The mutators are implemented alter on, lines begining with // are comments and do not become part of the program.
[sourcecode language=”cpp” gutter=”false”]
class person
{
private:
int age;
string name;

public:
// Constructors
person();
person(string newName, int newAge);

// Accessors
string getName() { return name; };
int getAge() { return age; };
// datatype getVariable() { return variable; };

// Mutators
void setName(string newName);
void setAge(int newAge);
// void setVariable(datatype newVariable);

};[/sourcecode]

The constructors are the bit of code that is ran when an instance of this class is created. There are two of them that take different inputs, this is called function overloading. There is nothing infront of them because they do not have a return type.

The accessors are functions used to get the variables from the class because they cannot be accessed directly from outside of the class due to the variables being private. They simply return the variable. You will see the the first word is lower class with the following words beinging with an capital letter, this is simply to make the code easier to read.

The mutators are used to modify the variable and will often filter the input to prevent the variable from becoming corrupted. Here they do not return anything (void) and take one variable which is the variable they will be changing. I’ve put the word ‘new’ in front of it to avoid the program ( / myself) becoming confused by having two variables of the same name to deal with.

The constructors and mutators are implemented out of the class definition. Remember that constructors do not return a datatype where as the mutators do, void being a kind of datatype. When implementing functions you need to put the class that it belongs to infront of the function with a doube collon seperating them.

[sourcecode language=”cpp” gutter=”false”]
person::person()
{
name = "Stranger";
age = 100;
}

person::person(string newName, int newAge)
{
name = newName;
age = newAge;
}

void person::setName(string newName)
{
name = newName;
}

void person::setAge(int newAge)
{
age = newAge;
}[/sourcecode]

Now for the main program. This is very simple; we create an instance of our class, in this example we use the second mutator to set the variables when we create it.

[sourcecode language=”cpp” gutter=”false”]
int main()
{
person steve("Steve", 20);
cout << steve.getAge() << endl;
steve.setAge("21");
cout << steve.getAge() << endl;
}[/sourcecode]

After we create an instance, called steve, we then use the getAge() accessor to get the age variable from the instance. cout is used to output to the terminal / command prompt, the arrows pointing left direct the output towards cout and endl is short for ‘end line’, this is basically the same as going ‘\n’.

After we output the variable once we want to change it using the mutator, in this program we insert the new value of 21 and then call the accessor again.
The functions are called via the instance name followed by a dot.

If your program is working the output should look like this:
[sourcecode]20
21[/sourcecode]

Below is the full source code for this guide.

[sourcecode language=”cpp”]
#include <iostream>
#include <string>

using namespace std;

class person
{
private:
int age;
string name;

public:
// Constructors
person();
person(string newName, int newAge);

// Accessors
string getName() { return name; };
int getAge() { return age; };
// datatype getVariable() { return variable; };

// Mutators
void setName(string newName);
void setAge(int newAge);
// void setVariable(datatype newVariable);

};

person::person()
{
name = "Stranger";
age = 100;
}

person::person(string newName, int newAge)
{
name = newName;
age = newAge;
}

void person::setName(string newName)
{
name = newName;
}

void person::setAge(int newAge)
{
age = newAge;
}

int main()
{
person steve("Steve", 20);
cout << steve.getAge() << endl;
steve.setAge(21);
cout << steve.getAge() << endl;
}[/sourcecode]

Best of luck,
Jon~

2 Replies to “A simple CPP program”

  1. Yes, I couldn’t agree added. And I’d prefer to add that you have received a terrific colour structure in your web-site, I endure with colour blindness and a number of webmasters do not give us a 2nd believed!

Comments are closed.