C++/Overload/Assign

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Another Demo: Overload the == and && relative to MyClass class.

<source lang="cpp">

  1. include <iostream>

using namespace std; class MyClass {

 int x, y; 

public:

 MyClass() { 
    x=0; 
    y=0; 
 }
 MyClass(int i, int j) { 
    x=i; 
    y=j; 
 }
 void getXY(int &i, int &j) { 
    i=x; 
    j=y; 
 }
 int operator==(MyClass object2);
 int operator&&(MyClass object2);

}; // Overload the == operator for MyClass. int MyClass::operator==(MyClass object2) {

 return x==object2.x && y==object2.y; 

} // Overload the && operator for MyClass. int MyClass::operator&&(MyClass object2) {

 return (x && object2.x) && (y && object2.y);

}

int main() {

 MyClass object1(10, 10), object2(5, 3), object3(10, 10), object4(0, 0);
 if(object1==object2) 
    cout << "object1 same as object2\n";
 else 
    cout << "object1 and object2 differ\n";
 if(object1==object3) 
    cout << "object1 same as object3\n";
 else 
    cout << "object1 and object3 differ\n";
 if(object1 && object2) 
    cout << "object1 && object2 is true\n";
 else 
    cout << "object1 && object2 is false\n";
 if(object1 && object4) 
    cout << "object1 && object4 is true\n";
 else 
    cout << "object1 && object4 is false\n";
 return 0;

}


 </source>


Operator Overloading: Overload the +, =,

<source lang="cpp">

  1. include <iostream>

using namespace std; class three_d {

 int x, y, z;

public:

 three_d operator+(three_d t);  
 three_d operator=(three_d t);  
    
 void show(void) ;  
 void assign(int mx, int my, int mz);  

} ;

// Overload the +. three_d three_d::operator+(three_d t) {

 three_d temp;  
    
 temp.x = x+t.x;  
 temp.y = y+t.y;  
 temp.z = z+t.z;  
 return temp;  

}

// Overload the =. three_d three_d::operator=(three_d t) {

 x = t.x;  
 y = t.y;  
 z = t.z;  
 return *this;  

}

// show X, Y, Z coordinates void three_d::show(void) {

 cout << x << ", ";  
 cout << y << ", ";  
 cout << z << "\n";  

}

// Assign coordinates void three_d::assign(int mx, int my, int mz) {

 x = mx;  
 y = my;  
 z = mz;  

}

main(void) {

 three_d a, b, c;  
    
 a.assign(1, 2, 3);  
 b.assign(10, 10, 10);  
    
 a.show();  
 b.show();  
    
 c = a+b;  
 c.show();  
    
 c = a+b+c; 
 c.show();  
    
 c = b = a;  
 c.show();  
 b.show();  
    
 return 0;  

}


 </source>


overloaded "+=" assignment operator

<source lang="cpp">

  1. include <iostream>
 using namespace std;
 class Distance{
    private:
       int feet;
       float inches;
    public:
       Distance() : feet(0), inches(0.0){  }
       Distance(int ft, float in) : feet(ft), inches(in){  }
       void getdist(){
          cout << "\nEnter feet: ";  cin >> feet;
          cout << "Enter inches: ";  cin >> inches;
       }
       void showdist() const{ 
          cout << feet << "\"-" << inches << "\""; 
       }
       void operator += ( Distance );
    };
 void Distance::operator += (Distance d2){
    feet += d2.feet;
    inches += d2.inches;
    if(inches >= 12.0)
       {
       inches -= 12.0;
       feet++;
    }
 }
 int main(){
    Distance dist1;
    dist1.getdist();
    cout << "\ndist1 = ";  dist1.showdist();
    Distance dist2(11, 6.25);
    cout << "\ndist2 = ";  dist2.showdist();
    dist1 += dist2;
    cout << "\nAfter addition,";
    cout << "\ndist1 = ";  dist1.showdist();
    cout << endl;
    return 0;
   }
 
   
 </source>


overloads assignment operator (=)

<source lang="cpp">

  1. include <iostream>

using namespace std; class alpha {

  private:
     int data;
  public:
     alpha(){ }
     alpha(int d){ data = d; }
     void display(){ cout << data; }
     alpha operator = (alpha& a){
        data = a.data;
        cout << "\nAssignment operator invoked";
        return alpha(data);
     }

}; int main(){

  alpha a1(37);
  alpha a2;
  a2 = a1;
  cout << "\na2=";
  a2.display();
  alpha a3 = a2;
  cout << "\na3=";
  a3.display();
  cout << endl;
  return 0;

}


 </source>