50% found this document useful (2 votes)
81 views2 pages

C++ Inheritance and Overloading Exercises

The assignment involves completing C++ programs using operator overloading and inheritance; students are to solve all problems independently and not copy solutions from websites. It includes 35 problems covering topics like complex number operations, rational number arithmetic, finding areas and volumes of shapes, inheritance between multiple classes, storing vehicle and student information using inheritance, and templated classes. Students are encouraged to reference websites but not copy solutions.

Uploaded by

Aaron Abraham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
81 views2 pages

C++ Inheritance and Overloading Exercises

The assignment involves completing C++ programs using operator overloading and inheritance; students are to solve all problems independently and not copy solutions from websites. It includes 35 problems covering topics like complex number operations, rational number arithmetic, finding areas and volumes of shapes, inheritance between multiple classes, storing vehicle and student information using inheritance, and templated classes. Students are encouraged to reference websites but not copy solutions.

Uploaded by

Aaron Abraham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 2

Assignment date:-13-07-2023

Inheritance and operator overloading programs

Students are encourage to do all problems yourself, you could reference website but could not copy.

1. C++ Program to add Two Strings using overloading


2. C++ Program to Perform Complex Operations(add, subtract) using Overloading
3. C++ program to Swap variables using function overloading
4. C++ program for show Counter using Overloading unary operator ++
5. C++ program to perform operations on complex numbers
6. C++ class Program to perform rational number arithmetic
7. C++ class Program to perform Complex Arithmetic using operator overloading
8. C++ Program to Find the Area of shapes using function overloading
9. C++ Program to check Palindrome using function overloading
10. C++ program to find area of square,rectangle,circle and triangle
11. C++ program to find volume of cube, cylinder, sphere by function overloading
12. C++ program to demonstrate example of simple inheritance.
13. C++ program to demonstrate example of private simple inheritance.
14. C++ program to read and print student's information using two classes and simple inheritance.
15. C++ program to demonstrate example of multilevel inheritance.
16. C++ program to read and print employee information using multiple inheritance.
17. C++ program to demonstrate example of multiple inheritance.
18. C++ program to demonstrate example of hierarchical inheritance to get square and cube of a number.
19. C++ program to read and print employee information with department and pf information using
20. C++ Program to access protected data member using Inheritance
21. C++ Program for enter Patient details using Inheritance
22. C++ Program to demonstrate an Example of Hybrid Inheritance
23. C++ program display student marksheet using multiple inheritance
24. C++ Program to find Area of Rectangle using inheritance
25. C++ Program to show access to private, public and protected using Inheritance
26. C++ Program to enter Student details of different Stream using Hierarchical Inheritance
27. C++ Program to find area and volume using multiple inheritance
28. C++ Program to Overriding the member functions using Inheritance
29. 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
30. 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.
31. 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 inheriting
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.
32. 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.
33. 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.
34. C++ Program of templated class derived from non-templated class
35. C++ Program of non-templated class derived from templated base class
36.C++ Program of templated class derived from another templated class

Common questions

Powered by AI

Hierarchical inheritance in C++ involves single base class inheritance by multiple derived classes. It provides a natural and scalable way to keep related classes organized by deriving multiple classes that share a common interface or set of functionalities from a single base. In the case of a class designed to compute powers, such as squares and cubes, hierarchical inheritance allows creating classes like Square and Cube derived from a single Mathematics base class. Each derived class can override a common interface or provide specialized implementations for its specific functionality, promoting code reuse and maintainability .

Operator overloading in C++ allows standard operators to have different implementations based on their operand types, enabling direct and natural manipulation of user-defined types like rational and complex numbers. For arithmetic operations, it means these complex data types can be used syntactically as if they were fundamental types. This enhances code readability and intuitiveness, allowing operations like addition or subtraction of complex numbers to be expressed with standard operators instead of verbose function calls, effectively integrating user-defined types into the core language features .

In C++, a protected member of a base class is accessible to its derived classes but remains hidden from classes that are not part of the inheritance chain. This facilitates class design by allowing derived classes to leverage and extend the functionality of base classes without exposing these details to outside classes, maintaining encapsulation. For example, a derived class can access and manipulate protected data, allowing modifications to the class behavior while hiding implementation details from users of the class hierarchy, promoting design modularity and security .

Function overloading for calculating areas and volumes allows use of the same function name to perform similar operations on different types or numbers of parameters, simplifying the interface and improving code readability. It enables flexible and intuitive use of functions tailored to specific shapes, like squares, rectangles, and circles. However, potential drawbacks include increased compiler workload due to multiple function signatures and the risk of ambiguity if function signatures are too similar or if auto type conversions are involved. Additionally, overly relying on overloading might complicate debugging processes due to unclear function calls .

Overriding member functions in derived classes is a crucial mechanism for enabling polymorphism in C++. It allows a derived class to provide a specific implementation for a method that is already defined in its base class, thereby tailoring or extending behavior. This is particularly significant when using pointers or references to base class types, allowing the actual type of the object to determine which function implementation is executed. Effective use of function overriding can lead to flexible and scalable software design, as it allows runtime determination of the appropriate method, handling diverse object types through a uniform interface .

Multiple inheritance allows a class to inherit features from more than one base class. In the case of a class inheriting from both Mammals and MarineAnimals, such as the BlueWhale class mentioned, multiple inheritance enables the composition of functionalities from both base classes. The BlueWhale class would be able to utilize and unify behaviors and attributes from both parents, demonstrating polymorphism by assessing methods of both Mammals and MarineAnimals, while also providing its unique functions. This approach, although powerful, can lead to complexity such as the 'diamond problem', which C++ addresses through virtual inheritance .

Function overloading allows defining multiple functions with the same name but different parameter lists. In the context of C++ programs performing arithmetic operations on complex numbers, such as addition and subtraction using operator overloading, function overloading enables polymorphism by allowing the same function name to operate on different types or numbers of parameters. This is particularly useful when manipulating complex numbers where different operations like addition, subtraction, and even multiplication or division need distinct implementations detail yet maintain a common interface .

Hybrid inheritance combines multiple and single inheritance patterns, which can lead to complexities such as the diamond problem, where a class inherits from two classes that share a common base. This creates ambiguity in accessing shared base class members. Solving this often involves using virtual inheritance, where a common base class is explicitly designated as virtual, ensuring only one instance of the base class is inherited regardless of the inheritance path. In the case of a BlueWhale inheriting from Mammals and MarineAnimals, hybrid inheritance helps blend functionalities, but care must be taken to manage shared members and methods to avoid conflicts and ambiguities in the class design .

In C++, private inheritance restricts access to the base class’s public and protected members as if they were private, meaning they are not accessible to derived classes or objects of derived classes. This makes it suitable for implementing certain aspects of class composition without exposing unnecessary details. Public inheritance, on the other hand, allows derived classes to access the public and protected members of the base class, facilitating true 'is-a' relationships between classes. The choice between private and public inheritance has significant implications for class hierarchies, particularly in terms of encapsulation, interface exposure, and the ability to use base pointers or references to access derived classes .

Multilevel inheritance in C++ is a form of inheritance where a class derives from another derived class, forming a hierarchy with multiple 'levels'. This helps structure complex systems by creating clear hierarchies where base classes define broad, general functionalities and more derived classes provide specific functionalities extending the base. For example, a system where a base class defines a generic object, an intermediate class adds specific behaviors, and further derived classes implement even more specialized features. Practically, this can be used in designing GUI frameworks, where a base widget class is extended by more specific components like buttons or text fields .

You might also like