C++ Programming Assignments (Sheet 1) - 24951730

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

C++ PROGRAMMING ASSIGNMENTS

1. Write a class called MyArray containing an integer array of


size 10. Provide proper constructor to initialize it from user
input . Also provide a method called reverse()that reverses the
array and stores the reverse in the array itself. Do not create
another array to hold the result. Finally display both original
and reversed array

2. Write a class called MyArray containing an integer array of


size 10. Provide proper constructor to initialize it from user
input . Also provide a method called sort() which sorts this
array , but the order of sorting (Ascending/Descending) should
be user’s choice. Finally display both sorted and unsorted
array.

3. Create a class called Employee that stores its name,


department, designation and basic salary. Write functions to
accept, display and get/set of the data. Also write method that
displays his incentives. The incentives includes HRA (20%),
DA (10%), CA(10%). Test the above code by creating an
implementation program

4. Write a class called Time, which models a specific instance of


time with hour, minute and second values, as shown in the
class diagram. Here ‘+’ means public members and ‘-‘ private
members
The class Time contains the following members:
A. Three private data members: hour (0-23), minute (0-59)
and second (0-59), with default values of 0.

B. A public constructor Time(), which initializes the data


members hour, minute and second with the values provided
by the caller.

C. public getters and setters for private data members:


getHour(), getMinute(), getSecond(), setHour(), setMinute(),
and setSecond().

D. A public member function setTime() to set the values of


hour, minute and second given by the caller.

E. A public member function print() to print this Time instance


in the format "hh:mm:ss", zero-filled, e.g., 01:30:04.

F. A public member function nextSecond(), which increase this


instance by one second. nextSecond() of 23:59:59 shall be
00:00:00.
5. A class called Account, which models a bank account, is
designed as shown in the class diagram. It contains:

A.Two private data members: accountNumber (int) and


balance (double), which maintains the current account
balance.

B. Public functions credit() and debit(), which adds or subtracts


the given amount from the balance, respectively. The
debit() function shall print "amount withdrawn exceeds the
current balance!" if amount is more than balance.

C. A public function print(), which shall print "A/C no: xxx


Balance=xxx" (e.g., A/C no: 991234 Balance=Rs 9650.45),
with balance rounded to two decimal places.

6. Write a class called Point class, as shown in the class


diagram, models 2D points with x and y co-ordinates.
In the class diagram, "-" denotes private member; "+" denotes
public member. "= xxx" specifies the default value of a data member.

The Point class contains the followings:

A. Private data members x and y (of type int), with default


values of 0.
B. A constructor, getters and setters for private data member x
and y.
C. A function setXY() to set both x and y coordinates of a
Point.
D. A function getMagnitude() which returns √(x2+y2). You can
use the built-in sqrt() function in <cmath> to compute the
square root.
E. A function getArgument() which returns tan-1(y/x). You can
use the built-in atan2(y, x) function in <cmath> to compute
the gradient in radians.
F. A function print() which prints "(x,y)" of this instance.

7. Define a class batsman with the following specifications:


Private members:
bcode 4 digits code number
bname 20 characters
innings, notout, runs integer type
batavg :it is calculated according to the formula – batavg
=runs/(innings-notout)

Public members:
readdata():Function to accept value from bcode, name,
innings, notout and invoke the function

calcavg() Function to compute batavg

displaydata() Function to display the data members on the


screen

8. Define a class called Flight with following description:


Private Members
A data member Flight number of type integer
A data member Destination of type string
A data member Distance of type float
A data member Fuel of type float
A member function calfuel() to calculate the value of Fuel as
per the following criteria
Distance Fuel
<=1000 500
more than 1000 and <=2000 1100
more than 2000 2200
Public Members
A function feedinfo() to allow user to enter values for Flight
Number, Destination, Distance & call function calfule() to
calculate the quantity of Fuel
A function showinfo() to allow user to view the content of all
the data members
9. Write the definition for a class called complex that has floating
point data members for storing real and imaginary parts. The
class has the following member functions:

void set(float, float) to set the specified value in object


void disp() to display complex number object
complex sum(complex) to sum two complex numbers & return
complex number
Write the definitions for each of the above member functions.
Write main function to create three complex number objects. Set the
value in two objects and call sum() to calculate sum and assign it in
third object. Display all complex numbers.

10. Write a program that will function as a point-of-sale


system at a snack bar. The snack bar sells only 3 items: a
sandwich for Rs 30.00, chips for Rs15.0 and a soft drink for Rs
20.00. All items are subject to 8.25% sales tax. The program
will repeatedly display the following menu until the sale is
totalled. The program should keep a running total of the
amount of the sale, based on the costs above. The running
total (excluding tax) and the number of items ordered should
be displayed on the screen each time the menu is displayed.
At the beginning, ask for the salesperson’s first and last name.
The user will enter the following codes (only one item at a time
can be ordered):

S = sandwich
C = chips
D = drink
X = Cancel sale and start over
T = Total the sale.

If the sale is canceled, clear the running total and display the
menu again. When the sale is totalled, calculate the sales tax
and print the salesperson’s name, the number of items, the
total before tax, the tax and final total, on the screen. Use a
sentinel loop with “T” as the sentinel value.No input,
processing or output should happen in the main function. All
work should be delegated to functions.

You might also like