Abstraction.cpp
#include<bits/stdc++.h>
using namespace std;
/*
Abstract class -->
1. Act as an interface for the outside world to operate the car.
2. This abstract class tells 'WHAT' all it can do rather then 'HOW' it does that.
3. Since this is an abstract class we cannot directly create Objects of this class.
4. We need to Inherit it first and then that child class will have the responsibility to
provide implementation details of all the abstract (virtual) methods in the class.
5. In our real world example of Car, imagine you sitting in the car and able to operate
the car (startEngine, accelerate, brake, turn) just by pressing or moving some
pedals/buttons/ steer the wheel etc. You dont need to know how these things work, and
also they are hidden under the hood.
6. This Class 'Car' denotes that (pedals/buttons/steering wheel etc).
*/
class Car{
public:
virtual void startEngine()=0;
virtual void shiftGear(int gear)=0;
virtual void accelerate()=0;
virtual void brake()=0;
virtual void stopEngine()=0;
virtual ~Car() {}
};
/*
1. This is a Concrete class (A class that provide implementation details of an abstract class).
Now anyone can make an Object of 'SportsCar' and can assign it to 'Car' (Parent class) pointer
(See main method for this)
2. In our real world example of Car, as you cannot have a real car by just having its body only
(all these buttons or steering wheel). You need to have the actual implementation of 'What' happens
when we press these buttons. 'SportsCar' class denotes that actual implementation.
3. Therefore, to denote a real world car in programming we created 2 classes.
One to denote all the user-interface like pedals, buttons, steering wheels etc ('Car' class).And,
Another one to denote the actual car with all the implementations of these buttons (SportsCar' class).
*/
class SportsCar : public Car{
public:
string brand;
string model;
bool isEngineOn;
int currentSpeed;
int currentGear;
SportsCar(string b,string m){
this->brand=b;
this->model=m;
isEngineOn=0;
currentSpeed=0;
currentGear=0;
}
void startEngine(){
isEngineOn=true;
cout<<brand<<' '<<model<<" :"<<"Schrodinger has started the engine"<<endl;
}
void shiftGear(int gear){
if(!isEngineOn){
cout<<brand<<' '<<model<<" :"<<"Schrodinger has not yet started the engine so cant shift the gear"<<endl;
return;
}
currentGear=gear;
cout<<brand<<' '<<model<<" :"<<"Schrodinger has shifted the gear to :"<<gear<<endl;
}
void accelerate(){
if(!isEngineOn){
cout<<brand<<' '<<model<<" :"<<"Schrodinger has not yet started the engine so cant accelerate"<<endl;
return;
}
currentSpeed+=50;
cout<<brand<<' '<<model<<" :"<<"Schrodinger has increased the speed to :"<<currentSpeed<<endl;
}
void brake(){
currentSpeed-=20;
currentSpeed=max(0,currentSpeed);
cout<<brand<<' '<<model<<" :"<<"Schrodinger has applied brake and speed reduced to "<<currentSpeed<<endl;
}
void stopEngine(){
currentGear=0;
currentSpeed=0;
isEngineOn=0;
cout<<brand<<' '<<model<<" :"<<"sadly Schrodinger has turned the engine off"<<endl;
}
};
int main(){
Car* schrodinger=new SportsCar("BMW","M4");
schrodinger->startEngine();
schrodinger->shiftGear(1);
schrodinger->accelerate();
schrodinger->shiftGear(2);
schrodinger->accelerate();
schrodinger->brake();
schrodinger->stopEngine();
delete schrodinger;
return 0;
}