- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第七章 继承性和派傻您类-刘丹
第七章 继承性和派生类;7.1 基类和派生类;生物类;通过继承机制,可以利用已有的数据类型来定义新的数据类型。所定义的新的数据类型不仅拥有新定义的成员,而且还同时拥有旧的成员。;;class 派生类名 :继承方式 基类名
{
派生类新定义成员
}; ;class 派生类名 :继承方式1 基类名1,
继承方式2 基类名2,…
{
派生类新定义成员
};;私有成员;类的成员可以有public、protected和private3种访问权限。;其特点是:基类的公有成员和保护成员作为派生类的成员时,都保持原有状态,而私有成员不被继承。;Evaluation only.
Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.
Copyright 2004-2011 Aspose Pty Ltd.;class student
{
private: int age;
protected: char sex;
public: int Getage( ) {return age;}
};
class graduatestudent: public student
{ private: int stunum;
public: void Getnum( );
};;;复习:公有继承;2、私有继承;Evaluation only.
Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.
Copyright 2004-2011 Aspose Pty Ltd.;class student {
private: int age;
protected: char sex;
public: int Getage( ) {return age;}
};
class graduatestudent: private student
{
private: int stunum;
public: void Getnum( );
};;;其特点是:基类的所有公有成员和保护成员都作为派生类的保护成员,并且只能被它的派生类成员函数或友元访问,基类的私有成员仍是私有的。
;Evaluation only.
Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0.
Copyright 2004-2011 Aspose Pty Ltd.;class student {
private: int age;
protected: char sex;
public: int Getage( ) {return age;}
};
class graduatestudent: protected student
{
private: int stunum;
public: void Getnum( );
};;;上述三种不同的继承方式可用表格表示如下:;派生类继承成员的调整;练习;#include iostream.h
class A
{
public:
void f(int i) {coutiendl;}
void g() {coutgendl;}
};;分析下列程序中的访问权限;class A
{
protected:
int X;
};
void main()
{
A a;
a.X=5;
};#include iostream.h
#include string.h
class A
{
public:
A(const char *nm) { strcpy(name,nm);}
private:
char name[80];
};;判断对错;派生类对象的存储;#includeiostream.h
class A
{
int no;
int tag;
public:
void seta(int n,int t)
{
no=n;
tag=t;
}
void dispa()
{
coutA:no=no ,tag=tagendl;
}
};;7.2 单继承;7.2.1 派生类构造函数和析构函数;#include iostream.h
class A
{
public:
A()
{ coutA constructor1endl; }
文档评论(0)