Cpp Class Template
A little simple example to illustrate classes in C++. See also Cpp Classless Template.
#include <stdio.h> #include <math.h> class geo_obj { protected: int m_x; int m_y; public: geo_obj(int x, int y) { this->m_x = x; this->m_y = y; } int X() { return this->m_x; } int Y() { return this->m_y; } void print() { printf("%d, %d", this->m_x, this->m_y); } }; class circle : public geo_obj { private: int m_r; public: circle(int x, int y, int r) : geo_obj(x, y) { this->m_r = r; } bool is_inside(geo_obj g) { return pow(g.X() - this->m_x,2) + pow(g.Y() - this->m_y,2) <= pow(this->m_r, 2); } void circle::print() { geo_obj::print(); printf(", r=%d", this->m_r); } }; int main(int argc, char * argv[]) { geo_obj point = geo_obj(2, 3); circle c = circle(1, -3, 8); printf("point at "); point.print(); printf(" is inside of circle "); c.print(); printf("? "); printf(c.is_inside(point) ? "yes" : "no"); printf("\n"); return 0; }
Download source code here: [1]
The output is of course point at 2, 3 is inside of circle 1, -3, r=8? yes
Belongs in Kategori Mallar