• c++类和对象上



    作者: sleepygod
    日期: 10-30
    也许 不负光阴就是最好的努力 而努力就是最好的自己

    微信图片_20221007212918

    类和对象上

    1.类的引入

    C++是基于面向对象的,关注的是对象,将一件事情拆分成不同的对象,靠对象之间的交互完成。

    C语言结构体中只能定义变量,在C++中,结构体内不仅可以定义变量,也可以定义函数。比如: 之前在数据结构初阶中,用C语言方式实现的栈,结构体中只能定义变量;现在以C++方式实现, 会发现struct中也可以定义函数。在C++中更喜欢用class来代替。

    下面构建一个日期类

    class Date
    {
    public:
    	Date(int year, int month, int day);
    	Date(const Date& d);
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    
    };
    
    Date::Date(int year, int month, int day)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    Date::Date(const Date& d)
    {
    	_year = d._year;
    	_month = d._month;
    	_day = d._day;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2.类的定义

    class className
    {
    // 类体:由成员函数和成员变量组成
    };  // 一定要注意后面的分号
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    class为定义类的关键字,ClassName为类的名字,{}中为类的主体,注意类定义结束时后面分号不能省略。

    类的两种声明方式:

    第一种声明和定义全部放在类体中,需注意:成员函数如果在类中定义,编译器可能会将其当成内 联函数处理。

    class Date
    {
    public:
    	Date(int year, int month, int day)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	Date(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    第二种类声明与成员函数分离,注意:成员函数名前需要加类名::

    class Date
    {
    public:
    	Date(int year, int month, int day);
    	
    	Date(const Date& d);
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    
    };
    
    Date::Date(int year, int month, int day)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    Date::Date(const Date& d)
    {
    	_year = d._year;
    	_month = d._month;
    	_day = d._day;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    一般情况下,更倾向于第二种写法.第二种写法当后期维护查看代码时,会比较轻松.

    3.类的封装

    1.访问限定符

    有public, protected,private三类

    public修饰的成员在类外可以直接被访问.

    protected,private修饰的成员在类外不可以直接被访问.

    m

    class的默认访问权限为private,struct为public.

    面向对象的三大特征:封装,继承,多态(当然不止这几个特征,因为这三个最出名).

    封装:将数据和操作数据的方法进行有机结合,隐藏对象的属性和实现细节,仅对外公开接口来 和对象进行交互.

    4.类的作用域与实例化

    类定义了一个新的作用域,类的所有成员都在类的作用域中。在类体外定义成员时,需要使用 :: 作用域操作符指明成员属于哪个类域.

    class Date
    {
    public:
    	Date(int year, int month, int day);
    	
    	Date(const Date& d);
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    
    };
    
    Date::Date(int year, int month, int day)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    Date::Date(const Date& d)
    {
    	_year = d._year;
    	_month = d._month;
    	_day = d._day;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    用类类型创建对象的过程,称为类的实例化

    如上面 Date A;即为实例化.

    5.类的大小

    类的存储方式:只保存成员变量,成员函数存放在公共的代码段.

    所以类的大小,实际就是该类的中"成员变量"之和,与结构体的大小计算相同,需要注意默认对齐数.

    当类为空类时,编译器会给空类分配一个字节来唯一标识这个类.

    6.this指针

    this指针类型: 类类型*const 即在函数中不能给this赋值,只能在成员函数中使用.

    this指针本质上是“成员函数”的形参,当对象调用成员函数时,将对象地址作为实参传递给 this形参。所以对象中不存储this指针.

    this指针是“成员函数”第一个隐含的指针形参,一般情况由编译器通过ecx寄存器自动传 递,不需要用户传递

    class Date
    {
    public:
    	Date(int year, int month, int day);
    	
    	Date(const Date& d);
    	void Print()
    	{
    		cout << _year;
    	}
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    
    };
    
    Date::Date(int year, int month, int day)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    Date::Date(const Date& d)
    {
    	_year = d._year;
    	_month = d._month;
    	_day = d._day;
    }
    
    
    
    
    
    int main()
    {
    
    	Date a(2001, 10, 20);
    	a.Print();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    对于上述代码解释 当调用成员函数Print()时,实际上是Print(Date *this);所以输出语句是 cout<_year;

    但是在一般情况下都不写,来简化代码. 注:this指针可以指向空.且this指针存储在栈区.

    01b5a5f231c7c866768410af8a2d993

  • 相关阅读:
    JavaScript框架的四个时代
    使用git-flow来帮助管理git代码
    web前端面试题
    探索Web Components
    Python基础入门——python中的时间包
    Spring实例化源码解析(一)
    【小程序源码】看成语猜古诗句好玩解闷小游戏
    数据结构与算法训练:第十三弹
    制造业企业设备管理常见的三个问题及对应的解决方案
    美团秋招笔试——算法岗
  • 原文地址:https://blog.csdn.net/gemgod/article/details/127606112