c++继承机制:

代码

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

class Derived:public Base //定义A_Derived以public继承Base,
{  public: //
  void call_fun() //
  void show() //
}; //

Base类别的成员 Base类别的存取权限 Derived类别public继承后存取权限的等级
pub_att public public
pro_att protected protected
pri_att private 隐藏
pub_show public public
pro_show protected protect
pri_show ptivate 隐藏

void Derived::call_fun() //
{
cout<<endl;
cout<<"Derived ::call_fun is called"<<endl;
pub_show();
pro_show();

//pri_show();
}
void Deeived::show()
{
cout<<endl;
cout<<"Derived ::show() is called!"<<endl;
cout<<"Base::pub_att="<<pub_att<<endl;
cout<<"Base::pro_att="<<pro_att<<endl;
//cout<<"Base::pri_att="<<pri_att<<endl;
}

int main()
{
  Derived A_Derived;
  cout<<"Accessing Derived's data members"
     <<"inherited form Base..."<<endl;
  cout<<"Derived::pub_att="<<A_Derived.pub_att<<endl;
  //cout<<"Derived::pro_att="<<A_Derived.pro_att<<endl;
  cout<<endl;
  cout<<"Call Derived's members funcitons"<<"inherited form Base.."<<endl;
  A_Derived.pub_show();
  //A_Derived.pro_show();
  //A_Derived.pri_show();
  A_Derived.call_show();
  A_Derived.show();
return 0;
}

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

根据以上报错,

一一分析排查:

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

主要还是大部分在细节上,非技术性错误.

经过修改在编译:

public_inh.cpp: In constructor `Base::Base()':
public_inh.cpp:19: class `Base' does not have any field named `pro_att'
public_inh.cpp: In member function `void Derived::show()':
public_inh.cpp:43: `pro_att' 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: `int Base::pri_att' is private
public_inh.cpp:44: within this context
public_inh.cpp: In function `int main()':
public_inh.cpp:54: `class Derived' has no member named `pro_att'
public_inh.cpp:14: `void Base::pro_show()' is protected
public_inh.cpp:59: within this context
public_inh.cpp:8: `void Base::pri_show()' is private
public_inh.cpp:60: within this context
public_inh.cpp:61: no matching function for call to `Derived::call_show()'

再修改,编译:

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

这就很好证实了在public继承中pri_att的不可见性

再次编译

出错情况:

[root@localhost code]# g++ public_inh.cpp
public_inh.cpp: In function `int main()':
public_inh.cpp:12: `int Base::pro_att' is protected
public_inh.cpp:54: within this context

[root@localhost code]# g++ public_inh.cpp
[root@localhost code]# g++ -o public_inh.cpp public_inh.out
g++: public_inh.out: No such file or directory
g++: no input files

但是:

[root@localhost code]# g++  public_inh.cpp -o public_inh.out 成功编译
[root@localhost code]#

结果如下:

[root@localhost code]# g++  public_inh.cpp -o public_inh.out
[root@localhost code]# ./public_inh.out
Accessing Derived's data membersinherited form Base...
Derived::pub_att=3
Call Derived'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=3
Base::pro_att=2

本程序测试的特点:

Base 类别成员 Base类别存取权限的等级 Derive类别public继承后存取权限的等级 在类别的外程序可否通过Derived类别的对象存取
pub_att      
pro_att      
pri_att      
pub_show      
pro_show      
       

代码:

#include<iostream>
#include<cstring>
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 =1;
      }
};
class Book :public library_object
{
  private:
  bool on_shelf;
  public :
  void show_data()
{
  cout<<"name:"<<name;

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

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

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

显然是一个小错:

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

程序分析:

pravite继承代码;

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

{
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<"Derived ::call_fun is called!"<<endl;
pub_show();
pro_show();
// pri_show();
}
void Derived::show()
{
  cout<<endl;
cout<<"Derived ::show() is called "<<endl;
  cout<<Derived.pubshow<<endl;
  cout<<Derived.proshow<<endl;
  cout<<"Base::pub_att="<<pub_att<<endl;
  cout<<"Base::pro_att="<<pro_att<<endl;
//  cout<<"Base::pri_att="<<pro_att<<endl;
}
int main()
{
return 0;

出错如下:

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

修改之后;

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

编译出错:

: `Second_Derived Second_Derived::call_fun()' and `void
   Second_Derived::call_fun()' cannot be overloaded
private_inh.cpp:53: semicolon missing after declaration of `class
   Second_Derived'
private_inh.cpp: In member function `void Second_Derived::show()':
private_inh.cpp:69: parse error before `{' token
private_inh.cpp:74: `A_Dervied' 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: `A_Derived' undeclared (first use this function)
private_inh.cpp:87: return-statement with a value, in function declared with a
   void return type
[root@localhost code]#

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

类别继承-程序代码再用相关推荐

  1. 智能卡操作系统的程序代码结构

    智能卡 操作系统的生命周期可分成两部分-卡完工之前和卡完工之后.在卡完工之前的期间,来自工厂的  微控制器 的E EPROM 是空的,所有程序都在ROM里运行.从EEPROM里既读不到数据,也没有任何 ...

  2. 12-Java 继承抽象类代码块(详解~)

    文章目录 1. 继承 1.1 继承的实现(掌握) 1.2 继承的好处和弊端(理解) 1.3. Java中继承的特点(掌握) 2. 继承中的成员访问特点 2.1 继承中变量的访问特点(掌握) 2.2 s ...

  3. day12_继承(继承-抽象类-代码块)

    文章目录 1.继承 1.1.继承的实现 1.1.继承的好处和弊端 1.3.Java中继承的特点 2.继承中的成员访问特点 2.1.继承中变量的访问特点 2.2.super 2.3.继承中构造方法的访问 ...

  4. PTrade交易程序代码——从零到实盘19

    如题,本文将介绍PTrade实盘交易程序的代码. 如何在PTrade中部署策略,可以参见前文中"PTrade部署策略过程"部分内容,然后把本文的代码粘贴到PTrade,就能完成&q ...

  5. 如何阅读他人的程序代码

    近日,在互联网上游荡,偶然发现一篇曾经的文章,是关于如何阅读他人程序代码的,阅后颇为受益,于是乎重新整理了一下格式,将此文转载如下: 如何阅读他人的程序代码 文/王建兴 作者简介: 王建兴,清华大学资 ...

  6. LabVIEW程序代码更新缓慢

    LabVIEW程序代码更新缓慢 LabVIEW在加载大型VI时会导致响应缓慢.当在前面板或框图上移动一个控件或函数时,它会挂起一秒钟,或者移动得非常缓慢.偶尔,当已打开程序框图时,有些连线会显示不全, ...

  7. master-worker常驻型程序代码修改哪些需要重启master或者worker

    之前在yii的项目里用redis作为消息队列,现在很多任务需要延迟需求,于是把之前redis的消息队列替换成了rabbitmq 于是使用yii的yii2-queue这个组件 但是由于提供的yii qu ...

  8. 《编写高质量代码:改善c程序代码的125个建议》——第1章 数据,程序设计之根本建议1:认识ANSI C...

    本节书摘来自华章计算机<编写高质量代码:改善c程序代码的125个建议>一书中的第1章,建议1,作者:马 伟 更多章节内容可以访问云栖社区"华章计算机"公众号查看. 第1 ...

  9. 批量备SAP中CBO ABAP 程序代码为TXT文件备份

    很想把生产机上所有后续开发的CBO程序都备份下来. 以备急用! 用过2种方法: 1.写BDC程序,模拟 TCODE:SE38 -->Program --> Utilities(M)--&g ...

最新文章

  1. Laravel核心解读--完结篇
  2. 如何使用Mybatis的拦截器实现数据加密与解密
  3. linux下ssh文件配置,允许root远程用密码登录
  4. Python里面数组拼接方法介绍
  5. 动手实现一个 LRU cache
  6. 小样本学习(Few-shot Learning)综述
  7. C语言程序设计0004,C语言程序设计0004.doc
  8. P1312 Mayan游戏 [模拟][搜索]
  9. SMS短信通API——(1)Java应用发送手机短信
  10. dev gridcontrol 根据数据获取索引_MySQL 索引分析除了 EXPLAIN 还有什么方法?
  11. [技术分享]20171130_Kendo UI _ datePicker日期控件如何只选择年,不选择月,日?
  12. 活动页面html设计,活动查看页面.html
  13. 3 分钟了解 JSON Schema
  14. 如何使用Postman和Newman在CI环境中自动化REST API端到端测试
  15. ubuntu下MySQL的安装及远程连接配置(转)
  16. sql日志文件查看工具
  17. Matlab中xtickformat函数
  18. 字典树(前缀树/后缀树)
  19. 高兴就好,简单就好,明白就好
  20. 国庆八天乐,码农长假怎么过?别加班了

热门文章

  1. 失眠——耳部按摩(组图)
  2. 程序人生【一些经典的资料】
  3. hdu 2065 红色病毒问题 (母函数)
  4. HTML中添加后退、前进、刷新的超链接
  5. 大家一起学面向对象设计模式系列Chapter 02 软件设计的基本原则
  6. ubuntu 16.04 编译 opencv_contrib 3.4, nonfree
  7. Mobileye采用单目摄像头做ADAS太不精确
  8. 【java】【mybatis】在使用mybatis进行批量插入,批量更新等批量操作时,切割In集合List进行分批批量操作的java中的切割代码...
  9. VS2010 LNK1123:转换到 COFF期间失败:文件无效或损坏”的解决方法
  10. netty 5 alph1源码分析(服务端创建过程)