当前位置:编程学习 > C/C++ >>

C++题目 很急 在线等

1.C++源程序编写要经历_________、_________、_________和_________这4个过程。
2.调式C++程序时主要出现的错误类型有_________、_________和_________。
3.char、short、int、float和double类型的大小分别为_________、_________、_________、_________和_________。
4.表达式float(25)/4和int(14.6)%5的值分别为_________和_________。.增量表达式++y表示成赋值表达式为_________。
5.若x=15,y=40,则x>y和x<=y的逻辑值分别为_________和_________。
6.假定a=5,则条件表达式”a==0?10:20”的值为_________。
7.假定一个一维数组的定义为”char *a[5];”,则该数组所含元素的个数为_________,所占存储空间的字节数为_________。
8.非成员函数应声明为类的 函数才能访问这个类的private成员。派生类中的成员不能直接访问基类中的 成员。
9.下列程序的输出结果为2,请将程序补充完整。
#include <iostream>
using namespace std;
class Base
{
public:
( ) void fun( ){ cout<<1; }
};
class Derived:public Base
{
public:
void fun( ) { cout<<2; }
};
int main( )
{
Base *p= new Derived;
p->fun( );
delete p;
return 0;
}
10.请将下列类定义补充完整。
class Base { public: void fun( ){ cout<<"Base::fun"<<endl; } };
class Derived : public Base {
public:
void fun( ) {
//显式调用基类的fun函数
cout<<"Derived::fun"<<endl;
}};

11、改错题
该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
A:a=5
display1:a=6
display2:a=5
源程序清单如下:
#include<iostream.h>
class A
{
public:
A(){}
void printa () {cout<<"A:a="<<a<<endl;}
private:
int a ;
class B;
};
class B
{
public:
void display1 (A t)
{
(*t).a++; cout<<"display1:a="<<(*t).a<<endl;
}
void display2(A t)
{
t.a--; cout<<"display2:a="<<t.a<<endl;
}
};
void main ()
{
A obj1;
B obj2;
obj1.printa();
obj2.display1(&obj1);
obj2.display2(obj1);
}


12.写出下列程序结果
#include <iostream.h>
class sample
{
int x;
static int y;
public:
sample(int a);
static void print(sample s);
};
sample::sample(int a)
{
x=a;
y+=x;
}
void sample::print(sample s)
{
cout<<"x="<<s.x<<",y="<<y<<endl;
}
int sample::y=0;
void main()
{
sample s1(10);
sample s2(20);
sample::print(s2);
}

13.写出下列程序结果
#include <iostream.h>
#include <fstream.h>
void main()
{
ofstream f1("abc.txt");
f1<<"Input a string!\n";
f1.close();
ofstream f2("abc.txt",ios::app);
f2<<"Output a string!";
f2.close();
char ch;
ifstream f3("abc.txt");
while(f3.get(ch))
cout<<ch;
cout<<endl;
f3.close();
}

14.将“+”、“-”运算重载为复数类的成员函数。规则:实部和虚部分别相加减。
操作数:两个操作数都是复数类的对象。

答案:1.C++源程序编写要经历____编辑_____、___编译______、______连接___和_____运行____这4个过程。
2.调式C++程序时主要出现的错误类型有_________、_________和_________。
3.char、short、int、float和double类型的大小分别为___1_____、____2__、__4__、___4____和____8____。
4.表达式float(25)/4和int(14.6)%5的值分别为____6.25_____和____4____。.增量表达式++y表示成赋值表达式为_____y=y+1____。
5.若x=15,y=40,则x>y和x<=y的逻辑值分别为_____0____和____1_____。
6.假定a=5,则条件表达式”a==0?10:20”的值为____20_____。
7.假定一个一维数组的定义为”char *a[5];”,则该数组所含元素的个数为____5_____,所占存储空间的字节数为______5___。
8.非成员函数应声明为类的 友元 函数才能访问这个类的private成员。派生类中的成员不能直接访问基类中的 私有 成员。
9.下列程序的输出结果为2,请将程序补充完整。
#include <iostream>
using namespace std;
class Base
{
public:
( virtual ) void fun( ){ cout<<1; }
};
class Derived:public Base
{
public:
void fun( ) { cout<<2; }
};
int main( )
{
Base *p= new Derived;
p->fun( );
delete p;
return 0;
}
10.请将下列类定义补充完整。
class Base { public: void fun( ){ cout<<"Base::fun"<<endl; } };
class Derived : public Base {
public:
void fun( ) {


Base::fun();
//显式调用基类的fun函数
cout<<"Derived::fun"<<endl;
}};

11、改错题
该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
A:a=5
display1:a=6
display2:a=5
源程序清单如下:
#include<iostream.h>
class A
{
public:
A(){}
void printa () {cout<<"A:a="<<a<<endl;}
// private: 去掉
int a ;
class B;
};
class B
{
public:
void display1 (A *t) //加*
{
(*t).a++; cout<<"display1:a="<<(*t).a<<endl;
}
void display2(A t)
{
t.a--; cout<<"display2:a="<<t.a<<endl;
}
};
void main ()
{
A obj1;
B obj2;

obj1.a=5; //加着一句
obj1.printa();
obj2.display1(&obj1);
obj2.display2(obj1);
}


12.写出下列程序结果 x=20,y=30
#include <iostream.h>
class sample
{
int x;
static int y;
public:
sample(int a);
static void print(sample s);
};
sample::sample(int a)
{
x=a;
y+=x;
}
void sample::print(sample s)
{
cout<<"x="<<s.x<<",y="<<y<<endl;
}
int sample::y=0;
void main()
{
sample s1(10);
sample s2(20);
sample::print(s2);
}

13.写出下列程序结果 Input a string!
#include <iostream.h> Output a string!
#include <fstream.h>
void main()
{
ofstream f1("abc.txt");
f1<<"Input a string!\n";
f1.close();
ofstream f2("abc.txt",ios::app);
f2<<"Output a string!";
f2.close();
char ch;
ifstream f3("abc.txt");
while(f3.get(ch))
cout<<ch;
cout<<endl;
f3.close();
}

14.将“+”、“-”运算重载为复数类的成员函数。规则:实部和虚部分别相加减。
操作数:两个操作数都是复数类的对象。


#include <iostream.h>
class complex
{
public:
complex()
{
real=imag=0;
}
complex (double r, double i)
real=r;
imag=i;
}
complex operator +(const comlex &c);
complex operator -(const comlex &c);
private :
double real,imag;
};
inline complex complex:: operator +(const complex c )
{
double r=real+c.real;
double i=imag+c.imag;
return complex(r,i);
}
complex:complex::operator-(const complex c){
double r=real-c.real;
double i=imag-c.imag;
return complex(r,i);
}
void print(const complex &c)
{if(c.imag<0)
count<<"c"<<real<<"-"<<imag<<"i"<<end;
else
count<<"c"<<real<<"+"<<imag<<"i"<<end;
}
void main()
{
complex c1(2.5,3.7),c2(4.2,6.5)
complex c;
c=c1-c2;
print(c);
c=c1+c2
print(c);
}



1) 源代码编辑 -> 编译 -> 链接 -> 装载 -> 执行

2)语法错误 连接错误 运行错误 逻辑错误

3) char 8位 /short 16位 / int 32位 /float 32位 /double 64位

4) 25.0 4 y+=1

5) FALSE TRUE

6) 20

7) 5 20


上一个:C++课程设计:哈夫曼编码器
下一个:谁学过C++ 指导指导

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,