/*
2.*Copyright(c) 2016.烟台大学计算机与控制工程学院
3.*ALL rights reserved.
4.*文件名称:main.cpp
5.*作者:孙亚茹
6.*完成日期:2016年5月17日
7.*问题描述:自我设计,以Point类为基类,派生出一个Circle类。
8.*/
#include <iostream>
using namespace std;
class Point
{
protected:
double x;
double y;
public:
Point(double x1,double y1):x(x1),y(y1){}
void show_Point();
};
class Circle:public Point
{
private:
double r;
public:
Circle(double a,double b,double c):Point(a,b),r(c){};
double area();
void show_Circle();
};
void Point::show_Point()
{
cout<<"坐标为:"<<x<<" "<<y<<endl;
}
double Circle::area()
{
return 3.14*r*r;
}
void Circle::show_Circle()
{
cout<<"面积为:"<<area()<<endl;
}
int main()
{
Point a(2.0,3.0);
Circle b(4.0,3.0,5.0);
a.show_Point();
b.show_Point();
b.show_Circle();
return 0;
}
总结:
在把Point类改为Circle类的基类时要把私有成员类型改为公有或保护类型,若不改则在派生类中无法访问。