Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
After you have read and studied this chapter, you should be able to: Define a class with multiple methods and data members, differentiate the local and instance variables, define and use value-returning methods, distinguish private and public methods, distinguish private and public data members, pass both primitive data and objects to a method. | CSC241: Object Oriented Programming Lecture No 14 Previous Lecture Protected members Generalization in UML representation Example program Counter CountDn Derived class Constructor Destructor Function overriding class A A() { } class B B() { } class C C() { } C c1; A() {} B() {} C() {} B b1; A() {} B() {} A a1; A() {} class A ~A() { } class B ~B() { } class C ~C() { } C *c1 = new C; delete c1; ~B() {} ~C() {} ~A() {} Today’s Lecture Function overriding Example program – Distance class Class hierarchy Employee program Public and private Inheritance Level of inheritance Overriding Member Functions Member functions in a derived class can be override, i.e. have the same name as those in the base class Stack, a simple data storage medium. It allowed you to push integers onto the stack and pop them off class A abc(int x) { } class B abc(int x) { } Example - stack class Stack { protected: int st[3]; int top; public: Stack() { top = -1; } void push(int var) { st[++top] = . | CSC241: Object Oriented Programming Lecture No 14 Previous Lecture Protected members Generalization in UML representation Example program Counter CountDn Derived class Constructor Destructor Function overriding class A A() { } class B B() { } class C C() { } C c1; A() {} B() {} C() {} B b1; A() {} B() {} A a1; A() {} class A ~A() { } class B ~B() { } class C ~C() { } C *c1 = new C; delete c1; ~B() {} ~C() {} ~A() {} Today’s Lecture Function overriding Example program – Distance class Class hierarchy Employee program Public and private Inheritance Level of inheritance Overriding Member Functions Member functions in a derived class can be override, i.e. have the same name as those in the base class Stack, a simple data storage medium. It allowed you to push integers onto the stack and pop them off class A abc(int x) { } class B abc(int x) { } Example - stack class Stack { protected: int st[3]; int top; public: Stack() { top = -1; } void push(int var) { st[++top] = var; } int pop() { return st[top--]; } }; class Stack2 : public Stack{ public: void push(int var1){ if(top >= 3 -1){ cout << “\nError: stack is full”; exit(1); } Stack::push(var); } int pop() { if(top < 0) { cout << “\nError: stack is empty”; exit(1); } return Stack::pop(); } }; Stack2 s1; cout <