日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

类别继承-程序代码再用

發(fā)布時(shí)間:2025/7/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 类别继承-程序代码再用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c++繼承機(jī)制:

代碼

#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)限的等級(jí)
pub_attpublicpublic
pro_attprotectedprotected
pri_attprivate隱藏
pub_showpublicpublic
pro_showprotectedprotect
pri_showptivate隱藏

?

?

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ù)以上報(bào)錯(cuò),

一一分析排查:

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

主要還是大部分在細(xì)節(jié)上,非技術(shù)性錯(cuò)誤.

經(jīng)過(guò)修改在編譯:

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

這就很好證實(shí)了在public繼承中pri_att的不可見(jiàn)性

再次編譯

出錯(cuò)情況:

[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

本程序測(cè)試的特點(diǎn):

Base 類別成員Base類別存取權(quán)限的等級(jí)Derive類別public繼承后存取權(quán)限的等級(jí)在類別的外程序可否通過(guò)Derived類別的對(duì)象存取
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.)

?

顯然是一個(gè)小錯(cuò):

[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;

出錯(cuò)如下:

[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

?

編譯出錯(cuò):

: `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é)

以上是生活随笔為你收集整理的类别继承-程序代码再用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩久久久精品 | 国产91专区 | 久久鲁视频 | 日韩视频中文字幕在线观看 | 欧美顶级少妇做爰hd | 尤物精品| 国产视频一区二区在线播放 | 鲁在线视频 | 日韩欧美视频免费观看 | 欧美日韩国产在线观看 | 成人做爰视频www网站小优视频 | 欧美日韩中文字幕在线 | 国产精品色综合 | 国产区一区二区三区 | 无码任你躁久久久久久老妇 | 日韩中文字幕一区 | ⅹxxxxhd亚洲日本hd老师 | 天天干天天色天天 | 欧美顶级少妇做爰hd | 亚洲欧美日韩国产综合 | 91精品一区 | 亚洲综合久久久 | 精品国产网站 | 无码国产伦一区二区三区视频 | 亚洲天堂手机 | 性午夜| 在线免费成人网 | 少妇被按摩师摸高潮了 | 精品在线观看视频 | 日本电影成人 | 伊人影视大全 | 白白色在线播放 | 粗大挺进潘金莲身体在线播放 | 污污污www精品国产网站 | av波多野吉衣 | 国产激情综合 | 一本一道精品欧美中文字幕 | 成人性视频免费网站 | 麻豆影视国产在线观看 | 国产精品又黄又爽又色无遮挡 | jizz黄色片 | 亚洲精品小说 | 黄色一级视频免费看 | 久久亚洲一区二区 | 欧美亚洲丝袜 | 亚洲一区二区av在线 | youjizz.com中国 | 国产免费内射又粗又爽密桃视频 | 成人片免费看 | 久久精品不卡 | 黄色片网站在线观看 | 久草网视频在线观看 | 精品成人免费一区二区在线播放 | 亚洲精品视频一区 | www.久操| 久久国产福利一区 | 黑人干亚洲女 | 精产国品一二三产区m553麻豆 | 无套日出白浆 | 超碰96在线| 99er久久| 美腿丝袜亚洲色图 | 成年在线视频 | 欧洲黄色网 | 精品无码久久久久久久 | 国产91av在线 | 玩日本老头很兴奋xxxx | 不卡av免费 | 人人干人人做 | 国产无限制自拍 | 97成人免费视频 | 日出白浆视频 | 美女诱惑一区二区 | 欧美日韩国产精品综合 | 久久这里| 久久婷婷国产 | 影音先锋成人资源网 | 欧美午夜久久久 | 国产乱码77777777 | 日本性久久 | 一级爱爱免费视频 | 亚洲91av | 高清国产一区二区三区四区五区 | 一本色道久久88综合日韩精品 | 色综合中文字幕 | 六月丁香av | 国产色频| 日批大全| 久久av免费看 | xx性欧美肥妇精品久久久久久 | 中文字幕精品一区二区精品 | 五月激情av | 免费毛片一级 | 国产一区二区视频在线 | 久久精品片| 欧美麻豆 | 麻豆一区二区三区在线观看 | 偷偷色噜狠狠狠狠的777米奇 | 久久婷婷激情 |