热门标签 | 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


推荐阅读
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
  • 本题探讨了一种字符串变换方法,旨在判断两个给定的字符串是否可以通过特定的字母替换和位置交换操作相互转换。核心在于找到这些变换中的不变量,从而确定转换的可能性。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
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社区 版权所有