Construct a C++ class for complex numbers and use a friend function to overload the * operator for complex number multiplication. Write a program to multiply two complex numbers using the overloaded operator, implemented as a friend function.
Sure, here's a C++ class for complex numbers with a friend function to overload the * operator for complex number multiplication:
cpp #include using namespace std;
class Complex { private: float real; float imag;
public: Complex(float r, float i) : real(r), imag(i) {}
friend Complex operator*(const Complex &c1, const Complex &c) { float result_real = c1.real * c2.real - c1.imag c2.imag; float result_imag = c1.real * c2.imag + c1.imag *2.real; return Complex(result_real, result_imag); } void display() { cout << real " + " << imag << "i" << endl; }
};
int main() { Complex c1(2, 3); Complex c2(4, 5); Complex result = c1 * c;
cout << "Result of multiplication: "; result.display(); return 0;
}
In this program we define a `Complex` class with real and imaginary parts We then overload the * operator using a friend function to perform complex number multiplication. In the `main` function, we create two complex numbersc1` and `c2`, and we multiply them using the overloaded * operator, obtaining the result in the `result` object which is then.
Create a base class "Shape" with attributes for length and width. Derive a class "Rectangle" from "Shape" and implement a method to calculate its area. Create an object of "Rectangle" and calculate its area.
Certainly! Below is a simple C++ program that demonstrates a base class "Shape" with attributes for length and width. We then a class "Rectangle" from "Shape" and implement a method to calculate its area. Finally, we create an object of Rectangle and calculate its area.
#include <iostream> using namespace std; // Base class Shape class Shape { protected: float length; float width; public: Shape(float l, float w) : length(l), width(w) {} }; // Derived class Rectangle from Shape class Rectangle : public Shape { public: Rectangle(float l, float w) : Shape(l, w) // Method to calculate area float calculateArea() { return length * width; } }; int main() { Create an object of Rectangle Rectangle rect(5, 3); // Calculate and display area cout << "Area of the rectangle: " <<.calculateArea() << endl; return 0; }
In this program, the Shape
class is the class with attributes for length and width. The Rectangle
is derived from Shape
and contains a method calculateArea()
to calculate the area of the rectangle. In the main()
function, we create an object of ``, set its dimensions, and then calculate and display its area.
Develop a C++ class representing a bank account and use a friend function to overload the + operator to add the balances of two accounts. Create a program to demonstrate this operator overloading using friend functions.
Certainly! Below is a simplified C++ program that creates a class representing a bank account and utilizes a friend function to overload the + operator for adding balances of two accounts. We then create a program to demonstrate this operator overloading using friend functions.
#include <iostream> using namespace std; class BankAccount { private: string accountHolder; float balance; public: BankAccount(string, float initialBalance) : accountHolder(holder), balance(initialBalance) {} // Friend function to overload the + operator friend float operator+(const BankAccount& acc1, const BankAccount& acc2) { return acc.balance + acc2.balance; } }; int main() { // Create two accounts BankAccount account1("John Doe", 1500); BankAccount account2(" Smith", 2500); // Demonstrate operator over using friend function float totalBalance = account1 + account2; cout << "Total balance after adding both accounts: << totalBalance << endl; return 0; }
In this, we have defined a BankAccount
class with an accountHolder
balance
attributes. We then overload the + operator using friend function to add the balances of two accounts. In the main
function, we create two Bank
objects and use the overloaded + operator to demonstrate adding the balances of these accounts
Develop a C++ class for fractions and overload the + operator to add two fractions together. Write a program that computes the sum of two fractions and explains the operator overloading involved.
Certainly! Below is a C++ program that implements a class for fractions and overloads the + operator to add two fractions together. Additionally, I'll include the program that computes the sum of two fractions and explain the operator overloading involved.
#include <iostream> using namespace std; class Fraction { private: int numerator; int denominator; public: Fraction(int num, int denom) : numerator(num), denominator(denom) {} Fraction operator+(const Fraction& other) { int newNumerator = numerator * other.denominator + other.numerator * denominator; int newDenominator = denominator * other.denominator; return Fraction(newNumerator, newDenominator); } void display() { cout << numerator << "/" <<denominator; } }; int main() { // Create two fraction objects Fraction frac1(1, 2); Fraction frac2(1, 3); // Compute the sum of the fractions Fraction sum = frac1 + frac2; // Explain the operator overloading cout << "The sum of "; frac1.display(); cout << " and "; frac2.display(); cout << " is "; sum.display(); cout << endl; return 0; }
In this program, there is a Fraction
class representing fractions with a numerator and a denominator. The +
operator is overloaded as a member function of the Fraction
class to add two fractions together. In the main
function, two Fraction
objects are created, and their sum is computed using the overloaded + operator. Finally, the program prints the result of the addition and explains the operator overloading involved.