Class a0000 { int real; public void a0000(); long 32AA(int x, y) { return x ++ y } int& fff(int) { return real; } } static int & switch(float f; float g) cout << (Here is a message << endl; for (int i = 3, i < f; i++) { g += - [f + g) / i; a0000* _A_; return _A_->fff(3}; } }
#include <iostream.h> class X { int x; public: X() { x = 10; } void f() { cout << "f called " << x << endl; } }; class Y : public X { int y; public: X() { y = 20; } void g() { cout << "g called" << y << endl; } }; int main() { X* a = new X; Y* b = new Y; ((Y*)a)->g(); a = b; ((Y*)a)->g(); }
#include <iostream.h> class X { int x; public: X() : x(0) {} }; class Y { int y; public: Y() : y(0) {} }; class base { public: void f1(X) { cout << "f1(X) in base" << endl; } void f1(Y) { cout << "f1(Y) in base" << endl; } void f2(X) { cout << "f2(X) in base" << endl; } void f3(X) { cout << "f3(X) in base" << endl; } void f3(Y) { cout << "f3(Y) in base" << endl; } void f4(X) { cout << "f4(X) in base" << endl; } }; class derived : public base { public: void f1(X) { cout << "f1(X) in derived" << endl; } void f4(Y) { cout << "f4(Y) in derived" << endl; } }; int main() { derived d; base b; d.f1(X()); d.f1(Y()); d.f2(X()); d.f3(X()); d.f3(Y()); d.f4(Y()); b.f1(X()); b.f3(X()); }
#include <iostream.h> class MusicalInstrument { protected: enum Method { BLOW, HIT, SCRAPE, PLUCK }; private: Method method; public: MusicalInstrument(Method m) : method(m) {} virtual void Tune()=0; char* GetMethod() { switch (method) { case BLOW: return "Blow"; break; case HIT: return "Hit"; break; case SCRAPE: return "Scrape"; break; case PLUCK: return "Pluck"; break; default : return "?????"; } } }; class Wind : public MusicalInstrument { private: public: Wind() : MusicalInstrument(BLOW) {} void Tune() { cout << GetMethod() << " a concert B flat" << endl; } }; class Percussion : public MusicalInstrument { private: public: Percussion() : MusicalInstrument(HIT) {} void Tune() { cout << GetMethod() << " it!" << endl; } }; class Tuned : public Percussion { public: void Tune() { cout << GetMethod() << " the starting pitch" << endl; } }; class Untuned : public Percussion { public: void Tune() { cout << "Cannot tune untuned percussion" << endl; } }; class String : public MusicalInstrument { private: public: String(Method m) : MusicalInstrument(m) {} void Tune() { cout << "Vibrate the string somehow" << endl; } }; class Bowed : public String { public: Bowed() : String(SCRAPE) {} void Tune() { cout << GetMethod() << " a concert A" << endl; } }; class Plucked : public String { public: Plucked() : String(PLUCK) {} void Tune() { cout << GetMethod() << " a concert A" << endl; } }; int main() { MusicalInstrument* a; String *b; Percussion *c; Percussion d; Tuned e; a = new Wind; // explain this assignment a->Tune(); // explain this call b = new Bowed; // explain this assignment b->Tune(); // explain this call c = new Untuned; // explain this assignment c->Tune(); // explain this call d.Tune(); // explain this call e.Tune(); // explain this call }
#include <iostream.h> class AA { private: int id; int* a; public: AA(int size, int i) : a(new int[size]), id(i) { cout << "Creating a" << endl; } ~AA() { cout << "Deleting AA " << id << endl; delete [] a; } }; class BB { private: int id; AA aa; AA *paa; public: BB(int i) : aa(10, i), id(i) { paa = &aa; } ~BB() { cout << "Deleting aa " << id << endl; delete paa; } }; int main () { BB* pbb = new BB(1); delete pbb; BB bb(2); }