Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
Created October 2, 2016 06:29
Show Gist options
  • Save ChunMinChang/3d8ceee0e663c79b3828331f777472bc to your computer and use it in GitHub Desktop.
Save ChunMinChang/3d8ceee0e663c79b3828331f777472bc to your computer and use it in GitHub Desktop.
Week 2 of C++ for C Programmers
/*
* Compile this file by C++11, e.g., g++ --std=c++11 <filename.cpp>
*/
#include <cstdint> // For using fixed width integer types and part of
// C numeric limits interface:
// std::int8_t
#include <iostream> // For input and output fundamentals:
// std::cout
// endl
// ostream
#include <string> // For C++ standard string classes and templates:
// std::string
using std::int8_t; // used as the underlying type of enum.
using std::cout;
using std::endl;
using std::ostream; // for << operator overloading.
using std::string;
#define DEBUG false
void log(string s) {
DEBUG && (cout << "[" << __FILE__ << "] " << s << endl);
}
// A scoped enumerations DAY whose underlying type is int8_t.
// (Unscoped enumeration has no keyword `class`.)
enum class DAY:int8_t {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
// Overwrite the increment of enum DAY.
DAY& operator++(DAY& d) // Prefix operator: ++DAY
{
log("Prefix ++");
// Wrap around to 0 if the result after increment reaches 7.
d = static_cast<DAY>((static_cast<int>(d) + 1) % 7);
return d;
}
DAY operator++(DAY& d, int) // Postfix operator: DAY++
{
log("Postfix ++");
// Create a temporary variable with our current value.
DAY beforeOperation = d;
// Use prefix operator to increase.
++d;
// Return temporary result.
return beforeOperation;
}
// Overwrite the decrement of enum DAY.
DAY& operator--(DAY& d) // Prefix operator: --DAY
{
log("Prefix --");
// Wrap round to 6 if the result after decrement is -1.
d = static_cast<DAY>((static_cast<int>(d) + 6) % 7);
return d;
}
DAY operator--(DAY& d, int) // Postfix operator: DAY--
{
log("Postfix --");
// Create a temporary variable with our current value.
DAY beforeOperation = d;
// Use prefix operator to decrease.
--d;
// Return temporary result.
return beforeOperation;
}
// Overwrite the output stream of enum DAY.
ostream& operator<<(ostream& out, const DAY& d)
{
std::string name[] = {
"Sunday",
"Monday",
"Tuesday",
"Wedensday",
"Thursday",
"Friday",
"Saturday"
};
out << name[static_cast<int>(d)];
return out;
}
int main() {
DAY day = DAY::SATURDAY;
cout << day << endl; // output: Saturday ( day is Saturday )
cout << ++day << endl; // output: Sunday ( day is Sunday )
cout << day++ << endl; // output: Sunday ( day is Monday )
cout << day << endl; // output: Monday ( day is Monday )
cout << day-- << endl; // output: Monday ( day is Sunday )
cout << day << endl; // output: Sunday ( day is Sunday )
cout << --day << endl; // output: Saturday ( day is Saturday )
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
using std::string;
#define DEBUG false
void log(string s) {
DEBUG && (cout << "[" << __FILE__ << "] " << s << endl);
}
class Point
{
public:
// Constructor
Point(double aX, double aY)
: mX(aX)
, mY(aY)
{
}
// Deconstructor
~Point()
{
}
friend Point operator+(const Point& aP1, const Point& aP2)
{
return Point(aP1.mX + aP2.mX, aP1.mY + aP2.mY);
}
// Overwrite the output stream
friend ostream& operator<<(ostream& out, const Point& p)
{
out << "(" << p.mX << ", " << p.mY << ")";
return out;
}
private:
double mX;
double mY;
};
int main() {
Point a(1.2, 3.4);
cout << a << endl; // (1.2, 3.4)
Point b(5.6, 7.8);
cout << b << endl; // (5.6, 7.8)
Point c = a + b;
cout << c << endl; // (6.8, 11.2)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment