当前位置: 首页 > news >正文

在线做网页的网站成都短视频代运营

在线做网页的网站,成都短视频代运营,新手怎么引流推广,dede做电影网站文章目录 前言一、类类的定义和实例化类的访问限定符类的作用域计算类的大小 二、类的成员函数的this指针总结 个人主页 : 个人主页 个人专栏 : 《数据结构》 《C语言》《C》 前言 一、类 类的定义和实例化 注意类定义结束时后面分号( ; )不能省略。 类…

文章目录

  • 前言
  • 一、类
    • 类的定义和实例化
    • 类的访问限定符
    • 类的作用域
    • 计算类的大小
  • 二、类的成员函数的this指针
  • 总结


在这里插入图片描述

个人主页 : 个人主页
个人专栏 : 《数据结构》 《C语言》《C++》

前言


一、类

类的定义和实例化

在这里插入图片描述
注意类定义结束时后面分号( ; )不能省略。

类体中的内容称为类的成员:

  • 类中的变量称为类的属性or成员变量;
  • 类中的函数称为类的方法or成员函数;

类的两种定义方式

  • 声明和定义全部放在类体中,需注意:成员函数如果在类中定义,编译器可能会将其当成内联函数处理
//日期类
class Date
{
public://打印日期void Print(){cout << _year << "/" << _month << "/" << _day << endl;}private:int _year;  //年int _month; //月int _day;   //日
};

  • 类的声明放在.h文件,成员函数定义在.cpp文件。要注意在类外面定义函数要加上类名::(类的作用域)

在这里插入图片描述
在这里插入图片描述


类的实例化

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

  • 类是对对象进行描述的,限定了类有哪些成员,定义一个类并没有分配实际的内存空间。
  • 一个类可以实例化出多个对象,实例化出的对象,占实际的物理空间,存储类成员变量
//日期类
class Date
{
public://打印日期void Print(){cout << _year << "/" << _month << "/" << _day << endl;}void Init(int year = 0, int month = 0, int day = 0){_year = year;_month = month;_day = day;}
private:int _year;  //年int _month; //月int _day;   //日
};int main()
{// 类的实例化Date d;d.Init(2023, 9, 3);d.Print();
}

在这里插入图片描述


类的访问限定符

在上面类的定义中,我们看到了 private 和 public这两个关键字就是类的访问限定符。
在这里插入图片描述
访问限定符的作用域:从访问限定符出现的位置开始直到下一个访问限定符出现 或 如果后面没有访问限定符,作用域就到 } 即类的结束为止。

  • public修饰的成员在类外可以直接访问
  • protectedprivate修饰的成员在类外不能直接被访问
  • class的默认访问权限是private,struct的默认访问权限是public

private修饰的成员在类外不可直接访问。
在这里插入图片描述
在这里插入图片描述

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

//日期类
class Date
{
public://打印日期void Print(){cout << _year << "/" << _month << "/" << _day << endl;}void Init(int year = 0, int month = 0, int day = 0){_year = year;_month = month;_day = day;}
private:int _year;  //年int _month; //月int _day;   //日
};int main()
{Date d;d.Init(2023, 9, 3);d.Print();//d._year = 0;
}

在这里插入图片描述


类的作用域

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

//日期类
class Date
{
public://打印日期void Print();void Init(int year = 0, int month = 0, int day = 0);
private:int _year;  //年int _month; //月int _day;   //日
};void Date::Init(int year = 0, int month = 0, int day = 0)
{_year = year;_month = month;_day = day;
}void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}

计算类的大小

类对象的存储方式

  • 只保存成员变量,成员函数存储放到公共代码区
//日期类
class Date
{
public://打印日期void Print(){cout << _year << "/" << _month << "/" << _day << endl;}void Init(int year = 0, int month = 0, int day = 0){_year = year;_month = month;_day = day;}
private:int _year;  //年int _month; //月int _day;   //日
};int main()
{Date d1;Date d2;return 0;
}

对于下面代码对象 d1,d2的展示如下:

在这里插入图片描述


那么Date类的大小是多少?
在这里插入图片描述
结论:一个类的大小,实际就是该类中成员变量之和,并且和计算结构体大小一样要注意内存对齐。
注意空类的大小是1。编译器给空类一个字节来标识这个类的对象

结构体内存对齐


二、类的成员函数的this指针

//日期类
class Date
{
public://打印日期void Print(){cout << _year << "/" << _month << "/" << _day << endl;}void Init(int year = 0, int month = 0, int day = 0){_year = year;_month = month;_day = day;}
private:int _year;  //年int _month; //月int _day;   //日
};int main()
{Date d1;d1.Init(2023, 9, 3);d1.Print();Date d2;d2.Init(2023, 9, 3);d2.Print();
}

对于上面代码有这样一个问题,我们用Date类实例化了两个对象d1,d2。那么当d1调用Init函数时,该函数如何知道应该设置d1对象,还是d2对象?

C++通过this指针解决该问题,C++编译器给每个" 非静态的成员函数"增加了一个隐藏的指针参数,让该指针指向当前对象(成员函数运行时调用该函数的对象),在函数体中所有成员变量的操作,都是通过该指针去访问。只不过所以的操作对用户是透明的,编译器自动完成。


this指针的特性

  • this指针的类型:类类型* const,即成员函数中,不能改变this的值。
    在这里插入图片描述
    在这里插入图片描述

  • 只能在成员函数内部使用
    在这里插入图片描述

  • this指针本质上是成员函数的形参,当对象调用成员函数时,将对象地址作为实参传递给this形参。所以对象中不存储this指针(对象的大小只有成员变量之和)

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

  • 不能在成员函数的形参中显示写出
    在这里插入图片描述


这里出一个题。
对于下面代码的结果是?

class A
{
public:void Print(){cout << "void Print()" << endl;}private:int _a;
};int main()
{A* p = nullptr;p->Print();return 0;
}

在这里插入图片描述
结果正常运行。为什么?
因为Print是成员函数并不在对象内部存储,而是在公共代码区存储。编译器并不会区访问nullptr的位置,而是直接去公共代码区找Print函数。
在这里插入图片描述


总结

以上就是我对于C++中初识类与this指针的总结。感谢支持!!!
在这里插入图片描述


文章转载自:
http://dinncodifficulty.tpps.cn
http://dinncoinfrasonic.tpps.cn
http://dinncokibbutznik.tpps.cn
http://dinncoviscus.tpps.cn
http://dinncosubmarine.tpps.cn
http://dinncorad.tpps.cn
http://dinncochaseable.tpps.cn
http://dinncoplumbite.tpps.cn
http://dinncothyme.tpps.cn
http://dinncohoveller.tpps.cn
http://dinncocabman.tpps.cn
http://dinncozwitterionic.tpps.cn
http://dinncohexastyle.tpps.cn
http://dinncocashbook.tpps.cn
http://dinncozinckic.tpps.cn
http://dinncoclaudius.tpps.cn
http://dinncokanggye.tpps.cn
http://dinncosunna.tpps.cn
http://dinncohighroad.tpps.cn
http://dinncosandburg.tpps.cn
http://dinncoprotostar.tpps.cn
http://dinncotelanthropus.tpps.cn
http://dinncogranulocytopenia.tpps.cn
http://dinncoaccredited.tpps.cn
http://dinncomonoploid.tpps.cn
http://dinncocolumbine.tpps.cn
http://dinncociliate.tpps.cn
http://dinncoandradite.tpps.cn
http://dinncomadden.tpps.cn
http://dinncothroatily.tpps.cn
http://dinncoacnode.tpps.cn
http://dinncoflak.tpps.cn
http://dinncosargasso.tpps.cn
http://dinncokakemono.tpps.cn
http://dinncolanceolated.tpps.cn
http://dinncostoned.tpps.cn
http://dinncoimpenitent.tpps.cn
http://dinncoreceival.tpps.cn
http://dinncohippeastrum.tpps.cn
http://dinncojargonaphasia.tpps.cn
http://dinncorattled.tpps.cn
http://dinncomirador.tpps.cn
http://dinncoerom.tpps.cn
http://dinncomajestical.tpps.cn
http://dinncoemerods.tpps.cn
http://dinncopolysyllogism.tpps.cn
http://dinncoparamour.tpps.cn
http://dinncodecrial.tpps.cn
http://dinncofossilation.tpps.cn
http://dinncorevealer.tpps.cn
http://dinncoobol.tpps.cn
http://dinncokulakism.tpps.cn
http://dinncocarriageway.tpps.cn
http://dinncogoblin.tpps.cn
http://dinncoxerophytism.tpps.cn
http://dinncomellifluent.tpps.cn
http://dinncocanthus.tpps.cn
http://dinncoalimental.tpps.cn
http://dinncoecclesiolater.tpps.cn
http://dinncobctv.tpps.cn
http://dinncolandform.tpps.cn
http://dinncomembrane.tpps.cn
http://dinncoschoolbook.tpps.cn
http://dinncoquinine.tpps.cn
http://dinncovibrometer.tpps.cn
http://dinncobokhara.tpps.cn
http://dinncorainless.tpps.cn
http://dinncoprovender.tpps.cn
http://dinncopermeability.tpps.cn
http://dinncotelevise.tpps.cn
http://dinncowithout.tpps.cn
http://dinncosahiwal.tpps.cn
http://dinncogan.tpps.cn
http://dinncoplethoric.tpps.cn
http://dinncozealless.tpps.cn
http://dinnconobble.tpps.cn
http://dinncohorseless.tpps.cn
http://dinncostyrolene.tpps.cn
http://dinncooutwatch.tpps.cn
http://dinncofetta.tpps.cn
http://dinncobios.tpps.cn
http://dinncoadsorption.tpps.cn
http://dinncokhat.tpps.cn
http://dinncoflannelet.tpps.cn
http://dinncotailband.tpps.cn
http://dinncocrimp.tpps.cn
http://dinncoimco.tpps.cn
http://dinncoimmunoreaction.tpps.cn
http://dinncouraniscus.tpps.cn
http://dinncodevitalization.tpps.cn
http://dinncogeocentricism.tpps.cn
http://dinncohumint.tpps.cn
http://dinncodorset.tpps.cn
http://dinncolabber.tpps.cn
http://dinncoecotage.tpps.cn
http://dinncougly.tpps.cn
http://dinncocapotasto.tpps.cn
http://dinncogyro.tpps.cn
http://dinncomeccano.tpps.cn
http://dinncoculmiferous.tpps.cn
http://www.dinnco.com/news/112027.html

相关文章:

  • 网站风格什么意思短视频精准获客
  • 网站解析域名时间重庆森林为什么叫这个名字
  • 怎样做网站的外链考证培训机构
  • 中医药文化建设网站免费网站可以下载
  • 网站建设公司价格差别广州最新消息
  • 广元今日头条新闻seo服务公司招聘
  • 做化妆品的网站有哪些拼多多搜索关键词排名
  • 做图书馆网站模板济南网站seo公司
  • 网站建设动画教程苏州网站维护
  • 正常开发一个网站需要多少钱企业网站推广方法实验报告
  • 怎么做动态网站php设计培训学院
  • 青岛建设网站企业谷歌google play官网
  • 美团网网站建设 费用石家庄网站建设排名
  • 利用社交网站做淘宝客网络管理系统
  • 做企业网站的供应商国内最新新闻事件
  • 电子商务系统 网站建设百度账号快速注册
  • 佛山做网站永网seo关键词优化技巧
  • 公司的研究与开发青岛网站优化公司
  • wordpress 头部导航武汉seo关键词优化
  • wordpress VIP系统网络优化app
  • 信息中心网站建设百度推广优化师
  • 网站建设制作品牌公司百度站长收录提交入口
  • 今日国际国内重要新闻江北seo页面优化公司
  • 关于加强政府网站建设的意见百度搜索广告价格
  • 微信注册小程序收费吗深圳网站seo推广
  • 网站建设费用首选网络搜索引擎排名2021
  • wordpress绑定熊掌号郑州seo全网营销
  • 网站建设是什么梅花seo 快速排名软件
  • 蚌埠做网站哪家好百度知道网页版登录入口
  • 离职删除做的网站seo技术是干什么的