Advanced Programming (C++)

Inheritance

Ahmad Yoosofan

University of Kashan

http://yoosofan.github.io/course/cpp.html

Simple Shape(I)

 1 class Shape{
 2   string name = "shape";//char name[100]; '\0' رشته
 3   string color = "white";//strcpy, strcmp; char*color; str1=str2
 4  public:
 5   int Area(void){return 0;}
 6   Shape() = default;
 7   friend ostream& operator <<(ostream& o1, const Shape& s1){
 8     o1 << "name: " << s1.name << "\tcolor: " << s1.color;
 9     return o1;
10   }
11 };
12 class Square: public Shape{
13   int d = 1;
14  public:
15   int Area(void){return d*d;}
16   Square(int d){this->d=d;}
17   Square() = default;
18   friend ostream& operator <<(ostream& o1, const Square& s1){
19     o1 << Shape(s1) << "\tside: " << s1.d;
20     return o1;
21   }
22 };
1 int main(){
2   Shape s;
3   cout<< s.Area() << endl;
4   Square sq(3);
5   cout<< sq.Area() << endl;
6   cout << sq << endl;
7 }

Simple Shape(II) - getColor

 1 class Shape{
 2   string name = "shape";
 3   string color = "white";
 4 public:
 5   Shape(string na, string co)
 6   {name=na; color = co;}
 7   int Area(void){return 0;}
 8   Shape() = default;
 9   friend ostream& operator <<(
10       ostream& o1, const Shape& s1){
11     o1 << "name: " << s1.name
12        << "\tcolor: " << s1.color;
13     return o1;
14   }
15   string getColor(void){return color;}
16 };
17 class Square: public Shape{
18   int d = 1;
19 public:
20   int Area(void){return d*d;}
21   Square(int d){this->d=d;}
22   Square() = default;
23   friend ostream& operator <<(
24       ostream& o1, const Square& s1){
25     o1<< Shape(s1)<<"\tside: "<<s1.d;
26     return o1;
27   }

Simple Shape(III) - getColor

 1 class Shape{
 2   string name = "shape";
 3   string color = "white";
 4 public:
 5   Shape(string na, string co)
 6   {name=na; color = co;}
 7   int Area(void){return 0;}
 8   Shape() = default;
 9   friend ostream& operator <<(
10       ostream& o1, const Shape& s1){
11     o1 << "name: " << s1.name
12        << "\tcolor: " << s1.color;
13     return o1;
14   }
15   string getColor(void){return color;}
16 };
17 class Square: public Shape{
18   int d = 1;
19 public:
20   int Area(void){return d*d;}
21   Square(int d){this->d=d;}
22   Square() = default;
23   friend ostream& operator <<(
24       ostream& o1, const Square& s1){
25     o1<< Shape(s1)<<"\tside: "<<s1.d;
26     return o1;
27   }

Simple Shape(IV) - Class A

 1 class A{
 2   public:
 3   void say(void){cout << "A" << endl;}
 4 };
 5 class Shape: public A{
 6   string name = "shape";
 7   string color = "shape";
 8 public:
 9   int Area(void){return 0;}
10   Shape() = default;
11   friend ostream& operator <<(
12       ostream& o1, const Shape& s1){
13     o1 << "name: " << s1.name << "\tcolor: " << s1.color;
14     return o1;
15   }
16 };
17 class Square: public Shape{
18   int d = 1;
19 public:
20   int Area(void){return d*d;}
21   Square(int d){this->d=d;}
22   Square() = default;
23   friend ostream& operator <<(
24       ostream& o1, const Square& s1){
25     o1 << Shape(s1) << "\tside: "
26        << s1.d;
27     return o1;

Simple Shape(V) - Struct A

 1 struct A{
 2   void say(void){cout << "A" << endl;}
 3 };
 4 class Shape: public A{
 5   string name = "shape";
 6   string color = "shape";
 7 public:
 8   int Area(void){return 0;}
 9   Shape() = default;
10   friend ostream& operator <<(
11       ostream& o1, const Shape& s1){
12     o1 << "name: " << s1.name
13        << "\tcolor: " << s1.color;
14     return o1;
15   }
16 };
17 class Square: public Shape{
18   int d = 1;
19 public:
20   int Area(void){return d*d;}
21   Square(int d){this->d=d;}
22   Square() = default;
23   friend ostream& operator <<(
24       ostream& o1, const Square& s1){
25     o1 << Shape(s1) << "\tside: "
26        << s1.d;
27     return o1;

Simple Shape(VI) - A aa

 1 struct A{
 2   void say(void){cout << "A" << endl;}
 3 };
 4 class Shape: public A{
 5   string name = "shape";
 6   string color = "shape";
 7 public:
 8   int Area(void){return 0;}
 9   Shape() = default;
10   friend ostream& operator <<(
11       ostream& o1, const Shape& s1){
12     o1 << "name: " << s1.name
13        << "\tcolor: " << s1.color;
14     return o1;
15   }
16 };
17 class Square: public Shape{
18   int d = 1;
19 public:
20   int Area(void){return d*d;}
21   Square(int d){this->d=d;}
22   Square() = default;
23   friend ostream& operator <<(
24       ostream& o1, const Square& s1){
25     o1 << Shape(s1) << "\tside: "
26        << s1.d;
27     return o1;

Simple Shape(VII) - protected(I)

 1 class A{};
 2 class Shape: public A{
 3   string name = "shape";
 4   string color = "shape";
 5  protected:
 6   int test = 1;
 7  public:
 8   int Area(void){return 0;}
 9   Shape() = default;
10   friend ostream& operator <<(ostream& o1, const Shape& s1){
11     o1 << "name: " << s1.name << "\tcolor: " << s1.color;
12     return o1;
13   }
14 };
15 class Square: public Shape{
16   int d = 1;
17  public:
18   int Area(void)
19   {test = 2; Shape::Area(); return d*d;}
20   Square(int d){this->d=d;}
21   int testValue(void){return test;}
22   Square() = default;
23   friend ostream& operator <<(
24       ostream& o1, const Square& s1){
25     o1 << Shape(s1) << "\tside: "
26        << s1.d;
27     return o1;

Simple Shape(VIII) - protected(II)

 1 class Shape{
 2 protected:
 3   string name = "shape";
 4   string color = "white";
 5   int test = 2;
 6 public:
 7   int Area(void){return 0;}
 8   int testValue(void){return test;}
 9   Shape() = default;
10   friend ostream& operator <<(ostream& o1, const Shape& s1){
11     o1 << "name: " << s1.name
12        << "\tcolor: " << s1.color;
13     return o1;
14   }
15 };
16 class Square: public Shape{
17   int d = 1;
18 public:
19   int Area(void){return d*d;}
20   Square(int d)
21   {this->d=d; test = 4; name="Square"; color="Red";}
22   int testValue(void){return test;}
23   Square() = default;
24   friend ostream& operator <<(
25       ostream& o1, const Square& s1){
26     o1 << Shape(s1) << "\tside: " << s1.d;
27     return o1;

Simple Shape(IX) - Error name

 1 using namespace std;
 2 class A{};
 3 class Shape: public A{
 4 protected:
 5   string name = "shape";
 6   string color= "blue";
 7 public:
 8   int Area(void){return 0;}
 9 };
10 class Square: public Shape{
11   int d = 1;
12 public:
13   int Area(void){return d*d;}
14   Square(int d=1){this->d=d;}
15 };
16 void f1(shape a)
17 {cout << a.Area() << endl;}
18 int main(){
19   Shape s;
20   f1(s);
21   Square sq(3);
22   cout << sq.Area() << endl;
23   Shape* ps = &s;
24   cout << ps->Area() << endl;
25   ps = &sq;
26   cout << ps->Area() << endl;
27 }

Simple Shape(X) - Correct Error

 1 class A{};
 2 class Shape: public A{
 3 protected:
 4   string name;
 5   string color;
 6 public:
 7   int Area(void){return 0;}
 8 };
 9 class Square: public Shape{
10   int d;
11 public:
12   int Area(void){return d*d;}
13   Square(int d=1){this->d=d;}
14 };
15 void f1(Shape a) // Correct
16 {cout << a.Area() << endl;}
17 int main(){
18   Shape s;
19   f1(s);
20   Square sq(3);
21   cout << sq.Area() << endl;
22   Shape* ps = &s;
23   cout << ps->Area() << endl;
24   ps = &sq;
25   cout << ps->Area() << endl;
26 }

Simple Shape(XI) - Function Call

 1 class A{};
 2 class Shape: public A{
 3 protected:
 4   string name;
 5   string color;
 6 public:
 7   int Area(void){
 8     cout << "in Shape" << endl;
 9     return 0;
10   }
11 };
12 class Square: public Shape{
13   int d;
14 public:
15   int Area(void){
16     cout << "in Square" << endl;
17     return d*d;
18   }
19   Square(int d=1){this->d=d;}
20 };
21 void f1(Shape a)
22 {cout << a.Area() << endl;}
23 int main(){
24   Shape s;
25   f1(s);
26   Square sq(3);
27   f1(sq);

Simple Shape(XII) - Private Inheritance

 1 using namespace std;
 2 class A{};
 3 class Shape: public A{
 4 protected:
 5   string name;
 6   string color;
 7 public:
 8   int Area(void){
 9     cout << "in Shape" << endl;
10     return 0;
11   }
12   void show(void)
13   {cout<<"show in shape"<<endl;}
14 };
15 class Square: private Shape{
16   int d;
17 public:
18   int Area(void){
19     cout << "in Square" << endl;
20     return d*d;
21   }
22   Square(int d=1){this->d=d;}
23 };
24 void f1(Shape a){
25   cout << a.Area() << endl;
26   a.show();
27 }

Simple Shape(XIII) - Protected Inheritance

 1 using namespace std;
 2 class A{};
 3 class Shape: public A{
 4 protected:
 5   string name;
 6   string color;
 7 public:
 8   int Area(void){
 9     cout << "in Shape" << endl;
10     return 0;
11   }
12   void show(void)
13   {cout<<"show in shape"<<endl;}
14 };
15 class Square: protected Shape{
16   int d;
17 public:
18   int Area(void){
19     cout << "in Square" << endl;
20     return d*d;
21   }
22   Square(int d=1){this->d=d;}
23 };
24 void f1(Shape a){
25   cout << a.Area() << endl;
26   a.show();
27 }

Simple Shape(XIV) - Drive1

 1 using namespace std;
 2 class A{};
 3 class Shape: public A{
 4 protected:
 5   string name;
 6   string color;
 7 public:
 8   int Area(void){
 9     cout << "in Shape" << endl;
10     return 0;
11   }
12   void show(void)
13   {cout<<"show in shape"<<endl;}
14 };
15 class Square: protected Shape{
16   int d;
17 public:
18   int Area(void){
19     cout << "in Square" <<endl;
20     return d*d;
21   }
22   Square(int d=1){this->d=d;}
23 };
24 class drive1 : public Square{
25   public:
26   drive1(int d=1):Square(d){}
27   void f(Shape a){

ComplexCls(I)

 1 struct myComplex{ // 100580
 2   double re, img;
 3   myComplex(const double a = 0,
 4             const double b = 0)
 5   {re = a; img = b;}
 6   myComplex(const myComplex&c){
 7     re=c.re; img=c.img;
 8     cout << "copy constructor"
 9          << endl;
10   }
11   void print(void){
12     cout << '(' << re << ", "
13          << img << ')' << endl;
14   }
15   void input(void){
16     cout << "Enter real "; cin >> re;
17     cout << "Enter imaginary ";cin >> img;
18   }
19   myComplex add(const myComplex& a){
20     myComplex r = a;  r.re  += re;
21     r.img += img;  return r;
22   }
23   myComplex sub(const myComplex& a){
24     myComplex r; r.re = re - a.re;
25     r.img = img - a.img; return r;
26   }
27   myComplex mul(const myComplex& a){
28     myComplex r;
 1     r.re = re * a.re - img * a.img;
 2     r.img = img * a.re + re * a.img;
 3     return r;
 4   }
 5 };
 6 void myFunction(void){
 7   myComplex a(2, 3), b=a, c(a);
 8   c = a.add(b); c = a.add(2);
 9   c.print();  a.input();
10   c = a.sub(b);// c = a - b
11   c.print();  b.input();
12   c = a.mul(b);// c = a * b
13   c.print();
14 }
15 int main(){myFunction();}/*
16 copy constructor
17 copy constructor
18 copy constructor
19 copy constructor
20 (4, 3)
21 Enter real 2
22 Enter imaginary 3
23 (0, 0)
24 Enter real 4
25 Enter imaginary 5
26 (-7, 22) */

ComplexCls(II)

 1 struct myComplex{ // op/1022
 2   double re, img;
 3   myComplex(const double a = 0,
 4             const double b = 0)
 5   {re = a; img = b;}
 6   myComplex(const myComplex&c){
 7     re=c.re; img=c.img;
 8     cout << "copy constructor"
 9          << endl;
10   }
11   void print(void){
12     cout << '(' << re << ", "
13          << img << ')' << endl;
14   }
15   void input(void){
16     cout << "Enter real "; cin >> re;
17     cout << "Enter imaginary ";cin >> img;
18   }
19   myComplex add(const myComplex& a){
20     myComplex r = a;  r.re  += re;
21     r.img += img;  return r;
22   }
23   myComplex sub(const myComplex& a){
24     myComplex r; r.re = re - a.re;
25     r.img = img - a.img; return r;
26   }
27   myComplex mul(const myComplex& a){
 1     myComplex r;
 2     r.re = re * a.re - img * a.img;
 3     r.img = img * a.re + re * a.img;
 4     return r;
 5   }
 6 };
 7 void myFunction(void){
 8   myComplex a(2, 3), b=a, c(a);
 9   c = a.add(b); c = a.add(2);
10   c.print();  a.input();
11   c = a.sub(b);// c = a - b
12   c.print();  b.input();
13   c = a.mul(b);// c = a * b
14   c.print();
15 }
16 int main(){myFunction();return 0;}/*
17 copy constructor
18 copy constructor
19 copy constructor
20 copy constructor
21 (4, 3)
22 Enter real 2
23 Enter imaginary 3
24 (0, 0)
25 Enter real 4
26 Enter imaginary 5
27 (-7, 22)
28 */

ComplexCls(III)

 1 struct myComplex{ // op/1024
 2   double re, img;
 3   myComplex(const double a = 0,
 4             const double b = 0)
 5   {re = a; img = b;}
 6   myComplex(const myComplex&c){
 7     re=c.re; img=c.img;
 8     cout << "copy constructor"
 9          << endl;
10   }
11   void print(void){
12     cout << '(' << re << ", "
13          << img << ')' << endl;
14   }
15   void input(void){
16     cout << "Enter real "; cin >> re;
17     cout << "Enter imaginary ";cin >> img;
18   }
19   myComplex add(const myComplex& a){
20     myComplex r = a;  r.re  += re;
21     r.img += img;  return r;
22   }
23   myComplex sub(const myComplex& a){
24     myComplex r; r.re = re - a.re;
25     r.img = img - a.img; return r;
26   }
27   myComplex mul(const myComplex& a){
 1     myComplex r;
 2     r.re = re * a.re - img * a.img;
 3     r.img = img * a.re + re * a.img;
 4     return r;
 5   }
 6 };
 7 void myFunction(void){
 8   myComplex a(2, 3), b=a, c(a);
 9   c = a.add(b); c = a.add(2);
10   c.print();  a.input();
11   c = a.sub(b);// c = a - b
12   c.print();  b.input();
13   c = a.mul(b);// c = a * b
14   c.print();
15 }
16 int main(){myFunction();return 0;}/*
17 copy constructor
18 copy constructor
19 copy constructor
20 copy constructor
21 (4, 3)
22 Enter real 2
23 Enter imaginary 3
24 (0, 0)
25 Enter real 4
26 Enter imaginary 5
27 (-7, 22)
28 */

ComplexCls(IV) / Operator +

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 class complexCls{
 5   double r,i;
 6  public:
 7   complexCls(int m=0,int n=0)
 8   {r=m;i=n;}
 9   void Show(void) const
10   {cout<<r<<"+ i "<<i<<endl;}
11   double Magnitude(void) const
12   {return sqrt(r*r+i*i);}
13   void input(void){
14     cout<<"Enter real:"; cin>>r;
15     cout<<"Enter imaginary:"; cin>>i;
16   }
17   complexCls operator+(complexCls b){
18     complexCls c;
19     c.r = r + b.r;
20     c.i = this->i + b.i;
21     return c;
22   }
23   friend complexCls add(
24       complexCls a, complexCls b);
25 };
26 complexCls add(complexCls a,
27     complexCls b){
28   complexCls c = a;
29   c.r += b.r;
29   c.i += b.i;
30   return c;
31 }
32 void f1(void){
33   complexCls a(2, 3), b(2, 1), c(a);
34   c = a + b; c.Show();
35   c = add(a, b); c.Show();
36   c = a.operator+(b); c.Show();
37   c = a + b + c; c.Show();
38   c = a.operator+(b.operator+(c));
39   c.Show();
40 }
41 int main(){f1();}
4+ i 4
4+ i 4
4+ i 4
8+ i 8
12+ i 12

ComplexCls(V) / Operator +

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 class complexCls{
 5   double r,i;
 6  public:
 7   complexCls(int m=0,int n=0)
 8   {r=m;i=n;}
 9   void Show(void) const
10   {cout<<r<<"+ i "<<i<<endl;}
11   double Magnitude(void) const
12   {return sqrt(r*r+i*i);}
13   void input(void){
14     cout<<"Enter real:"; cin>>r;
15     cout<<"Enter imaginary:"; cin>>i;
16   }
17   complexCls operator+(const complexCls& b){
18     complexCls c;
19     c.r = r + b.r;
20     c.i = this->i + b.i;
21     return c;
22   }
 1   friend complexCls add(const complexCls& a,
 2       const complexCls& b);
 3 };
 4 complexCls add(complexCls& a,
 5     complexCls& b){
 6   complexCls c = a;
 7   c.r += b.r;
 8   c.i += b.i;
 9   return c;
10 }
11 void f1(void){
12   complexCls a(2,3),b(2,1),c(a);
13   c=a+b;
14   c.Show();
15   c=add(a,b);
16   c.Show();
17 }
18 int main(){f1();}

ComplexCls(VI) / Operator + (Error)

 4 class complexCls{
 5   double r,i; // Error
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:";
16     cin >> i;
17   }
18   complexCls operator+(const complexCls& b){
19     complexCls c;
20     c.r = r + b.r;
21     c.i = this->i + b.i;
22     return c;
23   }
24   friend complexCls add(const complexCls& a,
25       const complexCls& b){
26     complexCls c = a;
27     c.r += b.r;
28     c.i += b.i;
29     return c;
30   }
31 };
32 void f1(void){
33   complexCls a(2, 3), b(2, 1), c(a);
34   c = a + 2;
35   c.Show();
36   c = add(a,b);
37   c.Show();
38 }
39 int main(){f1();}
In function ‘void f1()’:
1046.cpp:33:9: error: no match for ‘operator+’
 (operand types are ‘complexCls’ and ‘int’)
   33 |   c = a + 2;
      |       ~ ^ ~
      |       |   |
      |       |   int
      |       complexCls
1046.cpp:17:14: note: candidate:
 ‘complexCls complexCls::operator+(c

ComplexCls(VII) / Operator +

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:"; cin >> i;
16   }
17   complexCls operator+(const complexCls& b){
18     complexCls c;
19     c.r = r + b.r;
20     c.i = this->i + b.i;
21     return c;
22   }
23   friend complexCls add(const complexCls& a,
24       const complexCls& b){
25     complexCls c = a;
26     c.r += b.r;
27     c.i += b.i;
28     return c;
32   }
33 };
34 void f1(void){
35   complexCls a(2, 3), b(2, 1), c(a);
36   c = a + 2;
37   c.Show();
38   c = add(a,b);
39   c.Show();
40 }
41 int main(){f1();}
g++ src/op/1056.cpp
slide$ ./a.out
4+ i 3
4+ i 4

ComplexCls(VIII) / Operator + - *

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:"; cin >> i;
16   }
17   complexCls operator+(const complexCls& b){
18     complexCls c;
19     c.r = r + b.r;
20     c.i = this->i + b.i;
21     return c;
22   }
23   complexCls operator-(const complexCls& b){
24     complexCls c;
25     c.r = r - b.r;
26     c.i = this->i - b.i;
27     return c;
28   }
32   complexCls operator*(
33       const complexCls& b){
34     complexCls c;
35     c.r = r * b.r - i * b.i;
36     c.i = r * b.i + i * b.r;
37     return c;
38   }
39 };
40 void f1(void){
41   complexCls a(2, 3),
42     b(2, 1), c(a);
43   c = a + 2;  c.Show();
44   c = a - 2;  c.Show();
45 }
46 int main(){f1();}

ComplexCls(VIII) / Operator + - * =

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void)
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void)
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:";
16     cin >> i;
17   }
18   complexCls operator+(const complexCls& b){
19     complexCls c;
20     c.r = r + b.r;
21     c.i = this->i + b.i;
22     return c;
23   }
24   complexCls operator-(const complexCls& b){
25     complexCls c;
26     c.r = r - b.r;
27     c.i = this->i - b.i;
28     return c;
32   }
33   complexCls operator*(const complexCls& b){
34     complexCls c;
35     c.r = r * b.r - i * b.i;
36     c.i = r * b.i + i * b.r;
37     return c;
38   }
39   complexCls operator=(
40       const complexCls& b){
41     r = b.r;
42     i = b.i;
43     return *this;
44   }
45 };
46 void f1(void){
47   complexCls a(2, 3), b(2, 1), c(a);
48   c = a + 2;  c.Show();
49   c = a - 2;  c.Show();
50   b = a = c;
51   b.operator=(a.operator=(c));
52   b.Show();
53 }
54 int main(){f1();}/*
55 4+ i 3
56 0+ i 3
57 0+ i 3  */

ComplexCls(VIII) / Operator + - * = []

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:";
16     cin >> i;
17   }
18   complexCls operator+(
19       const complexCls& b){
20     complexCls c;
21     c.r = r + b.r;
22     c.i = this->i + b.i;
23     return c;
24   }
25   complexCls operator-(
26       const complexCls& b){
27     complexCls c;
28     c.r = r - b.r;
29     c.i = this->i - b.i;
30     return c;
30   }
31   complexCls operator*(
32       const complexCls& b){
33     complexCls c;
34     c.r = r * b.r - i * b.i;
35     c.i = r * b.i + i * b.r;
36     return c;
37   }
38   complexCls operator=(
39       const complexCls& b){
40     r = b.r;
41     i = b.i;
42     return *this;
43   }
44   double operator[](int index){
45     if(index < 0 || index > 1){
46       cout <<
47         "index is out of range"
48         << index << endl;
49       return r;
50     }
51     if(index) return i;
52     return r;
53   }
54 };
55 void f1(void){
56   complexCls a(2, 3),
57     b(2, 1), c(a);
58   c = a + 2;  c.Show();
59   c = a - 2;  c.Show();
60   cout << c[0] << endl;
61   cout << c[1] << endl;
62 }
63 int main(){f1();}/*
64 4+ i 3
65 0+ i 3
66 0

ComplexCls(VIII) / Operator + - * = []

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:"; cin >> i;
16   }
17   complexCls operator+(const complexCls& b){
18     complexCls c;
19     c.r = r + b.r;
20     c.i = this->i + b.i;
21     return c;
22   }
23   complexCls operator-(const complexCls& b){
24     complexCls c;
25     c.r = r - b.r;
26     c.i = this->i - b.i;
27     return c;
28   }
29   complexCls operator*(const complexCls& b){
29     complexCls c;
30     c.r = r * b.r - i * b.i;
31     c.i = r * b.i + i * b.r;
32     return c;
33   }
34   complexCls operator=(
35       const complexCls& b){
36     r = b.r;
37     i = b.i;
38     return *this;
39   }
40   double& operator[](int index){
41     if(index < 0 || index > 1){
42       cout <<
43         "index is out of range"
44         << index << endl;
45       return r;
46     }
47     if(index) return i;
48     return r;
49   }
50 };
51 void f1(void){
52   complexCls a(2, 3), b(2, 1), c(a);
53   c = a + 2;  c.Show();
54   c = a - 2;  c.Show();
55   c[0] = 56;
56   c.Show();
57 }
58 int main(){f1();}/*
59 4+ i 3
60 0+ i 3
61 56+ i 3   */

ComplexCls(VIII) / Operator + - * =

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:"; cin >> i;
16   }
17   complexCls operator+(const complexCls& b){
18     complexCls c;
19     c.r = r + b.r;
20     c.i = this->i + b.i;
21     return c;
22   }
23   complexCls operator-(const complexCls& b){
24     complexCls c;
25     c.r = r - b.r;
26     c.i = this->i - b.i;
27     return c;
28   }
29   complexCls operator*(const complexCls& b){
30     complexCls c;
31     c.r = r * b.r - i * b.i;
32     c.i = r * b.i + i * b.r;
33     return c;
34   }
34   complexCls operator=(const complexCls& b){
35     r = b.r;
36     i = b.i;
37     return *this;
38   }
39   double& operator[](int index){
40     if(index < 0 || index > 1){
41       cout << "index is out of range" << index << endl;
42       return r;
43     }
44     if(index) return i;
45     return r;
46   }
47 };
48 void f1(void){
49   complexCls a(2, 3), b(2, 1), c(a);
50   c = 2 + a;  c.Show();
51   // c.operator=(2.operator+(a));
52   c = a - 2;  c.Show();
53   // c.operator=(a.operator+(2));
54   c[0] = 56;
55   c.Show();
56 }
57 int main(){f1();}/*
58 In function ‘void f1()’:
59 1090.cpp:51:9: error: no match for ‘operator+’
60 * (operand types are ‘int’ and ‘complexCls’)
61    51 |   c = 2 + a;  c.Show();
62       |       ~ ^ ~
63       |       |   |
64       |       int complexCls
65 */

ComplexCls(IX) / friend

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void) const
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void) const
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:"; cin >> i;
16   }
17   friend complexCls operator+(
18       const complexCls& a,const complexCls& b){
19     complexCls c;
20     c.r = a.r + b.r;
21     c.i = a.i + b.i;
22     return c;
23   }
24   friend complexCls operator-(
25       const complexCls& a,const complexCls& b){
26     complexCls c;
27     c.r = a.r - b.r;
28     c.i = a.i - b.i;
29     return c;
30   }
31   friend complexCls operator*(
32       const complexCls& a,const complexCls& b){
33     complexCls c;
34     c.r = a.r * b.r - a.i * b.i;
34     c.i = a.r * b.i + a.i * b.r;
35     return c;
36   }
37   complexCls operator=(const complexCls& b){
38     r = b.r;
39     i = b.i;
40     return *this;
41   }
42   double& operator[](int index){
43     if(index < 0 || index > 1){
44       cout <<
45         "index is out of range"
46         << index << endl;
47       return r;
48     }
49     if(index) return i;
50     return r;
51   }
52 };
53 void f1(void){
54   complexCls a(2, 3), b(2, 1), c(a);
55   c = 2 + a;  c.Show();
56   // c.operator=(operator+(2, a));
57   // operator+(complexCls(2), a);
58   c = a - 2;  c.Show();
59   c[0] = 56; c.Show();
60 }
61 int main(){f1();}

ComplexCls(X) / Operator == != !

 4 class complexCls{
 5   double r, i;
 6  public:
 7   complexCls(int m = 0, int n = 0)
 8   {r = m; i = n;}
 9   void Show(void)
10   {cout << r << "+ i " << i << endl;}
11   double Magnitude(void)
12   {return sqrt(r * r + i * i);}
13   void input(void){
14     cout << "Enter real:"; cin >> r;
15     cout << "Enter imaginary:"; cin >> i;
16   }
17   friend complexCls operator+(
18       const complexCls& a,const complexCls& b){
19     complexCls c;
20     c.r = a.r + b.r;
21     c.i = a.i + b.i;
22     return c;
23   }
24   friend complexCls operator-(
25       const complexCls& a,const complexCls& b){
26     complexCls c;
27     c.r = a.r - b.r;
28     c.i = a.i - b.i;
29     return c;
30   }
31   friend complexCls operator*(
32       const complexCls& a,const complexCls& b){
33     complexCls c;
34     c.r = a.r * b.r - a.i * b.i;
35     c.i = a.r * b.i + a.i * b.r;
36     return c;
37   }
37  complexCls operator=(const complexCls& b){
38     r = b.r;
39     i = b.i;
40     return *this;
41   }
42   double& operator[](int index){
43     if(index < 0 || index > 1){
44       cout << "index is out of range"
45         << index << endl;
46       return r;
47     }
48     if(index) return i;
49     return r;
50   }
51   friend bool operator==(const complexCls& a,
52       const complexCls& b)
53   {return (a.r == b.r) && (a.i == b.i) ? true: false;}
54   friend bool operator!=(const complexCls& a,
55       const complexCls& b)
56   {return a == b ? false: true;}
57   bool operator!(void)
58   {return r == 0 && i == 0 ? true: false;}
59 };
60 void f1(void){
61   complexCls a(2, 3), b(2, 1), c(a);
62   //c = 2 + a;  c.Show();
63   c = a = a + b + 2;  c.Show();
64   if(a == b ) cout << "a == b" << endl;
65   else cout << "a != b" << endl;
66   if(!a) cout << "a is not zero" << endl;
67 }
68 int main(){f1();}/*
69 0+ i 3
70 a != b */

ComplexCls(VIII) / Operator ++ --

30       const complexCls& b){
31     complexCls c;
32     c.r = a.r - b.r;
33     c.i = a.i - b.i;
34     return c;
35   }
36  complexCls operator=(const complexCls& b){
37     r = b.r;
38     i = b.i;
39     return *this;
40   }
41   double& operator[](int index){
42     if(index < 0 || index > 1){
43       cout << "index is out of range"
44         << index << endl; return r;
45     }
46     if(index) return i;
47     return r;
48   }
49   friend bool operator==(
50       const complexCls& a,
51       const complexCls& b){
52     return (a.r == b.r) && (a.i == b.i) ?
53       true: false;
54   }
55   friend bool operator!=(
56       const complexCls& a,
57       const complexCls& b)
58   {return a == b ? false: true;}
59   bool operator!(void)
60   {return r == 0 && i == 0 ? true: false;}
61   complexCls operator++(void)// ++a
62   { i++; r++; return *this;}
63   complexCls operator++(int dummy){//a++
64     complexCls result=*this;
65     r++; i++; return result;
66   }
67   complexCls operator--(void) // --a
68   { i--; r--; return *this;  }
69   complexCls operator--(int dummy){//a--
70     complexCls result=*this;
71     r--; i--; return result;
72   }
73   friend complexCls operator*(
74       const complexCls& a,
75       const complexCls& b){
76     complexCls c;
77     c.r = a.r * b.r - a.i * b.i;
78     c.i = a.r * b.i + a.i * b.r;
79     return c;
80   }
81 };
82 void f1(void){
83   complexCls a(2, 3), b(2, 1), c(a);
84   c = 2 + a;  c.Show();
85   c = a++; // a.operator++(11);
86   c.Show(); c = ++a;// a.operator++();
87   c.Show();
88   c = a--; c.Show(); c = --a; c.Show();

---

END

1