Вы находитесь на странице: 1из 8

GALGOTIAS UNIVERSITY GREATER NOIDA

School of Computing Science & Engineering


Program: MCA-II Semester 2013, Course: OOP Lab
C++ BASICS:
1. Write a simple C++ program to print Hello World!.
2. WAP that generates the following table:
1990

135

1991

7290

1992

11300

1993

16200

3. WAP that generates the following output:


10
20
19
Use an integer constant for the 10, an arithmetic assignment operator to generate
the 20, and a decrement operator to generate the 19.
4. Assuming there are 7.481 gallons in a cubic foot, write a program that asks the
user to enter a number of gallons, and then displays the equivalent in cubic feet.
5. Write a C++ program to evaluate the following expression x=a/b-c, where a, b, c
are input from the keyboard.
6. Write a C++ program to calculate the area of triangle and square.
7. Write a program in C++ to check whether the string is palindrome or not.
8. Write a program in C++ to generate the Pascal triangle.
9. Write a program to evaluate the following investment equation: V = P(1+r) n. Test
your
program
for
following
values:P:
1000,
2000,
3000,
r:
0.10,0.11,0.12,.........0.20, n=1,2,3.............10.
10. Write a C++ program for swapping two integers by using call by value and call
by reference.
11. Write a C++ program for pass by value, pass by reference, pass by pointer for
passing objects as arguments to functions

12. A cricket team has the following table of batting figures. Write a program to
read the figures in the given format and calculate the batting averages and print the
complete table along with the batting averages.
Players Name
Sachin
Saurav
Rahul

Runs Scored
8530
4200
3350

Innings Played
230
130
105

Times Not Out


18
9
11

13. An electricity board charges the following rates to domestic users to discourage
the wastage of electricity. For the first 100 units: 60 P/unit. For the next 200 units:
80 P/unit. Beyond 300 units: 90 P/units. All users are charged a minimum of Rs.50. If
the total amount is more than Rs 300 then additional surcharge of 15% is added.
Write a program to read the names of users and number of units consumed and
print the total charges with names of consumers.

LOOPS AND DECISIONS:


14. Write a temperature conversion program that gives the user the option of
converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carryout the
conversion. Use floating point numbers. Interaction with the program might look like
this:
Type 1 to convert Fahrenheit to Celsius,
2 to convert Celsius to Fahrenheit: 1
Enter temperature in Fahrenheit: 70
In Celsius that is: 21.111111
15. Create the equivalent of four-function calculator. The program should request
the user to enter a number, an operator, and another number. It should then carry
out the specified arithmetical operation: adding, subtracting, multiplying, or dividing
the two numbers. (It should use a switch statement to select the operation.) Finally
it should display the result.
When it finishes the calculation, the program should ask if the user wants to
do another calculation. The response can be y or n. Some sample interaction with
the program might look like this:
Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n

STRUCTURES:
16. A phone number, such as (212) 767- 8900, can be thought of having three
parts: the area code (212), the exchange (767), and the number (8900). Write a
program that uses a structure to store these three parts of a phone number
separately. Call the structure phone. Create two structure variables of type phone.
Initialize one, and have the user input a number for the other one. Then display
both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212
17. A point in the two-dimensional plane can be represented by two numbers: an X
coordinate and a Y coordinate. For example, (4,5) represents a point 4 units to the
right of the origin along the X axis, and 5 units up the Y axis. The sum of two points
can be defined as a new point whose X coordinate is the sum of X coordinates of the
two points, and whose Y coordinate is the sum of their Y coordinates.
WAP that uses a structure called point to model a point. Define three points,
and have the user input values to two of them. Then set the third point equal to the
sum of the other two, and display the value of the new point. Interaction with
program might look like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinate for p1 + p2 are: 8, 11
18. Create a structure called Volume that uses three variables of type Distance to
model the volume of a room. Initialize a variable of type Volume to specific
dimensions, then calculate the volume it represents and printout the result. To
calculate the volume, convert each dimension from a Distance variable to a
variable of type float representing feet and fractions of a foot, and then multiply
the resulting three numbers.

FUNCTIONS:
19. Write a function called circarea() that finds the area of the circle. It should take
an argument of type float and return an argument of same type. Write a main()
function that gets a radius value from the user, calls circarea(), and displays the
result.
20. Raising a number n to a power p is the same as multiplying n by itself p times.
Write a function called power() that takes a double value for n and an int value for
p, and returns the result as double value. Use a default argument of 2 for p, so that
if this argument is omitted, the number will be squared. Write a main() function
that gets values from the user to test this function.

21. Write a function called zeroSmaller () that is passed two int arguments by
reference and then sets the smaller of the two numbers to 0. Write a main()
program to exercise this function.
22. Write a function that takes two Distance values as arguments and returns the
larger one. Include a main() program that accepts two Distance figures from the
user, compares them and displays the larger.

OBJECTS AND CLASSES:


23. Write a program in C++ to enter the information of student using the class
student.
24. Write a C++ program to convert temperature from Fahrenheit to Celsius using
the class temp.
25. Write a program in C++ to find the factorial of a given number using the class
fact.
26. Write a program in C++ to find Fibonacci series using class.
27. Construct a class named
account with member functions deposit and
withdraw. Test this in a C++ program.
28. Construct a class named date for displaying todays date using data members
and member functions. Test this in a C++ program.
29. Construct a class named stack for implementing stack operations. Test this in a
C++ program.
30. Write a C++ program to implement flight class with data member as flight no.,
source, destination and fare. Write a copy constructor and a member function to
display the flight information.
31. Write a C++ program to implement a sphere class with appropriate data
member and member functions to find the surface area and the volume.
(Surface area = 4 r2 and Volume = 4/ 3 r3 )
32. Define a class "BankAccount". Include the following members. Data members:
Name of depositor, Account number, Account type, Balance amount in the
account. Member Functions: To assign initial values, To deposit an amount, To
withdraw an amount after checking the balance, To display name and balance. Write
a program in C++ to test.
33. Create a class that imitates part of the functionality of the basic data type int.
Call the class Int (note different spelling). The only data in this class is an int
variable. Include member functions to initialize an Int to 0, to initialize it to an int
value, to display it(it looks just like an int), and to add two Int values.

WAP that exercises this class by creating two initialized and one uninitialized Int
values, adding these two initialized values and placing the response in the
uninitialized value, and then displaying this result.
34. Imagine a toolbooth at a bridge. Cars passing by the booth are expected to pay
a fifty-cent toll. Mostly they do, but sometimes a car goes by without paying. The
tollbooth keeps track of the number of cars that have gone by, and of the total
amount of money collected.
Model this tollbooth with a class called tollbooth. The two data items are a type
unsigned int to hold the total numbers of cars, and a type double to hold the total
amount of money collected. A constructor initializes both these to 0. A member
function called payingCar() increments the car total and adds 0.50 to cash total.
Another function called nopayCar(), increments the car total but adds nothing to
the cash total. Finally, a member function called display() displays the two totals.
Include a program to test this class. This program should allow the user to push one
key to count a nonpaying car. Pushing the ESC key should cause the program to
print out the total cars and total cash and then exit.
35. Create a class time that has separate int member data for hours, minutes, and
seconds. One constructor should initialize this data to 0, and another should
initialize it to fixed values. A member function should display it, in 11:59:59 format.
The final member function should add two objects of type time passed as
arguments.
A main() program should create two initialized time objects, and one that is
not initialized. Then it should add the two initialized values together, leaving the
result in the third time variable. Finally it should display the value of this third
variable.

ARRAYS:
36. Write a function called reverseit() that reverse a string (an array of char). Use
a for loop that swaps the first and last characters, then the second and next-to-last
characters, and so on. The string should be passed to reverseit() as an argument.
WAP to exercise reverseit(). The program should get a string from the user,
call reverseit(), and printout the result. Use an input method that allows
embedded blanks. Test the program with Napoleons famous phrase,Able was I ere
I saw elbA.
37. Create a class employee that contains a name (an array of char) and an
employee number (type long). Include a member function called getdata() to get
data from the user for insertion into the object, and another function called
putdata() to display the data. Assume the name has no embedded blanks.
Write a main() program to exercise this class. It should create an array of
type employee, and then invite the user to input data for upto 100 employees.
Finally, it should print out the data for all the employees.

38. Write a program that calculates the average of upto 100 English distances input
by the user. Create an array of objects of the Distance class. This class includes
two member data items feet and inches of type int and float respectively, and
two member functions getdist() and showdist(). To calculate the average, you can
use the dist_add() member function. Youll also need a member function that
divides a Distance value by an integer. Here is one possibility:
void Distance:: div_dist(Distance d2, int divisor)
{
float fltfeet = d2.feet + d2.inches/12.0;
fltfeet /= divisor;
feet = int (fltfeet);
inches = (fltfeet-feet);
}

OPERATOR OVERLOADING:
39. To the Distance class in the previous question, add an overloaded operator
for the Distance class that subtracts two distances. It should allow statements like
dist3 = dist2 dist1;. Assume the operator will never be used to subtract a larger
number from a smaller one (that is, negative distances are not allowed).
40. WAP that uses an overloaded += operator for string concatenation. This
operator should allow statements like
s1 += s2;
where s2 is added (concatenated) to s1 and the result left in s1. The operator
should also permit the results of the operation to be used in other calculations, as in
s3 =s1+=s2;.
41. Modify the time class from problem # 35 so that instead of a function
add_time() it uses the overloaded + operator to add two times. WAP to test this
class.
42. Create a class Int based on problem #33. Overload all five integer arithmetic
operators (+, -, *, /, %) so that they operate on objects of type Int. If the result of
any such arithmetic operation exceeds the normal range of ints from -32,768 to
32,767, have the operator print a warning and terminate the program. WAP to test
this class.

INHERITANCE:
43. Imagine a publishing company that markets both book and audio-cassette
versions of its works. Create a class publication that stores the title (a string) and
price (type float) of a publication. From this class derive two classes: book, which
adds a page count (type int); and tape, which adds a playing time in minutes (type

float). Each of these three classes should have a getdata() function to get its data
from the user at the keyboard, and a putdata() function to display its data.
Write a main() program to test the book and tape classes by creating
instances of them, asking the user to fill in their data with getdata(), and then
displaying the data with putdata().
44. Start with the problem #43, add a 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 a getdata() function to get three sales amounts from the
user, and a putdata() function to display the 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. Write a main()
function to create a book object and a tape object and exercise their input/output
capabilities.

POINTERS:
45. Write a program that reads a group of numbers from the user and places them
in an array of type float. Once the numbers are stored in the array, the program
should average them and print the result. Use pointer notation wherever possible.
46. Make a class String. Include a display() function to display the string. Add a
member function called upit() that converts the string to all upper case. You can
use the toupper() library function, which takes a single character as an argument
and returns a character that has been converted (if necessary) to uppercase. This
function uses the CTYPE.H header file. Write some code in main() to test this
function.
47. Use an array of pointers to strings to display the days of the week. Provide
functions to sort the strings into alphabetical order. Make functions for sorting and
ordering. Sort the pointers to the string, not the actual string.

FILES & STREAMS:


48. Refer to problem # 38 for Distance class. Use a loop to get a number of
Distance values from the user and write them to a disk file. Append them to
existing values in the file, if any. When the user signals that no more values will be
input, read the file and display all the values.
49. WAP that emulates the DOS COPY command. That is, it should copy the
contents of a character file (such as any cpp file) to another file. Invoke the program
with two command-line arguments the source file and the destination file like
this:
C> copy srcfile.ext destfile.ext
In the program, check that the user has typed the correct number of command-line
arguments, and that the files specified can be opened. Improve on the DOS TYPE
command by having the program signal an error if the destination file already
exists. This will prevent inadvertently writing over a valuable file.
50. WAP that returns the size in bytes of a program entered on the command line:

C> filesize program.ext