Post

Object Oriented Programming with C++

Classes & Objects and how to maniuplate data with member functions

Object Oriented Programming with C++

Object Oriented Programming

Before knowing what Object Oriented Programming (OOP) is, it’s important to know what Object Oriented Design is. The first steps to Object Oriented Design (OOD) is to identify components called objects. An object combines both data and the operations on that object.

Let’s use real world examples, after all, with most physical world objects, we provide it attributes or descriptions and apply some form of action with it.

A dog for example is our physical world object and for the purpose of this example, we won’t be giving specific details. Just vague descriptions of our dog - much like a blueprint. With a dog, we want to give it a name. Some action or behavior the dog may do is bark or eat. For simplicity, our dog will just bark.

Our object oriented design example may look like the following:

1
2
3
4
5
6
Dog
    attributes: 
        name

    interactions/behavior:   
        bark

What we just created is the first step to OOD. Now we will use object oriented programming (OOP) to implement OOD.

Classes

A class is a general blueprint of what our object is. Its a collection of components. Those components are called members of the class. In the example earlier, that is the blueprint of what we are about to create.

Here is an example of a class using C++ that is similar to what we created earlier but here we created it in a more programmy way.

1
2
3
4
5
6
7
8
9
10
11
// this is a Class of Dog
class Dog{
    // Class members
    // attributes
    private:
        string name;

    // actions (functions) we can perform
    public:
        void bark() const;
}

Object

Okay, this looks good. So we should be able to create an object with this. An object is an instance of a class, created from the class blueprint. An object has its own unique data. You can create multiple different objects using one class.

The following example creates an object called dog1 from the class Dog.

1
2
3
4
5
6
int main(){
    // Declaring an object called 'dog1'
    Dog dog1;

    return 0;
}

Member Functions

Now that we created these objects. We probably want to set the name for the object dog1 we created. But in order to do that, we need to create two new member functions we can use on the object to interact with it.

From our class Dog earlier we created earlier, we will also be adding two member functions setName(), getName(). These member functions will allow us to set the name of our dog, as well as retrieve that information.

1
2
void setName(string)const;
string getName(string);

We have declared our member functions. Let’s define what these member functions will do.

1
2
3
4
5
6
void setName(string name){
    this->name = name;
}
string getName(){
    return name;
}

the setName(string name) member function takes in a string name and sets that name to the object’s member variable. the getName() member function returns the value that is stored in the object’s member variable.

Putting it all together

1
2
3
4
5
6
7
8
9
10
11
12
13
class Dog{
    private:
        string name;

    public:
        void setName(string name){
            this->name = name;
        }
        string getName() const{
            return name;
        }
        void bark() const;
};

This looks amazing, we are definitely cooking with butter now! We can now manipulate the object’s data(s) and be able to retrieve those information.

We will be calling the setName() member function on the dog1 object and pass the string name “Casper”. This will set the member variable name to “Casper” and when we call the member function getName() on dog1 object, we get the name of the dog.

1
2
3
4
5
6
7
8
9
int main(){
    // Declaring an object called 'dog1'
    Dog dog1;                 

    dog1.setName("Casper");   //  sets the name to Casper
    cout << dog1.getName();   //  outputs Casper

    return 0;
}

Cool, let’s do more!

Our bark() function looks boring and isn’t really doing anything. Let’s define what it will do… Let’s add the word “WOOF!” to it, so when we call the function bark() on the dog, it will say “WOOF!”

1
2
3
    void bark() const{
        cout << "WOOF!"
    };

Back to our main code. When we call the bark() member function on dog1, we get the output “WOOF!”

1
2
3
4
5
6
7
8
9
10
11
int main(){
    // Declaring an object called 'dog1'
    Dog dog1;

    dog1.setName("Casper");   //  sets the name to Casper
    cout << dog1.getName()    //  Casper

    dog1.bark();              //  WOOF!

    return 0; 
}

In Summary

Object Oriented Programming uses classes and objects by packaging the data and operations on an object, we can then manipulate the object by creating our own member functions, much like the bark() member function for our object to use and operate on. With OOP we can represent data in such a way that we can manipulate it. This makes it so that we can reuse code and allows us to represent real-world entities.

With the Dog class, we can create more objects. We can create another dog2, dog3, etc... objects all by reusing the same class blueprint and for each object give it a different name using setName() member function.

This was a simple program, but im sure as I continue my programming journey, I will be learning and writing more key features of Object Oriented Programming concepts.

Thanks again for taking the time to read! Stay tune for more!

This post is licensed under CC BY 4.0 by the author.

Trending Tags