📄 模板继承.cpp
字号:
#include<iostream.h>
template <class TYPE>/*类模板1的定义*/
class Point
{
protected: TYPE a,b;/*受保护成员*/
public:Point(){}/*构造函数*/
Point(TYPE m,TYPE n)
{
a=m;
b=n;
}
Point(const Point &other)/*构造函数的重载*/
{
a=other.a;
b=other.b;
}
void enter()/*输入坐标值*/
{
cout<<"please input x,y of the point"<<endl;
cin>>a>>b;
}
void out()/*输出坐标值*/
{
cout<<"The point is"<<"("<<a<<","<<b<<")"<<endl;
}
};
template <class TYPE>/*类模板2的定义*/
class Line:public Point<TYPE>/*私有继承模板1*/
{
private:TYPE c,d;
public:
Line(){}
Line(TYPE A,TYPE B,TYPE C,TYPE D):Point<TYPE>(A,B)/*构造函数成员的初始化*/
{
c=C;
d=D;
}
void enter()
{
cout<<"please input two x,y of the line"<<endl;
cin>>a>>b>>c>>d;
}
void out()
{
cout<<"The line is"<<"("<<a<<","<<b<<")"<<"("<<c<<","<<d<<")"<<endl;
}
};
int main()/*主函数的实现*/
{
Point<int> a(0,0);
a.enter();
a.out();
Line<int>b(0,0,0,0);
b.enter();
b.out();
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -