Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created February 21, 2014 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoggy/9129994 to your computer and use it in GitHub Desktop.
Save yoggy/9129994 to your computer and use it in GitHub Desktop.
sample class definition for c++ ...
//
// 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