Created
August 20, 2012 03:05
-
-
Save raarce/3399944 to your computer and use it in GitHub Desktop.
Demo overload 01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program demonstrates how to overload | |
// the post-increment operator | |
// https://gist.github.com/3399944 | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
class Fraction { | |
private: | |
int n,d; // n: numerator, d:denominator | |
public: | |
Fraction(); | |
Fraction(int, int); | |
Fraction operator++(int); | |
void display(); | |
}; | |
Fraction Fraction::operator ++(int) { | |
Fraction tmp(n,d); | |
n = n + d; | |
return tmp; | |
} | |
void Fraction::display(){ | |
cout << n << " / " << d << endl; | |
} | |
// Class implementation | |
Fraction::Fraction() { | |
n = d = 0; | |
} | |
Fraction::Fraction(int myN, int myD) { | |
n = myN; d = myD; | |
} | |
// | |
// Main function | |
// | |
int main() { | |
Fraction F1(1,2); | |
Fraction F2; | |
F2 = F1++; | |
F1.display(); | |
F2.display(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment