class Vehicle public void moving class Car public
继承的定义 class Vehicle { public : void moving(); }; class Car: public Vehicle { public : void braking(); }; class Airplane: public Vehicle { public : void landing(); }; class Ship: public Vehicle { public : void porting(); };
继承的定义 v 子类重载 (override) 父类的方法 class Vehicle { public : void moving(); }; class Car: public Vehicle { public : void braking(); }; class Airplane: public Vehicle { public : void landing(); void moving(); }; class Ship: public Vehicle { public : void porting(); void moving(); };
继承的定义 void Vehicle: : moving() { cout<<“I am moving!”<<endl; } int main(int argc, char* argv[]) { Vehicle v; Car c; Ship s; Airplane a; void Ship: : moving() { cout<<“I am sailing in the sea!”<<endl; } void Airplane: : moving() { cout<<“I am flying in the sky!”<<endl; } v. moving(); c. moving(); s. moving(); a. moving(); return 0; }
继承的定义 void Vehicle: : moving() { cout<<“I am moving!”<<endl; } int main(int argc, char* argv[]){ Car *pc = new Car; Ship *ps = new Ship; Airplane *pa = new Airplane; void Ship: : moving() { cout<<“I am sailing in the sea!”<<endl; } void Airplane: : moving() { cout<<“I am flying in the sky!”<<endl; } pc-> moving(); ps-> moving(); pa-> moving(); delete pc; delete ps; delete pa; return 0; }
继承的方式 v 继承的权限问题 class Ship: public Vehicle { public : void porting(); void moving(); }; v 子类继承父类的方式同样有三种: § public 方式 § protected 方式 § private 方式
继承的方式 v 多重继承的例子 class Vehicle { public : void moving(); }; class Airplane: public Vehicle { public : void landing(); void moving(); }; class Weapon { public : void firing(); }; class Battleplane: public Airplane, public Weapon{ public : … …. };
多态与虚函数 class Vehicle { public : virtual void moving(); // moving() 声明为虚函数 }; int main(int argc, char* argv[]) { Vehicle* vehicles[3]; vehicles[0] = new Car(); vehicles[1] = new Ship(); vehicles[2] = new Airplane(); for (int i =0; i<3; i++) vehicles[i]->moving(); for (int i =0; i<3; i++) delete vehicles[i]; return 0; } I am running on the road! I am sailing in the sea! I am flying in the sky!
多态与虚函数 v 实验: § 将“对象指针数组”换为“对象数组”是否可以? int main(int argc, char* argv[]){ Char c; Ship s; Airplane a; Vehicle vehicles[3]={c, s, a}; for (int i =0; i<3; i++) vehicles[i]. moving(); return 0; } I am moving!
多态与虚函数 class Vehicle { public : virtual void moving(); // moving() 声明为虚函数 }; int main() { Ship s; Vehicle * pv = &s; pv -> moving(); return 0; } I am sailing in the sea! int main() { Ship s; Vehicle v = s; v. moving(); return 0; } I am moving! int main() { Ship s; Vehicle & v = s; v. moving(); return 0; } I am sailing in the sea!
抽象类与纯虚函数 class Vehicle { public: virtual void moving()=0; }; int main(int argc, char* argv[]) { Vehicle * vehicles[2]; vehicles[0] = new Car(); vehicles[1] = new Car(); class Car: public Vehicle { public: void moving(); void braking(); }; void Car: : moving() { cout<<“I am running on the road!”<<endl; } vehicles[0]->moving(); vehicles[1]->moving(); delete vehicles[0]; delete vehicles[1]; return 0; }; ✔
抽象类与纯虚函数 class Vehicle { public : virtual void moving()=0; }; int main(int argc, char* argv[]) { Vehicle * vehicles[3]; vehicles[0] = new Car(); vehicles[1] = new Car(); vehicles[2] = new Vehicle(); void Vehicle: : moving() { cout<<“I am moving!”<<endl; }; class Car: public Vehicle { public: void moving(); void braking(); }; void Car: : moving() { cout<<“I am running on the road!”<<endl; } vehicles[0]->moving(); vehicles[1]->moving(); vehicles[2]->moving(); delete vehicles[0]; delete vehicles[1]; delete vehicles[2] return 0; }; ✗
抽象类与纯虚函数 class Vehicle { public: virtual void moving()=0; }; int main(int argc, char* argv[]) { Vehicle * vehicles[3]; class Car: public Vehicle { public: void running(){ cout<<“I am running on the road!”<<endl; } }; vehicles[0] = new Car(); vehicles[1] = new Ship(); vehicles[2] = new Ship(); class Ship: public Vehicle { public: void moving(){ cout<<“I am sailing in the sea!”<<endl; } }; delete vehicles[0]; delete vehicles[1]; delete vehicles[2]; vehicles[0]->moving(); vehicles[1]->moving(); vehicles[2]->moving(); return 0; }; ✗
抽象类与纯虚函数 class Vehicle { public: virtual void moving()=0; }; int main(int argc, char* argv[]) { Car * cars[3]; cars[0] = new Car(); cars[1] = new Truck(); cars[2] = new Truck(); class Car: public Vehicle { public: void moving(){ cout<<“I am running on the road!”<<endl; } }; class Truck: public Vehicle { public: void carrying(){ } }; cars[0]->moving(); cars[1]->moving(); cars[2]->moving(); delete cars[0]; delete cars[1]; delete cars[2]; return 0; }; ✔
异常处理 class Stack. Exception { public: Stack. Exception(char* str){ msg = new char[strlen(str)+1]; strcpy(msg, str); } virtual ~Stack. Exception(){ if (msg!=NULL) delete msg; } void show(){ cout<<msg<<endl; } private: char* msg; }; void Stack: : pop(){ if (top==0) throw Stack. Excption(“Empty!”); top--; } void Stack: : push(int n){ if (top==size) throw Stack. Excption(“Full!”); data[top]=n; top++; } int Stack: : read. Top(){ if (top==0) throw Stack. Excption(“Empty!”); return data[top-1]; }
异常处理 int main(int argc, char *argv[]) { Stack *pst = new Stack(10); try{ pst->push(3); pst->pop(); } catch(Stack. Exception e){ //捕获栈异常 cout<<“An exception occurred!”<<endl; e. show(); // 显示异常信息 } catch (…) { //捕获其他异常 cout<<“Unexpected exception occurred!”<<endl; } if (pst) delete pst; return 0; }
异常处理 v C++标准异常类 § 在C++库中定义了标准的异常类 std: : exception; § 若使用了名字空间 std,则可直接使用该类,使用 时应包含头文件 <exception>。 class exception { throw() 表示该方法 public: 不会抛出异常 exception() throw(); exception(const exception&) throw(); exception& operator=(const exception) throw(); virtual ~exception() throw(); virtual const char* what() const throw(); };
本章作业 v 14. 4 v 14. 5 v 14. 6 v 15. 6 v 17. 8~17. 10
- Slides: 39