运算符重载。
提分程序。
类中有构造函数(确定学生人数)、数据输入函数、数据输出函数。
在类中对++运算符进行重载,给不及格的同学提分,60分的提到61分;不及格的同学提到60分。
在主函数中定义两个类的对象,使用++赋值后,输出对象数据。要求不改变++的左右位置运算属性。
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
class test
{
private:
int n;
vector <int> a;
public:
test(int nn)
{
n=nn;
}
void input()
{
int i,temp;
for(i=0;i<n;++i)
{
cout<<"input "<<i+1<<" :";
cin>>temp;
a.push_back(temp);
}
}
void output()
{
int i;
cout<<"----------------"<<endl;
for(i=0;i<n;++i)
{
cout<<"output "<<i+1<<" :"<<a[i]<<endl;
}
}
test& operator++(){
test u = *this;
int i;
for(i = 0;i<n;i++){
if(a[i]<60){
a[i] = 60;
}else if(a[i] == 60){
a[i] =61;
}
}
return *this;
}
};
int main()
{
test t1(5),t2(5);
t1.input();
t2=++t1;
t2.output();
t1.output();
system("pause");
return 0;
}
本文介绍了一个C++程序,通过重载++运算符实现对不及格学生的成绩提分功能。程序包含类的构造、数据输入输出及++运算符的具体实现。在主函数中,演示了如何使用该类进行数据操作。
829

被折叠的 条评论
为什么被折叠?



