Created
February 21, 2014 07:03
-
-
Save yoggy/9129994 to your computer and use it in GitHub Desktop.
sample class definition for c++ ...
This file contains hidden or 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
// | |
// test-class.cpp - sample class definition for c++ ... | |
// | |
// $ g++ test-class.cpp -o test-class && ./test-class | |
// c={a:1, b:2] | |
// d={a:4, b:7] | |
// | |
#include <iostream> | |
#include <sstream> | |
class Test { | |
public: | |
Test() : a_(0), b_(0) { | |
} | |
Test(const Test &val) { | |
this->a_ = val.a_; | |
this->b_ = val.b_; | |
} | |
Test(const int &a, const int &b) { | |
this->a_ = a; | |
this->b_ = b; | |
} | |
virtual ~Test() { | |
} | |
Test operator+(const Test &t) { | |
return Test(a_ + t.a_, b_ + t.b_); | |
} | |
std::string str() const { | |
std::stringstream ss; | |
ss << "{a:" << a_ << ", b:" << b_ << "]"; | |
return ss.str(); | |
} | |
int a() const { return a_; } | |
void a(const int &val) { a_ = val; } | |
int b() const { return b_; } | |
void b(const int &val) { b_ = val; } | |
private: | |
int a_; | |
int b_; | |
}; | |
std::ostream& operator<<(std::ostream& os, const Test &val) | |
{ | |
os << val.str(); | |
return os; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
Test a(1, 2); | |
Test b(3, 5); | |
Test c, d; | |
c = a; | |
d = a + b; | |
std::cout << "c=" << c << std::endl; | |
std::cout << "d=" << d << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment