类别继承-程序代码再用
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類別的存取權(quán)限 | Derived類別public繼承后存取權(quán)限的等級 |
| 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()'
根據(jù)以上報錯,
一一分析排查:
:pri_att(1),pro_att(2),pub_att(3){}
主要還是大部分在細節(jié)上,非技術(shù)性錯誤.
經(jīng)過修改在編譯:
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]#
結(jié)果如下:
[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類別存取權(quán)限的等級 | Derive類別public繼承后存取權(quán)限的等級 | 在類別的外程序可否通過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]#
轉(zhuǎn)載于:https://www.cnblogs.com/fleetwgx/archive/2009/04/30/1446632.html
總結(jié)
以上是生活随笔為你收集整理的类别继承-程序代码再用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VM虚拟机常见问题之五--网络相关的问题
- 下一篇: 使用ISA2004发布多个Web站点之二