Unit 7 Exercises
Unit 7 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.
# 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
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:
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’.
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.
#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