Computer Programming Assignment
Module 3:
Q1..Create a base class named Animal with a method speak(). Derive two classes, Dog and
Cat, that override the speak() method to provide specific sounds for each animal. Write a main
function to create objects of both derived classes and call their respective speak() methods.
2. Create two base classes, Artist (with attributes like name and style) and Writer (with attributes
like genre and publishedBooks). Derive a class called CreativePerson from both base classes.
Implement methods to display the details of the creative person, including information from both
base classes. Write a main function to demonstrate this multiple inheritance.
3. Define a class called Vector that represents a 2D vector with x and y components. Overload
the * operator to calculate the dot product of two Vector objects. Write a main function to
test this operator overloading.
4. Create a class named Fraction that represents a mathematical fraction with a numerator and
denominator. Overload the minus(-) operator to subtract two Fraction objects and simplify the
result. Implement a method to display the fraction in the form "numerator/denominator". Write a
main function to demonstrate this functionality.
5. Implement a base class named Account with attributes for accountNumber and balance.
Derive a class called SavingsAccount that adds an interest rate attribute and overrides a
method called calculateInterest(). Write a main function that creates an instance of
SavingsAccount, sets its attributes, and displays the calculated interest.
6. Create a base class called Employee with attributes like name and salary. Derive two classes:
FullTimeEmployee and PartTimeEmployee. Add specific attributes for each derived class (e.g.,
hours worked for part-time employees). Write methods in each class to calculate total
compensation based on their respective rules.
7. Extend the previous example(Q6) by overriding a method called calculateBonus() in both
derived classes (FullTimeEmployee and PartTimeEmployee). Implement different bonus
calculation logic for each subclass. Demonstrate this in your main program by creating instances
of both subclasses and calling the overridden method.
Module 4:
[Link] a base class Shape with a pure virtual function area(). Derive two classes, Circle
and Rectangle, and implement the area() function for both. Write a function that takes a
vector of Shape pointers and prints the area of each shape.
2. Create an abstract base class Animal with a pure virtual function speak(). Derive two
concrete classes, Dog and Cat, that implement the speak() function. Write a main function that
creates instances of Dog and Cat, stores them in a vector of Animal pointers, and calls speak()
on each animal.
3. Write a program that defines a class Person with attributes name and age. Implement a
method to serialize a Person object to a file and a method to deserialize a Person object
from a file. Test your implementation by writing a Person object to a file and reading it
back.
4. Write a program that prompts the user for a list of names and stores them in a text file.
After storing the names, read the file and print the names to the console.
5. Create a program that demonstrates the use of namespaces. Define two namespaces, Math
and Science, each containing a function named calculate(). Ensure that both functions
perform different operations (e.g., Math::calculate() adds two numbers while
Science::calculate() computes the average of three numbers). Write a main function to call
both functions using the namespace syntax.
6. Create a base class named Base with a virtual function called show(). Derive two
classes, Derived1 and Derived2, from Base. Then create another class, Derived3, that inherits
from both Derived1 and Derived2. Implement the show() method in each derived class. Write
a main function to demonstrate how calling the show() method from an object of Derived3
invokes the appropriate method based on which derived class's instance is being
Referenced.
7. Write a C++ program that creates a text file called "[Link]" and writes five lines of your
choice into it. Then, read the contents of the file back into the program and display them on
the console. Handle any file errors appropriately.
8. Create a function template called swapValues() that swaps two values of any data type (e.g.,
integers, floats, or strings). Write a main function to test this template by swapping different
types of values and printing the results.
9. Create a C++ program that performs division. Use exception handling to catch both division
by zero and invalid input (e.g., non-numeric input). Implement multiple catch blocks to handle
these exceptions separately.
10. Create a base class called Base with a virtual destructor. Derive a class called Derived. In
your main function, create an object of Derived but reference it through a pointer of type Base.
Demonstrate how the virtual destructor ensures proper cleanup.