如下程序定义了“单词”类word,类中重载了<运算符,用于比较“单词”的大小,返回相应的逻辑值。程序的输出结果为:After Sorting:Happy Welcome,请将程序补充完整。
include
include
using namespace std;
class Word{
public:
Word(string s):str(s){}
string getStr(){retum str;}
【 】const{retum(str
friend ostream&operator$amp;{output$amp;private:
string str:
};
int main(){
Word wl(”Happy”),w2(“Welcome”);
eout$amp;if(w1
}
第1题
辑值。程序的输出结果为: 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;
}
第2题
include<iostream>
include<string>
using namespace std;
class Word{
public:
Word(string s):str(s){}
string getStr(){retum str;}
【 】const{retum(str<w.str);}
friend ostream&operator<<(ostream&output,const W0rd&w)
{output<<w.str;return output;}
private:
string str:
};
int main(){
Word wl(”Happy”),w2(“Welcome”);
eout<<”After sorting:”:
if(w1<w2)tout<<wl<<‘ ’<<w2:
else cout<<w2<<‘ ’<<wl:
return 0;
}
第3题
include<iostream>
include<string>
using namespace std;
class Word{
public:
Word(strings):str(s){ }
string getStr( ){return str;}
______const{return(str<w.str);}
friend ostream& operator<<(ostream& output,const Word &w){output<<w.str;return output;}
private:
string str;
};
int main( ){
Word wl("Happy"),w2("Welcome");
cout<<"After sorting:";
if(wl<w2)cout<<w1<<"<<w2;
else cout<<w2<<''<<w1:
return 0;
}
第5题
1、定义复数Complex类,编写Add、Sub重载函数,运算符重载“+”、“-”,可分别求取复数/-复数、整数+/-复数,复数+/-整数、复数+/-复数+/-整数的加法和减法,并在主函数中,定义两个复数类对象a、b和一个整形变量c,实现读入c数据,并输出a+b、a+c、c+a 和a+b+c以及a-b、a-c、c-a和a-b-c的值。并回答问题:叙述函数重载和运算符重载端口进入的依据。 2、编写一个班级学生和任课教师的数据输入和显示程序。学生数据包括学生姓名(char *name)、成绩(double score),学生数(static int count) 。教师数据有职工号( char *name)、教授课程编号(int subject)、工资(int salary)、职称(int grade)。请按下列要求编程: (1)创建一组学生{Stu(“zhao”,81), Stu(“qian”,79), Stu(“sun”,67), Stu(“li”,85), Stu(“zhou”,82), Stu(“wu”,83), Stu(“wang”,78)}。求出此时学生数以及这组学生成绩的最大值和最小值,姓名为”sun”和”wang”的学生转走,又转进来一个姓名和成绩分别为”zheng”和90的学生,求出此时学生数以及这组学生成绩的最大值和最小值。 提示: Student 类:两个公有成员函数:void input();输入学生的姓名、成绩和学生数;该函数中应包含person::input();显示输出函数:viod display();显示输出学生的姓名、成绩和学生数,该函数应包含peson::display();。该类中有三个私有数据成员:学生姓名(char *name)、成绩(double score),学生数(static int count) 。 (2)现有讲授01课程的职称为1的“wang”老师, 02课程的职称为2的“sun”老师,讲授03课程的职称为3的“li”老师,显示输出教师的职工号、教授课程编号、工资、职称。其中对应1、2、3职称级别的工资分别为6000、7000、8000三个类别。 提示:Teacher 类,包括两个公有成员函数:输入函数void input();输入教师职工号、职称和系别;该函数中包含person::input();。显示输出函数:viod display();该类中有三个私有数据成员:职工号、教授课程编号、工资、职称。
第9题
A、除了后增后减单目运算符外,其他运算符重载为类的成员函数时,函数参数个数比运算符实际操作数个数少一个
B、运算符重载为类的成员函数时,函数中的this指针指向第一个运算数
C、只有运算符的第一个运算数是自定义类类型时,才能将该运算符重载为类的成员函数
D、所有运算符都可以以类成员函数形式进行重载
第10题
A、赋值运算符和下标运算符只能重载为类的成员函数
B、赋值运算符和下标运算符只能重载为类的友元函数
C、赋值运算符重载时一般以引用方式返回*this,表示返回赋值运算符的左操作数
D、使用重载的赋值运算符时,左操作数一定是自定义类型
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!