Skip to content

Instantly share code, notes, and snippets.

@prongs
Created June 18, 2013 08:33
Show Gist options
  • Save prongs/5803635 to your computer and use it in GitHub Desktop.
Save prongs/5803635 to your computer and use it in GitHub Desktop.
cpp test
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <typeinfo>
#define debug(args...) // Just strip off all debug tokens
using namespace std;
// CUT begin
#define debug(args...) {dbg,args;cout<<endl;}
struct debugger{template<typename T> debugger& operator ,(const T& v){cout<<v<<" ";return *this;}}dbg;
template <typename T1,typename T2> inline ostream& operator<<(ostream& os,const pair<T1,T2>& p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>inline ostream&operator<<(ostream& os,const vector<T>& v){string delim="[";for(unsigned int i=0;i < v.size();i++){os<<delim<<v[i];delim=", ";}return os<<"]";}
template<typename T>inline ostream&operator<<(ostream& os,const set<T>& v){string delim="[";for (typename set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){os<<delim<<*ii;delim=", ";}return os<<"]";}
template<typename T1,typename T2>inline ostream&operator<<(ostream& os,const map<T1,T2>& v){string delim="[";for (typename map<T1,T2>::const_iterator ii=v.begin();ii!=v.end();++ii){os<<delim<<*ii;delim=", ";}return os<<"]";}
// CUT end
class Vehicle
{
public:
int n;
Vehicle(int n):n(n){cout<<"Ctor Vehicle "<<n<<endl;}
Vehicle(Vehicle& v):n(v.n){cout<<"Copy Ctor Vehicle "<<n<<endl;}
virtual ~Vehicle(){cout<<"Dtor Vehicle "<<n<<endl;}
virtual ostream& dump(ostream& os) const {return os<<"Vehicle("<<n<<")";}
string to_str(){stringstream s; dump(s); return s.str();}
Vehicle& operator++(){n++;return *this;}
Vehicle operator++(int x){Vehicle v(*this); operator++(); return v;}
Vehicle& operator+=(const Vehicle& other){n+=other.n;return *this;}
};
class Car: public Vehicle
{
public:
Car(int n): Vehicle(n){cout<<"Ctor Car "<<n<<endl;}
virtual ~Car(){cout<<"Dtor Car "<<n<<endl;}
ostream& dump(ostream& os) const {return os<<"Car("<<n<<")";}
Car operator++(int x){Car v(*this); operator++(); return v;}
Car& operator++(){n++; return *this;}
Car& operator+=(const Car& other){n+=other.n;return *this;}
/* data */
};
ostream& operator<<(ostream& os, const Vehicle& v)
{
return v.dump(os);
}
class V
{
public:
int size; vector<int> elements;
V(int size): size(size), elements(size, 0){}
int& operator[](int index){return elements[index];}
const int operator[](int index) const {return elements[index];}
};
class A
{
public:
int x;
A(int x):x(x){}
};
class B: public virtual A
{
public:
B(int x):A(x){}
};
class C: public virtual A
{
public:
C(int x):A(x){}
};
class D: public B, public C
{
public:
D(int x):A(x+1), C(x+2), B(x+3){}
};
class X
{
public:
virtual int k(){return 1;}
};
class Y: public X
{
public:
int k(){return 2;}
};
class Z: public Y
{
public:
int k(){return 3;}
};
template<typename T>
T& get_max(T&a, T&b)
{
return (a>b)?a:b;
}
template<typename T>
class Convert
{
public:
Convert(T data): data(data){}
T data;
template<typename C>
operator C() const
{
return data;
}
};
void print (char * str)
{
str[0] = 'R';
cout << str << endl;
}
class ElisionTest
{
public:
int n;
ElisionTest(const ElisionTest& other): n(other.n) {cout<<"copy constructor"<<endl;}
ElisionTest(int n): n(n) {cout<<"constructor"<<endl;}
};
int main(int argc, char const *argv[])
{
ElisionTest et = 10;
// char name[10];
// strcpy(name, "rajat");
// const char* c = name;
// print(const_cast<char*>(c));
// Convert<int> ci(10);
// double x = ci;
// cout<<x<<endl;
// int x=1, y=2;
// get_max(x, y) = 0;
// X* x = new Z();
// cout<<(x->k())<<endl;
// delete x;
// D d(10);
// cout<<d.x<<endl;
// V vec(10);
// vec[0] = 10;
// cout<<vec[0]<<endl;
// Car c1(10);
// Car c2(100);
// int a = 10, b=20;
// cout<<(c1+=c2++)<<endl;
// cout<<c1<<endl;
// const string& s = "rajat";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment