C++ 定义一个类,类的成员变量没有指针

编程环境:win10,vs2017

#include <iostream>

 

代码01

namespace ds01

{

    //类定义

    struct DsSize

    {

         //没有明确写出构造和析构,编译器会使用默认构造、拷贝赋值、析构。

    };

 

    //测试程序

    void testDsSize()

    {

         DsSize s1;  //默认构造

         DsSize s2(s1);  //默认拷贝构造

         DsSize s3 = s2;  //默认拷贝赋值

 

         cout << "address s1 = " << &s1 << endl;

         cout << "address s2 = " << &s2 << endl;

         cout << "address s3 = " << &s3 << endl;

    }

}

 

int main()

{

    ds01::testDsSize();

    return 0;

}

输出

address s1 = 008FF8F7

address s2 = 008FF8EB

address s3 = 008FF8DF

创建三个对象,三个对象的地址不同,是三个不同的对象。

 

代码02

namespace ds02

{

    //类定义

    struct DsSize

    {       

    public:   //有成员变量

         int width;

         int height;

    };

 

    //测试程序

    void testDsSize1()

    {

         DsSize s1;

         //DsSize s2(s1);  // 错误  C4700    使用了未初始化的局部变量“s1”

         cout << "address s1 = " << &s1 << endl;

    }

 

    void testDsSize2()

    {

         DsSize s1;

         s1.width = 10; s1.height = 5;   //为s1的成员变量赋值

         DsSize s2(s1);  //s1不再是未初始化的,编译通过。

         cout << "address s1 = " << &s1 << endl;

         cout << "address s2 = " << &s2 << endl;

    }

}

ds02::testDsSize1() 的输出

address s1 = 010FFBAC

ds02::testDsSize2() 的输出

address s1 = 010FFBAC

address s2 = 010FFB9C

 

代码03

namespace ds03

{

    //类定义

    struct DsSize

    {

    public:

         int width = 1;

         int height = 1;

    };

 

    //测试程序

    void testDsSize1()

    {

         DsSize s1; 

         DsSize s2(s1);  //编译通过

         cout << "address s1 = " << &s1 << " width=" << s1.width << " height=" << s1.height << endl;

    }

}

ds03::testDsSize1() 的输出

address s1 = 0067F8F8 width=1 height=1

 

代码04

namespace ds04

{

    //类定义

    struct DsSize

    {

         DsSize() : width(0), height(0) {}   //在构造函数初始化成员数据

 

    public:

         int width = 1;

         int height = 1;

    };

 

    //测试程序

    void testDsSize1()

    {

         DsSize s1; 

         DsSize s2(s1);

 

         cout << "address s1 = " << &s1 << " width=" << s1.width << " height=" << s1.height << endl;

         cout << "address s2 = " << &s2 << " width=" << s2.width << " height=" << s2.height << endl;

    }

}

ds04::testDsSize1() 的输出

address s1 = 00F9FE08 width=0 height=0

address s2 = 00F9FDF8 width=0 height=0

s1 ,s2 成员数据初始化的结果是0,不是1.

 

代码05

namespace ds05

{

    struct DsSize

    {

    public:

         //构造

         DsSize() : width(0), height(0) {}

 

         //拷贝构造

         DsSize(const DsSize& size) : width(size.width), height(size.height) {}

 

         //拷贝赋值

         const DsSize& operator =(const DsSize& size)

         {

             if (this != &size)

             {

                  width = size.width;

                  height = size.height;

             }

             return *this;

         }

 

         //其他构造

         DsSize(const int&width, const int&height) :width(width), height(height) {}

         DsSize(const DsSize* size)

         {

             if (size == nullptr)

             {

                  width = 1;

                  height = 1;

             }

             else

             {

                  width = size->width;

                  height = size->height;

             }

         }

 

         //析构

         ~DsSize()

         {

             cout << "析构" << endl;

         }

 

    public:

         const int& GetWidth() const

         {

             return width;

         }

 

         const int& GetHeight() const

         {

             return height;

         }

 

    private:

         int width;

         int height;

    };

 

    //重载运算符 << ,不能在类的内部定义,只能放在类的外部定义。

    ostream& operator << (ostream& out, const DsSize& size)

    {

         out << "Address=0x" << &size << " Width=" << size.GetWidth() << " Height=" << size.GetHeight();

         return out;

    }

 

    //测试程序

    void testDsSize1()

    {

         DsSize s1;  //DsSize()

         DsSize s2(s1);  //DsSize(const DsSize& size)

         DsSize s3 = s2;  //const DsSize& operator =(const DsSize& size)

         DsSize s4(10, 5);  //DsSize(const int&width, const int&height)

         DsSize s5(nullptr);  //DsSize(const DsSize* size)

 

         cout << "s1 : " << s1 << endl;

         cout << "s2 : " << s2 << endl;

         cout << "s3 : " << s3 << endl;

         cout << "s4 : " << s4 << endl;

         cout << "s5 : " << s5 << endl;    

    }

}

 

ds05::testDsSize1() 的输出

s1 : Address=0x00D3FDB0 Width=0 Height=0

s2 : Address=0x00D3FDA0 Width=0 Height=0

s3 : Address=0x00D3FD90 Width=0 Height=0

s4 : Address=0x00D3FD80 Width=10 Height=5

s5 : Address=0x00D3FD70 Width=1 Height=1

析构

析构

析构

析构

析构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值