Tuesday, January 18, 2011

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 how enumerated 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:
1struct DateStruct
2{
3    int nMonth;
4    int nDay;
5    int nYear;
6};
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:
01// Declare a DateStruct variable
02DateStruct sToday;
03 
04// Initialize it manually
05sToday.nMonth = 10;
06sToday.nDay = 14;
07sToday.nYear = 2020;
08 
09// Here is a function to initialize a date
10void SetDate(DateStruct &sDate, int nMonth, int nDay, int Year)
11{
12    sDate.nMonth = nMonth;
13    sDate.nDay = nDay;
14    sDate.nYear = nYear;
15}
16 
17// Init our date to the same date using the function
18SetDate(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:
01struct DateStruct
02{
03    int nMonth;
04    int nDay;
05    int nYear;
06};
07 
08class Date
09{
10public:
11    int m_nMonth;
12    int m_nDay;
13    int m_nYear;
14};
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:
1Date cToday; // declare a variable of class Date
2 
3// Assign values to our members using the member selector operator (.)
4cToday.m_nMonth = 10;
5cToday.m_nDay = 14;
6cToday.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:
01class Date
02{
03public:
04    int m_nMonth;
05    int m_nDay;
06    int m_nYear;
07 
08    void SetDate(int nMonth, int nDay, int nYear)
09    {
10        m_nMonth = nMonth;
11        m_nDay = nDay;
12        m_nYear = nYear;
13    }
14};
Just like member variables of a struct or class, member functions of a class are accessed using the member selector operator (.):
1Date cToday;
2cToday.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 to cTomorrow.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:
01#include <iostream>
02class Employee
03{
04public:
05    char m_strName[25];
06    int m_nID;
07    double m_dWage;
08 
09    // Set the employee information
10    void SetInfo(char *strName, int nID, double dWage)
11    {
12        strncpy(m_strName, strName, 25);
13        m_nID = nID;
14        m_dWage = dWage;
15    }
16 
17    // Print employee information to the screen
18    void Print()
19    {
20        using namespace std;
21        cout << "Name: " << m_strName << "  Id: " <<
22            m_nID << "  Wage: $" << m_dWage << endl;
23    }
24};
25 
26int main()
27{
28    // Declare two employees
29    Employee cAlex;
30    cAlex.SetInfo("Alex", 1, 25.00);
31 
32    Employee cJoe;
33    cJoe.SetInfo("Joe", 2, 22.25);
34 
35    // Print out the employee information
36    cAlex.Print();
37    cJoe.Print();
38 
39    return 0;
40}
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!

No comments: