-
/****************方法1************************/ Coordinate?*p?=?NULL; p?=?new?Coordinate(3,5);???//或者:Coordinate?*p?=?new?coordinate(3,5); ????//?打印坐標 cout?<<"("<<p->m_iX<<","<<p->m_iY<<")"<<?endl; ????//?銷毀對象指針 delete?p; p?=?NULL; /****************方法2************************/ Coordinate?p1(3,5); Coordinate?*p2?=?&p1; ????//?打印坐標 cout?<<"("<<p2->m_iX<<","<<p2->m_iY<<")"<<?endl; ????//?銷毀對象指針 delete?p2; p2?=?NULL;
查看全部 -
/******************coordinate.h文件*************************/ //?stdafx.h?:?標準系統(tǒng)包含文件的包含文件, //?或是經(jīng)常使用但不常更改的 //?特定于項目的包含文件 // #pragma?once class?coordinate { public: coordinate(); coordinate(int?x,int?y); ~coordinate(); void?setX(int?x); int?getX(); void?setY(int?y); int?getY(); private: int?m_iX; int?m_iY; }; /******************Line.h文件*************************/ #pragma?once #include"coordinate.h" class?Line { public: Line(); Line(int?x1,?int?y1,?int?x2,?int?y2); //Line(const?Line&?line); ~Line(); void?setA(int?x1,?int?y1); void?setB(int?x2,?int?y2); void?printInfo(); private: coordinate?m_coorA; coordinate?m_coorB; }; /******************coordinate.cpp文件*************************/ #include<iostream> #include?"coordinate.h" using?namespace?std; coordinate::coordinate() { cout?<<?"coordinate()"?<<?endl; } coordinate::coordinate(int?x,?int?y):m_iX(x),m_iY(y) { cout?<<?"coordinate(int?x,int?y);"?<<?endl; } coordinate::~coordinate() { cout?<<?"~coordinate()"?<<?endl; } void?coordinate::setX(int?x)?{ m_iX?=?x; } int?coordinate::getX()?{ return?m_iX; } void?coordinate::setY(int?y)?{ m_iX?=?y; } int?coordinate::getY()?{ return?m_iY; } /******************Line.cpp文件*************************/ //?stdafx.cpp?:?只包括標準包含文件的源文件 //?test1.pch?將作為預(yù)編譯頭 //?stdafx.obj?將包含預(yù)編譯類型信息 #include<iostream> #include<stdlib.h> #include?"Line.h" using?namespace?std; Line::Line() { cout?<<?"Line()"?<<?endl; } Line::Line(int?x1,int?y1,int?x2,int?y2):m_coorA(x1,y1),m_coorB(x2,y2) { cout?<<?"Line(int?x1,int?y1,int?x2,int?y2)"?<<?endl; } /*Line::Line(const?Line&?line)?{ m_coorA?=?line.m_coorA; m_coorB?=?line.m_coorB; } */ Line::~Line() { cout?<<?"~Line()"?<<?endl; } void?Line::setA(int?x1,?int?y1)?{ m_coorA.setX(x1); m_coorA.setY(y1); } void?Line::setB(int?x2,?int?y2)?{ m_coorB.setX(x2); m_coorB.setY(y2); } void?Line::printInfo()?{ cout?<<?"("?<<?m_coorA.getX()?<<?","?<<?m_coorA.getY()?<<?")"?<<?endl; cout?<<?"("?<<?m_coorB.getX()?<<?","?<<?m_coorB.getY()?<<?")"?<<?endl; } /******************main.cpp文件*************************/ //?test1.cpp?:?定義控制臺應(yīng)用程序的入口點。 // #include<iostream> #include<stdlib.h> #include"Line.h" using?namespace?std; int?main()?{ Line?*p?=?new?Line(1,2,3,4); p->printInfo(); //cout?<<?coordinate::getX<<?endl; delete?p; p?=?NULL; system("pause"); return?0; }
查看全部 -
/******************array.h文件*************************/ class?Array{ public: ????Array(int?_cout); ????~Array(); ????Array(const?Array?&arr); ????void?setCount(int?_cout); ????int?getCount(); ????void?printAddr(); ????void?printArr(); private: ????int?m_iCount; ????int?*m_pArr; }; /******************array.cpp文件*************************/ #include<iostream> #include"array.h" using?namespace?std; Array::Array(int?_count) { ????m_iCount?=?_count; ????m_pArr?=?new?int[m_iCount]; ????for(int?i?=?0;i<m_iCount;i++){ ????????m_pArr[i]?=?i; ????} ????cout<<"Array"<<endl; } Array::~Array() { ????delete?[]m_pArr; ????m_pArr?=?NULL; ????cout<<"~Array"<<endl; } Array::Array(const?Array?&arr) { ????//m_iCount?=?arr.m_iCount; ????//m_pArr?=?arr.m_pArr; ????m_iCount?=?arr.m_iCount; ????m_pArr?=?new?int[m_iCount]; ????for(int?i?=?0;i<m_iCount;i++){ ????????m_pArr[i]?=?arr.m_pArr[i]; ????} ????cout<<"Array&"<<endl; } void?Array::setCount(int?_count) { ????m_iCount?=?_count; } int?Array::getCount() { ????return?m_iCount; } void?Array::printAddr(){ ????cout<<"Address_value:"<<m_pArr<<endl; } void?Array::printArr(){ ????for(int?i?=?0;i<m_iCount;i++){ ????????cout<<m_pArr[i]<<endl; ????} } /******************demo.cpp文件*************************/ #include<iostream> #include<stdlib.h> #include"array.h" using?namespace?std; int?main(void){ ????Array?arr1(5); ????//arr1.setCount(3); ????Array?arr2(arr1); ????//cout<<"arr2.m_iCount?"<<arr2.getCount()<<endl; ????//arr1.printAddr(); ????//arr2.printAddr(); ????arr1.printArr(); ????arr2.printArr(); ????system("pause"); ????return?0; }
查看全部 -
走迷宮案例:
左手原則:想象自己在家左手扶著墻一直走。
右手原則:想象自己在家右手扶著墻一直走。
要建立兩個類,迷宮類和人
成就感源于克服困難
查看全部 -
<br data-filtered=
"filtered"
>
碼上大佬的代碼,致敬!
#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如果當前格為第一排,則說明當前格為終點,結(jié)束。
2根據(jù)當前格的數(shù)據(jù),找到下一步需要面向的方向, 方法是,如果當前方向上有墻,則順時針(右手抹墻則逆時針)轉(zhuǎn)身,重復(fù)這一步驟直到面向的方向上可以行走
3沿當前方向走一步
4逆時針(右手抹墻則順時針)轉(zhuǎn)身一次,使當前面對方向為第3步之后的左手(或右手)方向, 然后回到步驟1
*/
//墻體和路常量
#define WOLD ?"■"
#define ROLD ?" ?"
//迷宮大小常量
const int X = 8;
const int Y = 8;
//計步器
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){} //默認人初始化為第一個位置
Human(string _man) :x(6), y(6), man(_man), direction(W){} //可以設(shè)置人的標識符
//橫縱坐標
int x;
int y;
//人物標識
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 ?右手抹墻,初始朝向為W
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;
}
查看全部 -
Coordinate * const pCoor = &coor1 : 這種方式定義的常指針“只能指向coor1,但可以調(diào)用coor1的不同的函數(shù)”;(擁有讀寫權(quán)限) const Coordinate *pCoor = &coor1 ?: 只能調(diào)用coor1的“常成員函數(shù)”。(只擁有讀權(quán)限)
常對象引用與常對象指針?
const Coordinate& coor2 ; const Coordinate *p; 只具有讀權(quán)限,所以只能調(diào)用常成員函數(shù)。調(diào)用成員函數(shù)過程其實就是一個隱式的this指針,普通成員函數(shù)傳遞的是Coordinate *this指針,而常成員函數(shù)傳遞的是常指針const Coordinate *this(只具有讀權(quán)限)。 注意,Coordinate * const p,該指針表明指針地址為常量,指針所指向的對象值是可以改變的,也就是說該指針具有讀寫權(quán)限
查看全部 -
為什么需要const成員函數(shù)? 我們定義的類的成員函數(shù)中,常常有一些成員函數(shù)不改變類的數(shù)據(jù)成員,也就是說,這些函數(shù)是"只讀"函數(shù),而有一些函數(shù)要修改類數(shù)據(jù)成員的值。如果把不改變數(shù)據(jù)成員的函數(shù)都加上const關(guān)鍵字進行標識,顯然,可提高程序的可讀性。其實,它還能提高程序的可靠性,已定義成const的成員函數(shù),一旦企圖修改數(shù)據(jù)成員的值,則編譯器按錯誤處理。 const成員函數(shù)和const對象 實際上,const成員函數(shù)還有另外一項作用,即常量對象相關(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ù)據(jù)成員作任何形式的修改,但是它沒有被const關(guān)鍵字限定的,也不能被常量對象調(diào)用。
查看全部 -
用const修飾對象成員,對象成員就變成了常對象成員 用const修飾成員函數(shù),成員函數(shù)就變成了常成員函數(shù)。const要放在函數(shù)的最后, 一旦初始化就不能再修改,const就是干這個的,長對象成員用初始化列表初始化 函數(shù)里看著沒有參數(shù),實際上隱藏著this指針。常成員函數(shù)中隱藏的是常this指針,常指針指向的數(shù)據(jù)不允許被修改 例如:void Coordinate::changeX(){m_iX = 10;}即為void Coordinate::changeX(Coordinate *this){this->m_iX = 10;} void Coordinate::changeX()const{m_iX = 10;}即為void Coordinate::changeX(const Coordinate *this){this->m_iX = 10;}這里的m_iX = 10;的寫法是錯誤的,this已經(jīng)是常指針,通過常指針是無法修改值的 void changeX();與void changeX() const互為重載 要調(diào)用const修飾的常成員函數(shù),實例化對象時,必須用const來修飾對象,const寫在最前面
查看全部 -
this的值是對象本身地址;*this 就是對象arr1 1、 Array ... return *this 相當于: Array arrX = arr1; arrX是一個新的對象。即返回的this指針為另一個臨時對象 2、 Array& ... return *this 相當于: Array & arrX = arr1; 此時arrX是arr1的別名。 3、 Array* ... return this 相當于: Array* arrX = this; 此時arrX的值 是地址,且是指向arr1的。用->訪問或者*p. 訪問
查看全部 -
this指針的作用就是:解決了參數(shù)和數(shù)據(jù)成員重名的問題,讓計算機清楚是參數(shù)傳給了數(shù)據(jù)成員; this指針一般都是系統(tǒng)默認調(diào)用,以防止在實例化對象調(diào)用成員函數(shù)的時候出現(xiàn)錯誤,保證一一對應(yīng),當數(shù)據(jù)成員和構(gòu)造函數(shù)中的形參名字相同是,計算機會分不清楚誰給誰賦值,這是需要人工加上this指針,用來區(qū)別。
查看全部 -
This 表示對象的地址,可以訪問到自身對象的數(shù)據(jù)成員 This 代表當前自身的對象,誰調(diào)用 Array 構(gòu)造對象,然后 This 就代表那個對象取代那個對象,也就是 ?Array arr1; Array arr1;arr1.setLen(5)的時候,{this -> len = len} ?中 this 就是代表了 arr1 對象,會取代 this ,而 this 其實是對象的地址,也就是指針,{this -> len = len} 就代表是 { arr1.len = len; },從而標記區(qū)別了數(shù)據(jù)成員和參數(shù)。
查看全部 -
(1)32位的編譯環(huán)境,一個指針占4個字節(jié),1一個字節(jié)就是一個基本的內(nèi)存單元,1Byte = 8 bit,即一個內(nèi)存單元里可以存儲2的8次方不同情況的高低電平。<br> (2)因為Line這個類中有,兩個對象成員指針,所以占了8個字節(jié)。<br> (3)sizeof()函數(shù)返回的是整數(shù),單位為“字節(jié)”. (4)64位的是32位的兩倍
查看全部 -
對象成員:一個對象作為另外一個類的數(shù)據(jù)成員<br><br><br> 對象成員指針:一個對象的指針作為另外一個類的數(shù)據(jù)成員<br><br> 1.對象成員指針的定義: 類名 * 指針名 是指針而不是對象<br> 2.指針在32位編譯器下占4個基本內(nèi)存單元<br><br> 3.若存在對象成員指針1,2……。sizeof(指針1,指針2……)只計算各指針所占內(nèi)存的總和,不計算對象成員所占內(nèi)存<br> 對象成員指針如果在構(gòu)造函數(shù)用 new 的方式從堆中申請內(nèi)存實例化2個 Coordinate 對象,那這2個對象都是在堆中,而不在 Line 對象中,因為每個指針占 4 個內(nèi)存單元,因此 sizeof(Line) 只占 8 個內(nèi)存單元,銷毀 Line 的時候,先銷毀隊中的內(nèi)存,在釋放 Line 本身的內(nèi)存
查看全部 -
查看全部
-
int main(void)
{
//Coordinate *p1 = NULL;//第一個點,指向NULL
//p1 = new Coordinate;//因為Coordinate是一個默認的構(gòu)造函數(shù)所以可以沒有參數(shù)
//Coordinate *p2 = new Coordinate();
//p1->m_iX = 10;//兩種不同的賦值方法
//p1->m_iY = 20;
//(*p2).m_iX = 30;
//(*p2).m_iY = 40;
//cout << p1->m_iX + (*p2).m_iX << endl;//輸出
//cout << p1->m_iY + (*p2).m_iY << endl;
//
//delete p1;
?//? ?p1 = NULL;
//delete p2;
//p2 = NULL;
/*************************************************************/
Coordinate p1;//從棧中實例化一個對象
Coordinate *p2 = &p1;//讓p2指向p1,運用了取地址的符號
//然后就可以用p2來操作p1的數(shù)據(jù)成員和成員函數(shù)了
p2->m_iX = 10;
p2->m_iY = 20;
cout << p1.m_iX << endl;
cout << p1.m_iY << endl;
system("pause");
return 0;
}
查看全部
舉報