类的继承

Yanger_xy 2009-03-19 10:14:57


#include <iostream.h>

//#define pi 3.1415926
double const pi = 3.1415926;

class Shape
{
public:
Shape () {}
virtual double area () const
{
return (0.0);
}
virtual double volum () const
{
return (0.0);
}
virtual void shape_name () const = 0;
};

//define a Point class

class Point:public Shape
{
public:
Point (double x=0, double y=0):point_x(x), point_y (y) {}
virtual void shap_name () const;
friend ostream & operator << (ostream & output, const Point & p);
virtual void set_point ();
virtual void get_point () const;
protected:
double point_x;
double point_y;
};

void Point::shap_name () const
{
cout << "Point:" << endl;
}

void Point::set_point ()
{
cout << "Please set the points:";
cin >> point_x >> point_y;
}

void Point::get_point () const
{
cout << "(" << point_x << ',' << point_y << ")" << endl;
}
ostream & operator << (ostream & output, const Point & p)
{
output << "(" << p.point_x << ',' << p.point_y << ")" << endl;
return output;
}

//define a Circle class

class Circle:public Point
{
public:
Circle (double x, double y, double r):Point (x, y), radius (r) {}
void set_radius ();
double get_radius () const;
virtual double area () const;
virtual void shap_name () const;
friend ostream & operator << (ostream &output, const Circle &c);
protected: //保护成员在派生类中可以被引用,而私有成员则不能!
double radius;
};

void Circle::shap_name () const
{
cout << "Circle:" << endl;
}

void Circle::set_radius ()
{
cout << "Please set the radius of the circle:" << endl;
cin >> radius;

}

double Circle::get_radius () const
{
return radius;
}

double Circle::area () const
{
return (pi*radius*radius);
}

ostream & operator << (ostream &output, const Circle &c)
{
output << '(' << c.point_x << ',' << c.point_y << ')' << "radius = " << c.radius << endl;
return output;
}

//Define a Cylinder class

class Cylinder:public Circle
{
public:
Cylinder (double x, double y, double r, double h):Circle (x, y, r), height (h) {}
void set_height ();
void get_height () const;
virtual double area () const;
virtual double volum () const;
virtual void shap_name () const;
friend ostream & operator << (ostream &output, Cylinder &cy);
private:
double height;
};

void Cylinder::shap_name () const
{
cout << "cylinder:" << endl;
}

void Cylinder::set_height ()
{
cout << "Please set the height of the cylinder:" << endl;
cin >> height;
}

void Cylinder::get_height () const
{
cout << height << endl;
}

double Cylinder::area () const
{
return (2*Circle::area () + 2*pi* Circle::get_radius () *height);
}

double Cylinder::volum () const
{
return (Circle::area () * height);
}

ostream & operator << (ostream &output, Cylinder &cy)
{
output << '(' << cy.point_x << ',' << cy.point_y << ')' << "radius = " << cy.radius
<< "volum = ";
cy.volum ();
return output;
}

void main ()
{
Point point (3.2, 4.5);
Circle circle (2.4, 1.2, 5.6);
Cylinder cylinder (3.5, 6.4, 5.2, 10.5);

point.shap_name ();
cout << point;

circle.shap_name ();
cout << circle;

cylinder.shap_name ();
cout << cylinder;

Shape *pt;
pt = &point;
point.get_point () ;
// pt->get_point (); //用指针调用函数为何不行?!Point是Shape的派生类,指针虽然指向的是Shape类,但是get_point ()声明的是虚函数,应该能用指针调用啊?!!
cout << "The area of the point is:" << endl;
pt->area ();

pt = &circle;
pt->shape_name ();
// cout << '(' << pt->point_x << ',' << pt->point_y << ')' << endl;
circle.get_point ();
cout << "radius = " << circle.get_radius ();
cout << "The area of the circle is:" << endl;
cout << circle.area () << endl;
// cout << pt->area () << endl;

pt = &cylinder;
pt->shape_name ();
cylinder.get_point ();
// cout << '(' << cylinder.point_x << ',' << cylinder.point_y << ')' << endl;
cylinder.get_radius ();
// cout << "radius = " << pt->get_radius () << endl;
cout << "height = ";
cylinder.get_height ();
// pt->get_height ();
cout << "The area of the cylinder is:" << endl;
// cylinder.volum ();
cout << pt ->volum ();
}
...全文
167 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
reallaoyao 2009-03-20
  • 打赏
  • 举报
回复
虚函数调用过程:1,把Point的对象地址取出
2,取出对象开头的指针变量中的地址,此即为VPTR的值
3,调用虚函数

照这样的话,pt->get_point ()是可以调用的,奥妙在哪儿,期待高手回答
konhon 2009-03-20
  • 打赏
  • 举报
回复
Shape裡好像沒get_point的聲明喲.
leo315 2009-03-20
  • 打赏
  • 举报
回复
好长的代码。
allen1981813 2009-03-20
  • 打赏
  • 举报
回复
楼主你想干嘛?
蜥蜴枪王 2009-03-20
  • 打赏
  • 举报
回复
hehe 每天晚上来小逛一下csdn 收获不小 o(∩_∩)o...
话说,LZ得代码太长了,困了,先帮顶下,明天来瞄瞄=。=
帅得不敢出门 2009-03-19
  • 打赏
  • 举报
回复
//#define pi 3.1415926
double const pi = 3.1415926;

class Shape
{
public:
Shape () {}
virtual double area () const
{
return (0.0);
}
virtual double volum () const
{
return (0.0);
}
virtual void shape_name () const = 0;
//你这里并不 virtual get_point函数的声明 所以下面指向调用get_point就有问题
};

Shape *pt;
pt = &point;
point.get_point () ;
// pt->get_point (); pt指向的虚函数表中并无get_point的函数地址 所以不行

//保护成员在派生类中可以被引用,而私有成员则不能!
这个复习下继承机制
公有继承的特点是基类的公有成员和保护成员作为派生类的成员时,它们都保持原有的状态,而基类的私有成员仍然是私有的。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧