Basic Object Oriented Programming with C++

Siddhant Khare
5 min readOct 22, 2020

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 = "Siddhant";
exhibitA.age = 18;
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.

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 = "Siddhant";
exhibitA.age = 18;
exhibitB.name = "AnyName";
exhibitB.age = 20;
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.

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 = "Siddhant";
exhibitA.run();
return 0;
}

As seen in the snippet above, a function was created to pass the public name variable to print “Siddhant 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 = "Siddhant";
exhibitA.eat();
return 0;
}

As seen above, we created a behavior named eat with no parameter, which should return "Siddhant is eating" or whatever name you declare as exhibitA name.

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 = "Siddhant";
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 "Siddhant is eating Rice" based on the object assigned to it.

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 = "Siddhant";
exhibitA.eat();
exhibitA.eat("Rice");
exhibitA.eat("Rice", "Beans");
return 0;
}

The final output for this snippet is “Siddhant 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.

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.

--

--