-
深拷貝好復(fù)雜
查看全部 -
對象數(shù)組的用法
? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
查看全部 -
this指針的用法
查看全部 -
A對象在B對象中,A需要參數(shù)進(jìn)行構(gòu)造
則必須使用初始化列表的方式對A進(jìn)行實(shí)例化
查看全部 -
成員變量命名建議加前綴m。
A對象在B對象中
實(shí)例化對象時,先實(shí)例化A對象,再實(shí)例化B對象,銷毀時相反
用初始化列表實(shí)例化A對象
查看全部 -
查看全部
-
p指向第一個對象。
執(zhí)行p++后則p指向第二個對象,此時p[0]表示的也是第二個對象,
只有當(dāng)p指向原來的位置時,才可以使用delete []p,否則會報錯
查看全部 -
在c++ 中只要一個文件里面包含了的頭文件,在另一個文件里引用了,就不需要重復(fù)的再去寫頭文件和命名空間
查看全部 -
Coordinate * const p 和 Coordinate const *p 是不一樣的。Coordinate * const p 中的const 修飾指針,代表指針指向的地址是一個常量,地址不能改。Coordinate const *p 中的const 修飾指針變量,代表指針指向的變量是個常量,變量不能改。
查看全部 -
指針的拷貝要new一個新的指針,然后把要復(fù)制的值復(fù)制到新的指針中。
查看全部 -
Const成員要在構(gòu)造函數(shù)的初始化列表中初始化
查看全部 -
碼上大佬的代碼,致敬!
#include <iostream>
#include <Windows.h>
#include <string>
#include <process.h>
#include <Windows.h>
using namespace std;
/* https://zhidao.baidu.com/question/1755742141667975828.html?
設(shè)定初始面向 East ( 如果是右手抹墻則是 West)
1如果當(dāng)前格為第一排,則說明當(dāng)前格為終點(diǎn),結(jié)束。
2根據(jù)當(dāng)前格的數(shù)據(jù),找到下一步需要面向的方向, 方法是,如果當(dāng)前方向上有墻,則順時針(右手抹墻則逆時針)轉(zhuǎn)身,重復(fù)這一步驟直到面向的方向上可以行走
3沿當(dāng)前方向走一步
4逆時針(右手抹墻則順時針)轉(zhuǎn)身一次,使當(dāng)前面對方向?yàn)榈?步之后的左手(或右手)方向, 然后回到步驟1
*/
//墻體和路常量
#define WOLD ?"■"
#define ROLD ?" ?"
//迷宮大小常量
const int X = 8;
const int Y = 8;
//計(jì)步器
int count = 0;
//方向枚舉
enum direction{
N = 0,
S = 1,
W = 2,
E = 3
};
//地圖類
class MazeMap {
public:
MazeMap() {}
void coutMap(string map[X][Y]) { //構(gòu)造地圖
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
cout << map[i][j] ;
}
cout << endl;
}
}
};
//人類類
class Human {
public:
Human() :x(6), y(6), man("☆"), direction(W){} //默認(rèn)人初始化為第一個位置
Human(string _man) :x(6), y(6), man(_man), direction(W){} //可以設(shè)置人的標(biāo)識符
//橫縱坐標(biāo)
int x;
int y;
//人物標(biāo)識
string man;
//方向
int direction;
// 移動方法
void humanMove(string map[X][Y], Human *man) {
MazeMap *mazeMap = new MazeMap;
map[man->x][man->y] = man->man;
mazeMap->coutMap(map);
cout << "READY?(Q:y/n)" << endl;
char flag = 'n';
cin >> flag;
//單向?qū)ぢ吩瓌t ?右手抹墻,初始朝向?yàn)閃
if (flag == 'y') {
do
{
turnBack(map, man);
move(map, man);
Sleep(1000);
system("cls");
mazeMap->coutMap(map);
} while (finsh(man));
cout << "YOU WIN!!!" << endl;
}
else {
cout << "YOU ARE A LOSE!!!" << endl;
}
delete mazeMap;
mazeMap = NULL;
}
//確定方向 如果方向上有墻就逆時針轉(zhuǎn)一下
void turnBack(string map[X][Y],Human *man) {
static int cache = 0;
if (man->direction == N) {
if (map[man->x - 1][man->y] == WOLD) {
man->direction = W;
cache++;
turnBack(map, man);
}
cache = 0;
return;
}
if (man->direction == S) {
if (map[man->x+1][man->y] == WOLD) {
man->direction = E;
cache++;
turnBack(map, man);
}
cache = 0;
return;
}
if (man->direction == W) {
if (map[man->x][man->y-1] == WOLD) {
man->direction = S;
cache++;
turnBack(map, man);
}
cache = 0;
return;
}
if (man->direction == E) {
if (map[man->x][man->y+1] == WOLD) {
man->direction = N;
cache++;
turnBack(map, man);
}
cache = 0;
return;
}
if (cache == 4) {
cache = 0;
return;
}
}
//移動一格 ?后順時針調(diào)轉(zhuǎn)方向
void move(string map[X][Y], Human *man) {
if (man->direction == N) {
man->direction = E;
map[man->x - 1][man->y] = man->man;
map[man->x][man->y] = ROLD;
man->x -= 1;
}else if (man->direction == S) {
man->direction = W;
map[man->x + 1][man->y] = man->man;
map[man->x][man->y] = ROLD;
man->x += 1;
}else if (man->direction == W) {
man->direction = N;
map[man->x][man->y - 1] = man->man;
map[man->x][man->y] = ROLD;
man->y -= 1;
}else if(man->direction == E) {
man->direction = S;
map[man->x][man->y + 1] = man->man;
map[man->x][man->y] = ROLD;
man->y += 1;
}
return;
}
//判斷是否完成
bool finsh(Human *man) {
if (man->x == 0)
return false;
return true;
}
};
int main(void) {
string map[X][Y] = { {WOLD,ROLD,WOLD,WOLD,WOLD,WOLD,WOLD,WOLD},
{WOLD,ROLD,ROLD,ROLD,ROLD,ROLD,WOLD,WOLD},
{WOLD,WOLD,WOLD,WOLD,WOLD,ROLD,ROLD,WOLD},
{WOLD,ROLD,ROLD,ROLD,ROLD,ROLD,WOLD,WOLD},
{WOLD,WOLD,ROLD,WOLD,ROLD,ROLD,WOLD,WOLD},
{WOLD,WOLD,ROLD,ROLD,ROLD,WOLD,WOLD,WOLD},
{WOLD,WOLD,WOLD,WOLD,ROLD,ROLD,ROLD,WOLD},
{WOLD,WOLD,WOLD,WOLD,WOLD,WOLD,WOLD,WOLD} };
Human *man = new Human("⊙");
man->humanMove(map,man);
delete man;
man = NULL;
return 0;
}
查看全部 -
/*
常對象成員,常函數(shù),常對象
要求:
? ? ?定義兩個類:
Coordinate:數(shù)據(jù)成員:m_iX,m_iY
? ? ? ? ? ? ?成員函數(shù):構(gòu)造函數(shù),析構(gòu)函數(shù),封裝函數(shù)
Line:數(shù)據(jù)成員:點(diǎn)A,點(diǎn)B
? ? ? ?成員函數(shù):構(gòu)造函數(shù),析構(gòu)函數(shù),封裝函數(shù),信息打印函數(shù)
*/
常對象只能調(diào)用常成員函數(shù)。
普通對象既可以調(diào)用普通成員函數(shù),也可以調(diào)用常成員函數(shù)。
常成員函數(shù)不能改變成員的值(因?yàn)椴粋魅雝his指針)
const 對象不能修改其成員變量,也就是說只能讀不能寫
為什么需要const成員函數(shù)? 我們定義的類的成員函數(shù)中,常常有一些成員函數(shù)不改變類的數(shù)據(jù)成員,也就是說,這些函數(shù)是"只讀"函數(shù),而有一些函數(shù)要修改類數(shù)據(jù)成員的值。如果把不改變數(shù)據(jù)成員的函數(shù)都加上const關(guān)鍵字進(jìn)行標(biāo)識,顯然,可提高程序的可讀性。其實(shí),它還能提高程序的可靠性,已定義成const的成員函數(shù),一旦企圖修改數(shù)據(jù)成員的值,則編譯器按錯誤處理。 const成員函數(shù)和const對象 實(shí)際上,const成員函數(shù)還有另外一項(xiàng)作用,即常量對象相關(guān)。對于內(nèi)置的數(shù)據(jù)類型,我們可以定義它們的常量,用戶自定義的類也一樣,可以定義它們的常量對象。例如,定義一個整型常量的方法為: const int i=1 ; 同樣,也可以定義常量對象,假定有一個類classA,定義該類的常量對象的方法為: const classA a(2); 這里,a是類classA的一個const對象,"2"傳給它的構(gòu)造函數(shù)參數(shù)。const對象的數(shù)據(jù)成員在對象生存期內(nèi)不能改變。但是,如何保證該類的數(shù)據(jù)成員不被改變呢? 為了確保const對象的數(shù)據(jù)成員不會被改變,在C++中,const對象只能調(diào)用const成員函數(shù)。如果一個成員函數(shù)實(shí)際上沒有對數(shù)據(jù)成員作任何形式的修改,但是它沒有被const關(guān)鍵字限定的,也不能被常量對象調(diào)用。
查看全部 -
常成員函數(shù):
聲明:<類型標(biāo)志符>函數(shù)名(參數(shù)表)const;
如果一個成員函數(shù)對類中數(shù)據(jù)成員只作訪問而不作直接或間接的修改,則最好將此函數(shù)設(shè)置為常成員函數(shù),以明確表示它對數(shù)據(jù)成員的保護(hù)性。
必須是常對象才能調(diào)用常成員函數(shù)。
常成員函數(shù)與普通成員函數(shù)互為重載,常成員函數(shù)只能由常對象調(diào)用,普通成員函數(shù)由普通對象調(diào)用。
查看全部 -
this指針就是當(dāng)前對象的地址,可以直接輸出cout<<this *this表示當(dāng)前實(shí)例對象 如果返回值為類 類型對象,那么其實(shí)是個臨時對象,并不是原來對象,如 Array getA() { ? ? return *this; } 此時得到的并不是原來的*this對象。 要獲得原對象實(shí)例可以改為Array&引用類型或Array*指針類型。
查看全部
舉報