So I am practicing virtual and I think im geting the hang of it. I was having trouble moreso with the concept of virtual and how it works than anything else but I just made a practice project, and I would like someone to review it so I'm not making mistakes. Thanks!
Code:#include<iostream>using namespace std;
class Vehicle
{
public:
virtual void Accelerate()
{ cout << "Generic Vehicle Acceleration Speed" << endl; }
void Brake()
{ cout << "Generic vehicle Braking Speed" << endl; }
};
class Car : public Vehicle
{
public:
void Accelerate()
{ cout << "Generic Car Class Acceleration Speed" << endl; }
void Brake()
{ cout << "Generic Car Class Braking Speed" << endl; }
virtual void CarHeadlightBrightness()
{ cout << "Default Brightness" << endl; }
};
class MonteCarlo : public Car
{
public:
void Accelerate()
{ cout << "Monte Carlo Accelerating" << endl; }
void Brake()
{ cout << "Monte Carlo Braking" << endl; }
void CarHeadlightBrightness()
{ cout << "Medium Brightness" << endl; }
};
int main()
{
Vehicle *vehicle;
Car *car;
MonteCarlo monteCarlo;
vehicle = &monteCarlo;
car = &monteCarlo; //Use brightness function from Monte Carlo class
//virtual function, binded at runtime
vehicle->Accelerate();
// Non-virtual function, binded at compile time
vehicle->Brake();
car->CarHeadlightBrightness();
}