C++ Object-Oriented Programming (OOP) Introduction šŸŽÆ

beginner
23 min

C++ Object-Oriented Programming (OOP) Introduction šŸŽÆ

Welcome to the exciting world of Object-Oriented Programming (OOP) in C++! This tutorial will guide you through the essentials of OOP, helping you understand why it's crucial for writing clean, maintainable, and efficient code. Let's embark on this journey together!

Understanding Object-Oriented Programming (OOP) šŸ“

OOP is a programming paradigm that focuses on creating objects to represent real-world entities and their interactions. In C++, we can achieve this by using classes and objects.

šŸ’” Pro Tip: Think of a class as a blueprint or template for creating objects, while an object is an instance of a class.

Classes and Objects šŸŽÆ

Creating a Class šŸ“

A class is a collection of variables (called attributes or properties) and functions (called methods) that define a set of properties and behaviors for an object.

cpp
#include <iostream> class Car { public: // Attributes (properties) std::string brand; int year; // Methods void printDetails() { std::cout << "Brand: " << brand << ", Year: " << year << std::endl; } };

In the example above, we have defined a Car class with two attributes (brand and year) and a method (printDetails()).

Creating an Object šŸŽÆ

Now, let's create an object from the Car class.

cpp
int main() { Car myCar; // Creating an object of type Car myCar.brand = "Tesla"; myCar.year = 2022; myCar.printDetails(); // Output: Brand: Tesla, Year: 2022 return 0; }

In the example above, we have created an object called myCar of type Car. We then set the attributes of the object and called the printDetails() method to print the details of the car.

Access Modifiers šŸŽÆ

Access modifiers determine the visibility and accessibility of class members. C++ provides three access modifiers: public, private, and protected.

  • public: Members are accessible from anywhere, including outside the class.
  • private: Members are only accessible within the class.
  • protected: Members are accessible within the class and its derived classes.

Inheritance šŸŽÆ

Inheritance allows one class (called the derived class) to acquire the properties and behaviors of another class (called the base class).

cpp
#include <iostream> class Vehicle { public: std::string type; void printType() { std::cout << "Type: " << type << std::endl; } }; class Car : public Vehicle { public: // Inherited attributes and methods using Vehicle::type; using Vehicle::printType; // Additional attributes and methods std::string brand; int year; void printDetails() { printType(); // Calling the inherited method std::cout << "Brand: " << brand << ", Year: " << year << std::endl; } }; int main() { Car myCar; myCar.type = "Car"; myCar.brand = "Tesla"; myCar.year = 2022; myCar.printDetails(); // Output: Type: Car, Brand: Tesla, Year: 2022 myCar.printType(); // Output: Type: Car return 0; }

In the example above, we have created a Car class that inherits from the Vehicle class. The Car class has access to the type attribute and the printType() method from the Vehicle class, in addition to its own attributes and methods.

Polymorphism šŸŽÆ

Polymorphism allows objects of different classes to be treated as objects of a common base class.

cpp
#include <iostream> #include <vector> class Vehicle { public: virtual void printDetails() = 0; // Pure virtual function }; class Car : public Vehicle { public: std::string brand; int year; void printDetails() override { std::cout << "Brand: " << brand << ", Year: " << year << std::endl; } }; class Bike : public Vehicle { public: std::string brand; int numberOfWheels; void printDetails() override { std::cout << "Brand: " << brand << ", Number of Wheels: " << numberOfWheels << std::endl; } }; int main() { std::vector<Vehicle> vehicles; Car myCar; myCar.brand = "Tesla"; myCar.year = 2022; vehicles.push_back(myCar); Bike myBike; myBike.brand = "Yamaha"; myBike.numberOfWheels = 2; vehicles.push_back(myBike); for (const auto& vehicle : vehicles) { vehicle.printDetails(); } return 0; }

In the example above, we have defined a Vehicle class with a pure virtual function called printDetails(). This function is then overridden by both the Car and Bike classes. In the main() function, we create a vector of Vehicle objects and add Car and Bike objects to it. We can then loop through the vector and call the printDetails() function on each object, and it will print the appropriate details for each object.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the purpose of a class in C++?


That's it for our C++ Object-Oriented Programming (OOP) Introduction! We covered classes, objects, access modifiers, inheritance, and polymorphism. Remember, practice makes perfect, so keep coding and exploring! šŸš€

Next time, we'll dive deeper into C++ OOP, covering topics like constructors, destructors, and more. Stay tuned! šŸŽÆ