Free Hosting

Classes and class members


While C++ provides a number of basic data types (eg. char, int, long, float, double, etc…) that are often sufficient for solving relatively simple problems, it can be difficult to solve complex problems using just these types. One of C++’s more useful features is the ability to define your own data types that better correspond to the problem being worked upon. You have already seen howenumerated types and structs can be used to create your own custom data types.
Here is an example of a struct used to hold a date:
1
2
3
4
5
6
struct DateStruct
{
    int nMonth;
    int nDay;
    int nYear;
};
Enumerated types and structs represent the traditional non-object-oriented programming world, as they can only hold data. If you want to initialize or manipulate this data, you either have to do so directly, or write functions that take a DateStruct as a parameter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Declare a DateStruct variable
DateStruct sToday;
// Initialize it manually
sToday.nMonth = 10;
sToday.nDay = 14;
sToday.nYear = 2020;
// Here is a function to initialize a date
void SetDate(DateStruct &sDate, int nMonth, int nDay, int Year)
{
    sDate.nMonth = nMonth;
    sDate.nDay = nDay;
    sDate.nYear = nYear;
}
// Init our date to the same date using the function
SetDate(sToday, 10, 14, 2020);
In the world of object-oriented programming, we often want our types to not only hold data, but provide functions that work with the data as well. In C++, this is done via the class keyword. Using the class keyword defines a new user-defined type called a class.
Classes
In C++, classes are very much like structs, except that classes provide much more power and flexibility. In fact, the following struct and class are effectively identical:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct DateStruct
{
    int nMonth;
    int nDay;
    int nYear;
};
class Date
{
public:
    int m_nMonth;
    int m_nDay;
    int m_nYear;
};
Note that the only difference is the public: keyword in the class. We will discuss it’s function in the next lesson.
Just like a struct definition, a class definition does not declare any memory. It only defines what the class looks like. In order to use a class, a variable of that class type must be declared:
1
2
3
4
5
6
Date cToday; // declare a variable of class Date
// Assign values to our members using the member selector operator (.)
cToday.m_nMonth = 10;
cToday.m_nDay = 14;
cToday.m_nYear = 2020;
In C++, when we declare a variable of a class, we call it instantiating the class. The variable itself is called an instance of the class. A variable of a class type is also called an object.
Member Functions
In addition to holding data, classes can also contain functions! Here is our Date class with a function to set the date:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Date
{
public:
    int m_nMonth;
    int m_nDay;
    int m_nYear;
    void SetDate(int nMonth, int nDay, int nYear)// Member
    {
        m_nMonth = nMonth;
        m_nDay = nDay;
        m_nYear = nYear;
    }
};
Just like member variables of a struct or class, member functions of a class are accessed using the member selector operator (.):
1
2
Date cToday;
cToday.SetDate(10, 14, 2020); // call SetDate() on cToday
Note that in the original struct version of SetDate(), we needed to pass the struct itself to the SetDate() function as the first parameter. Otherwise, SetDate() wouldn’t know what DateStruct we wanted to work on.
However, in our class version of SetDate(), we do not need to pass cToday to SetDate()! Because SetDate() is being called on cToday, the member variables in SetDate() will refer to the member variables of cToday! Thus, inside function SetDate(), m_nDay is actually referring to cToday.m_nDay. If we called cTomorrow.SetDate(), m_nDay inside of SetDate() would refer tocTomorrow.m_nDay.
Using the “m_” prefix for member variables helps distinguish member variables from function parameters or local variables inside member functions. This is useful for several reasons. First, when we see an assignment to a variable with the “m_” prefix, we know that we are changing the state of the class. Second, unlike function parameters or local variables, which are declared within the function, member variables are declared in the class definition. Consequently, if we want to know how a variable with the “m_” prefix is declared, we know that we should look in the class definition instead of within the function.
By convention, class names should begin with an upper case letter.
Here’s another example of a class:
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
#include <iostream>
class Employee
{
public:
    char m_strName[25];
    int m_nID;
    double m_dWage;
    // Set the employee information
    void SetInfo(char *strName, int nID, double dWage)
    {
        strncpy(m_strName, strName, 25);
        m_nID = nID;
        m_dWage = dWage;
    }
    // Print employee information to the screen
    void Print()
    {
        using namespace std;
        cout << "Name: " << m_strName << "  Id: " <<
            m_nID << "  Wage: $" << m_dWage << endl;
    }
};
int main()
{
    // Declare two employees
    Employee cAlex;
    cAlex.SetInfo("Alex", 1, 25.00);
    Employee cJoe;
    cJoe.SetInfo("Joe", 2, 22.25);
    // Print out the employee information
    cAlex.Print();
    cJoe.Print();
    return 0;
}
This produces the output:
Name: Alex  Id: 1  Wage: $25
Name: Joe  Id: 2  Wage: $22.25
Warning: One of the most common C++ mistakes is to forget to end all class (and struct) declarations with a semicolon. This can cause the compiler to report all sorts of weird, seemingly-unrelated errors!

0 comments:

Blogger Template by Clairvo