Professional Documents
Culture Documents
Name :Mohit
Roll no.= 2K22/A2/115
Programs are divided into small Programs are divided into objects or
programs or functions. entities.
Its main aim is to improve and increase Its main aim is to improve and increase both
quality, clarity, and development time quality and productivity of system analysis
of computer program. and design.
Q2. Take any real world entity and write a code snippet for identification of class, object,
encapsulation, abstraction, polymorphism, inheritance
#include <iostream>
using namespace std;
// Class definition
class Car {
public:
string make;
string model;
int year;
bool isRunning;
void start() {
isRunning = true;
cout << year << " " << make << " " << model << " is now running." << endl;
}
void stop() {
isRunning = false;
cout << year << " " << make << " " << model << " has stopped." << endl;
}
};
// Subclass ElectricCar inheriting from Car
class ElectricCar : public Car {
public:
ElectricCar(string mk, string mdl, int yr) : Car(mk, mdl, yr) {}
int main() {
// Creating objects
Car car1("Toyota", "Camry", 2022);
ElectricCar electricCar1("Tesla", "Model S", 2023);
electricCar1.start();
electricCar1.stop();
// Polymorphism
Car* cars[] = {&car1, &electricCar1};
for (Car* car : cars) {
car->start();
}
return 0;
}.