当前位置:文档之家› 上海大学 级C++试题

上海大学 级C++试题

{returnCompare(s1, s2) != 0;}
protected:
char*x;
intsize;
};
voiddisplay(constString &s1,constString &s2)
{
char*str[] = {"小于", "等于", "大于"};
cout << "\"" << s1 << "\" " << str[1+Compare(s1, s2)]
{
if(this==⑤&s)return*this;
if(x!=NULL)delete[] x;
size = s.size;
x =newchar[size];
if(x==NULL) size = 0;
for(int i=0; i<size; i++)
x[i] = s.x[i];
return⑥*this;
}
Test1::Test1(constTest1 &t):x(t.x)// 拷贝构造函数
{
num++; sum+=x;
}
Test1::~Test1()
{
num--; sum-=x;
}
Test1 & Test1::operator=(constTest1 &t)// 赋值运算符函数
intx;
};
voidShow();// 普通的C++函数声明
#endif
// Test03.cpp源程序文件
#include"Test03.h"
intTest1::num=0, Test1::sum=0;//静态数据成员定义及初始化
Test1::Test1(inta):x(a)// 构造函数
{
num++; sum+=x;
}
⑨friendistream &operator>>(istream &in, String &s)
{
sHale Waihona Puke ring str;in >> str;// 利用C++字符串
s = String(str.c_str());// 利用深赋值运算符
returnin;
}
friend⑩intCompare(constString &s1,constString &s2)
t=t0; cout << t++ << endl;
t=t0; t++;cout << t << endl;
t=t0; t++++; cout << t << endl;
t=t0; t=t+(-3600); cout << t << endl;
cout << "请输入时间(hh:mm:ss) : ";
}
ostream &operator<<(ostream &out,constTime &t)
{
out << setfill('0') << setw(2) << t.h << ':'
<< setw(2)<<t.m<< ':' << setw(2)<<t.s << setfill(' ');
returnout;
cin >> t;
cout << t << endl;
cout << ++t << endl;
cout << t+(10*60+20)*60+30 << endl;
return0;
}
3.2(10分)以下4小题所涉及的Test1类,相关头文件和源程序文件如下。
// test03.h头文件
#ifndefTEST03_H
一(20)
二(20)
三(20)
四(40)
得分
——————————————————————————————————————
一、判断题(每小题2分,共20分)
1.类的构造函数的函数名与类名相同,可以重载构造函数。(√)
2.类的析构函数可以被重载。(×)
3.重载运算符函数不能改变运算符的操作数个数、优先级和结合方向。(√)
return1;
elseif(i==s1.size && i<s2.size)
return-1;
else
return0;
}
friend bool operator<(constString &s1,constString &s2)
{returnCompare(s1, s2) < 0;}
friend bool operator<=(constString &s1,constString &s2)
<< " \"" << s2 << "\"\t" << endl;
}
intmain()
{String s1("Hello world!"), s2(s1);
display(s1, s2);
s2[0] = 'h';
display(s1, s2);
s2 = "Hello world ";
display(s1, s2);
上海大学2015~2016学年度秋季学期试卷(A卷)
课程名:面向对象程序设计课程号:08305121学分:5
应试人声明:
我保证遵守《上海大学学生手册》中的《上海大学考场规则》,如有考试违纪、作弊行为,愿意接受《上海大学学生考试违纪、作弊行为界定及处分规定》的纪律处分。
应试人应试人学号应试人所在院系
题号
t.s = atoi(str);
returnin;
}
//main.cpp源程序文件(测试程序)
intmain()
{
Time t0(23,59,50), t;
t=t0; cout << ++t << endl;
t=t0; ++t;cout << t << endl;
t=t0; ++++t; cout << t << endl;
4.引用在声明时必须对其初始化,以绑定某个已经存在的变量(或对象),
在该引用的生命期内,该绑定不能被更改。(√)
5.指针变量在定义时必须对其初始化,以锁定某个已经存在的目标变量(或
对象),在该指针变量的生命期内,该指向不能被更改。(×)
6.类的非静态成员函数均有一个隐含的形式参数this指针常量,用于指向
调用该函数的对象。函数体中不能改变该指针常量的指向(即锁定调用该
函数的对象)。(√)
7.派生类继承了基类的所有数据成员,并且在派生类的成员函数中都能直接
访问基类的访问属性为private的成员。(×)
8.构造派生类对象时,先调用基类的构造函数,后执行派生类的构造函数。
析构派生类对象时,先调用基类的析构函数,后执行派生类的析构函数。(×)
{
inti;
for(i=0; i<s1.size && i<s2.size && s1.x[i]==s2.x[i]; i++)
;
if(i<s1.size && i<s2.size)
returns1.x[i] > s2.x[i] ? 1 : -1;
elseif(i<s1.size && i==s2.size)
staticintNum();
staticintSum();
friendostream &operator<<(ostream &out,constTest1 &t);
friendistream &operator>>(istream &in, Test1 &t);
protected:
staticintnum, sum;
for(inti=0; i<size; i++)
x[i] = str[i];
}
String(constString &s) : x(③NULL)
{
*this= s;// 直接利用深赋值运算符函数
}
virtual~String()
{
if(x!=NULL)delete④[]x;
size = 0;
}
String &operator=(constString &s)
#include<cstring>
#include<string>
usingnamespace①std;
classString
相关主题