









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Shape, Hierrarchy, Class, Public, Void, Int, Static, Cast, Type, Circle, If, Else, Logic
Typology: Slides
1 / 16
This page cannot be seen from the preview
Don't miss anything!










class Triangle : public Shape {
class Triangle : public Shape {
public:
public:
Triangle(Line l1, Line l2, Triangle(Line l1, Line l2,
double angle)
double angle)
void draw(){ cout <<
void draw(){ cout << “
Triangle
Triangle \
n
n ”
int calcArea() { int calcArea() { …… }}
if ( _shape[i]
if ( _shape[i]
getType() ==
getType() == ‘
‘ L
L ’
’ )
)
static_cast<Line*>(_shape[i])
static_cast<Line*>(_shape[i])
draw();
draw();
else if ( _shape[i]
else if ( _shape[i]
getType() ==
getType() == ‘
‘ C
C ’
’ )
)
static_cast<Circle*>(_shape[i])
static_cast<Circle*>(_shape[i])
draw();
draw();
… …
►
►
►
►
►
►
class Shape {
class Shape {
virtual void draw();
virtual void draw();
virtual int calcArea(); virtual int calcArea();
class Line : public Shape { class Line : public Shape {
virtual void draw(); virtual void draw();
No type field
void drawShapes(Shape* _shape[], void drawShapes(Shape* _shape[],
int size) {
int size) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
_shape[i] _shape[i]-->draw();>draw();
Line _line;
Line _line;
_line.draw(); _line.draw(); // Always Line::draw// Always Line::draw
// called // called
Shape* _shape = new Line(); Shape* _shape = new Line();
_shape
_shape
draw(); // Shape::draw called
draw(); // Shape::draw called
// if draw() is not virtual // if draw() is not virtual
Shape* _shape = new Line();
Shape* _shape = new Line();
_shape _shape-->draw(); // Line::draw called>draw(); // Line::draw called
// if draw() is virtual
// if draw() is virtual