C++ Bike Race Simulation Program: Object-Oriented Design

Verified

Added on  2022/10/02

|6
|960
|17
Practical Assignment
AI Summary
This assignment presents a C++ program simulating a bike race between two bikes. The program utilizes object-oriented programming principles, defining `Bike1` and `Bike2` classes to represent each bike with attributes like acceleration, speed, and model. The `WinMain` function controls the race, taking user input to accelerate, brake, start, and stop the bikes. The program calculates speed and distance traveled at each time step, displaying the results on the console. The race continues until one or both bikes reach a specified distance. The program then declares the winner based on the time taken to complete the race. The code demonstrates basic game mechanics, user input handling, and object-oriented design, making it a practical example for understanding C++ programming concepts.
Document Page
Source Code
#include <iostream>
#include <string>
#include<stdio.h>
#include<conio.h>
using namespace std;
class Bike1
{
private:
int accln;
int speed;
string model;
public:
Bike1()
{
cout<<"Enter Model Name:"<<endl;
getline(cin,model);
accln=3;
speed=0;
}
void getdetails()
{
cout<<"The Details of the bike are:\n";
cout<<"Model Name:"<<model<<endl;
cout<<"Rate of Acceleration:"<<accln<<"m/s^2"<<endl;
}
int accelerate()
{
speed=speed+accln;
return speed;
}
int brake()
{
speed = speed-accln;
return speed;
}
int start()
{
return accelerate();
}
int stop()
{
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
speed=0;
return speed;
}
};
class Bike2
{
private:
int accln;
int speed;
string model;
public:
Bike2()
{
cout<<"Enter Model Name:"<<endl;
getline(cin,model);
accln=5;
speed=0;
}
void getdetails()
{
cout<<"The Details of the bike are:\n";
cout<<"Model Name:"<<model<<endl;
cout<<"Rate of Acceleration:"<<accln<<"m/s^2"<<endl;
}
int accelerate()
{
speed=speed+accln;
return speed;
}
int brake()
{
speed = speed-accln;
return speed;
}
int start()
{
return accelerate();
}
int stop()
{
speed=0;
return speed;
}
};
Document Page
int WinMain()
{
int d,t1=0,t2=0,flag=0,s1=0,s2=0,d1=0,d2=0;
char ch;
cout<<"Enter the details of 1st Bike:"<<endl;
Bike1 obj1;
cout<<"Enter the details of 2st Bike:"<<endl;
Bike2 obj2;
cout<<"Enter the distance to be covered by the bikes(in metres):";
cin>>d;
cout<<"You are controlling the 1st Bike! Press W to accelerate , S to brake , D to start
and A to stop !"<<endl;
cout<<"Timestamp(sec)\tSpeed(km/hr)\tDistance Travelled"<<endl;
while(1)
{
ch=getch();
if(ch=='d' || flag==1)
{
flag=1;
switch(ch)
{
case'w':
s1=obj1.accelerate();
t1++;
d1=d1+s1;
break;
case's':
if(s1==0)
{
cout<<"Bike is already stopped !"<<endl;
break;
}
s1=obj1.brake();
t1++;
d1=d1+s1;
break;
case'd':
if(s1>0)
{
cout<<"Wrong Input !"<<endl;
break;
}
s1=obj1.start();
t1++;
Document Page
d1=d1+s1;
break;
case 'a':
s1=obj1.stop();
t1++;
break;
default:
cout<<"Wrong Input !"<<endl;
}
if(d1>=d)
d1=d;
cout<<t1<<"\t\t\t"<<s1<<"\t\t"<<d1<<"\t"<<endl;
if(d1==d)
break;
}
}
ch='B';
flag=0;
cout<<"You are controlling the 2nd Bike! Press W to accelerate , S to brake , D to start
and A to stop !"<<endl;
cout<<"Timestamp(sec)\tSpeed(km/hr)\tDistance Travelled"<<endl;
while(1)
{
ch=getch();
if(ch=='d' || flag==1)
{
flag=1;
switch(ch)
{
case'w':
s2=obj2.accelerate();
t2++;
d2=d2+s2;
break;
case's':
if(s2==0)
{
cout<<"Bike is already stopped !"<<endl;
break;
}
s2=obj2.brake();
t2++;
d2=d2+s2;
break;
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
case'd':
if(s2>0)
{
cout<<"Wrong Input !"<<endl;
break;
}
s2=obj2.start();
t2++;
d2=d2+s2;
break;
case 'a':
s2=obj2.stop();
t2++;
break;
default:
cout<<"Wrong Input !"<<endl;
}
if(d2>=d)
d2=d;
cout<<t2<<"\t\t\t"<<s2<<"\t\t"<<d2<<"\t"<<endl;
if(d2==d)
break;
}
}
if(t1<t2)
cout<<"First bike has won the race !";
if(t2<t1)
cout<<"Second bike has won the race !";
if(t1==t2)
cout<<"It's a tie !'";
return 0;
}
Document Page
Output
Input for Bike 1
Input for Bike 2
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]