[Date Prev][Date Next][Date Index]
[intv] Operator overloading
//---------------main.cpp-----------
//Problem: Example of operator overloading.
//Save as:
#include <iostream>
using namespace std;
class skTime{
friend ostream &operator<< (ostream&, const skTime &);
private:
int min, hr, sec;
public:
skTime(int, int, int); //constructor
void printTime(void);
};
skTime::skTime(int a, int b, int c){
hr = a;
min = b;
sec = c;
}
void skTime::printTime(void){
cout << "Func:" << hr << ":" << min << ":" << sec << endl;
}
ostream &operator<< (ostream &output , const skTime &T){
output << "Operator overloaded:" << T.hr << "-" << T.min << "-" << T.sec;
return output;
}
int main(){
skTime T(10, 10, 10);
T.printTime();
cout << T << endl;
}
//---------------main.cpp-----------
Comments and corrections are appreciated and can be sent to
papers@mia.ece.uic.edu.
Click here for ©opyright information.
|