OJ对象数组求最大值
2015-06-15 14:33
323 查看
代码:
#include <iostream> #include <iomanip> using namespace std; class Student { public: Student(int n=0,double g=0); void input(); friend void max(Student*p,int n); private: int num; double grade; }; Student::Student(int n,double g) { num=n; grade=g; } void Student::input() { cin>>num>>grade; } void max(Student*p,int n) { int i,max,value; max=p[0].grade; for(i=1;i<n;i++) { if(max<p[i].grade) { max=p[i].grade; value=i; } } cout<<p[value].num<<" "<<p[value].grade<<endl; } int main() { void max(Student* ,int); const int NUM=10; Student stud[NUM]; int n,i; cin>>n; for(i=0; i<n; i++) stud[i].input(); cout<<setiosflags(ios::fixed); cout<<setprecision(2); Student *p=&stud[0]; max(p,n); return 0; }
运行结果:
知识点总结:
友元函数的应用
学习心得:
开始写的时候忘记要用友元函数了,按照自己的想法写完之后,一运行出现好几个错误。
相关文章推荐
- Spring Ldap 分页
- pager-taglib分页处理的使用
- VLC for Android : 编译问题 & 解决办法
- VISUAL 2010视频播放地址
- 支付宝集成——如何在回调地址中使用自定义参数
- Oracle索引(B*tree与Bitmap)的学习总结
- jQuery方法区别:click() bind() live() delegate()区别
- Linux下使用Sigar 获取磁盘信息
- JS——事件
- Lua与C——执行Lua脚本(二)
- Android调用系统截图时候出现黑框
- NSString特性分析学习
- 爬虫技术和爬虫需求现状和展望
- CSS3 transition 属性 过渡效果
- GRE填空词汇——显性类+隐性类
- 压缩感知(Compressive Sensing)一
- js 放置位置
- 怎样学习框架
- iOS开发 纯代码适配技巧 baseViewController
- 在Java中Highcharts前后台数据交互传输