cpp Polymorphism
Published on: 09 February 2025
Resources
Aim: Create a program that exhibits the usage of Operator Overloading (Compile Time Polymorphism)
Self Assessment
- Write a Program to demonstrate the concept of operator overloading in C++ by implementing vector subtraction.
Practice Exercise 14: Write a Program to demonstrate the concept of operator overloading in C++ by implementing vector addition.
Theory:
Operator Overloading is a feature in C++ that allows us to define custom behaviors for operators when applied to user-defined types. It provides compile-time polymorphism by enabling different behaviors for the same operator based on operand types. In two-dimensional space, vector addition follows the rule of adding corresponding components.
Given two vectors V1(x1, y1) and V2(x2, y2), their sum is calculated as:
$$ V3(x3, y3) = (x1 + x2, y1 + y2) $$
Algorithm:
- Define a class with private data members.
- Create a constructor to initialize the data members.
- Overload the operator function within the class using the 'operator' keyword.
- Implement the overloaded operator function to perform the required operation.
- Create a display function to print the object state.
- In the main function, create objects and use the overloaded operator.
- Display the results.
Code:
#include <iostream>
using namespace std;
class Vector {
private:
int x, y;
public:
// Constructor
Vector(int x = 0, int y = 0) : x(x), y(y) {}
// Overloading the + operator
Vector operator+(const Vector& v) {
return Vector(x + v.x, y + v.y);
}
// Display function
void display() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Vector v1(3, 4), v2(1, 2);
Vector v3 = v1 + v2; // Using overloaded + operator
cout << "Vector 1: "; v1.display();
cout << "Vector 2: "; v2.display();
cout << "Resultant Vector: "; v3.display();
return 0;
}
Aim: Create a program that exhibits the usage of Function Overloading (Compile Time Polymorphism)
Self Assessment
- Write a Program to demonstrate the concept of function overloading in C++ using work and energy example.
Practice Exercise 15: Write a program to calculate the resultant force when two forces act in the same direction or at an angle to each other.
Theory:
Forces in the Same Direction: When two forces act in the same direction, the resultant force is simply the sum of the magnitudes of both forces.
Formula:
$$ F_{\text{resultant}} = F_1 + F_2 $$
where:
- $ F_1 $ is the magnitude of the first force,
- $ F_2 $ is the magnitude of the second force.
Forces in Different Directions: When two forces act at an angle to each other, the resultant force is determined using the law of cosines. This formula accounts for both the magnitudes of the forces and the angle between them.
Formula:
$$ F_{\text{resultant}} = \sqrt{F_1^2 + F_2^2 + 2 F_1 F_2 \cos(\theta)} $$
where:
- $ F_1 $ is the magnitude of the first force,
- $ F_2 $ is the magnitude of the second force,
- $ \theta $ is the angle between the two forces.
Algorithm:
- Start
- Define Force class with two methods:
calculateForce(double force1, double force2)
for forces in the same direction.calculateForce(double force1, double force2, double angle)
for forces in different directions.- In main function:
- Initialize force1, force2, and angle.
- For forces in the same direction, call
calculateForce(force1, force2)
and print the result. - For forces in different directions, call
calculateForce(force1, force2, angle)
and print the result. - End
Code:
#include <iostream>
#include <cmath>
using namespace std;
class Force {
public:
// forces in the same direction
double calculateForce(double force1, double force2) {
return force1 + force2;
}
// forces in different directions
double calculateForce(double force1, double force2, double angle) {
return sqrt(force1 * force1 + force2 * force2 + 2 * force1 * force2 * cos(angle));
}
};
int main() {
Force f;
double force1 = 10.0, force2 = 15.0;
double angle = 3.14159 / 4; // 45 degrees in radians
// same direction
double resultantSame = f.calculateForce(force1, force2);
std::cout << "Resultant force (same direction): " << resultantSame << " N" << std::endl;
// different directions
double resultantDifferent = f.calculateForce(force1, force2, angle);
cout << "Resultant force (different directions): " << resultantDifferent << " N" << endl;
return 0;
}
Aim: Construct a program that demonstrates the implementation of Abstract Classes and Virtual Functions(Run Time Polymorphism)
Self Assessment
Practice Exercise 16:
Theory:
Algorithm:
Code:
References
There may be some AI Generated content in this article used for demonstration purposes.