0% found this document useful (0 votes)
45 views

Unit 7 Exercises

1. A C++ program is defined with a Rectangle class that has two private data members (int) and two public member functions: set_values() to initialize the rectangle values and area() to return the rectangle area. 2. The program contains an Account class with data members balance and accno. Three account objects are created and various member functions like assign(), disp() are called to demonstrate the functionality. 3. The question asks to calculate in how many years the population of country A, growing at 3% annually, will surpass the population of country B growing at 2% annually, given the initial populations.

Uploaded by

kubana friend
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Unit 7 Exercises

1. A C++ program is defined with a Rectangle class that has two private data members (int) and two public member functions: set_values() to initialize the rectangle values and area() to return the rectangle area. 2. The program contains an Account class with data members balance and accno. Three account objects are created and various member functions like assign(), disp() are called to demonstrate the functionality. 3. The question asks to calculate in how many years the population of country A, growing at 3% annually, will surpass the population of country B growing at 2% annually, given the initial populations.

Uploaded by

kubana friend
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

OBJECT ORIENTED PROGRAMMING EXERCISES

1. Write a C++ program using class called Rectangle and an object called rect.
This class should have four members: two data members of type int with
private access and two member functions with public access: set_values()
and area().Set_values() to initialize the values of rectangle and area() to
return the area of rectangle.

2. Give the output for the following program


A.
#include<iostream>
using namespace std;
class account
{
int balance,accno;
public:
account()
{balance=0;}
account(account &s)
{
balance = ++s.balance;
accno=0;
}
void assign( int u)
{
balance = 110; accno = u;
}
void disp()
{
cout<<”Balance”<<balance<<endl;
cout<<”Acc no.”<<accno<<endl;
}
};
int main()
{
account a1;
a1.assign(9);
a1.disp();
account a2(a1);
a2.disp();
account a3(a2);
int y=10;
a3.assign(-y);
a3.disp();
return 0; }
B.

# include <iostream>
# include <conio.h>
using namespace std;
class Trial
{
int a, b;
public:
Trial()
{
a=0;b=0;
}
void disp()
{
cout<<a<<b<<endl;
}
void raise()
{
a+=10;
b+=10;
}
void down()
{
a-=5;
b-=5;
} };
// Main Function
int main() {
Trial T;
T.disp();
T.raise();
T.disp();
T.down();
T.disp();
}
3. The country A has 50M inhabitants, and its population grows 3% per year. The
country B, 70M and grows 2% per year. Tell in how many years A will surpass B.
4. Define a class student with the following specification

Private members of class student:


admno integer
sname 20 character
eng. math, science float
total float
ctotal() a function to calculate eng + math + science with float return type.
Public member function of class student:
Takedata() Function to accept values for admno, sname, eng, science and invoke
ctotal() to calculate total.
Showdata() Function to display all the data members on the screen.

5. Define a class BOOK with the following specifications :


Private members of the class BOOK are:
BOOK NO integer type
BOOKTITLE 20 characters
PRICE float (price per copy)
TOTAL_COST() A function to calculate the total cost for N number of copies where N is
passed to the function as argument.
Public members of the class BOOK are
INPUT() function to read BOOK_NO. BOOKTITLE, PRICE
PURCHASE() function to ask the user to input the number of copies to be purchased.
It invokes TOTAL_COST() and prints the total cost to be paid by the user.
Note: You are also required to give detailed function definitions.

6. 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
1. Write the definitions for each of the above member functions.
2. Write main function to create three Distance objects. Set the value in
two objects and call add() to calculate sum and assign it in third object.
Display all distances.

7. Write the definition for a class called time that has hours and minutes as integer.
The 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
1. Write the definitions for each of the above member functions.
2. Write main function to create three time objects. Set the value in two
objects and call sum() to calculate sum and assign it in third object.
Display all time objects.
8. Find the errors in the following programs.

class circle
{
private
float radius;
public:
void getdata();
{
cout<<” Enter the radius “;
cout<<radius;
}
float area();
}
circle:: float area()
{
return (3.14 *radius*radius);
}

9. Answer the questions (i) to (iv) on the basis of the following code

i. Write the names of data members which are accessible from objects belonging to
class ‘branch’.
ii. Write the names of all the member functions which are accessible
from objects belonging to the class employee.
iii. Write the names of all the members which are accessible from member functions of
class ‘employee’.

class company long nocomputer;


{ protected:
char website[12]; float expense;
char name[20]; public:
protected: branch();
int noemployees; void enter();
public: void show();
company(); };
void registration(); class employee:private branch
void status(); {
}; int eno;
class branch:public company char ename[20];
{ float salary;
public:
employee();
void joining();
void showdetail();
}
10. Write a C++ program that execute contains three classes A,B and C where A is a
base class of B and B is a base class of C, then use an object of class C to Access
public members of class A.

11. Write a C++ program that calculate the sum of two numbers entered
by the user, using constructor to receive those numbers and destructor
for calculations and display of result, then use a copy constructor to print the new
sum if each of numbers entered is multiplied by 3.

12. What are the characteristics of constructor?

13. What is the default constructor? write an example of a program that


contain a default constructor

14. Which among the following is not true for polymorphism?


i. It is feature of OOP
ii. Ease in readability of program
iii. Helps in redefining the same functionality
iv. Increases overhead of function definition always

15. Why do we use the operator overloading in a C++ program?


16. Complete the following table with No or Yes

17. How friend function is declared


18. Why we use friend functions in C++
19. write a C++ program that apply friend function
20. Define data encapsulation
21. What is the difference between data encapsulation and data abstraction?
22. Write an example of program and show with comment the encapsulated data and
abstracted data
23. Write the syntax of class definition.
24. Create a class called person which has 2 functions: getdata() and
putdata(). The getdata() prompts the user to enter his or her first name
name, last name and age, the putdata() displays on the screen the user
first name last, name and age.
25. Write a C++ program which converts in Celsius the Fahrenheit degree
entered on the screen and displays it on the screen. Use the concepts of
class and object to resolve the problem. The class is called temperature
and the object is called Celsius.
Formula of conversion:
Celsius = (Fahrenheit − 32 )* 5/9
Fahrenheit = (Celsius * 9/5) +32
26. Write a C++ program which calculate the sum, the mean and the percentage of the
marks of a student in 5 courses namely Visual Basic (vb), C++ programming (cpp),
web design (wd), mathematics (math) and physics (phy). The User shall be asked to
enter the name and surname of the student then the marks obtained by that students
in 5 courses, the program will then calculate the sum, mean and the percentage then
display on the screen the name, surname, mean and percentage of that student.
Instructions: Use the concepts preserved by the Object Oriented Programming, by
creating a class which is called result and an object called test. Create 3 member
functions namely readident, readmarks, calculate and finally one Friend function
display. Define the methods inside the class. The function readident will be used to
read the name and surname of the student. • The function readmarks will receive the
marks of 5 courses in parameters and assigns the values of these 5 marks to 5
variables called vb, cpp, wd, math and phy respectively. • The function calculate will
be used to calculate the sum, mean and the percentage. Use the following variables to
store the values: sum, mean and percent. • The Friend function display will be used
to display the name, surname, courses studied by the students and the marks
obtained by the students in those 5 courses out of 20 marks in each course then the
sum, mean and the percentage.
27. What is the output of the following program

#include<iostream>
using namespace std;
class sum
{
int x,y,total;
public:
sum( int a,int b)
{
x=a;
y=b*2;
}
void display()
{
total = x + y;
cout<<total;
}
};
int main()
{
sum s(9,5);
s.display();
return 0;
}

28. The following are the principles of OOP: Object, Class, Encapsulation, inheritance
and Polymorphism. Explain them one by one.
29. What are the advantages of OOP?
30. Name the features that are added to standard C++.
31. The below piece of code is for class declaration, analyze it and answer the following
questions:
#include <iostream>
using namespace std;
class Rectangle
{
int width,height;
public;
set_values (int,int);
int area() {return width*height;}
}
a. Show errors found in the program above
b. Correct those errors
32. Which function is called during each function call in the program given:

33. Write a program that contains a class to calculate Fibonacci series of a number
given by the user.

34. Write a C++ program to create student class, read and print N student’s details
using array of objects.
35. Create class Factorial that calculate factorial of a number typed by the user.
36.

Create two classes named Mammals and MarineAnimals. Create another class named
BlueWhale which inherits both the above classes. Now, create a function in each of
these classes which prints "I am mammal", "I am a marine animal" and "I belong to
both the categories: Mammals as well as Marine Animals" respectively. Now, create an
object for each of the above class and try calling
1 - function of Mammals by the object of Mammal
2 - function of MarineAnimal by the object of MarineAnimal
3 - function of BlueWhale by the object of BlueWhale
4 - function of each of its parent by the object of BlueWhale

37.
Make a class named Fruit with a data member to calculate the number of fruits in a
basket. Create two other class named Apples and Mangoes to calculate the number of
apples and mangoes in the basket. Print the number of fruits of each type and the
total number of fruits in the basket.

38.

We want to calculate the total marks of each student of a class in Physics, Chemistry
and Mathematics and the average marks of the class. The number of students in the
class are entered by the user. Create a class named Marks with data members for roll
number, name and marks. Create three other classes to be inherited by the Marks
class, namely Physics, Chemistry and Mathematics, which are used to define marks in
individual subject of each student. Roll number of each student will be generated
automatically.

39.
We want to store the information of different vehicles. Create a class named Vehicle
with two data member named mileage and price. Create its two subclasses
*Car with data members to store ownership cost, warranty (by years), seating capacity
and fuel type (diesel or petrol).
*Bike with data members to store the number of cylinders, number of gears, cooling
type(air, liquid or oil), wheel type(alloys or spokes) and fuel tank size(in inches)
Make another two subclasses Audi and Ford of Car, each having a data member to
store the model type. Next, make two subclasses Bajaj and TVS, each having a data
member to store the make-type.
Now, store and print the information of an Audi and a Ford car (i.e. model type,
ownership cost, warranty, seating capacity, fuel type, mileage and price.) Do the same
for a Bajaj and a TVS bike.

40.
Create a class named Shape with a function that prints "This is a shape". Create
another class named Polygon inheriting the Shape class with the same function that
prints "Polygon is a shape". Create two other classes named Rectangle and Triangle
having the same function which prints "Rectangle is a polygon" and "Triangle is a
polygon" respectively. Again, make another class named Square having the same
function which prints "Square is a rectangle".
Now, try calling the function by the object of each of these classes.

41.
All the banks operating in India are controlled by RBI. RBI has set a well defined
guideline (e.g. minimum interest rate, minimum balance allowed, maximum
withdrawal limit etc) which all banks must follow. For example, suppose RBI has set
minimum interest rate applicable to a saving bank account to be 4% annually;
however, banks are free to use 4% interest rate or to set any rates above it.
Write a program to implement bank functionality in the above scenario. Note: Create
few classes namely Customer, Account, RBI (Base Class) and few derived classes (SBI,
ICICI, PNB etc). Assume and implement required member variables and functions in
each class.

42. For below code snippet, the public and protected members of Superclass becomes
_________ members of Sub class.
class subclass: private Superclass
A. public B. private C. protected D. None of the above
43. What will be the output of following code?
#include <iostream>
using namespace std;
class Animal
{
public:
int legs = 4;
};
class Dog : public Animal
{
public:
int tail = 1;
};
int main()
{
Dog d;
cout << d.legs;
cout << d.tail;
}
A. error B. 44 C. 40 D. 41
44. Do base class and its object have any knowledge about any classes derived from
base class?
A. Yes B. No
45. What is Multiple Inheritance?
A. Deriving a class from Base class
B. Deriving a derived class from two and more class
C. Deriving two or more class from a base class
D. None of the above
46. Whenever you create derived class object, first the base class default constructor is
executed and then the derived class constructor?
A. true B. false
47. Which of the following Function is not inherited?
A. constructor B. destructor C. Assignment operator (=) D. All of the above
48. What will be the output of following code?
#include <iostream>
using namespace std;
class Base
{
public:
Base() { cout << "Base"; }
};
class Derived : public Base
{
public:
Derived(int i) { cout << i; }
};
int main()
{
Derived d2(10);
return 0;
}
A. Base 10 B. 10 C. 10Base D. error
49. What will be the output of following code?
#include <iostream>
using namespace std;
class A
{
int x;
};
class B : public A
{
public:
void show()
{
x=10;
cout << x;
}
};
int main()
{
B b;
b.show();
return 0;
}
A. 10 B. 0 C. error D. garbage value
50. Which symbol is used to create multiple inheritance?
A. Dot(.) B. Comma(,) C. Colon(:) D. None of the above
51. All members of a Base class including private member are inherited in Derived
class.
A. true B. false

You might also like