C++ PointerArray - Seite 3

Seite 3 von 3 - Forum: Coding Stuff auf overclockers.at

URL: https://www.overclockers.at/coding-stuff/c_pointerarray_35364/page_3 - zur Vollversion wechseln!


FMFlash schrieb am 26.05.2002 um 15:31

hier ein beispiel für vererbung mit jede menge unnötigem zeug ... (das scheinen lehrer zu mögen :rolleyes: )

#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=&rect;
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;
}


Ringding schrieb am 27.05.2002 um 11:01

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 schrieb am 27.05.2002 um 14:10

oja, templates kann der uralte Turbo C++ Compiler doch (noch) ;)


Ringding schrieb am 27.05.2002 um 14:13

Aber nicht in dem Ausmaß, das man für die Standard C++ Library (oder STL) braucht.




overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025