Description
OOP in C++ (24 Pages)
🌟In Object-Oriented Programming (OOP) using C++, we define classes to represent real-world entities, encapsulating their attributes (data members) and behaviors (member functions).
🌟For example, a simple Car class could have private data members such as make, model, and year, along with public member functions like startEngine() and stopEngine().
🌟This class could be used to create objects of type Car, where each object can store its own values for the car’s make, model, and year, and perform actions like starting or stopping the engine.
🌟C++ supports key OOP principles such as inheritance, polymorphism, and encapsulation, allowing us to build more complex and reusable software structures.
🌟Here’s a simple implementation of the Car class in C++:
🌟 <iostream> include
using namespace std;
class Car {
private:
string make;
string model;
int year;
public:
// Constructor
Car(string m, string mod, int y) : make(m), model(mod), year(y) {}
// Member function to start the engine
void startEngine() {
cout << “The ” << make << ” ” << model << ” is now running.” << endl;
}
// Member function to stop the engine
void stopEngine() {
cout << “The ” << make << ” ” << model << ” is now off.” << endl;
}
// Function to display car details
void displayDetails() {
cout << “Car: ” << year << ” ” << make << ” ” << model << endl;
}
};
int main() {
Car myCar(“Toyota”, “Corolla”, 2020);
myCar.displayDetails();
myCar.startEngine();
myCar.stopEngine();
return 0;
}
Reviews
There are no reviews yet.