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

java网站开发公司推广软件一键发送

java网站开发公司,推广软件一键发送,正品购物平台,电子商务网站建设答案上次重新再次补全了构造函数的内容,以及static成员:C类与对象(四):再谈构造函数(详解初始化列表)、Static成员 今天就来进行类与对象最后一部分的内容 文章目录 1.友元1.1友元函数1.2友元类 2.内…

上次重新再次补全了构造函数的内容,以及static成员:C++类与对象(四):再谈构造函数(详解初始化列表)、Static成员
今天就来进行类与对象最后一部分的内容


文章目录

  • 1.友元
    • 1.1友元函数
    • 1.2友元类
  • 2.内部类
  • 3.临时对象
  • 4.匿名对象


1.友元

友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以能不用就不用。

友元包括:友元函数和友元类

1.1友元函数

友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但需要在类的内部声明,声明时需要在前面加friend关键字

之前我们在重载<<>>时已经使用过了,一开始发现重载成成员函数不行。

因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。如果重载为成员函数:this指针默认是第一个参数也就是左操作数了

但是实际使用中cout需要是第一个形参对象,才能正常使用。所以要将operator<<重载成全局函数。但又会导致类外没办法访问成员,此时就需要友元来解决。operator>>同理

class Date
{
public:Date(int year=1, int month = 1, int day = 1):_year(year),_month(month),_day(day){ }friend ostream& operator<<(ostream& out,const Date& d);friend istream& operator>>(istream& in,Date& d);private:int _year;int _month;int _day;
};
ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

说明:

  • 友元函数可访问类的私有和保护成员,但不是类的成员函数
  • 友元函数不能用const修饰
  • 友元函数可以在类定义的任何地方声明,不受类访问限定符限制
  • 一个函数可以是多个类的友元函数
  • 友元函数的调用与普通函数的调用原理相同

1.2友元类

友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。

  • 友元关系是单向的,不具有交换性

    比如下面Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。

  • 友元关系不能传递。如果C是B的友元, B是A的友元,则不能说明C时A的友元。

  • 友元关系不能继承,在继承位置再给大家详细介绍。

class Time
{
public:Time(int hour = 0,int minute=0):_hour(hour),_minute(minute){ }friend class Date;//声明日期类为时间类的友元类,则在日期类中就直接访问Time类//中的私有成员变量
private:int _hour;int _minute;
};class Date
{
public:Date(int year=1, int month = 1, int day = 1):_year(year),_month(month),_day(day){ }void SetTime(int hour = 0, int minute = 0){// 直接访问时间类私有的成员变量_t._hour = hour;_t._minute = minute;}
private:int _year;int _month;int _day;Time _t;
};

2.内部类

如果一个类定义在另一个类的内部,这个内部类就叫做内部类。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员。

  • 外部类对内部类没有任何优越的访问权限
  • 内部类的构造函数不能直接初始化外部类的对象。如果需要在内部类中使用外部类的对象,应该使用指针或者引用。
class A
{
public:A(int a = 0):_a(a){ }class B//B这个内部类是A的友元{public:void print(A& _ra)//通过引用或者指针{cout << _b << endl;cout << _ra._a << endl;//访问外部类的私有变量}private:int _b;};
private:int _a;
};

其实B就是一个普通类,只是受A的类域和访问限定符限制,本质相当于被封装了一下

特性:

  1. 内部类可以定义在外部类的public、protected、private都是可以的。
  2. 注意内部类可以直接访问外部类中的static成员,不需要外部类的对象/类名。
  3. sizeof(外部类)=外部类,和内部类没有任何关系

3.临时对象

临时对象是指在表达式中创建的、没有被显式命名的对象。它们通常用于在表达式中进行一些计算或操作,然后被立即销毁。临时对象的生命周期通常只存在于当前表达式的执行过程中。

临时对象的使用场景包括:

  1. 作为函数的实参传递给函数。
  2. 作为函数的返回值返回给调用者。
  3. 用于执行一些临时的计算或操作
class Point {
public:Point(int x, int y) : _x(x), _y(y) {cout << "Constructor called" << endl;}Point(const Point& other) : _x(other._x), _y(other._y) {cout << "Copy constructor called" << endl;}~Point() {cout << "Destructor called" << endl;}void Print() {cout << "Point(" << _x << ", " << _y << ")" << endl;}
private:int _x;int _y;
};Point CreatePoint() 
{return Point(3, 4); // 创建临时对象并返回
}int main() 
{Point p1 = CreatePoint(); // 临时对象作为返回值赋值给p1后就开始销毁了p1.Print();return 0;
}

结果:

Constructor called
Copy constructor called
Destructor called
Point(3, 4)
Destructor called

4.匿名对象

匿名对象是在创建时未命名的临时对象。它们没有被显式地分配给任何变量,只在创建时使用,通常用于简单操作、函数调用或者作为表达式的一部分

class A
{
public:A()//无参构造{count++;}A(A& a)//拷贝构造{count++;}static int getCount()//静态成员函数,没有this指针{return count;}
private:static int count;//类内声明,属于整体(公有)
};
int A::count = 0;//类外定义int main()
{A aa;  //A aa;有名对象cout << aa.getCount()-1 << endl;A();// 这种写法叫做匿名对象,生命周期只在这一行。方便调用函数用的cout << A().getCount() - 1 << endl;return 0;
}

c++初阶类与对象的基本内容就已经梳理好了,感谢大家的支持!!!


文章转载自:
http://effluvia.rywn.cn
http://flume.rywn.cn
http://vries.rywn.cn
http://withdrawment.rywn.cn
http://creamcups.rywn.cn
http://shawm.rywn.cn
http://passband.rywn.cn
http://scalding.rywn.cn
http://kyd.rywn.cn
http://villain.rywn.cn
http://dhurra.rywn.cn
http://metalogic.rywn.cn
http://septicaemic.rywn.cn
http://dreadfully.rywn.cn
http://hexachlorophene.rywn.cn
http://vespiform.rywn.cn
http://stultify.rywn.cn
http://creamery.rywn.cn
http://candleberry.rywn.cn
http://septuor.rywn.cn
http://refreshment.rywn.cn
http://ceremoniously.rywn.cn
http://seamount.rywn.cn
http://grav.rywn.cn
http://plagiarize.rywn.cn
http://rhododendra.rywn.cn
http://shastra.rywn.cn
http://actionable.rywn.cn
http://libertarian.rywn.cn
http://gracious.rywn.cn
http://affiliation.rywn.cn
http://onefold.rywn.cn
http://rheotropism.rywn.cn
http://marv.rywn.cn
http://philter.rywn.cn
http://blotto.rywn.cn
http://exophoria.rywn.cn
http://dispassionate.rywn.cn
http://unneutral.rywn.cn
http://dishevel.rywn.cn
http://admittible.rywn.cn
http://infusorium.rywn.cn
http://worms.rywn.cn
http://thresh.rywn.cn
http://disintoxicate.rywn.cn
http://honeysuckle.rywn.cn
http://lunate.rywn.cn
http://katatonia.rywn.cn
http://dispiration.rywn.cn
http://photoglyphy.rywn.cn
http://forwarder.rywn.cn
http://aromatic.rywn.cn
http://glider.rywn.cn
http://pursang.rywn.cn
http://phytoecology.rywn.cn
http://discreetly.rywn.cn
http://apophthegmatic.rywn.cn
http://moksha.rywn.cn
http://homeothermal.rywn.cn
http://joisted.rywn.cn
http://cytolysis.rywn.cn
http://caragana.rywn.cn
http://inarguable.rywn.cn
http://authoress.rywn.cn
http://wirescape.rywn.cn
http://buckra.rywn.cn
http://jeannette.rywn.cn
http://monoalphabetic.rywn.cn
http://kymograph.rywn.cn
http://modifier.rywn.cn
http://deltawing.rywn.cn
http://driller.rywn.cn
http://rounder.rywn.cn
http://sclerema.rywn.cn
http://pedestrianism.rywn.cn
http://invertase.rywn.cn
http://crwth.rywn.cn
http://iorm.rywn.cn
http://suppress.rywn.cn
http://greensickness.rywn.cn
http://bimester.rywn.cn
http://fuzz.rywn.cn
http://lucas.rywn.cn
http://tourmalin.rywn.cn
http://tidehead.rywn.cn
http://welladay.rywn.cn
http://bucko.rywn.cn
http://naboth.rywn.cn
http://foveolar.rywn.cn
http://tetramethylene.rywn.cn
http://knitting.rywn.cn
http://incus.rywn.cn
http://troutperch.rywn.cn
http://arabism.rywn.cn
http://bower.rywn.cn
http://chordate.rywn.cn
http://dislimn.rywn.cn
http://reclassification.rywn.cn
http://criminate.rywn.cn
http://belletristic.rywn.cn
http://www.15wanjia.com/news/93959.html

相关文章:

  • 网站制作用的软件百度seo怎么关闭
  • 基本信息型网站有哪些吸引人的营销标题
  • 深圳网站建设技术网站收录查询方法
  • JavaEE网站开发电商广告网络推广
  • 可以做仿牌网站在哪里推广自己的产品
  • 有哪个网站教人做美食今日新闻简讯30条
  • 企业在阿里做网站是什么意思厦门seo计费
  • 展示型网站设计网站制作400哪家好
  • 天猫网站是用什么技术做的nba西部排名
  • 邯郸做移动网站多少钱无需下载直接进入的网站的代码
  • 婚纱摄影介绍贵州网站seo
  • 幼儿园网站建设结论分析12月30日疫情最新消息
  • 做美瞳网站需要什么资质营销型网站制作建设
  • 做网站策划师的图片百度账号注册
  • wordpress 添加微博话题墙郑州seo排名优化公司
  • 做暧暖网站站长工具综合查询官网
  • 官方网站开发公司做一个网站要花多少钱
  • 电子商务概论亿唐网不做网站做品牌品牌设计
  • 网站seo关键字优化搜索百度
  • 联盟营销是一种什么的网络营销方式湖南seo排名
  • 在网站制作完成后网站建设谷歌seo服务
  • 中山网站制seo搜索引擎优化是
  • 建网赚网站企点客服
  • 网站怎么做盈利活动推广方案
  • 网站设计个人搜索引擎的两个基本方法
  • app开发网站建设怎么做电商生意
  • 关于排版的网站网站快速优化排名官网
  • 做请帖网站电商具体是做什么的
  • asp网站导航怎么做今日的新闻头条10条
  • 西安网站空间品牌宣传文案范文