Harold Serrano

View Original

User-Defined Types in C++: Part II

The second mechanism used to create User-Defined Types is a class. A class is made up of data and function members. A function that belongs to a class is called a Method.

Syntax

A Class consists of the following syntax:

class User_Defined_Type_Name{

 private:

 public:

 };

The class keyword declares a class of type "User_Defined_Type_Name" and defines the data members within the class.

A class has a Constructor and Destructor method. The constructor method initializes the class to a safe state. The destructor cleans up after the class object is no longer needed. Furthermore, the constructor and destructor have the same name as the class.

For example, let's define a Student class with a Constructor and Destructor methods.

class Student{

 private:

 public:
    Student();  //1. Constructor

    ~Student(); //2. Destructor

};

Line 1 shows the constructor of the class. Line 2 illustrates the destructor of the class. Notice the tilde symbol "~" prefixed to the destructor's name.

In practice, you create a class object as shown in the snippet below (line 3). Upon creation of the object, the constructor method is called. When the class object is destroyed, the destructor method is called.

int main(){

    Student harold; //3. Create a class object

    return 0;
}

Access Specifiers

As mentioned, a class contains data members and methods. These members can be accessed through the dot operator "." However, their access is controlled by the class Access Specifier.

There are three kinds of Access Specifier: Public, Private or Protected. Members declared within the Private Access Specifier are not accessible outside the class. Whereas, members declared within the Public Access Specifier are accessible outside the class.

For example, line 1 shows the data member "age" defined within the private access specifier.

class Student{

 private:

    int age; //1. define data member

 public:
     Student();  //2. Constructor

     ~Student(); //3. Destructor

};

If you try to access this data member as shown below, you will get an error (line 2). Data members can only be accessed outside the class if it is defined within the public access specifier.

int main(){

    Student harold; //1. Create a class object

    harold.age=29; //2. ERROR. Age is a private data member
    return 0;
}

So, why not declare all members as Public?

C++ is an Object-Oriented Programming (OOP) language. One of the principles of OOP is to encapsulate data members as private and access them through the public access interface.

Instead of making "age" public, we are going to keep it as private but access it through a method declared in the public access specifier, also know as the Public Interface.

For example, in the snippet below, the "setAge" method is declared in the Public Interface (line 4). And will be responsible for setting the value of the data member "age."

class Student{

private:

    int age; //1. define data member

public:
    Student();  //2. Constructor

    ~Student(); //3. Destructor

    void setAge(int uAge); //4. Method to access the private "age" member
};

Now, let's define the "setAge()" method.

Defining Methods in C++

In C++, a method is defined with the following syntax:

return_type class_name::method_name(arguments){}

For example, the setAge() method is defined as follows

void Student::setAge(int uAge){}

The Constructor and Destructor methods are defined similarly but without a return type.

Student::Student(){} //Constructor
Student::~Student(){} //Destructor

The code snippet below shows the definition of the constructor, destructor and "setAge" methods (see lines 6,7 & 8).

Notice an additional method "getAge" defined in line 9. This method retrieves the value of the data member.

class Student{

private:

    int age; //1. define data member

public:
    Student();  //2. Constructor

    ~Student(); //3. Destructor

    void setAge(int uAge); //4. Method to set the value of private "age" member

    int getAge(); //5. Method to retrieve the value of private "age" member
};

Student::Student(){

    std::cout<<"Creating Class"<<std::endl; //6. Print "Creating Class"
}

Student::~Student(){

    std::cout<<"Removing Class"<<std::endl; //7. Print "Removing Class"
}

void Student::setAge(int uAge){

    age=uAge; //8. Set "age" value
}

int Student::getAge(){

    return age; //9. Return "age" value

}

Initializing Data Members

Data members must be initialized in the constructor using an Initialization List. The syntax is as follows:

class_name::constructor():data_member1(value1),data_member2(value2){}

In the example below, the "age" data member is initialized to zero. See line 1.

Student::Student():age(0){ //1. Initialization "age" to zero.

}

Using the Class

The final version of the Student Class is as follows:

class Student{

private:

    int age; //1. define data member

public:
    Student();  //2. Constructor

    ~Student(); //3. Destructor

    void setAge(int uAge); //4. Method to set the value of private "age" member

    int getAge(); //5. Method to retrieve the value of private "age" member
};

Student::Student():age(0.0){

    std::cout<<"Creating Class"<<std::endl; //6. Print "Creating Class"
}

Student::~Student(){

    std::cout<<"Removing Class"<<std::endl; //7. Print "Removing Class"
}

void Student::setAge(int uAge){

    age=uAge; //8. Set "age" value
}

int Student::getAge(){

    return age; //9. Return "age" value

}

In practice, you create a class object as shown in the snippet below (line 1).

Line 2 shows how to set the value of "age" using the "setAge" method. Line 3 shows how to retrieve the value of "age" using the "getAge" method. Lastly, the age is printed in line 4.

int main(){

    Student harold; //1. Create a class object

    harold.setAge(29); //2. set age to 29

    int studentAge=harold.getAge(); //3. get the current age

    std::cout<<"Age: "<<studentAge<<std::endl; //4. print the age

    return 0;
}

Now that you know about classes, it's time to learn about the new and delete operators utilized in the creation and destruction of a class.

Hope this helps.