回 帖 发 新 帖 刷新版面

主题:求助C++考试卷

[color=00FF00][size=4]由于本特殊原因,本人辅修专业未能开课,老师让自己自学作题,由于考试没时间看教材,特!!!!!!!![/size][/color]

1. C++类中的成员可以是public或protected或______________。其中______________虽然是私有成员,但能被派生类继承。
5. 类BB私有继承类AA,类CC公有继承类BB;则类BB应如下定义:

class  ________________________________
{  
 // 略 
} ; 
类AA的公有成员____________能在类CC中使用。
6. 函数sum能返回任意类型四个参数的和,则应将sum函数定义为函数模板 。定义格式如下:

template  <class  Ty>

___________________________________

{ return  a+b+c+d ; }

7. ______________函数虽不是类的成员函数,但它也能够存取类中的私有数据成员。

8. 定义基类中的void func( )是纯虚函数,应将其定义为_________________________________,

该基类不能定义对象,只能定义对象指针,我们称该基类为___________________类。

9. 模板函数与重载函数相比较,模板函数的功能必定是____________________;而重载函数的
功能可以_________________。

10. 定义函数时,如果将形式参数定义为______________(类型),则调用该函数时,实在参数
只能是 变量 ,这些变量可以将函数的多个值返回。

11、 当建立一个新对象时,程序自动调用________________。 

13、 定义重载函数时,应在参数个数或参数类型上________________。 

14、当一个成员函数被调用时,该成员函数的_____________指向调用它的对象。 


15、派生类可以定义其________________中不具备的数据和操作。  

二、改错题(根据编译时的错误提示,将你认为有错的语句下划线,然后在右边改正;要删除语句,应说明原因;不得增添新语句,
1.本题8分。(提示:本题4处错误)
class complex
{
double  real, imag;
public:
complex(double x,double y)    //
{ x = real; y = imag;}       //
complex operator +(complex  c1, complex  c2)  //
{
complex t;
t.real=c1.real+c2.real; 
t.imag=c1.imag+c2.imag;
return  t;
}
};
void main( )
{
complex  ob1,ob2(1.2,2.456);ob3(3.14,4);   //
ob1=ob2+ob3;
}
错误提示:Compiling...
F:\Text1.cpp(7) : error C2804: binary 'operator +' has too many parameters
F:\Text1.cpp(7) : error C2333: '+' : error in function declaration; skipping function body
F:\Text1.cpp(16) : error C2512: 'complex' : no appropriate default constructor available
F:\Text1.cpp(16) : error C2065: 'ob3' : undeclared identifier
F:\Text1.cpp(17) : error C2676: binary '+' : 'class complex' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.
2.本题10分。(提示:本题5处错误)
#include <iostream.h> 
#include <stdlib.h>  //
class STRING  //

char  contents;  int length;  //
public:
string(char *s)
{ length=strlen(s);
contents=new char[length+1];     
contents=s; }     //
~string( ) {delete contents;}
void show( ) {cout<<"string contents:"<<contents<<endl;}
string & appstring(char *s); 
};
string &  appstring(char *s)   //
{
char *t=new char[length+1];
strcpy(t,contents);
delete contents;
contents=new char[length+strlen(s)+1];
strcpy(contents,t);
strcat(contents,s);
delete t;
length=strlen(contents);
return *this; }
main( )
{
string  s1("Hello,"), s2("Worldcup!");
s1.show( ); s2.show( );
//s1.appstring(s2);
s2.appstring("Korea & Japan");
s1.show( ); s2.show( );
return 168;
}
错误提示:Compiling...
F:\Text1.cpp(10) : warning C4183: 'string': member function definition looks like a ctor, but name does not match enclosing class
F:\Text1.cpp(11) : error C2523: 'STRING::~string' : destructor tag mismatch
F:\Text1.cpp(13) : error C2501: 'string' : missing storage-class or type specifiers
F:\Text1.cpp(16) : error C2501: 'appstring' : missing storage-class or type specifiers
F:\Text1.cpp(17) : error C2065: 'length' : undeclared identifier
F:\Text1.cpp(18) : error C2065: 'strcpy' : undeclared identifier
F:\Text1.cpp(18) : error C2065: 'contents' : undeclared identifier
F:\Text1.cpp(19) : error C2541: delete : cannot delete objects that are not pointers
F:\Text1.cpp(20) : error C2065: 'strlen' : undeclared identifier
F:\Text1.cpp(20) : error C2440: '=' : cannot convert from 'char *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
F:\Text1.cpp(22) : error C2065: 'strcat' : undeclared identifier
F:\Text1.cpp(28) : error C2065: 's1' : undeclared identifier
F:\Text1.cpp(28) : error C2065: 's2' : undeclared identifier
Error executing cl.exe.
3.本题12分。(提示:本题6处错误)
#include <iostream.h>
#include <math.h>       //
#include <iomanip.h>
const  size=120        //
class student

    int no; char name[10];int age;
    char major[10];double score[3];double average;  //
public:
    friend istream& operator>>(istream&,student&);
    friend ostream& operator<<(ostream&,student&);
    friend ifstream& operator>>(ifstream&,student&);
};
istream& operator>>(istream& in,student s)   //
{
    cout<<"No  Name  Age  Major  Sub1  Sub2  Sub3:\n";
    in>>s.no;
    in>>s.name;
    in>>s.age;
    in>>s.major;
    in>>s.score[0];
    in>>s.score[1];
    in>>s.score[2];
    s.avg=(s.score[0]+s.score[1]+s.score[2])/3;
    return in;
}

ostream& operator<<(ostream&out,student &s)
{
    out<<setiosflags(ios::right|ios::fixed)<<setprecision(2);
    out<<setw(6)<<s.no;
    out<<setw(12)<<s.name;
    out<<setw(12)<<s.major;
    out<<setw(8)<<s.avg;
    out<<endl;
    return out;
}
ifstream &  operator>>(ifstream& in,student &s)
{
    in>>s.no;
    in>>s.name;
    in>>s.major;
    in>>s.avg;
    return fin;   //
}
void mian()    //
{
    student s;
    cout<<"Input a student:\n";
    ofstream fout("st.rec");
    for (int ii=1;ii<=size;ii++)
    {cin>>s; fout<<s;}
    fout.close();

    ifstream fin("st.rec");
    for (int jj=1;jj<=size;jj++)
    {fin>>s; cout<<s; }
    fin.close();
}错误提示:Compiling...
test3.cpp
f:\test3.cpp(2) : fatal error C1083: Cannot open include file: 'ifstream.h': No such file or directory
Error executing cl.exe.

三、综合题(前2题必做,每题10分;后两题任选其一,每题20分;共40分)
1. 下面是一个Ctime类的定义,请在类外完成该类的实现。(不需要写main( )函数)
class Ctime
{
int  hour, minute, second;
public:
Ctime(int, int, int);                   //赋时间初值h, m, s
void set_time(int, int, int);             //设置时间h, m, s
void get_time(int &, int &, int & );      //取当前时间
void show_time( );                  //显示当前时间,显示格式为hh:mm:ss
void increase( );                    //给当前时间的second值增1
}; 
2. 下面是一个测试 类Cstudent 的主函数,请完成该类的构造。
void main( )

Cstudent  STU("李兰慧", 18, "计算机系98级");
STU.show( );                //显示student中的数据,包括姓名、学号、班级
STU.change("李壮");         //改变姓名
STU.show( );                //显示student中的数据
}
4. 利用C++的多态性,编写一个计算圆锥、球体、圆柱、椭圆体的体积。(提示:先定义一公共基类,然后由其派生出圆锥、球体、圆柱、椭圆体四个类)
5.设计父亲类Cfather,母亲类Cmother及子女类Cchild,类Cchild继承类Cfather和类Cmother。要求:
① 子女的姓名如下构成:父亲的姓+母亲的名。
② 子女的才能融合了父亲和母亲的才能。

回复列表 (共1个回复)

沙发

????????????

我来回复

您尚未登录,请登录后再回复。点此登录或注册