C++ PointerArray
-wrax- 25.05.2002 - 15:24 2245 33
FMFlash
tranceCoder
|
hier ein beispiel für vererbung mit jede menge unnötigem zeug ... (das scheinen lehrer zu mögen ![:rolleyes:](/images/smilies/rolleyes.gif) ) #include <iostream.h> namespace nsp{ template <class T> class CPoly{ protected: T width, height; public: void set_values(T a, T b) { width=a; height=b;} virtual T area(void) { return(0); } }; template <class T> class CRect: public CPoly<T>{ public: T area() {return(width*height);} }; template <class T> class CTria: public CPoly<T>{ public: T area() {return(width*height/2);} }; } void main () { nsp::CPoly<int> poly; nsp::CRect<double> rect; nsp::CTria<float> trgl; nsp::CRect<long> lrect; nsp::CPoly<double> *ppoly1=▭ nsp::CPoly<float> *ppoly2=&trgl; nsp::CPoly<int> *ppoly3=&poly; nsp::CPoly<long> *ppoly4=&lrect; ppoly1->set_values(1.15,2.6541); ppoly2->set_values(2.5,3.4); ppoly3->set_values(3,4), ppoly4->set_values(100000,156); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ppoly3->area() << endl; cout << ppoly4->area() << endl; }
Bearbeitet von FMFlash am 26.05.2002, 19:08
|
Ringding
Pilot
|
Die Templates wird der uralte Turbo C++ Compiler wohl nicht können. Wenn dein Lehrer euch Polymorphie beibringen will, dann hat er sich aber ein denkbar ungünstiges Beispiel ausgesucht. Denn die Zusammenfassung von mehreren Klassen zu einer Basisklasse zum alleinigen Zweck, sie in eine Kollektion zu geben, ist eine Krücke und nicht mehr.
Sinnvollerweise sollte man für solche Standard Kollektionen die C++ Standard Library verwenden, aber da ist Turbo C++ natürlich schon viel zu alt.
|
wobbo
...
|
oja, templates kann der uralte Turbo C++ Compiler doch (noch)
|
Ringding
Pilot
|
Aber nicht in dem Ausmaß, das man für die Standard C++ Library (oder STL) braucht.
|