C++利用继承设计的方法构造新的类

来源:百度知道 编辑:UC知道 时间:2024/06/01 03:20:53
尽快!急用!
(一)利用组合和派生的方法构成新的类(必做)
要求:
要求使用Point类,构成新的类Line。本设计任务可以使用四种方法实现,要求学生在不同的实现方法中,如何设计相应的构造函数和复制构造函数,进一步理解程序调用它们及析构函数的执行顺序。
1)利用包含设计的方法构造新的类
2)使用模板设计的方法构造新的类
3)利用继承设计的方法构造新的类
4)利用模板继承的设计方法构造新的类

/*********************包含*************************/

#include <iostream>
using namespace std;

class Point
{
public:
void show()
{
cout << '.';
}
};

class Line
{
public:
void show()
{
for(int i = 0; i < 10; ++i)
{
pt.show();
}
cout << endl;
}

private:
Point pt;
};

int main()
{
Point pt;
Line ln;

cout << "A point: ";
pt.show();

cout << "\nA line: ";
ln.show();
}

/*********************模板*************************/

#include <iostream>
using namespace std;

template<int n>
class draw
{
public:
void show()
{
for(int i = 0; i < n; ++i)
{
cout << '.';
}
}
};