Lecture Object oriented programming - Lecture no 14

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, . 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, . 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 <<(); (11); (22); (33); cout <<(); cout <<(); cout <<(); top st[3] S1 -1 11 0 12 1 13 2 Go to program Which function is called? (11); Which function is executed? Derive or Base class Rule: When the same function exists in both the base class and the derived class, the function in the derived class will be executed This is true for objects of the derived class Objects of the base class don’t know anything about the derived class and will always use the base class functions Scope Resolution with Overridden Functions How do push() and pop() in Stack2 access push() and pop() in Stack? They use the scope resolution operator, ::, in the statements .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.