教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 高等教育 >

c++练习习题(带答案)40656(2)

来源:网络收集 时间:2026-09-12
导读: 欢迎共阅 C. 声明 class B:virtual public A 说明类B为虚基类 D. 建立派生类对象时,首先调用虚基类的构造函数 23.有下类定义 Class A { Char *a; Public: A():a(0){} A(char *aa) {//把aa所指字符串拷贝到a所指向

欢迎共阅

C. 声明 class B:virtual public A 说明类B为虚基类 D. 建立派生类对象时,首先调用虚基类的构造函数 23.有下类定义

Class A { Char *a; Public: A():a(0){}

A(char *aa) {//把aa所指字符串拷贝到a所指向的存储空间 A=___________________; Strcpy(a,aa); ~A() {delete [] a;} }; 正确的选择是(A) A. new char[strlen(aa)+1] B. char[strlen(aa)+1] C. char[strlen(aa)] D. new char[sizeof(aa)-1] 24.假定AA为一个类,a为该类私有的数据成员,GetValue( )为该类公有函数成员,它返回a的值,x为该类的一个对象,则访问x对象中数据成员a的格式为(d )。 A) x.a B) x.a() C) x->GetValue() D) x.GetValue( ) 25.假定AA为一个类,int a()为该类的一个成员函数,若该成员函数在类定义体外定义,则函数头为( A )。 A) int AA::a( ) B) int AA:a() C) AA::a() D) AA::int a() 26.有如下程序: #include using namespace std; class Test{ public: Test(){} ~Test(){cout<<'#';} };

int main(){ Test temp[2], *pTemp[2]; return 0; }

执行这个程序输出星号(#)的个数为(B )。 A)1 B)2 C)3 D)4 27.有如下程序:

#include using namespace std;

欢迎共阅

class MyClass{ public:

MyClass(int i=0){cout<<1;}

MyClass(const MyClass&x){cout<<2;}

MyClass& operator=(const MyClass&x){cout<<3; return *this;} ~MyClass(){cout<<4;} };

int main(){

MyClass obj1(1),obj2(2),obj3(obj1); obj1=obj2; return 0; }运行时的输出结果是( A)。 28.有如下程序: #include using namespace std; class point {

public: static int number; public: point() { number++;} ~point() {number--;} };

int point::number=0; void main() { point *ptr; point A,B; {

point *ptr_point=new point[3]; ptr=ptr_point; }

point C;

delete[] ptr;

cout<

程序结果为(A)

A)3 B)4 C)6 D)7

29.友元运算符obj++被C++编译器解释为( D )。

A. operator++(obj) B. operator++(obj,0)

欢迎共阅

B. obj.operator++( ) D. obj.operator++(0) 二、程序填空

1.用递归法求从1到n的立方和: f(1)=1 (n=1) f(n)=f(n-1)+n3 (n>1) #include using namespace std; int f(int); int main(){ int n,s;

cout<<\ cin>>n; s= f(n) ; cout<<\ return 0; }

int f(int n){ //递归法求立方和函数 if ( n==1 ) return 1; else return ( f(n-1)+n*n*n ); }

3. 请将下列程序补充完整,使得输出结果为“Destructor Derived Destructor Base”。 #include using namespace std; class Base { public: virtual ~Base () { cout << “Destructor Base”<< endl; } }; class Derived : public Base { public: ~Derived(){ cout << “Destructor Derived” << endl; } };

void main () {

Base *pBase = new Derived; delete pBase ; }

欢迎共阅

三、阅读程序,写出运行结果 1.

#include #include using namespace std;? class CD {

char name[20]; int number; public:

void Init(char* aa, int bb) { strcpy(name,aa); number=bb; } char* Getname () {return name;} int Getnumber () {return number;} void Output() {cout<< name <<' '<< number <

CD dy; dx.Init(\歌唱祖国\ dy.Init(\义勇军进行曲\ dx.Output(); dy.Output(); }

歌唱祖国 30 义勇军进行曲 95 2.#include using namespace std; int f(int a){ return ++a; }

int g(int& a){ return ++a; }

void main(){ int m=0,n=0; m+=f(g(m)); n+=f(f(n));

cout<<\

欢迎共阅

cout<<\ }

3. #include using namespace std; class Demo {

public:

Demo(){cout<<\

Demo(const Demo &x){cout<<\};

Demo userCode(Demo b){Demo c(b);return c;} void main() {

Demo a,d; cout<<\ d = userCode(a); }

执行上面的程序的过程中,构造函数Demo()和Demo(const Demo &x)被调用的次数分别是多少次? 五、编写程序 1、编写函数计算y=1!+2!+3!...n! ,n作为参数传递,在主函数调用该函数并输出结果。

2、编写函数找出二维数组(5*4)中元素的最大值与最小值,并返回其最大值与最小值。测试结果。 3、编写一个三角形类,有计算面积,显示面积的功能等,并测试。要求有参数的构造函数及复制构造函数

…… 此处隐藏:636字,全部文档内容请下载后查看。喜欢就下载吧 ……
c++练习习题(带答案)40656(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/612984.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)