C++封装篇 (下)

来源:互联网 发布:淘宝发布宝贝产品规格 编辑:程序博客网 时间:2024/06/06 02:40

对象数组

#include<iostream>#include "Coordinate.h"using namespace std;int main() {    Coordinate coor[3];// 从栈上    coor[0].m_iX = 3;    coor[0].m_iY = 5;    Coordinate *p = new Coordinate[3];    p->m_iX = 7;    p[0].m_iY = 9;    p++;    p->m_iX = 11;    p[0].m_iY = 13;    p[1].m_iX = 15;    p++;    p->m_iY = 17;    for (int i = 0; i < 3; i++) {        cout << "coor_x " << coor[i].m_iX << endl;        cout << "coor_y " << coor[i].m_iY << endl;    }    for (int i = 0; i < 3; i++) {        cout << "p_x " << p->m_iX << endl;        cout << "p_y " << p->m_iY << endl;        p--;    }    p++;    delete []p;// []很重要,如果不是[],就只会释放p[0]    p = NULL;    return 0;}

对象成员

创建时,先创建对象成员,再穿件对象销毁时,先销毁对象,在销毁对象成员(栈的数据结构)

深拷贝和浅拷贝

内存的拷贝,引用的拷贝Array::Array(const Array &arr) {    cout << "Array &" << endl;    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];    }}

对象指针

对象成员指针

this指针

被隐藏了,默认的代码区只有一份,成员变量有多份,通过this来区分不同的对象返回对象时,要注意引用和非引用(临时对象)

常对象成员

常成员函数

void changX() const;// 相当于void changX(const Coordinate *this);

常指针与常引用

#include <iostream>using namespace std;class Coordinate {public:    Coordinate(int x, int y):m_iX(x), m_iY(y) {    }    // 实现常成员函数    void printInfo() const {        cout << "(" << m_iX << "," << m_iY << ")" << endl;    }public:    int m_iX;    int m_iY;};int main(void) {    const Coordinate coor(3, 5);    // 创建常指针p    const Coordinate *p = &coor;    // 创建常引用c    const Coordinate &c = coor;    coor.printInfo();    p->printInfo();    c.printInfo();    return 0;}

结业小程序:走迷宫

介绍:一个小人走迷宫,左手或右手规则

类:Maze Person

代码:

Public.h // 常量定义
#pragma once

const int FAST = 1;const int MIDD = 2;const int SLOW = 3;const char WALL = '*';const char ROAD = ' ';enum OBJECTIVE_DIRECTIONS {    UP, DOWN, LEFT, RIGHT};

Maze.h
#pragma once
#include “Public.H”
#define MAX_HEIGHT 25
#define MAX_WIDTH 80

class Maze {public:    Maze(char road, char wall);    Maze();    ~Maze();    void setMap(char *map, int width, int height);    char* getMap();    int getWidth();    int getHeight();    bool isRoad(int x, int y);    void drawMap();private:    char m_cRoad;    char m_cWall;    int m_iWidth;    int m_iHeight;    char m_cMap[MAX_WIDTH][MAX_HEIGHT];};

Maze.cpp
#include “Maze.h”
#include
using namespace std;

Maze::Maze(char road, char wall) :m_cRoad(road), m_cWall(wall) {}Maze::Maze() {}Maze::~Maze() {}void Maze::setMap(char * map, int width, int height) {    for (int i = 0; i < height; i++) {        for (int j = 0; j < width; j++) {            m_cMap[i][j] = *map;            map++;        }    }    m_iHeight = height;    m_iWidth = width;}char* Maze::getMap() {    return &m_cMap[0][0];}int Maze::getWidth() {    return m_iWidth;}int Maze::getHeight() {    return m_iHeight;}bool Maze::isRoad(int x, int y) {    bool ret = false;    if (m_cMap[y][x] == ROAD || x == -1 || y == -1 || x == m_iWidth || y == m_iHeight) {        ret = true;    }    return ret;}void Maze::drawMap() {    for (int i = 0; i < m_iHeight; i++) {        for (int j = 0; j < m_iWidth; j++) {            cout << m_cMap[i][j];        }        cout << endl;    }}

Person.h
#pragma once
#include “Public.h”
#include “Maze.h”
#include
using namespace std;

class Person {public:    Person(int speed, int direction, int curX, int curY, char symbol);    ~Person();    bool isOutOfMaze();    void goNextStep();    void start(Maze &maze);    void gotoxy(int x, int y);    void drawPerson();private:    int m_iSpeed;    int m_iDirection;    int m_iCurX;    int m_iCurY;    int m_iPreX;    int m_iPreY;    char m_cSymbol;    Maze m_maze;};

Person.cpp
#include “Person.h”
#include “cstdlib”
#include “windows.h”

Person::Person(int speed, int direction, int curX, int curY, char symbol):m_iSpeed(speed * 100),m_iDirection(direction),m_iCurX(curX),m_iCurY(curY),m_cSymbol(symbol) {}Person::~Person() {}bool Person::isOutOfMaze() {    bool ret = false;    if (m_iCurX == -1 || m_iCurY == -1 || m_iCurX == m_maze.getWidth() || m_iCurY == m_maze.getHeight()) {        ret = true;    }    return ret;}// 左手原则void Person::goNextStep() {    m_iPreX = m_iCurX;    m_iPreY = m_iCurY;    switch (m_iDirection) {    case UP:        if (m_maze.isRoad(m_iCurX, m_iCurY - 1)) {            m_iCurY--;            if (m_maze.isRoad(m_iCurX, m_iCurY) && !isOutOfMaze()) {                m_iDirection = LEFT;            }        } else {            m_iDirection = RIGHT;        }        break;    case DOWN:        if (m_maze.isRoad(m_iCurX, m_iCurY + 1)) {            m_iCurY++;            if (m_maze.isRoad(m_iCurX, m_iCurY) && !isOutOfMaze()) {                m_iDirection = RIGHT;            }        } else {            m_iDirection = LEFT;        }        break;    case LEFT:        if (m_maze.isRoad(m_iCurX - 1, m_iCurY)) {            m_iCurX--;            if (m_maze.isRoad(m_iCurX, m_iCurY) && !isOutOfMaze()) {                m_iDirection = DOWN;            }        } else {            m_iDirection = UP;        }        break;    case RIGHT:        if (m_maze.isRoad(m_iCurX + 1, m_iCurY)) {            m_iCurX++;            if (m_maze.isRoad(m_iCurX, m_iCurY) && !isOutOfMaze()) {                m_iDirection = UP;            }        } else {            m_iDirection = DOWN;        }        break;    default:        break;    }}void Person::start(Maze &maze) {    m_maze = maze;    while (!isOutOfMaze()) {        drawPerson();        goNextStep();    }    gotoxy(0, m_maze.getHeight());    cout << "走出迷宫" << endl;}void Person::gotoxy(int x, int y) {// windows.h    COORD cd;    cd.X = x;    cd.Y = y;    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);}void Person::drawPerson() {    gotoxy(m_iPreX, m_iPreY);    cout << ROAD;    gotoxy(m_iCurX, m_iCurY);    cout << m_cSymbol;    Sleep(m_iSpeed);}
0 0