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

石家庄做网站推广最新wordpress教程

石家庄做网站推广,最新wordpress教程,企业信息服务规划与建设,网站的推广策略友元类的概念和使用 (1)将类A声明为B中的friend class后#xff0c;则A中所有成员函数都成为类B的友元函数了 (2)代码实战#xff1a;友元类的定义和使用友元类是单向的 (3)友元类是单向的#xff0c;代码实战验证 互为友元类 (1)2个类可以互为友元类#xff0c;代码实战…友元类的概念和使用 (1)将类A声明为B中的friend class后则A中所有成员函数都成为类B的友元函数了 (2)代码实战友元类的定义和使用友元类是单向的 (3)友元类是单向的代码实战验证 互为友元类 (1)2个类可以互为友元类代码实战验证 (2)互为友元类要注意互相引用的细节规则 (1)友元类其实就是批量制造友元函数 (2)友元类中所有全部成员都成为了友元函数相当于一次打了很多洞极大破坏了面向对象 (3)除非确实有必要否则建议按需定义友元函数尽量维护面向对象让代码更安全健壮 #include iostream #include stringclass Country; // Forward declaration of Country// External friend function declaration void disp_info_external(const Country ct);class InfoDisplayer {public:// Member function declarationvoid disp_info_member(const Country ct) const; };class InfoFriendClass {public:// Member function to display informationvoid display(const Country ct) const; };class Country {private:std::string privateName;protected:int protectedPopulation;public:std::string publicCapital;// ConstructorCountry(std::string name, int population, std::string capital): privateName(name),protectedPopulation(population),publicCapital(capital) {}// Friend function declarationfriend void disp_info_external(const Country ct);// friend void InfoDisplayer::disp_info_member(const Country ct);//error: no// declaration matches ‘void InfoDisplayer::disp_info_member(const Country)’friend class InfoFriendClass; // Friend class declaration };// Definition of the external friend function void disp_info_external(const Country ct) {std::cout disp_info_external Name: ct.privateName \n;std::cout disp_info_external Population: ct.protectedPopulation \n;std::cout disp_info_external Capital: ct.publicCapital \n; }// Definition of the member friend function void InfoDisplayer::disp_info_member(const Country ct) const {// std::cout disp_info_member Name: ct.privateName \n;// error: ‘int Country::protectedPopulation’ is protected within this context// std::cout disp_info_member Population: ct.protectedPopulation // \n; std::cout disp_info_member Capital: ct.publicCapital // \n; }// Definition of the friend class member function void InfoFriendClass::display(const Country ct) const {std::cout InfoFriendClass Name: ct.privateName \n;std::cout InfoFriendClass Population: ct.protectedPopulation \n;std::cout InfoFriendClass Capital: ct.publicCapital \n; }int test070401() {// Create an object of CountryCountry myCountry(Wonderland, 5000000, Magic City);// Display information using the external friend functiondisp_info_external(myCountry);// Create an object of InfoDisplayerInfoDisplayer displayer;// Display information using the member friend functiondisplayer.disp_info_member(myCountry);// Create an object of InfoFriendClassInfoFriendClass friendClass;// Display information using the friend class member functionfriendClass.display(myCountry);return 0; } #include iostream #include stringclass Vegetable; // Forward declaration of Vegetableclass Fruit {private:std::string name;protected:std::string color;public:// ConstructorFruit(std::string n, std::string c) : name(n), color(c) {}// Friend class declarationfriend class Vegetable;// Definition of the friend functionvoid displayVegetable(const Vegetable veg); };class Vegetable {private:std::string name;protected:std::string color;public:// ConstructorVegetable(std::string n, std::string c) : name(n), color(c) {}// Friend class declarationfriend class Fruit;// Member function declarationvoid displayFruit(const Fruit fruit) const {std::cout Vegetable accessing Fruits name: fruit.name \n;std::cout Vegetable accessing Fruits color: fruit.color \n;} };void Fruit::displayVegetable(const Vegetable veg) {std::cout Fruit accessing Vegetables name: veg.name \n;std::cout Fruit accessing Vegetables color: veg.color \n; }int test070402() {// Create objects of Fruit and VegetableFruit apple(Apple, Red);Vegetable spinach(Spinach, Green);apple.displayVegetable(spinach);// Display information using the member function of Vegetablespinach.displayFruit(apple);return 0; } 为什么会有友元函数 使用友元函数的优缺点 (1)缺点破坏了封装机制尽量不使用友元函数不得已才使用友元函数 (2)优点在实现类之间数据共享时减少系统开销提高效率。 使用友元函数的两种情况 (1)运算符重载的某些场合需要使用友元 (2)两个类要共享数据的时候 两个类如何共享数据 (1)类内的数据其实就是类的成员变量 (2)2个类共享数据方法1将共享数据访问权限设置为public。 (3)2个类共享数据方法2通过第三个专门封装数据的类和2个类中带参数的成员函数来传参共享 (4)2个类共享数据方法3通过友元函数打洞 友元函数和类的成员函数的区别 (1)成员函数有this指针而友元函数没有this指针。为什么因为友元只是朋友并不是类内“自家人” (2)友元函数是不能被继承的就像父亲的朋友未必是儿子的朋友。 (3)友元关系不具有传递性。类B是类A的友元类C是B的友元类C不一定是类A的友元要看类中是否有相应的声明 共有友元函数 (1)1个函数同时成为2个类的友元函数 (2)共有友元函数可以是外部函数也可以是某个第3个类的成员函数 (3)共有友元函数内可同时访问2个类的受保护成员间接将2个完全无关的类的数据打通了 总结 有元函数是单向的 两个类可以互为有元类、可以相互拥有有元方法 C编译器在寻找运算符函数时找的自己内部的函数有就用显示实现的函数没有的话就使用默认实现的有元函数是外部函数就不太编译器寻找范围内 学习记录侵权联系删除。 来源朱老师物联网大课堂
http://www.hkea.cn/news/14506705/

相关文章:

  • 招生平台网站开发公众号开发者
  • 怎么在百度上创建网站购物网站如何做
  • 备案网站主办者承诺书网站建设明薇通网络售后好
  • 网站开发人员应该用什么浏览器以背景做网站视频为
  • 本地扬中网站建设长沙公共资源交易电子服务平台
  • 盐城做网站的哪家公司好网站后台怎样批量上传
  • 网站建设 绵阳医疗网站跳出率
  • 网站 模板 安装app开发一般收费
  • 景安建网站网站建设选天祥
  • 计算机网站开发工作证wordpress 百度优化
  • 宁德市住房和城乡建设局网站打不开免费域名注册永久
  • 网站按钮特效抖音做我女朋友的网站
  • 全国十大网站建设公司咖搭姆少儿编程加盟
  • 汝州建设局网站苏州做网站公司排名
  • 设计平台网站13572074638网站建设
  • 宁波龙山建设有限公司网站开发公司管理规章制度
  • 百度网站如何优化排名wordpress single
  • 电商网站界面规范电子商务网站例
  • 北京网站建站系统平台网站开发文档范文
  • 阜宁做网站工作室深圳高端企业网站建设公司
  • 创建网站时间代码网站建设客户沟通
  • 开发网站公司怎么样wordpress繁体版下载
  • 网站做视频wordpress 兔
  • 海淀西北旺网站建设如何做视频网站 需要注意的地方
  • 内蒙古地区做推广网站做滋补品销售有什么网站
  • 企业网站如何设计合肥网站公司哪家好
  • 网站重新建设的通知定安免费建站公司
  • 建房子找哪个网站设计下一页360
  • 学做网站从什么开始怎么设置网站支付功能
  • 网站建设有前景吗如何创建自己的网站链接