
概念填空题
1.运算符 能够用来访问与局部变量同名的全局变量。
2.运算符 动态分配一个对象。
3.类的 成员只能被该类的成员函数或友元函数访问。
4.类成员的默认访问模式是 的。
5.类的 数据成员是该类的所有对象共享的信息。
6.关键字 指定了不可修改的对象或变量。
7.要在类的对象上使用运算符,除了运算符 和 外,其它的必须都要被重载。
8.重载不能改变原运算符的 、 、 和对内部类型对象的原有含义。
9. 类的对象可作为 类的对象处理。
10.友元函数中可以直接访问类的 和 成员。
1l.公有成员函数的集合常称为类的 函数。私有成员函数的集合常称为类的 函数。
12.为了访问某个类的私有数据成员,必须在该类中声明该类的 。
13. 提供了一种描述通用类的方法。
14.运算new分配的内存要用运算符 回收。
15.参数 表示重载后缀 ++ 运算符函数。
16.当用受保护的继承从基类派生一个类时,基类的公有成员成为派生类的
的成员,基类的受保护成员成为派生类的 成员。
17.在C++中,关键字 、 和 用来建立新的数据类型。
18. 限定符用来声明只读变量。
19.函数 能够定义一个在不同数据类型基础上完成同一任务的函数。
20.指向基类对象的指针可以指向其 派生类的对象,但是不允许指向其 派生类的对象。
答案:
1:: 2 new 3私有和保护 4私有 5静态
6const 7=& 8 优先级 结合性 操作数个数 9派生类 基类
10 私有 受保护 11 接口 工具 12 友元 13 类模板
14 delete 15 int 16受保护 受保护 17class struct union 18 const 19模板 20公有 私有和保护
阅读程序写结果
1. #include void main() { int a(6),b(8),c; c=(a>b?++a:b-=2); cout< c=(a-b?a+b:a-6?b:a-6); cout<} 输出结果: 答案:输出结果: 6,6,6 6,6,0 2. #include void main() { int i,j; for(i=11; i<=20 ; i+=2) { for(j=2;j if(i%j==0) break; if(j!=i) cout< } cout< 输出结果: 答案:12 14 15 16 18 3.#include #include int f(int p); void main() { int a[]={1,2,3,4,5}; for(int i=0;i<5;i++) cout< int f(int p) { static int s=1; s*=p; return s; } 输出结果: 答案: 1 2 6 24 120 4.#include int f(int *p); void main() { int a[]={1,2,3,4,5} for(int i=0;i<4;i++) cout< int f(int *p) { static int s=0; s+=*p; return s; } 输出结果: 答案: 1 3 6 10 15 4. #include void main() { int a[5],x,i=0; do { cin>>x; if(x%2==0) { a[i]=x; i++; } }while(i<5); for(i=0;i<5;i++) cout< cout< 输入: 1 6 5 8 2 0 7 10 输出结果: 答案:6 8 2 0 10 5. #include class myclass { private: int a,b; static int s; public: myclass(int i, int j) { a=i; b=j; cout<<"Constructor.\\n"; } ~myclass() { cout<<"Destructor.\\n"; } void sum() { s+=a*b; } void print() { cout< } }; int myclass::s =0; void main() { myclass *a1, *a2; a1=new myclass(1,1); a2=new myclass(10,20); (*a1).sum(); (*a2).sum(); a1->print(); a2->print(); delete a1; delete a2; } 输出结果: 答案: Constructor. Constructor. 1,1 sum=201 10,20 sum=201 Destructor. Destructor. 6. #include void main() { } 输出结果: 答案: a=1b=11 7. #include class A { private: int a,b; public: A(int i, int j) { a=i; b=j; } void move(int m,int n) { a+=m; b+=n; } void show() { cout<<"("< } }; class B: public A { private: int x,y; public: B(int i, int j,int k,int l):A(i,j) { x=k; y=l; } void show() { cout< void f1(){A::show();} }; void main() { A e(10,20); e.show (); B b(30,40,50,60); b.fun (); b.show(); b.f1(); } 输出结果: 答案: (10,20) 50,60 (60,90) 8. #include class B{ private: int Y; public: B(int y=0) { Y=y; cout<<″B(″< void print() { cout < class D: public B{ private: int Z; public: D (int y, int z):B(y) { Z=z; cout<<″D(″< ~D() { cout<<″~D()\\n″; } void print() { B∶∶print(); cout < }; void main() { D d(11,22); d.print(); } 输出结果: 答案: B(11) D(11,22) 112 ~D() ~B() 9. #include #include void main() { 控制输出行 每行前的空格 cout<<" "; //输出四个空格 } 输出结果: 答案: 1 2 1 1 2 3 2 1 10. #include class A { private: double X,Y; public: A(double xx=0, double yy=0) { X=xx; Y=yy; cout<<″构造函数被调用(″< A(A &p) { X=p.X; Y=p.Y; } }; A f() { A a(1,2); return a; } void main() { A a(4,5); A b(a); b = f(); } 输出结果: 答案: 构造函数被调用(4,5) 构造函数被调用(1,2) 11. #include class Areaclass { public: Areaclass(double x=0,double y=0) { height=x; width=y; } protected: double height; double width; }; class Box: public Areaclass { public: Box(double h,double w):Areaclass (h,w){ } double Area(); }; class Triangle:public Areaclass { public: Triangle(double h,double w):Areaclass(h,w){ } double Area( ); }; double Box::Area(){ return height*width; } double Triangle::Area(){ return width *height *0.5; } void main() { Box obj1(2.5,4.0); Triangle obj2(4.0,3.5); cout<<"Box="< 输出结果: 答案: Box=10 Triangle=7 12. #include void main() { int a[9]={1,2,3,4,5,6,7,8,9}; for(int i=0; i<9; i++) { cout << setw(4) << a[i]; if(i%3==2) cout< } 输出结果: 答案: 1 2 3 4 5 6 7 8 9 13. #include template void print(T a[],int n ) { for(int i=0; i cout< cout< void main() { int a[]={1, 2, 3, 4, 5, 6, 7}; double b[4]={8, 9, 10, 11 }; print(a,sizeof(a)/sizeof(int)); print(b,4); } 输出结果: 答案: 1 2 3 4 5 6 7 8 9 10 11 14. #include class A { private: static int n; int X; public: A(int x=0) { X=x; n++; } ~A() { n-- ; } static int GetNum(){ return n; } void print(); }; void A∶∶print() { cout << ″n=″ << n << ″, X=″ << X<< endl; } int A∶∶n = 0; void main() { A *p=new A(12); p->print(); A a(34); a.print(); delete p; cout << ″n=″ << A::GetNum() << endl; } 输出结果: 答案: n=1,X=12 n=2,X=34 n=1 15. #include void main(void) { int n=6, k; cout << n << " Factors "; for (k=2; k < n; k++) if (n % k == 0) cout << k << " "; cout << endl; } 输出结果: 答案: 6 Factors 2 3 16. #include class myclass { private: int x,y; static long sum; public: myclass(int a,int b) { x=a;y=b;} void getxy() { sum*=x*y; cout<<"sum="< }; long myclass::sum=1; void main() { myclass ob1(1,3); ob1.getxy(); myclass ob2(2,4); ob2.getxy(); myclass ob3(5,6); ob3.getxy(); } 输出结果: 答案: sum=3 sum=24 sum=720 17.#include #include void main() { int i; char *max, str[3][10]={"Wang max=str[0]; for(i=0;i<3;i++) if(strcmp(max,str[i])<0) max=str[i]; cout<<"The max string is: "< 输出结果: 答案: The max string is: Zhang 三、程序填空 1.菲波纳齐数列为:1,1,2,3,5,8,13,……;下面是实现计算该数列前10项的程序,请在空白处填入合适的内容。 #include long fa(int m) //递归函数 { long f; if(m>2) ____________ else _____________ return f; } void main() { for(int i=1;i<11;i++) cout<<______ <<“ ”; cout< 答案:f=fa(n-1)+fa(n-2); f=1; fa(i) 2.下面是实现字符在字符串中出现频率的程序,请在空白处填入合适的内容。 #include int nchar(char *s,char c) { int n(0); for(int i=0;____________;i++) if(s[i]==c) n++; _________________ } void main() { char str[80],ch; cout<<"输入字符串:"; cin>>str; cout<<"输入一个字符:" ; cin>>ch ; cout< 答案: s[i] return n; nchar(str,ch) 3.下面是将一个一维数组及各元素乘积写入文本文件data.txt的程序,请在空白处填入合适的内容。 _________________ #include void main(){ int a[10]={1,2,3,4,5,6,7,8,9,10},cum=0; _____________________ if(!fout){ //打开文件失败 cout<<"Cannot open the file!"< } int i; for(i=0;i<10;i++){ cum*=a[i]; _________________ //将数组元素写入文件 } _________________ //将元素的乘积写入文件 fout.close(); }
