int x = 4; class A { int x; public: A (int n = 1); int f (int a = 0, int b = 0); }; class B: public A { int x; public: B (int n = 2); int f (int a); }; class C: public B { int x; public: C (int n = 3); int f (int a, int b); int g (A * p); }; A * p; B b; C c; int main () { p = & b; x = c.g (& c); x = c.f (); x = c.f (x); x = c.f (x, 1); x = p -> f (); x = p -> f (x); x = p -> f (x, 1); return 1; }
class A {public: static void f (int x) { h (x); cout << "A::f," << x << endl; } void g () { cout << "A::g" << endl; } void h (int x) { g (); cout << "A::h," << x << endl; } }; class B: virtual public A { public: static void f (int x) { h (x); cout << "B::f," << x << endl; } void g () { cout << "B::g" << endl; } void h (int x) { g (); cout << "B::h," << x << endl; } }; int main( ){ B::f (0); B b; A * p = & b; p -> f (1); p -> g (); p -> h (2); A::f (3); return 0; }
int f (int a = 0) { return a; } int f (double a) { return a; } int main () { short int s; int i; bool b; enum e {A, B, C}; float f1 = 1.0f; f (); f (s); f (f1); f (b); f (A); }
int main () { C c; A a = c; struct D { B b; D (): b (5) {} } d; }
template <class T> class cl { /* ... */ public: cl (const T s = 0); operator T (); }; template <class T> class ObjPtr { /* ... */ cl<T> * c; public: /* ... */ };Для класса «ObjPtr» написать реализацию операций сложения «+» таким образом, чтобы правильными оказались выражения (обязательно использовать и методы, и внешние функции):
ObjPtr <int> t1, t2; /* ... */ (t1 + 3) + (5 + t1) + (t1 + t2) /* ... */
void f(X & x, int n); struct X { X () { try { f(*this, -1); cout << "a"; } catch (X){ cout << "b"; } catch (int){ cout << "c"; } } X (X &) { cout << "d"; } virtual ~X () { cout << "e"; } }; struct Y: X { Y () { try { f (*this, 0); cout << "f"; } catch (Y) { cout << "g"; } catch (int){ cout << "h"; } cout << "i"; } Y (Y &){ cout << "j"; } ~Y (){ cout << "k"; } }; void f(X & x, int n) { try { if (n < 0) throw -n; else if (n == 0) throw x; else throw n; } catch (int){ cout << "l"; } } int main() { try { Y a; } catch (...){ cout << "m"; return 1; } cout << "n"; return 0; }
class A { public: int * n; int m; }; class B: public A { public: int * p; }; class C: public A { public: int * c; }; class D: public B, public C { public: int * e; }; int main () { D fA, * f = new D; fA.m =0; return *((* f).e = & fA.m); }