Understanding Basic Object Oriented Programming with C++
Tags: oop, cpp, programming, objectoriented
Published: 2018-09-30 16:12:10

---
title: Understanding Basic Object Oriented Programming with C++
published: true
description: Learn the fundamentals of OOP 
tags: oop, cplusplus, programming, object-oriented
cover_image: https://res.cloudinary.com/ichtrojan/image/upload/v1538320006/banner_1_s4y70n.png
---

## Prerequisites
  * A C++ Compiler installed on your Machine
  * Basic Understanding of C++
  * An IDE or [Text Editor](https://atom.io)
  * Time
  * Open mind

## Introduction

Object-oriented programming is a paradigm in programming that represents real-life objects or entities in code,
For starters, there are two basic but vital concepts you have to understand in OOP namely **Classes** and **Objects**.

## What is a Class?

A class is a blueprint that specifies the attributes and behavior of an Object. Let's assume we are to create a new class human, we can specify some attributes that are specific to human beings.

```c++
...
class human {
  public:
    string name;
    int age;
};
...
```

In the snippet above we created a class with attributes `name` and `age`.

## What is an Object?

An Object is an instance of a class, an instance in the sense that it can assume any property or attribute from a class. To make things clear we humans are instances of our genes.

Using the Class we created earlier, we can create an object from it and then assign the attributes `name` and `age` a value.

```c++
#include <iostream>
using namespace std;

class human {
  public:
    string name;
    int age;
};

int main () {
  human exhibitA;

  exhibitA.name = "Michael";
  exhibitA.age = 25;

  cout <<"Exhibit A's Name is "<< exhibitA.name << endl;
  cout <<"Exhibit A's Age is "<< exhibitA.age << endl;

  return 0;
}
```

In the snippet above we have successfully created a Class and we have also created an object and assigned that object attributes from the parent class.

![object created](https://res.cloudinary.com/ichtrojan/image/upload/v1538312314/Screenshot_2018-09-30_at_1.57.45_PM_mb0z8z.png)

At this point, you should have an understanding of how classes and objects. Note that a class is not limited to just one object, you can create as much object as you want.

```c++
#include <iostream>
using namespace std;

class human {
  public:
    string name;
    int age;
};

int main () {
  human exhibitA;
  human exhibitB;

  exhibitA.name = "Michael";
  exhibitA.age = 25;

  exhibitB.name = "Nerfetiti";
  exhibitB.age = 24;

  cout <<"Exhibit A's Name is "<< exhibitA.name << endl;
  cout <<"Exhibit A's Age is "<< exhibitA.age << endl;

  cout <<"Exhibit B's Name is "<< exhibitB.name << endl;
  cout <<"Exhibit B's Age is "<< exhibitB.age << endl;

  return 0;
}
```

As seen in the Snippet above, a new object was created and was assigned attributes as defined in the parent class.

![Two objects created](https://res.cloudinary.com/ichtrojan/image/upload/v1538312453/Screenshot_2018-09-30_at_1.59.54_PM_nrejof.png)

## What are Behaviours?

Another concept we would be looking into is behaviors, behaviors are actions that an object can perform. For example, a human can run, eat, sleep and so on.

```c++
#include <iostream>
using namespace std;

class human {
  public:
    string name;
    int age;

    void run () {
      cout << name <<" is running" << endl;
    }
};

int main () {
  human exhibitA;

  exhibitA.name = "Michael";  
  exhibitA.run();

  return 0;
}
```

![Output](https://res.cloudinary.com/ichtrojan/image/upload/v1538312115/Screenshot_2018-09-30_at_1.51.45_PM_bn7aba.png)

As seen in the snippet above, a function was created to pass the public name variable to print "Michael is running". Based on the object assigned to that function/ behavior it would pass the parameters required by that function/ behavior via the dot notation.

## What is Overloading?

Overloading is a concept that allows you to use the same function name to perform different operations based on the parameters passed to it. For this example, we would be creating a new behavior `eat()`, with no parameter, with one parameter and two parameters.

```c++
#include <iostream>
using namespace std;

class human {
  public:
    string name;
    int age;
    
    void eat () {
      cout << name <<" is eating" << endl;
    }
};

int main () {
  human exhibitA;
  
  exhibitA.name = "Michael";
  exhibitA.eat();
  
  return 0;
}

```

As seen above, we created a behavior named `eat` with no parameter, which should return "Michael is eating" or whatever name you declare as `exhibitA` name.

![Michael is eating](https://res.cloudinary.com/ichtrojan/image/upload/v1538313193/Screenshot_2018-09-30_at_2.12.41_PM_jntguj.png)

Moving on we would overload the `eat` behavior by requiring a parameter for another behavior also named eat.

```c++
#include <iostream>
using namespace std;

class human {
  public:
    string name;
    int age;
    
    void eat () {
      cout << name <<" is eating" << endl;
    }
    
    void eat (string food1) {
      cout << name <<" is eating "<< food1 << endl;
    }
};

int main () {
  human exhibitA;
  
  exhibitA.name = "Michael";
  exhibitA.eat();
  exhibitA.eat("Rice");
  
  return 0;
}
```

The snippet above has a second `eat` function but requires a parameter, this parameter was passed to form "Michael is eating Rice" based on the object assigned to it.

![Michael is eating rice](https://res.cloudinary.com/ichtrojan/image/upload/v1538313662/Screenshot_2018-09-30_at_2.20.29_PM_1_vbvi8k.png)

Now finally let us create a new `eat` behavior that accepts two parameters.

```c++
#include <iostream>
using namespace std;

class human {
  public:
    string name;
    int age;
    
    void eat () {
      cout << name <<" is eating" << endl;
    }
    
    void eat (string food1) {
      cout << name <<" is eating "<< food1 << endl;
    }
    
    void eat (string food1, string food2) {
      cout << name <<" is eating "<< food1 << " and " << food2 << endl;
    }
};

int main () {
  human exhibitA;
  
  exhibitA.name = "Michael";
  exhibitA.eat();
  exhibitA.eat("Rice");
  exhibitA.eat("Rice", "Beans");
  
  return 0;
}
```

The final output for this snippet is "Michael is eating Rice and Beans", the last `eat` call triggers the overloaded `eat` behavior with two parameters declared in the human class because we passed exactly two arguments.

![two parameters](https://res.cloudinary.com/ichtrojan/image/upload/v1538314708/Screenshot_2018-09-30_at_2.37.07_PM_ybmmsu.png)

If you Notice all other `eat` behaviors declared still works regardless of other `eat` behaviors declared after or before.

## Conclusion

By now you should have an Idea of how object-oriented programming works but not just in C++, this concept is applicable to other programming languages but in a different syntax. I missed some concept like Inheritance, Polymorphism, Data abstraction and Interfaces. I would try to cover these concepts in another article, Have fun.