Object-Oriented Programming in C++
By: Fidan Musazade
Tags: Encapsulation, Object Oriented, Polymorphism, C Programming, C

Source: OnceHub

C++ is one of the most powerful languages currently present. And, believe it or not, one of the reasons it emerged, is support for OOP (object-oriented programming).

OOP means creating and interacting with objects instead of writing procedural code. If you imagine a human being as a class, for example, it may have subclasses of Female and Male, and so on. There are various benefits brought by OOP support in the language, however, that is out of the scope of the current discussion. The article aims at exploring how OOP is supported in C++.

As with any objects, C++ objects have methods and properties. Methods are represented as functions that can be used on a given object and properties are data about a given object. For example, a class Dog may have properties of the breed, color, age, and methods of run, bark, and so on. This is how a sample C++ class looks like:

#include <iostream>

using namespace std; class Dog

{

//properties

char name[20];

char breed[20];

int age; public:

//methods

void run(){ cout << "Running" << endl;}

void bark(){

if (self.age<20) cout << "Hav-hav" << endl;

else cout << "Woof-woof" << endl;}

};



int main()

{

//barkie is an object of class Dog

Dog barkie;

}

Encapsulation

As in other OOP supporting languages, in C++, you put the data and functions associated with a certain class inside the class definition and therefore can make properties private and have public methods to modify them. For example, one may have getters and setters to access and modify data. Let’s extend our previous code to have this concept:

#include <iostream>

using namespace std; class Dog

{

private:

//properties

char name[20];

char breed[20];

int age; public:

//methods

void run(){ cout << "Running" << endl;}

void bark(){

if (self.age<20) cout << "Hav-hav" << endl;

else cout << "Woof-woof" << endl;}

void setAge(int age){self.age = age;} // setter

int getAge() {return self.age;} // getter };



int main()

{

//barkie is an object of class Dog

Dog barkie;

barkie.setAge(20); //now barkie's age is 20

cout << barkie.getAge(); //this will print 20

}

Abstraction

It is possible in C++ to hide internal details of the program and provide only meaningful methods to execute them. For example, if something can be calculated inside the class using data and methods present, abstraction can be achieved by hiding those details and having only method call in the main part of the program. Looking at our example above, we do not care about how the bark of the dog is implemented, we call bark on our object and get the output expected.

Inheritance

Objects in C++ can also have parents and inherit their properties and methods. This makes it possible to build a basic class and extend it to newer objects. For example, we could have an Animal class and extend it to get Dog, Cat, and other classes.

class Animal {

// eat() function

// sleep() function

};



class Dog : public Animal {

// bark() function

}; class Cat: public Animal {

// meow() function

};

We could have a blueprint for an animal and later create similar functions. This powerful feature makes C++ even more powerful.

With the addition of inheritance, some aspects become vaguer. For example, are private features inherited? What about protected and public? The answer is quite simple. Private features are not inherited. If we want to restrict the access from outside the class, but want the property or method to be inherited we need to use protected. As it is logically expected, public ones can be accessed from anywhere and are inherited.

Now regarding how we inherit classes. In the examples above, we use public access mode. However, there are two more options possible: private and protected. Now on the differences:

Public access mode makes the class inherit everything as they are. For example, if age is public property in the parent’s definition, it is inherited as public. Private access mode makes everything private when inherited. Protected access mode converts all public members to protected.

Overriding is also possible in C++. If parent class and child class have a member with the same name, the child’s member is used.

Polymorphism

Last, but not least, polymorphism is also supported. It refers to the ability of the object to take on many forms. There are, in total, four ways to use polymorphism in C++: function overloading, operator overloading, function overriding, and virtual functions.

For example, two functions could have the same names and have different arguments. And then, depending on the arguments, one of them is called. This is called function overloading. Example code:

#include <iostream>

using namespace std; int calcSalary(int base, int premium) {

return base + premium;

} double calcSalary(double ratio, double base) {

return base*ratio;

} int calcSalary(int base, int premium, int bonus) {

return base + premium + bonus;

} int main(void) {

cout << calcSalary(5, 10); //15 is printed

cout << calcSalary(0.5, 1500.0); //750 is printed

cout << calcSalary(1000, 100, 100); //1200 is printed

return 0;

}

Another possible option is operator overloading. Here, we can define a new logic for a usual operator we know. However, you cannot do it for primitive types, such as int, double, float, etc. Example:

#include <iostream>

using namespace std; class Human {

private:

int age;

char name[20];

public:

void operator ++() {age = age + 1;} //operator overloading

void setAge(int age) {self.age = age;}

}; int main(void) {

Human fidan;

fidan.setAge(22);

fidan++; //now age is 23

}

As discussed in the previous section, functions can be overridden in child classes, and depending on the object calling this function, the same function can execute differently. For example, Animal class may have an “eat” function. If Dog class inherits from Animal and also has an “eat” function written explicitly in Dog, then if “eat” is called on Dog, it will perform what is in Dog’s specification. But if you call “eat” on Animal, its eat function will be performed. This is known as function overriding.

Another important concept in C++ is virtual functions. These functions are used when we use pointers of the parent class to point at child class. In this case, to make sure that the function is overridden, we have to use virtual keyword.

#include <iostream>

using namespace std;



class Parent {

public:

virtual void print() {cout << "I am the parent" << endl;}

};



class Child: public Base {

public:

void print() {cout << "I am the child" << endl;}

};



int main() {

Child childInstance;

Parent* parentPoint = &childInstance;

parentPoint->print(); //this will print "I am the child"

return 0;

}

Therefore, we have achieved overriding by making the parent’s function virtual.