Anda di halaman 1dari 7

Assignment-1

Object Oriented Programming using C++


(CST-157)
Date of allocation of Assignments 17-Jan-19
Date of Submission of Assignments 15-Feb-19

S Group Assignment
No.
1 Group Q1. Write a program that uses same function name as the name of class to get two input
A
values from user and display the values.
Q2. Write the difference between structure and class with examples.

Q3. Write a program to print first N natural numbers using inline function.
Q4. Write a function that, when you call it, displays a message telling how many times it
has been called: “I have been called 3 times”, for instance. Write a main()program that
calls this function at least 10 times. Try implementing this function in two different ways.
First, use a global variable to store the count. Second, use a local static variable. Which is
more appropriate? Why can’t you use a local variable.
Q5. Create a class phone having data members: 1. The STD code 2. The Exchange code
3.Phone Number Ex :- 212-766-8901 Write a C++ program to accept details from user
(max 10) and change input phone number to new phone number using following criteria:
a) Add 1 to 1st digit of STD code. (If digit is 9 it becomes 10)
b) The exchange code is retained as it is.
c) In 3rd part of structure, 1st two digits should be reversed. Ex: I/P : 212-766-890 =>
O/P : 312-766-980 Display all changed phone numbers.
2 Group Q1. Write a function to calculate the factorial value of any integer as an argument. Call
B this function from main( ) and print the results in main( ).
Q2. Write the differences between object oriented and procedure oriented programming.
Q3.Write a C++ program to create student class, read and print N student's details .
Q4. Create a Function Demo as the name of the class to get the parameters from main ()
function. Also create a display() function to display parameter values.
Q5. Write a C++ program to perform different arithmetic operation such as addition,
subtraction, division, modulus and multiplication using inline function.
3 Group Q1.Define a class to represent a bank account which includes following members : Data
C members –
1) Name
2) Account number
3) Type of account
4) Bal. amt
Member functions –
a. To assign initial value
b. To deposit an account
c. To withdraw an account
d. To display name, account number & balance.
Q2. What is the difference between friend function and inline function? Explain with
examples.
Q3. Write a function called hms_to_secs() that takes three int values—for hours, minutes,
and seconds—as arguments, and returns the equivalent time in seconds (type long). Create
a program that exercises this function by repeatedly obtaining a time value in hours,
minutes, and seconds from the user (format 12:59:59), calling the function, and displaying
the value of seconds it returns.
Q4. An electricity board charges the following rates to users – For first 100 units : 40p per
unit – For next 200 units : 50p per unit – Beyond 300 units : 60p per unit All users are
charged a minimum of Rs.150. If the total cost is more than Rs.250.00 then an additional
charges of 15% are added. Write a C++ program using class to read the name of users &
number of units consumed & print out the charges with names.
Q5.Write a program to find the cube of a number by showing use of inline function.
4. Group Q1.Write a program to check whether the entered character is a digit or not using inline
D function.
Q2. What is the need of Constructor & Destructor?
Q3. When it finishes the calculation, the program should ask whether 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

Q4.Program to store price list of 5 items and to print the largest price as well as the sum
of all prices using class in C++ .
Q5. Write a function called swap() that interchanges two int values passed to it by the
calling program. (Note that this function swaps the values of the variables in the calling
program, not those in the function.) You’ll need to decide how to pass the arguments.
Create a main() program to exercise the function.
5 Group Q1. Create a class calculate that uses two separate functions:-a function to multiply two
E float numbers and another to divide two float numbers using the concept of inline function.
Also, create a method to print the result on the screen.
Q2. Write a program to demonstrate example of friend function with class.
Q3.What are different type of constructors? Explain with Example.
Q4. Write a C++ program to sort an array of integer in ascending order using a function
called exchange( ) which accepts two integer arguments by reference.
Q5. Create a class called employee that contains a name (an object of class string) 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 up to 100 employees. Finally, it should print out
the data for all the employees.
Group Q1.Explain with example the use of inline function.
6 F Q2. Create a class called ITEM and display itemno and price for 5 number of items. Use
methods getdata() to input the data and putdata() to print the result on screen for the same.
Q3. 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.
Q4.Write a program to display area of circle and rectangle using classes with primitive
data members.
Q5.Create a class called 'EMPLOYEE' that has
- EMPCODE and EMPNAME as data members
- member function getdata( ) to input data
- member function display( ) to output data
Write a main function to create EMP, an array of EMPLOYEE objects. Accept and
display the details of at least 6 employees.
7 Group Q1. Write a program to demonstrate copy constructor.
G Q2. Create a class named BOX having members length,breadth and height.Use a class
instance to display the box length,box breadth and box height. Also display the volume
of box.
Output should be in the format:-
Enter the length of box:-
12.0
Enter the breadth of box:-
13.0
Enter the height of box:-
10.0
Volume of Box is:-length*height*breadth
Q3. Create classes FIRST and SECOND with its data members and methods. Write a
program to implement friend function of class SECOND.
Q4.ImplementC++ program to create a class to read and add two distance.

Q5. Create a constructor to calculate area of a rectangle and display it on the screen.
8 Group Q1.How we can say that OOP concept is taken from real life? What is the benefit and
H application of it?
Q2. Write a program to enter the roll no and fees of a student using constructor.
Q3. Give a C++ program to demonstrate the working of friend function which measure
distance in meters and return the same back.
Q4.WAP to find the largest of two numbers using inline function.

Q5. Write a language program in C++ which accepts the user's first and last name and print
them in reverse order with a space between them.
9 Group Q1.A common place to buy candy is from a machine. The machine sells candies, chips,
I gum, and cookies. You have been asked to write a program for this candy machine.
The program should do the following:
1. Show the customer the different products sold by the candy machine.
2. Let the customer make the selection.
3. Show the customer the cost of the item selected.
4. Accept money from the customer.
5. Release the item.

Q2. Answer the questions (i) and (iii) after going through the following class:
class Seminar
{
int time;
public:
Seminar() //Function 1
{
time = 30;
cout<< "Seminar starts now" <<endl;
}

void lecture() //Function 2


{
cout<< "Lectures in the seminar on" <<endl;
}

Seminar(int duration) //Function 3


{
time = duration;
cout<< "Seminar starts now" <<endl;
}

~Seminar() //Function 4
{
cout<< "Thanks" <<endl;
}
};
i. Write statements in C++ that would execute Function 1 and Function 3 of class
Seminar.
ii. In Object Oriented Programming, what is Function 4 referred as and when does it get
invoked/called?
Q3. Give a C++ program to print first N natural numbers using inline function.

Q4.WAP to add members of two different classes using friend function.

Q5. Discuss the importance of different access specifiers with examples.


10 Group
Q1. Consider the definition of the following class:
J
class Sample
{
private:
int x;
double y;
public :
Sample(); //Constructor 1
Sample(int); //Constructor 2
Sample(int, int); //Constructor 3
Sample(int, double); //Constructor 4};

i. Write the definition of the constructor 1 so that the private member variables are
initialized to 0.

ii. Write the definition of the constructor 2 so that the private member variable x is
initialized according to the value of the parameter, and the private member variable y
is initialized to 0.

iii. Write the definition of the constructors 3 and 4 so that the private
member variables are initialized according to the values of the parameters.
Q2. Answer the following questions after going through the following class:
class Test
{
char paper[20];
int marks;
public:
Test () // Function 1
{
strcpy (paper, "Computer");
marks = 0;
}

Test (char p[]) // Function 2


{
strcpy(paper, p);
marks = 0;
}

Test (int m) // Function 3


{
strcpy(paper,"Computer");
marks = m;
}

Test (char p[], int m) // Function 4


{
strcpy (paper, p);
marks = m;
}
};
Write statements in C++ that would execute Function 1, Function 2, Function 3 and
Function 4 of class Test.

Q3. Write a simple and complete C++ program to implement friend function globally.

Q4.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)
calcavg() Function to compute batavg
Public members:
readdata() Function to accept value from bcode, name, innings, notout and
invoke the function calcavg()
displaydata() Function to display the data members on the screen.

Q5. Write a function that receives two numbers as an argument and display all prime
numbers between these two numbers. Call this function from main( ).
11 Group Q1. Write the definition for a class called time that has hours and minutes as integer. The
K class has the following member functions:
void settime(int, int) to set the specified value in object
void showtime() to display time object
time sum(time) to sum two time object & return time

Q2. Write a C++ program using function which accept two integers as an argument and
return its sum. Call this function from main( ) and print the results in main( ).
Q3. Write a program to convert temperature in Celsius with the help of friend function.

Q4.Write a program that lets the user perform arithmetic operations on two numbers. Your
program must be menu driven, allowing the user to select the operation (+, -, *, or /) and
input the numbers. Furthermore, your program must consist of following functions:
1. Function showChoice: This function shows the options to the user and explains how to
enter data.
2. Function add: This function accepts two number as arguments and returns sum.
3. Function subtract: This function accepts two number as arguments and returns their
difference.
4. Function mulitiply: This function accepts two number as arguments and returns product.
5. Function divide: This function accepts two number as arguments and returns quotient.

Q5.Write the definition for a class called Distance that has data member feet as integer and
inches as float. The class has the following member functions:
void set(int, float) to give value to object
void disp() to display distance in feet and inches
Distance add(Distance) to sum two distances & return distance.
12 Group Q1. Write a program which accepts a character and display its next character.
L Q2. Write a program that takes length as input in feet and inches. The program should
then convert the lengths in centimeters and display it on screen. Assume that the given
lengths in feet and inches are integers.
Q3 Write a program to check whether a triangle is valid or not, when the three angles of
the triangle are entered by the user. A triangle is valid if the sum of all the three angles is
equal to 180 degrees.
Q4. C++ Program to Define a Class Test and accessing member function using its object.

Q5. Write a program to calculate area of rectangle using friend function.

Anda mungkin juga menyukai