YTU 2618: B 求类中数据成员的最大值-类模板
2618: B 求类中数据成员的最大值-类模板
时间限制: 1 Sec 内存限制: 128 MB
提交: 430 解决: 300
题目描述
声明一个类模板,类模板中有三个相同类型的数据成员,有一函数来获取这三个数据成员的最大值。
类模板声明如下:
template<class numtype>
class Max
{
public:
Max(numtype a,numtype b,numtype c);
numtype getMax();
private:
numtype x,y,z;
};
请在下面的程序段基础上完成整个设计:
#include <iostream>
#include <iomanip>
using namespace std;
template<class numtype>
class Max
{
public:
Max(numtype a,numtype b,numtype c);
numtype getMax();
private:
numtype x,y,z;
};
//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************
//********************* end ********************
int main()
{
int i1,i2,i3;
cin>>i1>>i2>>i3;
Max<int> max1(i1,i2,i3);
cout<<max1.getMax()<<endl;
float f1,f2,f3;
cin>>f1>>f2>>f3;
Max<float> max2(f1,f2,f3);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
cout<<max2.getMax()<<endl;
char c1,c2,c3;
cin>>c1>>c2>>c3;
Max<char> max3(c1,c2,c3);
cout<<max3.getMax()<<endl;
return 0;
}
输入
分别输入3个整数,3个浮点数,3个字符
输出
3个整数的最大值
3个浮点数中的最大值
3个字符中的最大值
样例输入
9 5 6
1.1 3.4 0.9
a b c
样例输出
9
3.40
c
提示
在类模板外定义各成员函数。
只提交begin到end部分的代码。
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
#include <iomanip>
using namespace std;
template<class numtype>
class Max
{
public:
Max(numtype a,numtype b,numtype c);
numtype getMax();
private:
numtype x,y,z;
};
template<class numtype>
Max<numtype>::Max(numtype a,numtype b,numtype c)
{
x=a;
y=b;
z=c;
}
template<class numtype>
numtype Max<numtype>::getMax()
{
return (x>y&&x>z)?x:(y>x&&y>z)?y:z;
}
int main()
{
int i1,i2,i3;
cin>>i1>>i2>>i3;
Max<int> max1(i1,i2,i3);
cout<<max1.getMax()<<endl;
float f1,f2,f3;
cin>>f1>>f2>>f3;
Max<float> max2(f1,f2,f3);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
cout<<max2.getMax()<<endl;
char c1,c2,c3;
cin>>c1>>c2>>c3;
Max<char> max3(c1,c2,c3);
cout<<max3.getMax()<<endl;
return 0;
}
#include <iomanip>
using namespace std;
template<class numtype>
class Max
{
public:
Max(numtype a,numtype b,numtype c);
numtype getMax();
private:
numtype x,y,z;
};
template<class numtype>
Max<numtype>::Max(numtype a,numtype b,numtype c)
{
x=a;
y=b;
z=c;
}
template<class numtype>
numtype Max<numtype>::getMax()
{
return (x>y&&x>z)?x:(y>x&&y>z)?y:z;
}
int main()
{
int i1,i2,i3;
cin>>i1>>i2>>i3;
Max<int> max1(i1,i2,i3);
cout<<max1.getMax()<<endl;
float f1,f2,f3;
cin>>f1>>f2>>f3;
Max<float> max2(f1,f2,f3);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
cout<<max2.getMax()<<endl;
char c1,c2,c3;
cin>>c1>>c2>>c3;
Max<char> max3(c1,c2,c3);
cout<<max3.getMax()<<endl;
return 0;
}
YTU 2618: B 求类中数据成员的最大值-类模板的更多相关文章
- YTU 2639: 改错题:类中私有成员的访问
2639: 改错题:类中私有成员的访问 时间限制: 1 Sec 内存限制: 128 MB 提交: 431 解决: 297 题目描述 /* 改错题: 设计一个日期类和时间类,并编写全局函数displ ...
- 正确理解Widget::Widget(QWidget *parent) :QWidget(parent)这句话(初始化列表中无法直接初始化基类的数据成员,所以你需要在列表中指定基类的构造函数)
最近有点忙,先发一篇我公众号的文章,以下是原文. /********原文********/ 最近很多学习Qt的小伙伴在我的微信公众号私信我,该如何理解下面段代码的第二行QWidget(parent) ...
- 『无为则无心』Python面向对象 — 51、私有成员变量(类中数据的封装)
目录 1.私有成员变量介绍 (1)私有成员变量概念 (2)私有成员变量特点 (3)私有成员变量体验 2.属性私有化工作原理 3.定义成员变量的标识符规范 4.私有成员变量的获取和设置方式 1.私有成员 ...
- C++类静态数据成员与类静态成员函数
from:://http://blog.csdn.net/taina2008/article/details/1684834 把类中的函数都定义成静态函数,这样相当于在编译时就分配了空间,这样不需要实 ...
- 如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类
本文翻译自 https://support.microsoft.com/zh-cn/help/168958/how-to-export-an-instantiation-of-a-standard-t ...
- 《挑战30天C++入门极限》C++类静态数据成员与类静态成员函数
C++类静态数据成员与类静态成员函数 在没有讲述本章内容之前如果我们想要在一个范围内共享某一个数据,那么我们会设立全局对象,但面向对象的程序是由对象构成的,我们如何才能在类范围内共享数据呢? ...
- synchronized 方式实现监控器中数据成员的同步
要对监控器中的数据成员进行访问,在考虑到多线程的情况下必须使用同步代码块来改变监控器中数据成员的值: synchronized (mAdapterLocking) { if (pEvery == 0) ...
- 类的数据成员加前缀 m_(表示 member)
类的数据成员加前缀 m_(表示 member),这样可以避免数据成员与 成员函数的参数同名. 例如: void Object::SetValue(int width, int height) { m_ ...
- C++(十六) — 类中引用成员函数、命名空间的使用
1.为什么类中引用成员函数? 类将属性和方法做了封装.类是一种数据类型,也就是:固定大小内存块的别名. 类的定义是一个抽象的概念,定义时不分配内存,当用类定义对象时,才分配一个固定大小的内存块. 此时 ...
随机推荐
- 【Java】Eclipse导出jar包与javadoc
1.导出jar包 2.导出javadoc 3.jar包添加javadoc 4.出错解决 参考资料: http://www.cnblogs.com/cyh123/p/3345889.html http: ...
- short-path problem (Spfa) 分类: ACM TYPE 2014-09-02 00:30 103人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #i ...
- Configuration Management小结
一.git branch和patch区别 patch,只是把diff部分创建一个分支.Detail: http://www.cnblogs.com/y041039/articles/2411600.h ...
- Jmeter测试Mysql
一.在测试计划下,找到Add directory or jar to classpath下填入jdbc驱动路径. 二.新建线程组. 三.在线程组下,添加配置元件—JDBC Connection Con ...
- HDU 4937 Lucky Number (数学,进制转换)
题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator fi ...
- idHTTP访问百度
百度屏蔽了indy的客户端标识的 Mozilla/3.0 (compatible; Indy Library),把‘Indy Library’去掉就可以了. try IdHTTP1.Request.U ...
- Javascript 正则表达式_4
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- DB2学习
1.为了使用ORG_ID,由于OCRM_F_MM_MKT_PLAN a 没有ORG_ID,但是OCRM_F_MM_EXECUTE_INST_DESC d 表里面有,可以使a表连接b表,创建临时表t. ...
- POJ 3335 Rotating Scoreboard(半平面交求多边形核)
题目链接 题意 : 给你一个多边形,问你在多边形内部是否存在这样的点,使得这个点能够看到任何在多边形边界上的点. 思路 : 半平面交求多边形内核. 半平面交资料 关于求多边形内核的算法 什么是多边形的 ...
- hdu 4628(状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 思路:首先把所有的回文找出来,如果当前状态为回文,则dp[state]=1,否则dp[state ...