jeudi 16 juillet 2020

C++ Opeartor oveloading: I wanna make a Class object multiply by a float

I have to make a class that have an overloading operator two for increment and one for make a subtract object from object, I made these three overloading but I have to make an operator to make it legal to multiply the class object by float variable.

this is the code

class time{
private:
    int hours;
    int min;
    int sec;
    
public:
    time():hours(0),min(0),sec(0){}; // default constructor 
    time(int h,int m,int s):hours(h),min(m),sec(s){}; 
    void display(){ // display the time
        cout << hours << ":";
        if(min < 10) cout << "0";
        cout << min << ":"; 
        if(sec < 10)cout << "0";
        cout << sec << endl;
    }

    time operator ++ (){ //prefix increment
        ++hours;
        ++min;
        ++sec;
        return time(hours,min,sec);
    }

    time operator ++ (int){ // post-fix increment  
        hours++;
        min++;
        sec++;
        return time(hours,min,sec);
    }

    
    time operator - (time t2){ // subtract operator
        time temp;
        temp.hours = hours - t2.hours;
        temp.min = min - t2.min;
        temp.sec = sec - t2.sec;
        return temp;
    }

    
};

int main(){
    time s1(10,33,2),s2(1,2,3),s3;
    s3 = s1 - s2;
    s3.display();
    return 0;
}

so I know i must make an overloading operator for float like this operator float(){} but I don't know what to type inside and how to return all the class member that i can multiply the hours,min and sec.

Aucun commentaire:

Enregistrer un commentaire