Advertisement
frentzy

supraincarcare operatori

Apr 23rd, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. class Complex {
  8. private:
  9.     float re;
  10.     float im;
  11. public:
  12.     Complex(float re = 0, float im = 0);
  13.     void afisare();
  14.     Complex& operator ++();
  15.     Complex operator +(Complex z);
  16.     Complex& operator ++(int);
  17.     Complex operator ~();
  18.     Complex operator +=(Complex z);
  19.     Complex operator /(Complex z);
  20.     friend Complex operator -(Complex z1,
  21.         Complex z2);
  22.     friend Complex operator -(Complex z);
  23. };
  24.  
  25. Complex& Complex::operator ++() {
  26.     //re += 1;
  27.     ++re;
  28.     printf("preincrementare\n");
  29.     return *this;
  30. }
  31.  
  32. Complex& Complex::operator ++(int n) {
  33.     re++;
  34.     printf("postincrementare\n");
  35.     return *this;
  36. }
  37.  
  38. Complex::Complex(float re, float im) {
  39.     this->re = re;
  40.     this->im = im;
  41. }
  42. void Complex::afisare() {
  43.     printf("%-g%+g*i\n", re, im);
  44. }
  45. Complex Complex::operator +(Complex z) {
  46.     Complex rez;
  47.     rez.re = this->re + z.re;
  48.     rez.im = this->im + z.im;
  49.     return rez;
  50. }
  51. Complex Complex::operator /(Complex z) {
  52.     Complex rez;
  53.     rez.re = this->re + z.re;
  54.     rez.im = this->im + z.im;
  55.     return rez;
  56. }
  57. Complex Complex::operator +=(Complex z) {
  58. //  Complex rez;
  59.      re += z.re;
  60.      this->im += z.im;
  61.  
  62.     return *this;
  63. }
  64.  
  65. Complex Complex::operator ~() {
  66.     return Complex(re, -im);
  67. }
  68. Complex operator -(Complex z1, Complex z2) {
  69.     cout <<"hihi";
  70.     return Complex(z1.re - z2.re, z1.im - z2.im);
  71. }
  72. Complex operator -(Complex z) {
  73.     cout << "haha";
  74.     return Complex(-z.re, -z.im);
  75. }
  76.  
  77. void main() {
  78.     Complex z1(4, 5), z2(3, 1), z;
  79.     z = z1 + z2; z.afisare();
  80.     z = z1 - z2; z.afisare();
  81.     z = ~z1; z.afisare();
  82.     z = -z1; z.afisare();
  83.     z = z1;
  84.  
  85.    
  86.     cout << endl << endl;
  87.     z.afisare();
  88.     z1 += z1;
  89.     z1.afisare();
  90.     z2.afisare();
  91.     z = z1 / z2;
  92.     z.afisare();
  93.     ++z;
  94.     z++;
  95.     z.afisare();
  96.     _getch();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement