zoukankan      html  css  js  c++  java
  • 类别继承程序代码再用

    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]#

  • 相关阅读:
    Oracle SQL语句收集
    SqlParameter In 查询
    SQL 性能优化
    Entity Framework
    【XLL API 函数】 xlfSetName
    【XLL API 函数】xlfUnregister (Form 2)
    【XLL API 函数】xlfUnregister (Form 1)
    【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句
    【Excel 4.0 函数】REGISTER
    【Bochs 官方手册翻译】 第一章 Bochs介绍
  • 原文地址:https://www.cnblogs.com/fleetwgx/p/1446632.html
Copyright © 2011-2022 走看看