为了账号安全,请及时绑定邮箱和手机立即绑定
  • 好好学习
    查看全部
    3 采集 收起 来源:C++封装概述

    2015-06-16

  • 深拷贝实例

    class Array { public: Array(int count); Array(const Array &arr); ~Array(); void setCount(int count); int getCount(); void printAddr(); void printArr(); private: int m_iCount; int *m_pArr; }; #include"Array.h" #include <iostream> 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(const Array &arr) { 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; } Array::~Array() { delete []m_pArr; m_pArr = NULL; cout <<"~Array" <<endl; } void Array::setCount(int count) { m_iCount = count; } int Array::getCount() { return m_iCount; } void Array::printAddr() { cout <<"m_pArr=" <<m_pArr <<endl; } void Array::printArr() { for(int i=0;i<m_iCount;i++) { cout << m_pArr[i] <<endl; } }


    查看全部

  • 码上大佬的代码,致敬!

    #include <iostream>

    #include <Windows.h>

    #include <string>

    #include <process.h>

    #include <Windows.h>


    using namespace std;


    /* https://zhidao.baidu.com/question/1755742141667975828.html 

    设定初始面向 East ( 如果是右手抹墙则是 West)


    1如果当前格为第一排,则说明当前格为终点,结束。

    2根据当前格的数据,找到下一步需要面向的方向, 方法是,如果当前方向上有墙,则顺时针(右手抹墙则逆时针)转身,重复这一步骤直到面向的方向上可以行走

    3沿当前方向走一步

    4逆时针(右手抹墙则顺时针)转身一次,使当前面对方向为第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]) { //构造地图

    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){} //可以设置人的标识符

    //横纵坐标

    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;

    //单向寻路原则  右手抹墙,初始朝向为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;

    }


    //确定方向 如果方向上有墙就逆时针转一下

    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;

    }

    }

    //移动一格  后顺时针调转方向

    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;

    }



    查看全部
    4 采集 收起 来源:开篇案例

    2019-09-30

  • 如果坐标类有一个默认构造函数(即不带参数的构造函数),那么在实例化线段对象的时候,不使用初始化列表。 如果坐标类要求必须有参数传入,那么在实例化线段类的时候,必须用初始化列表讲相应的值传递给坐标类 实例化Line时,要先实例化m_coorA和m_coorB,但是这两个对象没有默认构造函数,所以要用初始化列表。(初始化列表执行 在构造函数之前!!!) 如果Coordimate类有默认构造函数,也就是不带参数的构造函数,那么Line类就可以不使用初始化列表。如果只有带参数的构造函数,那么必须用初始化列表

    查看全部
  • Array Array::printInfo(){   //要求返回值为一个对象
        return *this;     //this是一个指针,*this代表一个对象
    }

    *this返回的对象是另外的一个对象(不是Array实例化的arr1),一个临时对象,要想让返回的对象就是arr1,需要加【引用】的符号【&】

    Array& Array::printInfo(){
        return *this;
    }

    在main中,如果用了Array&,则可以通过以下方法改变len的值

    Array arr1(10);
    arr1.printInfo().setLen(5); //两个点连用,可以把两个方法串起来

    或者用同样的Array&方法改造setLen(),则可以:

    Array arr1(10);
    arr1.printInfo().setLen(5).printInfo();//第一个printInfo打印出10,第二个打印出5

    【还可以通过指针来声明】:

    Array* Array::printInfo(){
        return this;  //类型已经是指针,此处用this,不加星号
    }

    【用Array* 时,在main中应该:】

    Array arr1(10);
    arr1.printInfo()->setLen(5)->printInfo();//指针用“->”符号而不用“.”

    【this指针的地址=实例化后的arr1的地址:】

    cout<<this<<" "<<&arr1<<endl;  //打印出来的这两个值完全相同


    查看全部
  • 深拷贝实例

    class Array { public: Array(int count); Array(const Array &arr); ~Array(); void setCount(int count); int getCount(); void printAddr(); void printArr(); private: int m_iCount; int *m_pArr; }; #include"Array.h" #include <iostream> 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(const Array &arr) { 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; } Array::~Array() { delete []m_pArr; m_pArr = NULL; cout <<"~Array" <<endl; } void Array::setCount(int count) { m_iCount = count; } int Array::getCount() { return m_iCount; } void Array::printAddr() { cout <<"m_pArr=" <<m_pArr <<endl; } void Array::printArr() { for(int i=0;i<m_iCount;i++) { cout << m_pArr[i] <<endl; } }

    查看全部
  • 因为初始化列表会比构造函数先执行,再因为Coordinate是Line的对象成员,会优先于Line执行构造函数,所以如果不把Coordinate的那两个对象放在初始化列表中进行初始化,将会导致Coordinnate的对象使用默认构造函数进行初始化,然后你又没有写默认的构造函数,所以会报错,不过如果你写了默认构造函数,会导致多出来两个对象。
    查看全部
  • 栈中内存被销毁是在整个main函数执行完后销毁; 而堆中由指针P所指向的内存是可手动控制何时销毁(利用delete )可通过此来显示析构函数的存在;
    查看全部
  • 1.对象指针:类名 * 指针名 = new 类名 2.C语言的malloc与C++的new都具有申请新内存空间的作用,但是new会调用对象的构造函数,而malloc不会调用 3. C++在new时的初始化的规律可能为:对于有构造函数的类,不论有没有括号,都用构造函数进行初始化;如果没有构造函数,则不加括号的new只分配内存空间,不进行内存的初始化,而加了括号的new会在分配内存的同时初始化为0。
    查看全部
    3 采集 收起 来源:[C++]对象指针

    2017-03-16

  • 关于 深拷贝与浅拷贝需要知道的基本概念和知识: (1)什么时候用到拷贝函数? a.一个对象以值传递的方式传入函数体; b.一个对象以值传递的方式从函数返回; c.一个对象需要通过另外一个对象进行初始化。 如果在类中没有显式地声明一个拷贝构造函数,那么,编译器将会自动生成一个默认的拷贝构造函数,该构造函数完成对象之间的位拷贝。位拷贝又称浅拷贝 如果一个类拥有资源,当这个类的对象发生复制过程的时候,资源重新分配,这个过程就是深拷贝
    查看全部
  • 作为对象成员来说,用sizeof这个对象的话,他应该是里面所有对象的体积的总和;指针则不同一个指针在32位的编译器中只占四个基本内存单元,当在对象成员指针中用sizeof时只能打印出指针所占的内存单元,而不是指针指向对象的内存单元。 对象成员:对象成为另一个类的数据成员。 对象成员指针:对象的指针成为另一个类的数据成员。
    查看全部
  • 1. 常对象只能调用常成员函数。 2. 普通对象可以调用全部成员函数。 3. 当对一个对象调用成员函数时,编译程序先将对象的地址赋给this指针,然后调用成员函数,每次成员函数存取数据成员时,由隐含使用this指针。 4. 当一个成员函数被调用时,自动向它传递一个隐含的参数,该参数是一个指向这个成员函数所在的对象的指针。 5. 在C++中,this指针被隐含地声明为: X *const this,这意味着不能给this 指针赋值;    在X类的const成员函数中,this指针的类型为:const X* const, 这说明this指针所指向的这种对象是不可修改的(即不能对这种对象的数据成员进行赋值操作); 6. 由于this并不是一个常规变量,所以,不能取得this的地址。 由于a是const对象,所以a只能调用类A中的常成员函数。 那么为什么会提示:“不能将this指针.....”的语句呢 因为对于c++的成员函数(当然不是静态成员函数),都会含有一个隐藏的参数,对于上例A中的int GetValue()函数,在编译后会变成: int GetValue(A * const this);  //不能修改this变量,但可以修改this指向的内容,即:this是常量指针。 而对于int GetValue()const ,编译后是: int GetValue(const A* const this);  只所以this指针是const类型,从编译后的结果看就很清楚了, 因为a是const,所以其this指针就对应: const A* const this ; 而print函数被编译出来后对应的是void print(A* const this); 在进行参数匹配时, 所以就会提示 “不能将“this”指针从“const A .." this指针的出现就解释了,用哪一个对象的数据成员。通常情况下,this指针是隐含存在的,也可以将其显示的表示出来(即如上例中的 this->mValue。不过this指针只能在类中使用) 还有就是  this指针是一个const指针;
    查看全部
  • class Array { public: Array(int count); Array(const Array &arr); ~Array(); void setCount(int count); int getCount(); void printAddr(); void printArr(); private: int m_iCount; int *m_pArr; }; #include"Array.h" #include <iostream> 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(const Array &arr) { 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; } Array::~Array() { delete []m_pArr; m_pArr = NULL; cout <<"~Array" <<endl; } void Array::setCount(int count) { m_iCount = count; } int Array::getCount() { return m_iCount; } void Array::printAddr() { cout <<"m_pArr=" <<m_pArr <<endl; } void Array::printArr() { for(int i=0;i<m_iCount;i++) { cout << m_pArr[i] <<endl; } }
    查看全部
  • #include "Line.h" #include <iostream> using namespace std; Line::Line() { cout <<"Line()" <<endl; } Line::~Line() { cout <<"~Line" <<endl; } void Line::setA(int x,int y) { m_coorA.setX(x); m_coorA.setY(y); } void Line::setB(int x,int y) { m_coorB.setX(x); m_coorB.setY(y); } void Line::printInfo() { cout << "(" << m_coorA.getX() <<"," << m_coorA.getY() <<")"<<endl; cout << "(" << m_coorB.getX() <<"," << m_coorB.getY() <<")"<<endl; } #include <iostream> #include <stdlib.h> #include "Line.h" using namespace std; int main(void) { Line *p = new Line(); delete p; p = NULL; system("pause"); return 0; }
    查看全部
  • 我不是笨,而是太善良
    查看全部
    2 采集 收起 来源:[C++]对象数组

    2016-03-26

举报

0/150
提交
取消
课程须知
本课程是C++初级课程 需要掌握C++语言基础语法 如果不太熟悉的话,可以观看: 《C++远征之起航篇》 《C++远征之离港篇》 《C++远征之封装篇(上)》
老师告诉你能学到什么?
1、对象数组的定义和使用 2、对象成员的定义和使用 3、深拷贝和浅拷贝 4、对象指针、对象引用的定义和使用 5、常对象指针、常对象引用、常成员函数的定义和使用

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!