基类对象的指针指向公用派生类对象的应用。
#include <iostream>
using namespace std;
class Point
{public:
Point(double a = 0, double b = 0) { x = a; y = b; }
void Show( )
{ cout << "The coordinates of the point: (" << x <<", "<< y << ")" << endl; }
protected:
double x, y;
};
class Circle: public Point
{public:
Circle(double a = 0, double b = 0, double c = 1): Point(a, b){ r = c; }
void Show( )
{ cout << " The center coordinates of the Circle: ";
cout << "(" << x << ", " << y << ")" << endl;
cout << "radius: " << r << endl;
}
private:
double r;
};
int main( )
{ Point point(0, 0);
Circle circle(1, 1, 2);
Point *p = &point;
p -> Show( );
p = &circle;
p -> Show( );
return 0;
}
#include <iostream>
using namespace std;
class Point
{public:
Point(double a = 0, double b = 0) { x = a; y = b; }
void Show( )
{ cout << "The coordinates of the point: (" << x <<", "<< y << ")" << endl; }
protected:
double x, y;
};
class Circle: public Point
{public:
Circle(double a = 0, double b = 0, double c = 1): Point(a, b){ r = c; }
void Show( )
{ cout << " The center coordinates of the Circle: ";
cout << "(" << x << ", " << y << ")" << endl;
cout << "radius: " << r << endl;
}
private:
double r;
};
int main( )
{ Point point(0, 0);
Circle circle(1, 1, 2);
Point *p = &point;
p -> Show( );
p = &circle;
p -> Show( );
return 0;
}