You are on page 1of 7

National University of Computer and Emerging Sciences

School of Computing FALL 2023 Islamabad Campus

CS-1004: Object-Oriented Programming


Assignment 3
(Deadline: 23rdNov, 2023 11:59 PM)

Submission: Create a .cpp file for each question, name the .cpp file with your Roll
number and header . E.g ROLLNUM_SECTION_Car.h and
ROLLNUM_SECTION_Car.cpp. Incase of wrong or false naming conventions zero
marks will be awarded.
Implement each question in the created .cpp file, you will also need to include the
header in your cpp file. You need to submit both header file and cpp file for each
question.
All cpp files must contain your name, student-id, and assignment # on the top of the
file in the comments. Place all your .cpp files (only) in a folder named your
ROLLNUM_SECTION (e.g. 22i-0001_A). Compress the folder as a zip file and
upload on google classroom.
Deadline: The deadline to submit the assignment is 23rd Nov, 2023 11:59 PM. Correct
and timely submission of the assignment is the responsibility of every student.
Plagiarism: This is an individual assignment; any kind of plagiarism will result in a
zero.
Restrictions: The use of libraries such as strings, cmath is NOT allowed. DO NOT
change the function headers/prototypes in the header files or the test cases. All
functions will be out-of-line.
Evaluation: The assignment is of 200 marks.
Note: If you fail to follow any of the above instructions. You will be awarded zero in
this assignment
Q1: A triangle is defined as a two-dimensional shape with three sides, three interior
angles, and three vertices where the sum of all interior angles must be equal to 180°. You
are required to implement a Triangle class which will consist of a POINT class having
the vertices x and y. You will need to implement the following functions in triangle class:
(15 marks)

Class point{
int x,y;
Public:
point();
point(int x,int y);
int getx();
int gety();
void setx(int);
void sety(int);
}

Class triangle{
private:
//think of the data members
Public:
triangle();
triangle(triangle& obj);
triangle(int x1,int y1.int x2.int y2.int x3,int y3);
int calculateArea();
bool check(); //this function will check if a triangle can be formed from the vertices or
not?
/* Hint: a triangle is formed if the sum of two sides of a triangle is greater than the third
side. If the result is false then you are supposed to show a prompt that the user has
entered wrong vertices and the triangle is not formed */
void display(); //this function will display the AREA and TYPE (equilateral, isosceles,
and scalene) of the triangle
bool operator==(const triangle& obj);
};
You are required to create a main function for this question in which you will implement
all the functionalities. Create objects of the triangle class and provide appropriate inputs
to check all scenarios in which a triangle can or cannot be formed. Additionally,
implement a scenario in which you are asked to determine whether two triangles are
equal or not.

Q2: Trains in the modern day world play an important role in transportation. They have
minimized the time taken to travel from one city to another. A train consists of different
compartments and each of them has a specific number of passengers. Your task is to
design a train by using the basic concepts of OOP which you have learned so far. You
will need to implement classes of: coach, seat and Train. You might need to use your
imagination powers for the sole purpose of implementation of these concepts. The basic
implementation of train class is given below: (40 marks)

class train{
Private:
//think of private data members
Public:
train();
train operator+(coach& obj); //add a coach into the train
train operator-(coach&obj); //remove a coach from the train
train operator+(seat& obj); //Add a seat in a coach
train operator-(seat& obj); //remove a seat a coach
friend ostream operator<<(ostream& out , const& obj); // this function will display all the
elements of the train, including total passengers, berths/seats and coaches
};

You are required to make a main function where you are going to make objects for the
train, passengers, seats and coaches respectively and then show your working.
Sample output:
NOTE:Initially the train will not have any coaches,passengers and seats
Train t;
cout<<t;

total coaches: 0
Total seats: 0

Coach c;
t+=c;
cout<<t;

Total coaches: 1
Total seats: 0

Seat s;
t+=s;

Total coaches: 1
Total passengers: 0
Total seats: 1

NOTE: The provided sample output serves as a general blueprint to help you
comprehend the functioning of the code
Q3: A metro station is a crucial component of a rapid transit system, facilitating the
efficient movement of people within urban areas. It serves as a stopping point for trains in
the subway or metro system, providing a platform for passengers to board and disembark.
Each metro station has a unique name, contributing to its identity within the transit
network. Now that you understand the concept of a metro station, let's put your
knowledge into practice. Your task is to create a simple representation of a metro station
using a C++ class. Your 'MetroStation' class should include the following attributes
stationName: A char* variable to store the name of the metro station.
nextStation: A pointer to the next metro station in the line
previousStation: A pointer to the next metro station in the line
You need to implement another class of MetroSystem which will connect all the metro
stations all together it will consist of following attributes:
MetroStation: A pointer which will have the address of the first metro station
You need to overload the plus(+),minus(-),equal(=) operators to add,remove and store a
metro station. You also need to overload the << and >> operators for the I/O handling.
(40 marks)

struct MetroStation{
char* stationName;
MetroStation* nextStation;
MetroStation* previousStation;
//think of other data members by yourself
};

Class MetroSystem{
MetroStation* Metro;
Public:
//overload the operators as described above and implement the constructors and
destructors accordingly
};
Q4: You've already implemented the MetroSystem and Train classes, where you can
manage metro stations, trains, and their interactions. Now, let's extend your program to
simulate the movement of trains between different metro systems and provide
functionality to add, drop, and remove trains. Utilize the existing MetroStation class to
associate trains with metro stations. Extend the MetroStation class to include information
about the train currently located at that station. Implement a function within the
MetroStation class that allows you to move a train from one station to another within the
same metro system. (80 marks)

● Create instances of multiple metro systems and metro stations, each with its own
set of metro stations and trains. Ensure that each train is initially associated with a
specific metro station within a metro system.
● Demonstrate the movement of trains between different metro stations, both within
the same metro system. This includes moving a train from one station to another.
● Implement the functionality for adding, removing trains from a MetroStation using
operator overloading.
● Utilize I/O handling to display the movement of trains between metro stations and
systems using the overloaded <<. This will help you track the trains' journey as
they move through the metro stations and systems.

Note: For this question you need to complete question number 2 and 3 first

Q5: The US dollar is composed of many different coins, which include nickels, cents,
and quarters, which can be combined to make up any amount of money. In this question,
you are required to create a Money Class in which a specific amount of money will be
represented in terms of dollars, quarters, nickels, and cents in that precedence. This
money class will consist further of the dollar, quarter, nickel, and cent classes. For
example, if your amount of money is 4.56, your class will first store the maximum
possible amount in dollars, then it will move on to quarters, nickels, and cents. You are
required to implement the following functions: (25 marks)
class Money {
// think about the private data members
public:
Money();// default constructor
Money(double);// parameterized constructor
//Implement getters and setter functions
Money operator+(Money m); //
Money operator-(Money m); //
Money operator+(Dollar d): //
Money operator-(Dollar d); //
Money operator+(Nickel d): //
Money operator-(Nickel d); //
Money operator+(Quarter d): //
Money operator-(Quarter d); //
Money operator+(Cent d): //
Money operator-(Cent d); //
//These are all addition and subtraction on overloads to add specific coins to the total
amount of money
Money operator ++() //Round up the current amount of money to the nearest quarter
Money operator --() //Round down the current amount of money to the nearest quarter
Bool operator>=()
Bool operator<=() //Comparison operators
Quarters operator!()
//Returns the maximum number of quarters that can be obtained from Money
Nickels operator~() //Returns the maximum number of nickels that can be obtained from
Money
Money operator/(int n) //Returns the money object created if the current money amount
were divided into n parts
Money operator*(int n) //Returns the money object created by multiplying the current
total amount by n
};

You might also like