Skip to content

Instantly share code, notes, and snippets.

@petrbel
Last active January 2, 2016 13:58
Show Gist options
  • Save petrbel/8313378 to your computer and use it in GitHub Desktop.
Save petrbel/8313378 to your computer and use it in GitHub Desktop.
c++ examples (exam 2014)
#include <iostream>
using namespace std;
class Base
{
public:
Base () { cout << "Base::Base" << endl; };
};
int main (int argc, char ** argv)
{
Base * arr = new Base[10];
Base ** arr_ptr = new Base*[10];
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
// virtual is optional
virtual Base & operator= (const Base & x) { cout << "Base::operator=" << endl; };
};
class Derived : public Base
{
// according to lecture slides (pg. 188 - last bullet)
// operator= is not inherited
};
int main (int argc, char ** argv)
{
Base b1, b2;
Derived d1, d2;
b1 = b2; // Base::operator=
// WTF?
d1 = d2; // Base::operator=
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class Base
{
public:
virtual ~Base(){};
};
class D1 : public Base
{
public:
virtual ~D1(){};
void f() { cout << "D1::f" << endl; };
};
class D2 : public Base
{
public:
virtual ~D2(){};
void g() { cout << "D2::g" << endl; };
};
class D3 : public Base
{
public:
virtual ~D3(){};
void h() { cout << "D3::h" << endl; };
};
int main (int argc, char ** argv)
{
vector< Base * > container;
container.push_back( new D1() );
container.push_back( new D2() );
container.push_back( new D3() );
container.push_back( new D1() );
// looks like 'instance-of'
for (auto item : container) {
D1 * d1 = dynamic_cast< D1 * >(item);
if (d1) {
cout << "d1" << endl;
d1->f();
continue;
}
D2 * d2 = dynamic_cast< D2 * >(item);
if (d2) {
cout << "d2" << endl;
d2->g();
continue;
}
D3 * d3 = dynamic_cast< D3 * >(item);
if (d3) {
cout << "d3" << endl;
d3->h();
continue;
}
}
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
Base () { cout << "Base" << endl; };
};
class Derived : public Base
{
public:
Derived () { cout << "Derived" << endl; };
};
int main (int argc, char ** argv)
{
Base b; // Base
Derived d; // Base Derived
// Base constructor is not inherited but only called instead
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
void f() { g(); };
virtual void g() { cout << "Base::g" << endl; };
};
class Derived : public Base
{
public:
void f() { g(); };
virtual void g() { cout << "Derived::g" << endl; };
};
int main (int argc, char ** argv)
{
Base * x = new Derived();
x->f(); // Derived::g
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
virtual Base& operator= (const Base &b) { cout << "Base::operator=" << endl; return *this; };
virtual Base& operator+= (const Base &b) { cout << "Base::operator+=" << endl; return *this; };
};
class Derived : public Base
{
public:
virtual Derived& operator= (const Derived &b) { cout << "Derived::operator=" << endl; return *this; };
virtual Derived& operator+= (const Derived &b) { cout << "Derived::operator+=" << endl; return *this; };
};
class Foo : public Base
{};
int main (int argc, char ** argv)
{
Base b1, b2;
Derived d1, d2;
b1 = b2; // Base::operator=
d1 = d2; // Derived::operator=
cout << endl;
b1 += b2; // Base::operator+=
d1 += d2; // Derived::operator+=
cout << endl;
b1 = d1; // Base::operator=
b2 += d2; // Base::operator+=
cout << endl;
// bullshit
// d1 = b1;
// d2 += b2;
Base * b3 = new Derived();
Base * b4 = new Derived();
// WTF?
*b3 = *b4; // Base::operator=
cout << endl;
Foo f1, f2;
// WTF?
f1 = f2; // Base::operator=
f1 += f2; // Base::operator+=
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f () { cout << "Base::f" << endl; };
virtual void g () { cout << "Base::g" << endl; };
void h () { cout << "Base::h -> "; f(); };
void i () { cout << "Base::i" << endl; };
virtual void j () { cout << "Base::j -> "; i(); };
};
class Derived : public Base
{
public:
// virtual labels are in Derived optional
void f () { cout << "Derived::f" << endl; };
void g () { cout << "Derived::g" << endl; };
void h () { cout << "Derived::h -> "; g(); };
void i () { cout << "Derived::i" << endl; };
void j () { cout << "Derived::j -> "; i(); };
void k () { cout << "Derived::k" << endl; };
};
int main (int argc, char ** argv)
{
cout << endl << "Derived * d = new Derived();" << endl;
Derived * d = new Derived();
// simple version, Base is never used
d->f(); // Derived::f
d->g(); // Derived::g
d->h(); // Derived::h -> Derived::g
d->i(); // Derived::i
d->j(); // Derived::j -> Derived::i
d->k(); // Derived::k
// ==============================================================
cout << endl << "Base * b = new Derived();" << endl;
Base * b = new Derived();
// base type means compiler tries to use Base methods
// but those labeled as virtual are rewritten in VMT
// to derived class methods.
b->f(); // Derived::f
b->g(); // Derived::g
b->h(); // Base::h -> Derived::f
b->i(); // Base::i
b->j(); // Derived::j -> Derived::i
// b->k(); // illegal - Base::k does not exist
// ==============================================================
// cout << endl << "Derived * d2 = new Base();" << endl;
// Derived * d2 = new Base(); // invalid conversion (warning only)
// ==============================================================
cout << endl << "Derived y; Base x = y;" << endl;
Derived y;
Base x = y;
// when copying INSTANCES (not pointer/references)
// VMT are never copied, so compiler determines
// methods to use when object is created.
x.f(); // Base::f
x.g(); // Base::g
x.h(); // Base::h -> Base::f
x.i(); // Base::i
x.j(); // Base::j -> Base::i
// ==============================================================
// cout << endl << "Derived n = Base m;" << endl;
// Base m;
// Derived n = m; // illegal conversion - undefined Derived::k
// ==============================================================
cout << endl << "Derived v; Base &u = v;" << endl;
Derived v;
Base &u = v;
// actually pretty same as pointer version above
u.f(); // Derived::f
u.g(); // Derived::g
u.h(); // Base::h -> Derived::f
u.i(); // Base::i
u.j(); // Derived::j -> Derived::i
return 0;
}
#include <iostream>
using namespace std;
class T
{
public:
double f(char c)
{
cout << "double char" << endl;
return c;
}
};
class U:public T
{
int f(int i)
{
cout << "int int" << endl;
return i;
}
public:
char f(double d)
{
cout << "char double" << endl;
return (int)d;
}
};
int main (int argc, char ** argv)
{
U z;
char x = z.f('A');
// wtf.cpp: In function ‘int main(int, char**)’:
// wtf.cpp:16:6: error: ‘int U::f(int)’ is private
// wtf.cpp:32:18: error: within this context
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment