例:类的继承 5:13:38 class vehicle vehicle类是基类或父类,car类 int wheels; 是派生类或子类 float weight >Car类从其基类 vehicle类中继承 float loading 了所有的成员变量和成员函数 public (构造函数与析构函数除外) vehicle(int wheels, float weight, float loading) int get_wheels o float get-weight O;void maino( float get-loading o Car car(4,1000,1000,4) cout < car passengers o class car: public vehicle < end1 int passenger-load; cout < car get_wheels O public: < car. get-weight O Car(int in-wheels, floa < car. get-loading O int peoples=4) < end1 int passengers O
例:类的继承 15:13:38 class vehicle{ int wheels; float weight; float loading; public: vehicle(int wheels,float weight,float loading); int get_wheels(); float get_weight(); float get_loading(); }; class Car:public vehicle{ int passenger_load; public: Car(int in_wheels,float in_weight,float in_loading, int peoples=4); int passengers(); }; ➢vehicle类是基类或父类,car类 是派生类或子类 ➢Car类从其基类vehicle类中继承 了所有的成员变量和成员函数 (构造函数与析构函数除外) void main(){ Car car(4,1000,1000,4); cout << car.passengers() << endl; cout << car.get_wheels() << car.get_weight() << car.get_loading() << endl; }
说明 15:13:38 >派生类继承了基类中除构造函数、析构函数和重载的“=” 运算符对应的函数以外的所有成员 在派生类中动问基类成员时,仍要受基类动问控制的限制, 派生类新添加的成员函数中不能直接访问基类的 private 成员,但可访问基类的 public成员和 protected成员 例:在派生类中访问基类成员 //A.h class af #inc1ude“A.h” int x; class B: public A void fo: public protected void funco i int y, x=0;f0;//错,基类的私有成员 void g0; y=10;g0;//正确,基类的保护成员 public z=10;h0;//正确,基类的公有成员 int void ho
15:13:38 说明 ➢派生类继承了基类中除构造函数、析构函数和重载的“=” 运算符对应的函数以外的所有成员 ➢在派生类中访问基类成员时,仍要受基类访问控制的限制, 派生类新添加的成员函数中不能直接访问基类的private 成员,但可访问基类的public成员和protected成员 例:在派生类中访问基类成员 //A.h class A{ int x; void f(); protected: int y; void g(); public: int z; void h(); }; #include “A.h” class B:public A{ public: void func(){ x=0;f(); //错,基类的私有成员 y=10;g(); //正确,基类的保护成员 z=10;h(); //正确,基类的公有成员 } };
5:18:38 说明(续) 继承方式决定了从基类继承而来的成员在派生类中的访问 控制权限,用关键字 public、 private和 protected表示 分别为公有继承、私有继承和保护继承 ★私有继承使基类的保护成员和公有成员成为派生类的 私有成员 ★保护继承使基类的保护成员和公有成员成为派生类的 保护成员 ★公有继承使基类的保护成员和公有成员仍然成为派生 类的保护成员和公有成员 C++中默认的继承方式是私有继承 一般多使用公有继承,故应重点掌握公有继承
15:13:38 说明(续) ➢ 继承方式决定了从基类继承而来的成员在派生类中的访问 控制权限,用关键字public、private和protected表示, 分别为公有继承、私有继承和保护继承 ★私有继承使基类的保护成员和公有成员成为派生类的 私有成员 ★保护继承使基类的保护成员和公有成员成为派生类的 保护成员 ★公有继承使基类的保护成员和公有成员仍然成为派生 类的保护成员和公有成员 ➢ C++中默认的继承方式是私有继承 ➢ 一般多使用公有继承,故应重点掌握公有继承
5:18:38 公有继承 基类的公有成员和保护成员成为派生类的公有成员和保护 成员 #include <iostream> using namespace std class a protected int X, y, public: void get-XYo t cout<<‘ Enter two numbers of x,y:”; cin >>x >>y, void put-XY O cout<“x=“<<x<<“,y=<y<“n’;}
15:13:38 ➢公有继承 基类的公有成员和保护成员成为派生类的公有成员和保护 成员 #include <iostream> using namespace std; class A{ protected: int x,y; public: void get_XY(){ cout << “Enter two numbers of x,y:”; cin >> x >> y; } void put_XY() { cout << “x=“ << x << “,y=“ << y << ‘\n’;} };
例:公有继承(续) 15:13:38 class b: public Al protected int s: public int get-so return s; void make-S0{s=x*y;}//x、y都是为B的保护成员 class C: public Bi protected: int h.v. public: void get-H0{out<“ Enter h:”;cin>>h;} int get-vo return v; void make-vo i make-S0;∥/s=x*y;//、x、y都是C的保护成员 v=get_S0*h;//v=x*y*h
15:13:38 例:公有继承(续) class B:public A{ protected: int s; public: int get_S(){return s;} void make_S(){s=x*y;} //x、y都是为B的保护成员 }; class C:public B{ protected: int h,v; public: void get_H(){cout << “Enter H:”; cin >> h;} int get_V(){return v;} void make_V(){ make_S(); //s=x*y; //s、x、y都是C的保护成员 v=get_S()*h; //v=x*y*h; } };