This tutorial aims to guide you through the implementation of two fundamental Object-Oriented Programming (OOP) principles, Encapsulation, and Abstraction, in C++.
By the end of this tutorial, you will:
To get the most out of this tutorial, you should have a basic understanding of:
Encapsulation is one of the four fundamentals of OOP. It refers to the bundling of data (variables) and methods (functions) into a single unit called a class. In C++, we achieve encapsulation through 'classes'.
A key feature of encapsulation is data hiding - the class's data can only be accessed through the methods of that class.
class EncapsulationExample {
private:
int x; // private data can only be accessed by methods within the class
public:
void setX(int a) { // method to set the value of x
x = a;
}
int getX() { // method to get the value of x
return x;
}
};
Abstraction, another fundamental concept in OOP, is about hiding the complex details and showing only the essentials. In C++, we achieve this through 'abstract classes' and 'interfaces'.
class AbstractExample {
public:
virtual void function1() = 0; // pure virtual function makes this class Abstract
};
class ImplementAbstract : public AbstractExample {
public:
void function1() { // implementation of the abstract function
//...
}
};
This example shows how to encapsulate data and methods within a class.
class Car {
private:
int speed;
public:
void setSpeed(int s) {
if(s < 0) {
cout << "Speed cannot be negative." << endl;
return;
}
speed = s;
}
int getSpeed() {
return speed;
}
};
int main() {
Car car;
car.setSpeed(50);
cout << "The car is travelling at " << car.getSpeed() << " km/h." << endl;
return 0;
}
In this example, the speed
variable is encapsulated in the Car
class. It can only be accessed and modified through the setSpeed
and getSpeed
methods.
This example shows how to use abstract classes in C++.
class AbstractClass {
public:
virtual void doSomething() = 0; // Pure virtual function
};
class ConcreteClass : public AbstractClass {
public:
void doSomething() {
cout << "Doing something!" << endl;
}
};
int main() {
ConcreteClass cc;
cc.doSomething(); // Prints "Doing something!"
return 0;
}
In this example, AbstractClass
is an abstract class with a pure virtual function. ConcreteClass
is a derived class that implements the pure virtual function.
This tutorial has covered:
To further your understanding, you can implement more classes using these concepts. You can also explore the other two principles of OOP - Inheritance and Polymorphism.
Person
class with encapsulated name
and age
variables. Provide methods to set and get these variables.Shape
class with a pure virtual calculateArea
method. Then create Rectangle
and Circle
classes that implement this method.Person
classclass Person {
private:
string name;
int age;
public:
void setName(string n) {
name = n;
}
void setAge(int a) {
if(a < 0) {
cout << "Age cannot be negative." << endl;
return;
}
age = a;
}
string getName() {
return name;
}
int getAge() {
return age;
}
};
Shape
class and its implementationsclass Shape {
public:
virtual double calculateArea() = 0;
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double calculateArea() {
return width * height;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() {
return 3.14 * radius * radius;
}
};
These exercises help you understand encapsulation and abstraction better. Try creating more examples for more practice. Happy coding!