how does c++ compiler know doing practical duty call?


here an instance polymorphism (edited readability):



// summary bottom class
#include <iostream>
using namespace std;

class polygon {
protected:
int width;
int height;
public:
vacant set_values(int a, int b) { extent = a; tallness = b; }
practical int area(void) =0;
};

class rectangle: open polygon {
public:
int area(void) { relapse extent * height; }
};

class triangle: open polygon {
public:
int area(void) { relapse extent * tallness / 2; }
};

int sure () {
rectangle rect;
triangle trgl;
polygon * ppoly1 = &rect;
polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << endl; // outputs 20
cout << ppoly2->area() << endl; // outputs 10
relapse 0;
}


my doubt does compiler know ppoly1 rectangle ppoly2 triangle, repremand area() function? out looking during "polygon * ppoly1 = ▭" line meaningful rect rectangle, nonetheless wouldn't work cases, it? something this?



cout << ((polygon *)0x12345678)->area() << endl;


assuming you're certified opening futile area memory.



i exam out nonetheless i can't mechanism i'm during moment.



(i wish i'm blank something obvious...)



Comments

Popular posts from this blog

list macos calm editors formula editors

how i practical urls indicate .aspx pages asp.net deployed an iis? (preferably but iis)

jaxb - xjc - reworking generated typesafe enum category members