Nothing Special   »   [go: up one dir, main page]

C Lab Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lab 1

Problem 1:

Create a 'DISTANCE' class with :


- feet and inches as data members
- member function to input distance
- member function to output distance
- member function to add two distance objects
Write a main function to create objects of DISTANCE class. Input two distances
and
output the sum.

Problem 2:

Write a program that determines the maximum and the minimum of three
numbers.

Problem 3:
Write a program that checks the order of a medicine from a pharmacy store.
The program should read the order quantity and the medicine quantity in the
store. When the order quantity is more than the store quantity, your
program should display the message “No enough quantity”. When the order
quantity is less than the store quantity, you have to check that the order
quantity must be not more than 16 except there is more than 40 items in the
store. The messages that will be displayed in that cases are “Your order is
accepted” OR “You cannot order more than 16 item”

Sample outputs:

Enter the store quantity: 60


Enter the order quantity: 100
No enough quantity

Enter the store quantity: 41


Enter the order quantity: 20
Your order is accepted

Enter the store quantity: 40


Enter the order quantity: 20
You cannot order more than 16 item

Problem 4:
Program: Given that an EMPLOYEE class contains following members:
data members: Employee number, Employee name, Basic, DA, IT, Net Salary
and print data members. Write a C++ program to read the data of N employee
and compute Net salary of each employee (DA=52% of Basic and Income Tax
(IT) =30% of the gross salary) .
Problem 5:

To write a program to categorize employees based on designation using


static data members.

Algorithm:

1. Start
2. Create a class for employee with two count variables declaring as a static
member and employee id, name and designation as other data members.
3. Get the employee id name and designation from the user.
4. If the designation is MANAGER increment first count by 1 and if it is
ENGINEER increment second count by 1.
5. Finally display the total number of managers, engineers and employees.
6. Stop.

To write a C++ program to add two private data members using friend functions.

Lab Number:2
1. To write a C++ program to swap two private data members using friend
functions.
2. To write a C++ program to overload all relational operators to compare
US currency with Nepalese currency. Use conversion rate $1=NRs
101.36(Note: make two classes to represent each currency)
3. To write a C++ program to manipulate complex numbers using operator
overloading. (addition,subtraction,multiplication,division)

4. Create a class called 'TIME' that has


- three integer data members for hours, minutes and seconds
- constructor to initialize the object to zero
- constructor to initialize the object to some constant value
- overload +,- to add and subtract two TIME objects
- overload > function to compare two time
- member function to display time in HH:MM:SS format

5. Create a 'STRING' class which char* and length as data member and
overloads ‘+’ ,’=’ and ‘ = = ‘ to perform respective operations. Use
constructor and destructor whenever suitable.

6. Modify above class to work with compile time memory allocation i.e. with
char [].
7. Overload >> and << operators to input and display time.

8. A parking garage charges a $2.00 minimum fee to park for up to three


hours. The garage charges an additional $0.50 per hour for each hour or
part thereof in excess of three hours. People who park their cars for
longer than 24 hours will pay $8.00 per day. Write a program that
calculates and prints the parking charges. The inputs to your program
are the date and time when a car enters the parking garage, and the date
and time when the same car leaves the parking garage. Keep track of
number of cars in the garage overloading ++ and -- operators.

9. Create a 'MATRIX' class of size m X n. Overload the ‘+’ operator to


Add Two MATRIX objects. Write a main function to implement it.

10. Modify all member operator functions to friend function.


Lab No:3 (Type Conversion)
1. Program to convert Nepalese Currency (Rupees and Paisa) to US
Currency (Dollar and Cents). (Rs. 98.51=1$)
2. Program to convert Polar Coordinate to Cartesian.
3. Program to convert Cartesian coordinate to Polar.
4. Program to convert time (hour, minute and second) in 24-hr format to
12-hr format.
5. Program to convert time (hour, minute and second) in 12-hr format to
24-hr format.
Lab No: 4(Inheritance)
1. Assume class person stores the Name, Age and ID, class employee
stores the designation, basic salary, total overtime(hr) in a month
and hourly rate and class computedSalary stores the total salary of
the employee. Implement above concept using appropriate inheritance.

2. Create a class publication that stores the title and price of a


publication. From this class derive two classes:book, which adds a
page count and tape, which adds a playing time in minutes. Each of
these three classes should have getdata() function to get its data
from the user at the keyboard and putdata() function to display its
data.

3. Modify Q.2 to add base class sales that holds an array of three
floats so that it can record the dollar sales of a particular
publication for the last three months. Include getdata() function to
get three Sales amount from the user and a putdata() function to
display sales figures. Alter the book and tape classes so they are
derived from both publication and sales. An object of class book or
tape should input and output sales data along with its other data.

4.
class Date
{
int day,month,year;
public:
Date();
~Date();
void display(); // displays the date
Date get(); // accesses the date members
void set(); // sets the date members
};

class Time
{
int hour;
int minute;
int second;
public:
Time();
~Time();
void display(); // displays the time
Time get();// accesses the time members
void set(); // sets the time members
};

class DateAndTime : public Date, public Time


{
public:
void display(); // prints date and time
};
a. Define an instance object of class DateTime called Watch.
b. Write a main () function that would initialize the values through the constructor
functions, and then allows them to be reset through the set () functions. Be sure and
display the results following the constructor before you use the set functions.
c. Through the use of the display () function, the time and date are to be displayed. Note
that the display () functions in all three classes need to be defined, as well as the
constructor and all the access functions.

5. class Inventory
{
private:
int quant; // number on hand
int reorder; // reorder quantity
double price; // price per unit
char* descrip; // description of item
public:
Inventory (int q, int r, double p, char* d)
{ // initialize data members }
~Inventory()
{ //release dynamically allocated memory}
void print();
};
//
// first derived class
//
class Auto : public Inventory
{
char * manufacturer;
public:
Auto (int q, int r, double p, char * d, char * man)
{ //initialize base and derive members }
}
~Auto()
{}
void print();
};
//
// Second derived class
//
class Transmission : public Inventory
{
char * vendor;
public:
Transmission (int q, int r, double p, char * d, char * ven)
{}
~ Transmission (){}
void print();
};
a. Write a main () function that creates an instance object of Auto called Car, which has the
following initial data: there is a quantity of five(5) on inventory, and two(2) on reorder;
the price per unit is $15,545.91, and the description is that of the car obtained from the
Toyota. Initialize the object Car, and print out all its vital facts.

b. A Transmission is purchased from Aztec Inc., and must be inventoried. There are 25 of
them, with 10 more on reorder. Their price is $1789.98. Instantiate and initialize an
object for Transmission, and provide the same information.

Student

Int rn
6.
Char name[20]

InternalExam FinalExam

Int m[7] Int m[7]

Float
avgmark
Result

You might also like