1 CSC241: Object Oriented Programming Lecture No 13.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
1 CSC241: Object Oriented Programming Lecture No 28.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ Inheritance Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CSC241: Object Oriented Programming Lecture No 27.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
1 CSC241: Object Oriented Programming Lecture No 12.
1 CSC241: Object Oriented Programming Lecture No 22.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
1 CSC241: Object Oriented Programming Lecture No 16.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 CSC241: Object Oriented Programming Lecture No 25.
1 CSC241: Object Oriented Programming Lecture No 02.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
1 CSC241: Object Oriented Programming Lecture No 11.
Chapter -6 Polymorphism
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
1 CSC241: Object Oriented Programming Lecture No 08.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Data Structure Lecture 1.  The Logical Organization of Data is called data structures  organize data  more efficient programs.  More powerful computers.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Constructors and Destructors
Visit for more Learning Resources
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
CSC241: Object Oriented Programming
Lecture 22 Inheritance Richard Gesick.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Constructors and Destructors
Object Oriented Programming Using C++
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Overview of C++ Polymorphism
Institute of Petroloeum Technology, Gandhinagar
Chapter 9 Inheritance.
Java Programming Language
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Object-Oriented Programming (Part 2)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Constructors and Deconstructor
Objects as Function Arguments
Constructors & Destructors
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 13

2 Previous Lecture Implicit conversion Explicit constructor Overloading – Stream insertion << – Stream extraction >> Inheritance

3 Today’s Lecture Inheritance Protected members Constructor in derived class Function overriding

4 Inheritance Base class Derive class – A derived class represents a more specialized group of objects – Inherit behaviors of base class and can customize the inherited behavior Direct base class is the base class from which a derived class explicitly inherits Indirect base class is inherited from two or more levels up in the class hierarchy single inheritance and multiple inheritance

5 protected Members Access specifiers – public: accessible within the body of base class and anywhere that the program – private: accessible only within the body of base class and the friends of base class A base class's protected members can be accessed within – the body of that base class, – by members and friends of that base class, and – by members and friends of any classes derived from that base class

6 Generalization in UML Class Diagrams

7

8 Cont.

9

10 Inheritance – Example program class Counter { protected: unsigned int count; public: Counter() : count(0) { } int get_count() { return count; } Counter operator ++ () { return Counter(++count); } }; main() { CountDn c1; cout << “\nc1=” << c1.get_count(); ++c1; ++c1; ++c1; cout << “\nc1=” << c1.get_count(); --c1; cout << “\nc1=” << c1.get_count(); } class CountDn : public Counter { public: Counter operator -- () { return Counter(--count); } }; Go to program

11 Base Class Unchanged Inheritance doesn’t work in reverse If other classes have been derived from base class, the base class remains unchanged Counter c2; The base class and its objects don’t know anything about any classes derived from the base class Objects of class Counter, such as c2, can’t use the operator--() function in CountDn

12 Dangers of protected Access Specifier Suppose you’ve written a class library, which you’re distributing to the public Any programmer who buys this library can access protected members of your classes simply by deriving other classes from them This makes protected members considerably less secure than private members To avoid corrupted data, it’s often safer to force derived classes to access data in the base class using only public functions in the base class

13 Derived class constructor Instantiating a derived-class object begins a chain of constructor calls, – invokes its direct base class's constructor either explicitly or implicitly – Then derived class constructor is executed class A A() { } class A A() { } class B B() { } class B B() { } class C C() { } class C C() { } C c1; A() {..} A() {..} B() {..} B() {..} C() {..} C() {..} B b1; A() {..} A() {..} B() {..} B() {..} A a1; A() {..} A() {..}

14 Derived Class Constructors class Counter { protected: unsigned int count; public: Counter() : count(0) { } Counter(int c) : count(c) { } int get_count(){ return count; } Counter operator ++ (){ return Counter(++count); } }; CountDn c1; CountDn c2(100); class CountDn : public Counter { public: CountDn() : Counter() { } CountDn(int c) : Counter(c) { } Counter operator -- (){ return Counter(--count); } }; Go to program

15 Derived class destructor When a derived-class object is destroyed, the chain of calling destructor begins in reverse order as that of constructor – derived-class destructor performs its task, – then invokes the destructor of the next base class up the hierarchy class A ~A() { } class A ~A() { } class B ~B() { } class B ~B() { } class C ~C() { } class C ~C() { } C *c1 = new C; … delete c1; C *c1 = new C; … delete c1; ~B() {..} ~B() {..} ~C() {..} ~C() {..} ~A() {..} ~A() {..} Go to program

16 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 A abc(int x) { } class B abc(int x) { } class B abc(int x) { }

17 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 <<s1.pop(); s1.push(11); s1.push(22); s1.push(33); cout <<s1.pop(); top st[3] S Go to program

18 Which function is called? s1.push(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 of 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

19 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 Stack::push(var); and return Stack::pop(); Without the scope resolution operator, the compiler would think the push() and pop() functions in Stack2 were calling themselves

20 Inheritance in the Distance Class enum DSign { pos, neg }; //for sign in DistSign class Distance { protected: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() //get length from user { cout > feet; cout > inches; } void showdist() const //display distance { cout << feet << “ : ” << inches ; } }; class DistSign : public Distance //adds sign to Distance { private: DSign sign; //sign is pos or neg public: DistSign() : Distance() //call base constructor { sign = pos; } // 2 or 3 argument constructor DistSign(int ft, float in, DSign sg=pos) : Distance(ft, in) //call base constructor { sign = sg; } //set the sign void getdist() //get length from user { Distance::getdist(); //call base getdist() char ch; //get sign from user cout > ch; sign = (ch==’+’) ? pos : neg; } void showdist() const //display distance { //show sign cout << ( (sign==pos) ? “(+)” : “(-)” ); Distance::showdist(); //ft and in } };

21 Cont. int main() { DistSign alpha; //no-arg constructor alpha.getdist(); //get alpha from user DistSign beta(11, 6.25); //2-arg constructor DistSign gamma(100, 5.5, neg); //3-arg constructor //display all distances cout << “\nalpha = “; alpha.showdist(); cout << “\nbeta = “; beta.showdist(); cout << “\ngamma = “; gamma.showdist(); cout << endl; return 0; } Program Output Enter feet: 6 Enter inches: 2.5 Enter sign (+ or -): - alpha = (-)6 : 2.5 beta = (+)11 : 6.25 gamma = (-)100 : 5.5

22