StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover 12. Program to design a class representing complex numbers and perform addition and multiplication using operator overloading.
Download
  1. C++ Programs list
11. Program to swap private data members of classes named `class1` and `class2` using a friend function. : 13. Program to create a class called `Cube` with data members `length`, `breadth`, and `height` and member functions to accept details, calculate the volume, and display the details.
C++ Programs list

1.      Program to design a class representing complex numbers and perform addition and multiplication using operator overloading.

  1. #include <iostream.h>

  2. #include<conio.h>

  3. class Complex {

  4. public:

  5.     double real, imag;

  6.  

  7.  

  8.     // Constructor

  9.     Complex(double r = 0, double i = 0) {

  10.                 real = r;

  11.                 imag = i;

  12.     }

  13.  

  14.     // Overloading + operator

  15.     Complex operator+(const Complex& obj) {

  16.                 Complex res;

  17.                 res.real = real + obj.real;

  18.                 res.imag = imag + obj.imag;

  19.                 return res;

  20.     }

  21.  

  22.     // Overloading * operator

  23.     Complex operator*(const Complex& obj) {

  24.                 Complex res;

  25.                 res.real = real * obj.real - imag * obj.imag;

  26.                 res.imag = real * obj.imag + imag * obj.real;

  27.                 return res;

  28.     }

  29.  

  30.     void print() {

  31.                 cout << real << " + i" << imag << endl;

  32.     }

  33. };

  34.  

  35. int main() {

  36.     int c11 , c12 ,c21,c22= 0;

  37.  

  38.     cout << "Enter 1st Real number & Imagnory number: ";

  39.     cin >> c11 >> c12;

  40.  

  41.     cout << "Enter 2nd Real number & Imagnory number: ";

  42.     cin >> c21 >> c22;

  43.  

  44.     Complex c1(c11, c12), c2(c21, c22);

  45.     Complex c3 = c1 + c2;

  46.     Complex c4 = c1 * c2;

  47.  

  48.     cout << "Sum: ";

  49.     c3.print();

  50.  

  51.     cout << "Product: ";

  52.     c4.print();

  53.     getch();

  54.     return 0;

  55. }

11. Program to swap private data members of classes named `class1` and `class2` using a friend function. 13. Program to create a class called `Cube` with data members `length`, `breadth`, and `height` and member functions to accept details, calculate the volume, and display the details.
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website