This tutorial aims to introduce you to the fundamental principles of Object-Oriented Programming (OOP) in C++. OOP is a design philosophy that uses "objects" to develop software. The core ideas of OOP are encapsulation, inheritance, polymorphism, and abstraction.
By the end of this tutorial, you will have a clear understanding of these four principles and know how to implement them in C++.
Prerequisites:
- Basic understanding of C++
- Familiarity with the concepts of variables, functions, arrays, and pointers in C++
Encapsulation refers to binding the data (variables) and the methods (functions) together within an object (class). This helps maintain integrity of the data by hiding it from the outside world.
class Car {
private: // data members are private
int speed;
string color;
public: // methods are public
void setSpeed(int s) { speed = s; }
int getSpeed() { return speed; }
};
Inheritance is a process by which one class can acquire the properties (methods and fields) of another. With the use of inheritance, information is made manageable in a hierarchical order.
class Vehicle { // base class
public:
void generalInfo() { cout << "General vehicle information." << endl; }
};
class Car: public Vehicle { // derived class
};
Polymorphism allows one interface to be used for a general class of actions. It provides a means to manage and implement different types of objects (that have a common supertype) in a uniform manner.
class Vehicle { // base class
public:
virtual void generalInfo() { cout << "General vehicle information." << endl; }
};
class Car: public Vehicle { // derived class
public:
void generalInfo() override { cout << "Car info." << endl; }
};
Abstraction simplifies complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
class Vehicle { // base class
public:
virtual void generalInfo() = 0; // pure virtual function
};
Let's look at a series of examples that implement OOP principles in C++.
// Class definition
class Circle {
private:
double radius; // Private attribute
public:
// Setter
void setRadius(double r) {
radius = r;
}
// Getter
double getRadius() {
return radius;
}
double getArea() {
return 3.14 * radius * radius;
}
};
int main() {
Circle c1; // Create an object of Circle
c1.setRadius(5.0); // Call the method with an argument
cout << "Radius is: " << c1.getRadius() << "\n";
cout << "Area is: " << c1.getArea();
return 0;
}
Expected output:
Radius is: 5
Area is: 78.5
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n";
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
Expected output:
Tuut, tuut!
Ford Mustang
In this tutorial, you have learned about the four main concepts of OOP in C++: encapsulation, inheritance, polymorphism, and abstraction. Understanding these ideas is crucial for developing complex C++ applications.
Exercise 1: Create a class 'Rectangle' with two private integer members 'length' and 'breadth'. Create public member functions to set and get the values of both.
Exercise 2: Create a base class 'Shape' with a function 'shapeName'. Derive two classes 'Circle' and 'Square' from Shape, both having a function 'shapeName' which overrides the function in the base class.
Exercise 3: Implement polymorphism by creating a base class 'Animal' with a function 'sound'. Derive classes 'Dog' and 'Cat' from Animal, both having a function 'sound' which overrides the function in the base class.
class Rectangle {
private:
int length;
int breadth;
public:
void setLength(int len) {
length = len;
}
void setBreadth(int b) {
breadth = b;
}
int getLength() {
return length;
}
int getBreadth() {
return breadth;
}
};
class Shape {
public:
virtual void shapeName() { cout << "This is a general shape." << endl; }
};
class Circle: public Shape {
public:
void shapeName() override { cout << "This is a circle." << endl; }
};
class Square: public Shape {
public:
void shapeName() override { cout << "This is a square." << endl; }
};
class Animal {
public:
virtual void sound() { cout << "This is a generic animal sound." << endl; }
};
class Dog: public Animal {
public:
void sound() override { cout << "Woof Woof!" << endl; }
};
class Cat: public Animal {
public:
void sound() override { cout << "Meow Meow!" << endl; }
};
Keep practicing and experimenting with these concepts to improve your understanding. Happy coding!