热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

类别继承程序代码再用

c继承机制:代码#includeusingnamespacestd;classBase{private:intpri_att;voidpri_show(){co

c++继承机制:

代码

#include
using namespace std;
class Base
{
   private:
   int pri_att;
   void pri_show()
   {
     cout<<"Base ::pri_show() is called!"<    }
   protected:
   int pro_stt;
   void pro_show()
   {
   cout<<"Base::pro_show() is called!"<   }
   public:
    Base()
   :pri_att(1);pro_att(2);pub_att(3){}
   int pub_att;
  void pub_show()
  {
   cout<<"Base::pub_show() is called !"<  }
}

class Derived:public Base &#xff0f;&#xff0f;定义&#xff21;&#xff3f;&#xff24;&#xff45;&#xff52;&#xff49;&#xff56;&#xff45;&#xff44;以&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;继承&#xff22;&#xff41;&#xff53;&#xff45;&#xff0c;
{  public: &#xff0f;&#xff0f;
  void call_fun() &#xff0f;&#xff0f;
  void show() &#xff0f;&#xff0f;
}; &#xff0f;&#xff0f;

&#xff22;&#xff41;&#xff53;&#xff45;类别的成员&#xff22;&#xff41;&#xff53;&#xff45;类别的存取权限&#xff24;&#xff45;&#xff52;&#xff49;&#xff56;&#xff45;&#xff44;类别&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;继承后存取权限的等级
&#xff50;&#xff55;&#xff42;&#xff3f;&#xff41;&#xff54;&#xff54;&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;
&#xff50;&#xff52;&#xff4f;&#xff3f;&#xff41;&#xff54;&#xff54;&#xff50;&#xff52;&#xff4f;&#xff54;&#xff45;&#xff43;&#xff54;&#xff45;&#xff44;&#xff50;&#xff52;&#xff4f;&#xff54;&#xff45;&#xff43;&#xff54;&#xff45;&#xff44;
&#xff50;&#xff52;&#xff49;&#xff3f;&#xff41;&#xff54;&#xff54;&#xff50;&#xff52;&#xff49;&#xff56;&#xff41;&#xff54;&#xff45;隐藏
&#xff50;&#xff55;&#xff42;&#xff3f;&#xff53;&#xff48;&#xff4f;&#xff57;&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;
&#xff50;&#xff52;&#xff4f;&#xff3f;&#xff53;&#xff48;&#xff4f;&#xff57;&#xff50;&#xff52;&#xff4f;&#xff54;&#xff45;&#xff43;&#xff54;&#xff45;&#xff44;&#xff50;&#xff52;&#xff4f;&#xff54;&#xff45;&#xff43;&#xff54;
&#xff50;&#xff52;&#xff49;&#xff3f;&#xff53;&#xff48;&#xff4f;&#xff57;&#xff50;&#xff54;&#xff49;&#xff56;&#xff41;&#xff54;&#xff45;隐藏

 

 

void Derived::call_fun() &#xff0f;&#xff0f;
{
cout<cout<<"Derived ::call_fun is called"<pub_show();
pro_show();

&#xff0f;&#xff0f;&#xff50;&#xff52;&#xff49;&#xff3f;&#xff53;&#xff48;&#xff4f;&#xff57;&#xff08;&#xff09;&#xff1b;
}
void Deeived::show()
{
cout<cout<<"Derived ::show() is called!"<cout<<"Base::pub_att&#61;"<cout<<"Base::pro_att&#61;"<&#xff0f;&#xff0f;cout<<"Base::pri_att&#61;"<
}

int main()
{
  Derived A_Derived;
  cout<<"Accessing Derived&#39;s data members"
     <<"inherited form Base..."<  cout<<"Derived::pub_att&#61;"<  &#xff0f;&#xff0f;cout<<"Derived::pro_att&#61;"<  cout<  cout<<"Call Derived&#39;s members funcitons"<<"inherited form Base.."<  A_Derived.pub_show();
  &#xff0f;&#xff0f;A_Derived.pro_show();
  &#xff0f;&#xff0f;A_Derived.pri_show();

  A_Derived.call_show();
  A_Derived.show();
return 0;
}

[root&#64;localhost code]# g&#43;&#43; public_inh.cpp
public_inh.cpp:19: function body for constructor missing
public_inh.cpp:19: invalid data member initialization
public_inh.cpp:19: (use &#96;&#61;&#39; to initialize static data members)
public_inh.cpp:19: ISO C&#43;&#43; forbids declaration of &#96;pro_att&#39; with no type
public_inh.cpp:19: ISO C&#43;&#43; forbids declaration of &#96;pub_att&#39; with no type
public_inh.cpp:19: syntax error before &#96;{&#39; token
public_inh.cpp:19: missing &#39;;&#39; before right brace
public_inh.cpp:20: semicolon missing after declaration of &#96;Base&#39;
public_inh.cpp: In constructor &#96;Base::Base()&#39;:
public_inh.cpp:19: parse error before &#96;int&#39;
public_inh.cpp: At global scope:
public_inh.cpp:20: extraneous &#96;int&#39; ignored
public_inh.cpp:20: conflicting types for &#96;Base pub_att&#39;
public_inh.cpp:19: previous declaration as &#96;int pub_att&#39;
public_inh.cpp:25: parse error before &#96;}&#39; token
public_inh.cpp:29: parse error before &#96;void&#39;
public_inh.cpp:30: missing &#39;;&#39; before right brace
public_inh.cpp:38: syntax error before &#96;::&#39; token
public_inh.cpp:41: syntax error before &#96;<<&#39; token
public_inh.cpp:42: syntax error before &#96;<<&#39; token
public_inh.cpp:43: syntax error before &#96;<<&#39; token
public_inh.cpp:44: syntax error before &#96;<<&#39; token
public_inh.cpp: In function &#96;int main()&#39;:
public_inh.cpp:53: &#96;class Derived&#39; has no member named &#96;pub_att&#39;
public_inh.cpp:58: no matching function for call to &#96;Derived::pub_show()&#39;
public_inh.cpp:14: &#96;void Base::pro_show()&#39; is protected
public_inh.cpp:59: within this context
public_inh.cpp:8: &#96;void Base::pri_show()&#39; is private
public_inh.cpp:60: within this context
public_inh.cpp:61: no matching function for call to &#96;Derived::call_show()&#39;
public_inh.cpp:62: no matching function for call to &#96;Derived::show()&#39;

根据以上报错&#xff0c;

一一分析排查&#xff1a;

:pri_att(1),pro_att(2),pub_att(3){}

主要还是大部分在细节上&#xff0c;非技术性错误&#xff0e;

经过修改在编译&#xff1a;

public_inh.cpp: In constructor &#96;Base::Base()&#39;:
public_inh.cpp:19: class &#96;Base&#39; does not have any field named &#96;pro_att&#39;
public_inh.cpp: In member function &#96;void Derived::show()&#39;:
public_inh.cpp:43: &#96;pro_att&#39; undeclared (first use this function)
public_inh.cpp:43: (Each undeclared identifier is reported only once for each
   function it appears in.)
public_inh.cpp:6: &#96;int Base::pri_att&#39; is private
public_inh.cpp:44: within this context
public_inh.cpp: In function &#96;int main()&#39;:
public_inh.cpp:54: &#96;class Derived&#39; has no member named &#96;pro_att&#39;
public_inh.cpp:14: &#96;void Base::pro_show()&#39; is protected
public_inh.cpp:59: within this context
public_inh.cpp:8: &#96;void Base::pri_show()&#39; is private
public_inh.cpp:60: within this context
public_inh.cpp:61: no matching function for call to &#96;Derived::call_show()&#39;

再修改&#xff0c;编译&#xff1a;

[root&#64;localhost code]#  g&#43;&#43; public_inh.cpp
public_inh.cpp: In member function &#96;void Derived::show()&#39;:
public_inh.cpp:6: &#96;int Base::pri_att&#39; is private
public_inh.cpp:44: within this context
public_inh.cpp: In function &#96;int main()&#39;:
public_inh.cpp:12: &#96;int Base::pro_att&#39; is protected
public_inh.cpp:54: within this context
public_inh.cpp:14: &#96;void Base::pro_show()&#39; is protected
public_inh.cpp:59: within this context
public_inh.cpp:8: &#96;void Base::pri_show()&#39; is private
public_inh.cpp:60: within this context

这就很好证实了在&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;继承中&#xff50;&#xff52;&#xff49;&#xff3f;&#xff41;&#xff54;&#xff54;的不可见性

再次编译

出错情况&#xff1a;

[root&#64;localhost code]# g&#43;&#43; public_inh.cpp
public_inh.cpp: In function &#96;int main()&#39;:
public_inh.cpp:12: &#96;int Base::pro_att&#39; is protected
public_inh.cpp:54: within this context

 

[root&#64;localhost code]# g&#43;&#43; public_inh.cpp
[root&#64;localhost code]# g&#43;&#43; -o public_inh.cpp public_inh.out
g&#43;&#43;: public_inh.out: No such file or directory
g&#43;&#43;: no input files

但是&#xff1a;

[root&#64;localhost code]# g&#43;&#43;  public_inh.cpp -o public_inh.out 成功编译
[root&#64;localhost code]#

结果如下&#xff1a;

[root&#64;localhost code]# g&#43;&#43;  public_inh.cpp -o public_inh.out
[root&#64;localhost code]# ./public_inh.out
Accessing Derived&#39;s data membersinherited form Base...
Derived::pub_att&#61;3
Call Derived&#39;s members funcitonsinherited form Base..
Base::pub_show() is called !
Derived ::call_fun is called
Base::pub_show() is called !
Base::pro_show() is called!
Derived ::show() is called!
Base::pub_att&#61;3
Base::pro_att&#61;2

本程序测试的特点&#xff1a;

&#xff22;&#xff41;&#xff53;&#xff45; 类别成员&#xff22;&#xff41;&#xff53;&#xff45;类别存取权限的等级&#xff24;&#xff45;&#xff52;&#xff49;&#xff56;&#xff45;类别&#xff50;&#xff55;&#xff42;&#xff4c;&#xff49;&#xff43;继承后存取权限的等级在类别的外程序可否通过&#xff24;&#xff45;&#xff52;&#xff49;&#xff56;&#xff45;&#xff44;类别的对象存取
&#xff50;&#xff55;&#xff42;&#xff3f;&#xff41;&#xff54;&#xff54;   
&#xff50;&#xff52;&#xff4f;&#xff3f;&#xff41;&#xff54;&#xff54;   
&#xff50;&#xff52;&#xff49;&#xff3f;&#xff41;&#xff54;&#xff54;   
&#xff50;&#xff55;&#xff42;&#xff3f;&#xff53;&#xff48;&#xff4f;&#xff57;   
&#xff50;&#xff52;&#xff4f;&#xff3f;&#xff53;&#xff48;&#xff4f;&#xff57;   
    

 

 

代码&#xff1a;

#include
#include
using namespace std;
class library_object
{
  protected:
    char name[30];
    long index;
  public:
    void set_data(const char *i_name)
     {
       strcpy(name,i_name);
       index &#61;1;
      }
};
class Book :public library_object
{
  private:
  bool on_shelf;
  public :
  void show_data()
{
  cout<<"name:"<

  cout<<"index:"<  if(on_shelf&#61;&#61;true)
     cout<<"on shelf"<  else
     cout<<"not on shelf "<}
};
class Reader :public library_object
{
  public :
   void show_data()
    {
     cout<<"name:"<     cout<<"index:"<    }
};
int main()
{
  Reader A_Reader ;
  Book  A_Book;
  A_Reader.set_data("Jorn");
  A_Reader.show_data();
  A_Book.set_data("the C&#43;&#43; Bible");

A_Book.show-data();
return 0;
}

[root&#64;localhost code]# g&#43;&#43; inheritance.cpp
inheritance.cpp: In function &#96;int main()&#39;:
inheritance.cpp:47: &#96;class Book&#39; has no member named &#96;show&#39;
inheritance.cpp:47: &#96;data&#39; undeclared (first use this function)
inheritance.cpp:47: (Each undeclared identifier is reported only once for each
   function it appears in.)

 

显然是一个小错&#xff1a;

[root&#64;localhost code]# g&#43;&#43; inheritance.cpp -o inheritance.out
[root&#64;localhost code]# ./inheritance.out
name:Jornindex:1
name:the C&#43;&#43; Bibleindex:1not on shelf
[root&#64;localhost code]#

程序分析&#xff1a;

 

 

 

 

 

 

 

 

 

&#xff50;&#xff52;&#xff41;&#xff56;&#xff49;&#xff54;&#xff45;继承代码&#xff1b;

#include
using namespace std;
class Base
{
  private: int pri_att;
           void pri_show()
           { cout<<"Base::pri_show() is called!"<  protected: int pro_att;
              void pro_show()
           { cout<<"Base::pro_show() is called!"<   public : int pub_att;
              void pub_show()
           { cout<<"Base::pub_show() is called!"<           Base()
           :pri_att&#61;1;pro_att&#61;1;pub-att&#61;3;
}
class Derived:private Base
{
public :
   void call_fun();
   void show();
};
void Derived ::call_fun()

{
cout<cout<cout<cout<cout<<"Derived ::call_fun is called!"<pub_show();
pro_show();
// pri_show();
}
void Derived::show()
{
  cout<cout<<"Derived ::show() is called "<  cout<  cout<  cout<<"Base::pub_att&#61;"<  cout<<"Base::pro_att&#61;"<//  cout<<"Base::pri_att&#61;"<}
int main()
{
return 0;

&#xff5d;

出错如下:

[root&#64;localhost code]# g&#43;&#43; private_inh.cpp
private_inh.cpp:15: function body for constructor missing
private_inh.cpp:15: ISO C&#43;&#43; forbids declaration of &#96;pro_att&#39; with no type
private_inh.cpp:15: ISO C&#43;&#43; forbids initialization of member &#96;pro_att&#39;
private_inh.cpp:15: making &#96;pro_att&#39; static
private_inh.cpp:15: ISO C&#43;&#43; forbids in-class initialization of non-const static
   member &#96;pro_att&#39;
private_inh.cpp:15: declaration of &#96;int Base::pro_att&#39;
private_inh.cpp:8: conflicts with previous declaration &#96;int Base::pro_att&#39;
private_inh.cpp:15: syntax error before &#96;-&#39; token
private_inh.cpp:15: duplicate member &#96;Base::pro_att&#39;
private_inh.cpp:17: semicolon missing after declaration of &#96;Base&#39;
private_inh.cpp: In constructor &#96;Base::Base()&#39;:
private_inh.cpp:15: parse error before &#96;:&#39; token
private_inh.cpp: At global scope:
private_inh.cpp:22: multiple types in one declaration
private_inh.cpp: In member function &#96;void Derived::show()&#39;:
private_inh.cpp:38: parse error before &#96;.&#39; token
private_inh.cpp:39: parse error before &#96;.&#39; token
private_inh.cpp: At global scope:
private_inh.cpp:50: parse error before &#96;}&#39; token

修改之后;

[root&#64;localhost code]# g&#43;&#43; private_inh.cpp
private_inh.cpp:17: semicolon missing after declaration of &#96;Base&#39;
private_inh.cpp:22: multiple types in one declaration
private_inh.cpp: In member function &#96;void Derived::show()&#39;:
private_inh.cpp:38: parse error before &#96;.&#39; token
private_inh.cpp:39: parse error before &#96;.&#39; token

 

编译出错&#xff1a;

: &#96;Second_Derived Second_Derived::call_fun()&#39; and &#96;void
   Second_Derived::call_fun()&#39; cannot be overloaded
private_inh.cpp:53: semicolon missing after declaration of &#96;class
   Second_Derived&#39;
private_inh.cpp: In member function &#96;void Second_Derived::show()&#39;:
private_inh.cpp:69: parse error before &#96;{&#39; token
private_inh.cpp:74: &#96;A_Dervied&#39; undeclared (first use this function)
private_inh.cpp:74: (Each undeclared identifier is reported only once for each
   function it appears in.)
private_inh.cpp:81: &#96;A_Derived&#39; undeclared (first use this function)
private_inh.cpp:87: return-statement with a value, in function declared with a
   void return type
[root&#64;localhost code]#


转载于:https://www.cnblogs.com/fleetwgx/archive/2009/04/30/1446632.html


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了最长上升子序列问题的一个变种解法,通过记录拐点的位置,将问题拆分为左右两个LIS问题。详细讲解了算法的实现过程,并给出了相应的代码。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
author-avatar
翔英建辉千慧
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有