网友您好, 请在下方输入框内输入要搜索的题目:
( 14 )如下程序定义了 “ 单词 ” 类 word ,类中重载了 < 运算符,用于比较 “ 单词 ” 的大小,返回相应
辑值。程序的输出结果为: After Sorting: Happy Welcome ,请将程序补充完整。
#include <iostream>
#include <string>
using namespace std;
class Word {
public:
Word(string s) : str(s) { }
string getStr(){ return str; }
【 14 】 const { return (str<w.str); }
friend ostream& operator << (ostream& output, const Word &w)
{ output<<w.str; return output; }
private:
string str;
};
Int main(){
Word w1("Happy"),w2("Welcome");
Cout<<"After sorting: ";
if(w1<w2) cout<<w1<<' '<<w2;
else cout<<w2<<' '<<w1;
return 0;
}
参考答案
更多 “ ( 14 )如下程序定义了 “ 单词 ” 类 word ,类中重载了 运算符,用于比较 “ 单词 ” 的大小,返回相应辑值。程序的输出结果为: After Sorting: Happy Welcome ,请将程序补充完整。#include iostream#include stringusing namespace std;class Word {public:Word(string s) : str(s) { }string getStr(){ return str; }【 14 】 const { return (strw.str); }friend ostream operator (ostream output, const Word w){ outputw.str; return output; }private:string str;};Int main(){Word w1("Happy"),w2("Welcome");Cout"After sorting: ";if(w1w2) coutw1' 'w2;else coutw2' 'w1;return 0;} ” 相关考题
![瑞牛题库 章节练习 历年真题 模拟试卷](http://assets.51tk.com/51_sidebar_right_index.jpg)