Assignment for Tutorial
1. Write a C++ program that compares all three access specifiers.
• Create a class with:
a) one private member
b) one protected member
c) one public member
• Demonstrate accessibility:
a) inside the class
b) inside derived class
c) outside the class
2. Write a C++ program to demonstrate public inheritance.
a) Create a base class Vehicle
b) Create a derived class Car
c) Access inherited public members through derived class objects.
d) Explain member visibility after public inheritance.
3. Write a C++ program to find the maximum of:
a) Two integers
b) Three integers (using function overloading)
4. Write a C++ program to store n integers in a vector, sort them in ascending order and
search for a given element using STL algorithms.
5. Suppose you have a piggie bank with an initial amount of $50, and you have to add some
more amount to it. Create a class ‘AddAmount’ with a data member named ‘amount’ with
an initial value of $50. Now make two constructors of this class as follows:
a) Without any parameter – no amount will be added to the piggie bank
b) Having a parameter, which is the amount that will be added to the piggie bank
Create an object of the ‘AddAmount’ class and display the final amount in the piggie bank.
6. Make a class named Fruit with a data member to calculate the number of fruits in a basket.
Create two other classes 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.
7. Write a C++ program to create a file and store multiple student records sequentially. Then
read and display the contents.
8. Write a C++ program using a function template to swap two values.
Swap:
a) integers
b) characters
c) floating-point values
Display values before and after swapping.
9. Write a C++ program using a class template to implement a stack.
Implement:
a. push()
b. pop()
c. display()
Demonstrate stack operations for integers.
10. Write a C++ program to demonstrate the use of a virtual function.
Create a base class Animal
Create derived classes:
a. Dog
b. Cat
Define a virtual function sound() in the base class.
Override the function in derived classes.
Use base class pointer to call derived class functions.