0% found this document useful (0 votes)
113 views

National University of Sciences and Technology (NUST), H-12 Sector Islamabad

This document contains code for a traffic light simulation program written in C++. It includes class definitions for Lights (with subclasses Red, Orange, Green), Sides (with subclasses North, South, East, West), and functions to control the light signals and side movements. The main program initializes objects for each side and runs a loop that sets light signals, pauses for the time duration of each signal, and displays the side movements. It takes user input for the number of vehicles on a side to determine the green light duration.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

National University of Sciences and Technology (NUST), H-12 Sector Islamabad

This document contains code for a traffic light simulation program written in C++. It includes class definitions for Lights (with subclasses Red, Orange, Green), Sides (with subclasses North, South, East, West), and functions to control the light signals and side movements. The main program initializes objects for each side and runs a loop that sets light signals, pauses for the time duration of each signal, and displays the side movements. It takes user input for the number of vehicles on a side to determine the green light duration.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

National University of Sciences and

Technology (NUST), H-12 Sector


Islamabad

Object Oriented Programming

Name: Ch Muhammad Shaheer Yasir

Class: BEE 11-B

Registration Number: 286021


Assignment No 3

Main.cpp:
//Name: Ch Muhammad Shaheer Yasir
//Class: BEE 11-B
//Registration Number: 286021
//This file contains the main program
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include "Header.h"
using namespace std;
void timer(Lights *obj)
{
int time = obj->getTime();
Sleep(time);
}
Lights* turnSignal(Lights *obj)
{
Red * obj2 = new Red;
Orange * obj3 = new Orange;
Green *obj4 = new Green;
if (obj->getLightColor() == "RED")
{
obj = dynamic_cast <Lights*> (obj3);
return obj;
}
else if (obj->getLightColor() == "ORANGE")
{
obj = dynamic_cast <Lights*> (obj4);
return obj;
}
else if (obj->getLightColor() == "GREEN")
{
obj = dynamic_cast <Lights*> (obj2);
return obj;
}
}
int main()
{
cout << "\t\t\t\t TRAFFIC MANAGEMENT PROGRAM" << endl << endl;
cout << "ENTER SPACE TO START PROGRAM!!" << endl << endl;
_getch();
North * northObject = new North;
West * westObject = new West;
South * southObject = new South;
East * eastObject = new East;
Green * greenObject = new Green;
for (int i = 0; i <= 3; i++)
{
greenObject->signalTime(northObject);
cout << "THE " << greenObject->getLightColor() << " SIGNAL IS ON! " << endl
<< endl;
northObject->sideMoving();
timer(greenObject);
Lights * obj = turnSignal(greenObject);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON! " << endl <<
endl;
northObject->sideWaiting();
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON!" << endl << endl;
eastObject->sideReady();
obj->signalTime(eastObject);
timer(obj);
obj = turnSignal(obj);
obj->signalTime(eastObject);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON! " << endl <<
endl;
eastObject->sideMoving();
timer(obj);
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON! " << endl <<
endl;
eastObject->sideWaiting();
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON!" << endl << endl;
southObject->sideReady();
obj->signalTime(southObject);
timer(obj);
obj = turnSignal(obj);
obj->signalTime(southObject);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON! " << endl <<
endl;
southObject->sideMoving();
timer(obj);
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON! " << endl <<
endl;
southObject->sideWaiting();
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON!" << endl << endl;
westObject->sideReady();
obj->signalTime(westObject);
timer(obj);
obj = turnSignal(obj);
obj->signalTime(westObject);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON!" << endl << endl;
westObject->sideMoving();
timer(obj);
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON!" << endl << endl;
westObject->sideWaiting();
obj = turnSignal(obj);
cout << "THE " << obj->getLightColor() << " SIGNAL IS ON!" << endl << endl;
northObject->sideReady();
}
}
Header.h:
//Name : Ch Muhammad Shaheer Yasir
//Class: BEE 11-B
//Registration Number: 286021
//This header file contain declaration of classes and functions
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
class Sides{
protected:
string sideName;
public:
string getSideName();
void sideMoving();
void sideReady();
void sideWaiting();
};
class Lights{
protected:
string lightColor;
int time;
public:
string getLightColor();
void setTime(int t);
int getTime();
virtual void signalTime(Sides *obj) = 0;
};
class Red : public Lights{
public:
Red();
void signalTime(Sides *obj);
};
class Orange : public Lights{
public:
Orange();
void signalTime(Sides *obj);
};
class Green : public Lights{
public:
Green();
void signalTime(Sides *obj);
};
class North : public Sides{
public:
North();
};
class West : public Sides{
public:
West();
};
class South : public Sides{
public:
South();
};
class East : public Sides{
public:
East();
};

Test.cpp:
// Name: Ch Muhammad Shaheer Yasir
// Class: BEE 11-B
// Registraion Number: 286021
// This file contains the implementation of class functions
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include "Header.h"
using namespace std;
string Sides::getSideName()
{
return sideName;
}
void Sides :: sideMoving()
{
cout << sideName << " IS MOVING NOW!" << endl << endl;
}
void Sides :: sideReady()
{
cout << sideName << " IS READY TO MOVE!" << endl << endl;
}
void Sides :: sideWaiting()
{
cout << sideName << " IS STOPPED AND WAITING NOW!" << endl << endl << endl;
}
string Lights :: getLightColor()
{
return lightColor;
}
void Lights :: setTime(int t)
{
time = t * 1000;
}
int Lights :: getTime()
{
return time;
}
Red :: Red(){ lightColor = "RED"; }
void Red :: signalTime(Sides *obj)
{
setTime(60);
}
Orange :: Orange(){
lightColor = "ORANGE";
}
void Orange :: signalTime(Sides *obj)
{
setTime(3);
}
Green :: Green(){
lightColor = "GREEN";
}
void Green :: signalTime(Sides *obj)
{
int numberOfVehicles;
cout << "PLEASE ENTER THE NUMBER OF VEHICLES ON " << obj->getSideName() << " : ";
cin >> numberOfVehicles;
cout << endl;
if (numberOfVehicles <= 5)
{
time = 3000;
}
else if (numberOfVehicles <= 20 && numberOfVehicles > 5)
{
time = 5000;
}
else if (numberOfVehicles <= 50 && numberOfVehicles > 20)
{
time = 10000;
}
else
{
time = 30000;
}
}
North :: North(){
sideName = "NORTH";
}
West :: West(){
sideName = "WEST";
}
South :: South(){
sideName = "SOUTH";
}
East :: East(){
sideName = "EAST";
}

You might also like