模板继承.cpp
来自「课程设计 大二的课程设计源码 有需要的都来下啊 能出结果挺好用的」· C++ 代码 · 共 57 行
CPP
57 行
#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 + =
减小字号Ctrl + -
显示快捷键?