1. 程式人生 > >習題(針對學習筆記16-30)

習題(針對學習筆記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
            
           

相關推薦

習題針對學習筆記16-30

1、什麼是物件陣列? https://blog.csdn.net/aaqian1/article/details/84061049 2、什麼是 this 指標?它的主要作用是什麼? https://blog.csdn.net/aaqian1/article/details/841

類和物件C++學習筆記 16

類(class)是一種資料型別,是一種使用者定義的抽象的資料型別。 類是一種抽象的資料型別,它不佔儲存空間,不能容納具體的資料,因此在類宣告中不能給資料成員賦初值。 類代表了一批物件的共性和特徵。 類是進行封裝和資料隱藏的工具,它將資料與操作緊密地結合起來。

傳智168期 day61 redis 筆記2017年8月25日19:16:30

inux -s 入門 cnblogs com 操作 筆記 image 密碼 redis入門筆記,介紹了如何在Linux下搭建redis,開啟redis服務,還有常見的操作。 筆記下載: 鏈接:https://pan.baidu.com/s/1sl9GneP 密碼:

Java學習筆記16面向對象九:補充內容

nal ati 接收 pri version prot sys add [] 總是看到四種權限,這裏做一個介紹: 最大權限是public,後面依次是protected,default,private private修飾的只在本類可以使用 public是最大權限,可以跨包使用

Angular6學習筆記16:核心知識-元件component

元件 簡介 元件控制螢幕上被稱為檢視的一小片區域。將檢視等同於一個小汽車,那麼元件就是組成汽車的每一個零件。在類中定義元件的應用邏輯,為檢視提供支援。元件通過一些由屬性和方法組成的API與檢視互動。當用戶在整個應用中操作的時候,Angular就會建立,更新,銷燬一些元件。應用可以通過一些可以

常型別C++學習筆記 30

因為程式中各種形式的資料共享,在不同程度上破壞了資料的安全性。引入常型別的目的是,為了保證資料共享又防止資料被改動。 常型別是指使用型別修飾符 const 說明的型別,常型別的變數或物件成員的值在程式執行期間是不可改變的。 本文將從三方面入手:常引用、常物件和常物件成員。

ROS學習筆記16編寫簡單的訊息釋出器和訂閱器 (Python)

1 編寫釋出者節點 “節點”是連線到ROS網路的可執行檔案ROS術語。在這裡,我們將建立一個持續廣播訊息的釋出者(“talker”)節點。 將目錄更改為您在早期教程中建立的的beginner_tutorials包,並建立一個包: $ roscd beginner_tut

python學習筆記16遞迴、棧和佇列

遞迴、棧和佇列 遞迴呼叫: 一個函式,呼叫了自身,稱為遞迴呼叫 遞迴函式: 一個會呼叫自身的函式稱為遞迴函式 特點: 凡是迴圈能幹的事,遞迴都能幹 過程: 1、寫出臨界條件 2、找這一次和上一次的關係 3、假設當前函式已經能用,呼叫自身計算上一次的結果,再求出本次的結果 #輸入一

java個人學習筆記16多執行緒+extends Thread+implements Runnable

1.多執行緒 程序:正在執行的應用程式在記憶體中分配的空間 執行緒:是程序中負責程式執行的執行單元,也稱執行路徑 一個程序中至少有一個執行緒在負責該程序的執行 多執行緒技術:解決多部分程式碼同時執行的需求,合理使用cpu資源,提高使用者體驗。(微觀上序列,並未實際上提高效率

MYSQL學習筆記3mysql兩行數據合並成一行

mysql使用SUM函數,加上GROUP BY人員ID就可以實現了:SELECT SUM(PZ+CPJS+BZ+GC+SB+TG+MJ+CL+CCLW+GJ+ZL+CBZZ) as count, SUM(PZ) as PZ,SUM(CPJS) as CPJS,SUM(BZ) as BZ,SUM(GC)

MYSQL學習筆記2多表連接查詢

mysql3種連接方式的區別:INNER JOIN(內連接,或等值連接):獲取兩個表中字段匹配關系的記錄。LEFT JOIN(左連接):獲取左表所有記錄,即使右表沒有對應匹配的記錄。RIGHT JOIN(右連接): 與 LEFT JOIN 相反,用於獲取右表所有記錄,即使左表沒有對應匹配的記錄。3個表連接查詢

MYSQL學習筆記4事務的開啟、提交、回滾

mysql 事務 使用事務要註意以下三點:1、在 MySQL 中只有使用了 Innodb 數據庫引擎的數據庫或表才支持事務。2、事務處理可以用來維護數據庫的完整性,保證成批的 SQL 語句要麽全部執行,要麽全部不執行。3、事務用來管理 insert,update,delete 語句MYSQL 事務處理

我的第一個spring boot程序spring boot 學習筆記之二

獲取json 了解 訪問 static 依賴 過程 public 獲取數據 gap 第一個spring boot程序 寫在前面:鑒於spring註解以及springMVC的配置有大量細節和知識點,在學習理解之後,我們將直接進入spring boot的學習,在後續學習中用到註

讀入優化~~~個人學習筆記

false == 學習 學習筆記 筆記 scanf ios sca tchar 基本模板:inline int read(){ int x=0,w=1; char ch=0; while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)w=-1;ch=

Disconf 學習系列之全網最詳細的最新穩定Disconf 搭建部署基於Ubuntu14.04 / 16.04圖文詳解

class 6.0 conf ubuntu14 穩定 div ubun 搭建 學習   不多說直接上幹貨! https://www.cnblogs.com/wuxiaofeng/p/6882596.html (ubuntu16.04) https

模塊、類和對象python學習筆記

python 類 模塊 對象 模塊、類和對象 1.字典,記住鍵值對的概念,記住從鍵值對 mystuff = {‘apple‘:"I am apples"} print mystuff[‘apple‘] 2.模塊 ‘‘‘ 模塊 1.模塊是包含函數和變量的文件 2.模塊這個

python基礎自主學習筆記

持續更新如果字符內既包含‘又包含"可以用轉義字符 \ 來標識\n 表示換行,\t 表示制表符, 用 \ \ 表示字符 \ ,可以用 r" 表示 “內部的字符串默認不轉義” ,當字符串內部存在很多換行時,用‘‘‘...‘‘‘的格式來換行跟 \n 的用法一樣,方便閱讀。用 True、False

js 數組方法個人學習筆記

rev 了解 很多 com 過濾 接收參數 版本 push 操作數 首先,創建數組的兩個方法:   1.構造函數:  var array = new Array();//新建一個空數組 var array1 = new Array(20);//新建一個長度為20位的空數組

js 字符串方法個人學習筆記

new substring for name var 基本 正則表達 pattern 都是 首先,創建數組的兩個方法:   1.構造函數: var str = new String("a"); console.log(str);//"a"   2.字面量表示: var

js Math對象方法 個人學習筆記

最大 eof null alert XP source floor app math.sqrt 方法: 1.丟棄小數部分,保留整數部分 parseInt(5/2) 2.向上取整,有小數就整數部分加1 Math.ceil(5/2) 3,四舍五入.