回复:C++数据结构学习:在链表中链入对象
class Circle : public Shape
{
public:
void Input()
{
cout << endl << "输入圆的参数";
cout << endl << "输入圆心点的坐标:" << endl;
center.Put();
cout << endl << "输入半径:";
cin >> radius;
}
void Print()
{
cout << endl << "圆的参数为";
cout << endl << "圆心点的坐标:" << endl;
center.Get();
cout << endl << "半径:" << radius;
}
virtual ~Circle(){};
private:
int radius;
Point center;
};
#endif
【说明】圆的类定义与实现。继承Shape类的行为。
#ifndef Line_H
#define Line_H
#include "Shape.h"
#include "Point.h"
class Line : public Shape
{
public:
void Input()
{
cout << endl << "输入直线的参数";
cout << endl << "输入端点1的坐标:" << endl;
point1.Put();
cout << endl << "输入端点2的坐标:" << endl;
point2.Put();
}
void Print()
{
cout << endl << "直线的参数为";
cout << endl << "端点1的坐标:";
point1.Get();
cout << endl << "端点2的坐标:";
point2.Get();
}
virtual ~Line(){};
private:
Point point1;
Point point2;
};
#endif
【说明】直线类的定义与实现。继承Shape的行为。
#ifndef ListTest_H
#define ListTest_H
#include
#include "List.h"
#include "Circle.h"
#include "Line.h"
void ListTest_MObject()
{
List a;
Shape *p1 = new Circle;
Shape *p2 = new Line;
p1->Input();
p2->Input();
a.Insert(p1);
a.Insert(p2);
Shape *p = *a.Next();
p->Print();
delete p;
a.Put(NULL);
p = *a.Next();
p->Print();
delete p;
a.Put(NULL);
}
#endif