assignment operator overloading in c++

前端 未结 6 1939
醉梦人生
醉梦人生 2021-02-01 15:07

I have used the following code for assignment operator overloading:

SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs)
{
     if(this == &rhs         


        
6条回答
  •  悲&欢浪女
    2021-02-01 15:41

    this might be helpful:

    // Operator overloading in C++
    //assignment operator overloading
    #include
    using namespace std;
    
    class Employee
    {
    private:
    int idNum;
    double salary;
    public:
    Employee ( ) {
        idNum = 0, salary = 0.0;
    }
    
    void setValues (int a, int b);
    void operator= (Employee &emp );
    
    };
    
    void Employee::setValues ( int idN , int sal )
    {
    
    salary = sal; idNum = idN;
    
    }
    
    void Employee::operator = (Employee &emp)  // Assignment operator overloading function
    {
    salary = emp.salary;
    }
    
    int main ( )
    {
    
    Employee emp1;
    emp1.setValues(10,33);
    Employee emp2;
    emp2 = emp1; // emp2 is calling object using assignment operator
    
    }
    

提交回复
热议问题