习题(针对学习笔记16-30)

1、什么是对象数组?

https://blog.csdn.net/aaqian1/article/details/84061049

2、什么是 this 指针?它的主要作用是什么?

https://blog.csdn.net/aaqian1/article/details/84137285

3、友元函数有什么作用?

https://blog.csdn.net/aaqian1/article/details/84427884
   友元函数不是当前类的成员函数,而是独立于当前类的外部函数,但它可以访问该类所有的成员,包括私有成员、保护成员和公有成员。
   当一个函数需要访问多个类时,友元函数非常有用,普通的成员函数只能访问其所属的类,但是多个类的友元函数能够访问相应的所有类的数据。此外,在某些情况,例如运算符被重载时,需要用到友元函数。

4、假设在程序中已经声明了类 point ,并建立了其对象 p1 和 p4 。那么 “p4=p1” 是什么意思?

  将对象 p1 数据成员的值拷贝到对象 p4 中,这个过程是通过默认赋值运算符函数实现的。
  对象的赋值指对其中的数据成员赋值,而不对成员函数赋值。两个对象之间的赋值,仅仅使这些对象中的数据成员相同,而两个对象仍是分离的。

5、在声明类时,正确的是( )。

A. 可以在类的声明中给数据成员赋初值。
B. 数据成员的数据类型可以是 register。
C. private、public、protected 可以按任意顺序出现。
D. 没有用 private、public、protected 定义的数据成员是公有成员。

答:选 C。可以参考:https://blog.csdn.net/aaqian1/article/details/83795185

6、下面有关静态成员函数的描述中,正确的是( )。

A. 在静态成员函数中可以使用 this 指针。
B. 在建立对象前,就可以为静态数据成员赋值。
C. 静态成员函数在类外定义时,要用 static 前缀。
D. 静态成员函数只能在类外定义。

答:选B。可以参考:https://blog.csdn.net/aaqian1/article/details/84329893

7、在下面有关友元函数的描述中,正确的说法是( )。

A. 友元函数是独立于当前类的外部函数。
B. 一个友元函数不能同时定义为两个类的友元函数。
C. 友元函数必须在类的外部定义。
D. 在外部定义友元函数时,必须加关键字 friend。

答:选 A。https://blog.csdn.net/aaqian1/article/details/84427884

8、友元的作用之一是( )。

A. 提高程序的运行效率。
B. 加强类的封装性。
C. 实现数据的隐藏性。
D. 增加成员函数的种类。

答:选 A。https://blog.csdn.net/aaqian1/article/details/84427884

9、以下程序用到的知识是

(1)参考:https://blog.csdn.net/aaqian1/article/details/84494415

#include<iostream>
using namespace std;
class B{
	private:
		int x,y;
	public:
		B(){}
		B(int i,int j){
			x=i;
			y=j;
		}
		void printb(){
			cout<<x<<","<<y<<endl;
		}
};
class A{
	private:
		B c;
	public:
		A(){}
		A(int i,int j);
		void printa();
};
A::A(int i,int j):c(i,j){}
void A::printa(){
	c.printb();
}
int main(){
	A a(7,8);
	a.printa();
	return 0;
}

(2)同上

#include<iostream>
using namespace std;
class A{
	private:
		int x,y;
	public:
		void set(int i,int j){
			x=i;
			y=j;
		}
		int get_y(){
			return y;
		}
};
class Box{
	private:
		int length,width;
		A lable;
	public:
		void set(int l,int w,int s,int p){
			length=l;
			width=w;
			lable.set(s,p);
		}
		int get_area(){
			return length*width;
		}
};
int main(){
	Box b;
	b.set(4,6,1,20);
	cout<<b.get_area()<<endl;
	return 0;
}

(3)参考:https://blog.csdn.net/aaqian1/article/details/84502579

#include<iostream>
using namespace std;
class Sample{
	private:
		int x,y;
	public:
		Sample(int i,int j){
			x=i;
			y=j;
		}
		void disp(){
			cout<<"disp1"<<endl;
		}
		void disp() const{
			cout<<"disp2"<<endl;
		}
}; 
int main(){
	const Sample a(1,2);  //常对象 
	a.disp();
	return 0;
}

(4)同上

#include<iostream>
using namespace std;
class R{
	private:
		int R1,R2;
	public:
		R(int r1,int r2){
			R1=r1;
			R2=r2;
		}
		void print();
		void print() const;
};
void R::print(){
	cout<<R1<<","<<R2<<endl;
}
void R::print() const{
	cout<<R1<<","<<R2<<endl;
}
int main(){
	R a(6,8);
	a.print();
	const R b(56,58);
	b.print();
	return 0;
}

(5)参考:https://blog.csdn.net/aaqian1/article/details/84061049

#include<iostream>
using namespace std;
class Toy{
	private:
		int quan,price;
	public:
		Toy(int q,int p){
			quan=q;
			price=p;
		}
		int get_quan(){
			return quan;
		}
		int get_price(){
			return price;
		}
};
int main(){
	Toy op[3][2]={
		Toy(10,20),Toy(30,40),
		Toy(50,60),Toy(70,80),
		Toy(90,100),Toy(110,120)
	};
	for(int i=0;i<3;i++){
		cout<<op[i][0].get_quan()<<",";
		cout<<op[i][0].get_price()<<"\n";
		cout<<op[i][1].get_quan()<<",";
		cout<<op[i][1].get_price()<<"\n";
	}
	return 0;
}

(6)参考: https://blog.csdn.net/aaqian1/article/details/84146224

#include<iostream>
using namespace std;
class Example{
	private:
		int i;
	public:
		Example(int n){
			i=n;
			cout<<"Constructing\n";
		}
		~Example(){
			cout<<"Destructing\n";
		}
		int get_i(){
			return i;
		}
};
int sqr_it(Example o){
			return o.get_i()*o.get_i();
}
int main(){
	Example x(10);
	cout<<x.get_i()<<endl;
	cout<<sqr_it(x)<<endl;
	return 0;
}

执行结果:
在这里插入图片描述
(7)参考:
https://blog.csdn.net/aaqian1/article/details/84329893
https://blog.csdn.net/aaqian1/article/details/83717529

#include<iostream>
using namespace std;
class A{
	private:
		static int total;
	public:
		A(){
			total++;
		}
		~A(){
			total--;
		} 
		int gettotal(){
			return total;
		}
}; 
int A::total=0;
int main(){
	A o1,o2,o3;
	cout<<o1.gettotal()<<" objects in existence\n";
	A *p;
	p=new A;  //使用 new 分配内存空间
	if(!p){
		cout<<"Allocation error\n";
		return 1;
	} 
	cout<<o1.gettotal();
	cout<<" objects in existence after allocation\n";
	delete p;
	cout<<o1.gettotal();
	cout<<" objects in existence after deletion\n";
	return 0;
}

执行结果:
在这里插入图片描述
说明:这个程序使用静态数据成员追踪记载创建对象的个数。

(8)重点记住

#include<iostream>
using namespace std;
class Test{
	private:
		int i;
	public:
		Test();
		~Test(){};
};
Test::Test(){
	i=25;
	for(int ctr=0;ctr<10;ctr++){
		cout<<"Couting at "<<ctr<<"\n";
	}
}
Test anObject;
int main(){
	return 0;
}

执行结果:
在这里插入图片描述
说明:在主函数 main 中只包括了一个 return 语句,竟然有内容输出 ! 那么是什么时候调用了构造函数呢?
构造函数在对象被定义时调用,那么 anObject 是何时被调用的呢?在 main 之前,语句“ Test anObject ”时。
(9)参考:https://blog.csdn.net/aaqian1/article/details/84061049

#include<iostream>
using namespace std;
class A{
		int a,b;
	public:
		A(){
			a=0;
			b=0;
			cout<<"Default constructor called\n";
		}
		A(int i,int j){
			a=i;
			b=j;
			cout<<"Constructor:a="<<a<<",b="<<b<<endl;
		}
};
int main(){
	A a[3];
	A b[3]={A(1,2),A(3,4),A(5,6)};
	return 0;
}

执行结果:
在这里插入图片描述
(10)https://blog.csdn.net/aaqian1/article/details/84192809

#include<iostream>
using namespace std;
class Test{
	private:
		int val;
	public:
		Test(){
			cout<<"default."<<endl;
		}
		Test(int n){
			val=n;
			cout<<"Con."<<endl;
		}
		Test(const Test &t){
			val=t.val;
			cout<<"Copy con."<<endl;
		}
};
int main(){
	Test t1(6);
	Test t2=t1;
	Test t3;
	t3=t1;
	return 0;
}

执行结果:
在这里插入图片描述
(11)

#include<iostream>
using namespace std;
class N{
	private:
		int A;
		static int B;
	public:
		N(int a){
			A=a;
			B+=a;
		}
		static void f1(N m);
};
void N::f1(N m){
	cout<<"A="<<m.A<<endl;
	cout<<"B="<<m.B<<endl;
}
int N::B=0;
int main(){
	N P(5),Q(9);  //定义类 N 的对象 P,Q,调用含一个整型参数的构造函数 
	N::f1(P);  //调用函数 f1 时,实参 P 是类 N 的对象。将调用拷贝构造函数,初始化形参对象P
	N::f1(Q);  
	return 0;
}

执行结果:
在这里插入图片描述
(12)https://blog.csdn.net/aaqian1/article/details/84146224

#include<iostream>
using namespace std;
class M{
		int x,y;
	public:
		M(){
			x=y=0;
		}
		M(int i,int j){
			x=i;
			y=j;
		}
		void copy(M *m);
		void setxy(int i,int j){
			x=i;
			y=j;
		}
		void print(){
			cout<<x<<","<<y<<endl;
		}
};
void M::copy(M *m){
	x=m->x;
	y=m->y;
}
void fun(M m1,M *m2){
	m1.setxy(12,15);
	m2->setxy(22,25);
}
int main(){
	M p(5,7),q;  //分别调用含有两个参数和不含参数的构造函数
	q.copy(&p);
	fun(p,&q);
	p.print();
	q.print();
	return 0; 
}

执行结果:
在这里插入图片描述
(13)参考:https://blog.csdn.net/aaqian1/article/details/83999225 ,明白析构函数在什么时候被调用。

#include<iostream>
using namespace std;
class M{
		int A;
		static int B;
	public:
		M(int a){
			A=a;
			B+=a;
			cout<<"Constructing"<<endl;
		}
		static void f1(M m);
		~M(){
			cout<<"Destructing"<<endl;
		}
};
void M::f1(M m){
	cout<<"A="<<m.A<<endl;
	cout<<"B="<<m.B<<endl;
}
int M::B=0;
int main(){
	M P(5),Q(10);
	M::f1(P);  //调用一次析构函数
	M::f1(Q);   //调用一次析构函数
	return 0;    //调用两次析构函数
}

执行结果:
在这里插入图片描述

10、(1)构建一个类 book,其中含有两个私有数据成员 qu 和 price ,将 qu 初始化为 1~5,将 price 初始化为 qu 的 10 倍,建立一个有 5 个元素的数组对象,显示每个对象数组元素的 qu*price 的值。

#include<iostream>
using namespace std;
class Book{
	private:
		int qu;
		int price;
	public:
		Book(int a,int b){
			qu=a;
			price=b;
		}
		void show_money(){
			cout<<qu*price<<endl;
		}
};
int main(){
	Book ob[5]={
		Book(1,10),
		Book(2,20),
		Book(3,30),
		Book(4,40),
		Book(5,50),
	};
	for(int i=0;i<5;i++){
		ob[i].show_money();
	}
	return 0;
}

执行结果:
在这里插入图片描述

(2)修改10.(1),通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的 qu*price 的值。

#include<iostream>
using namespace std;
class Book{
	private:
		int qu;
		int price;
	public:
		Book(int a,int b){
			qu=a;
			price=b;
		}
		void show_money(){
			cout<<qu*price<<endl;
		}
};
int main(){
	Book ob[5]={
		Book(1,10),
		Book(2,20),
		Book(3,30),
		Book(4,40),
		Book(5,50),
	};
	Book *p;
	p=&ob[4];
	for(int i=0;i<5;i++){
		p->show_money();
		p--;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aaqian1/article/details/84557578