Introduction to Classes

C++ provides you with the int, double, string and other data types, have you ever wanted to make your own? As in, create a data type that stores maybe information relevant to a student, building, or even a simple person? You might have also encountered something like a vector. Maybe you want to create something that performs a unique function. Well, you can by using classes.

Example for you to understand classes

When you buy a car, you might have no clue how it works. You simply know that the gas and brake pedal work alongside with the steering wheel in order to perform a desired function.

When creating a class, you create something much like a black box. Inputs go into the box, and outputs follow thereafter producing the desired function. In other words, when you press the gas pedal, you don’t know what happens, you just know the car should accelerate.

When creating classes, just make sure you provide the right functionality and give the user the right tools to get the job done. They don’t care how it works, they simply want it to perform the right duty. Just make sure you do it as efficient as possible. You don’t want them to receive a car that might be faulty do you?

Creating a Student class

Let’s say we want to create a Student that contains an identification number, major, and name. Well, we should start by defining an interface where we’ll have our class definition. An example of the interface is below.

class Student {

     private:
           string major;
           string name;
           int identificationNumber;

     public:
           Student();
           Student(string name);

           void set_Name(string nameOfStudent);
           string get_Name() const;

           void set_Major(string chosenMajor);
           string get_Major() const;
};

The above is the class header that is composed of member variables (major, name, identificationNumber) and functions. It is also composed of public and private member functions and/or variables.

Member simply means part of the class.

Public members are accessible by the user and by the class.

Private members are only accessible by the members of the class.

What do you mean by private?

Imagine you are creating an ATM class. You would probably have a member variable of double amount_of_cash, that would determine the number of cash the certain person has.

By having the amount_of_cash public, the person would have access to this variable and thus would change it to their benefit. By setting this to private you’d then only give permission to member functions of the class to change the value of this variable. An example would be something like void deposit_Cash(double amount_of_cash_deposited); Once the person deposits cash, then the value of the variable would change. By having member variables private, you’re making sure the user can’t change the values of certain variables without proper permission.

The implementation of the functions aren’t displayed, since this is merely the class header. The implementation of the functions may follow right after, or within (I’ll show an example of this later).

The Constructors

The constructor initializes our new object with values we desire by passing in parameters.

A default constructor is simply a constructor with no parameters. It is called when we create an object with no parameters.

Student bob;

We write the implementation of such default constructor in the following way:

Student::Student()
{
     name = "Default";
     major = "Undeclared";
     identificationNumber = 000;
}

The Student:: prefix means we are defining a member function for such class.

We would write the implementation of the other constructor in the following way:

Student::Student(string nameOfStudent)
{
     name = nameOfStudent;
     major = "Undeclared";
     identificationNumber = 000;
}

Having two functions with the same name but with different parameters is called operator overloading.

The accessor functions

The accessor functions simply return specific information about the object without doing any modifications.

string Student::get_Name() const
{
     return name;
}

The const attribute simply denotes that the function will not modify the object.

The following are several examples of how we would use accessor functions.

Student joe = Student("Joe");

cout << joe.get_Name() << endl;

Student joe2 = Student(joe.get_Name());

if ( joe2.get_name() == "Joe" )

The mutator functions

The mutator functions simply mean a function that will cause modifications to the object. You can think of it as mutating the object, "changing its form".

void Student::set_Name(string nameOfStudent)
{
     name = nameOfStudent;
}

The following is an example of how we would use mutator functions.

Student bob;

bob.set_Name("Bob");

Other type of functions

We can also have other type of functions that perform more than simply access or change data. We can have functions that perform a calculation and return a value, or do anything really. An example is the following.

 void Student::print()
{
    cout << "Student Name: " << name << endl;
    cout << "Identification Number: " << identificationNumber << endl;
    cout << "Major: " << major << endl << endl;
}

Visit Page 2 for more on Classes.

Tags: , , , , , , , , , , , , , , , ,