DEV Community

Cover image for Understanding Basic Object Oriented Programming with C++
Michael Okoh
Michael Okoh

Posted on • Updated on

Understanding Basic Object Oriented Programming with C++

Prerequisites

  • A C++ Compiler installed on your Machine
  • Basic Understanding of C++
  • An IDE or Text Editor
  • 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.

...
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.

#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

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.

#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

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.

#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

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.

#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

Moving on we would overload the eat behavior by requiring a parameter for another behavior also named eat.

#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

Now finally let us create a new eat behavior that accepts two parameters.

#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

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.

Top comments (6)

Collapse
 
unitev1 profile image
Unitev1

Finally understood the topic, this was a good start thanks.

Collapse
 
ichtrojan profile image
Michael Okoh

Glad you did, thanks for the feedback

Collapse
 
ben profile image
Ben Halpern

Love this cover image πŸ˜„

Collapse
 
ichtrojan profile image
Michael Okoh

Thanks 😊

Some comments may only be visible to logged-in visitors. Sign in to view all comments.