C++-继承与派生学习资料.pptVIP

  • 4
  • 0
  • 约8.53千字
  • 约 80页
  • 2020-06-20 发布于浙江
  • 举报
第七章 继承与派生;本章主要内容;类的继承与派生;继承与派生问题举例;继承与派生问题举例;继承与派生问题举例;继承与派生问题举例;继承与派生的目的;派生类的声明;派生类生成过程;继承方式;公有继承(public);例7-1 公有继承举例;class Rectangle: public Point //派生类声明 { public: //新增公有函数成员 void InitR(float x, float y, float w, float h) { InitP(x,y); //调用基类公有成员函数 W=w; H=h; } float GetH() { return H; } float GetW() { return W; } private: //新增私有数据成员 float W,H; };;#includeiostream #includecmath using namespace std; int main() { Rectangle rect; //派生类对象 rect.InitR(2,3,20,10); //访问 派生类的公有成员 //通过派生类对象访问基类的公有成员 rect.Move(3,2); coutrect.GetX(), rect.GetY(), rect.GetH(), //访问派生类的公有成员 rect.GetW()endl; //访问派生类的公有成员 return 0; };私有继承(private);例7-2 私有继承举例;#includeiostream #includecmath using namespace std; int main() { Rectangle rect; //派生类对象 //通过派生类对象只能访问本类公有成员 rect.InitR(2,3,20,10); rect.Move(3,2); coutrect.GetX(), rect.GetY(), rect.GetH(),rect.GetW()endl; return 0; };保护继承(protected);protected 成员的特点与作用;例7-3 protected 成员举例;class A { protected: int x; } class B: public A{ //派生类 public: void Function(); }; void B:Function() { x=5; //正确 };*;基类与派生类的对应关系;多继承时派生类的声明;多继承举例;void A::setA(int x) { a=x; } void B::setB(int x) { b=x; } void C::setC(int x, int y, int z) { //派生类成员直接访问基类的 //公有成员 setA(x); setB(y); c=z; } //其它函数实现略;*;继承时的构造函数;单一继承时的构造函数;单一继承时的构造函数举例;B::B() //默认构造函数 { b=0; coutBs default constructor called.endl; } B::B(int i) //带参数的构造函数 { b=i; coutBs constructor called. endl; } B::~B() //析构函数 { coutBs destructor called.endl; } void B::Print() const //输出函数 { coutbendl; };class C: public B { public: C(); C(int i,int j); ~C(); void Print() const; private: int c; };;C::C() //默认构造函数 { c=0; coutCs default constructor called.\n; } C::C(int i,int j):B(i) //带参数的构造函数 { c=j; coutCs constructor called.\n; } C::~C() //析构函数 {coutCs destructor called.\n;} void C::Print() const //输出函数 { B::Print(); coutcendl; } void main() //主函数

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档