Inheritance: The Fundamental Functions

Slides:



Advertisements
Similar presentations
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 14: Overloading and Templates
Classes Separating interface from implementation
Operator Overloading and Memory Management. Objectives At the conclusion of this lesson, students should be able to: Overload C++ operators Explain why.
Data Structures Using C++ 2E
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 CSC241: Object Oriented Programming Lecture No 03.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Learners Support Publications Constructors and Destructors.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Memory Management.
Chapter 2 Objects and Classes
Constructors and Destructors
C++ Programming:. Program Design Including
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.
Programming with ANSI C ++
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
CS Data Structures Chapter 8 Lists Mehmet H Gunes
CISC181 Introduction to Computer Science Dr
C++ Object-Oriented Programming
System Programming Practical Session 8 C++ classes.
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
Classes with Dynamically Allocated Data
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Contents Introduction to Constructor Characteristics of Constructor
Chapter 15 Pointers, Dynamic Data, and Reference Types
Inheritance: Polymorphism and Virtual Functions
Constructors and Destructors
Doubly Linked List Implementation
Dynamic Data Structures
Summary: Abstract Data Type
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming (OOP) Lecture No. 22
Lecture 16 Section 6.2 Thu, Mar 1, 2007
9-10 Classes: A Deeper Look.
Chapter 8: Class Relationships
NAME 436.
Classes: A Deeper Look, Part 1
Inheritance: Polymorphism and Virtual Functions
The Constructors Lecture 7 Fri, Feb 2, 2007.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
ENERGY 211 / CME 211 Lecture 22 November 10, 2008.
9-10 Classes: A Deeper Look.
Inheritance in C++ Inheritance Protected Section
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
Presentation transcript:

Inheritance: The Fundamental Functions Lecture 24 Mon, Mar 26, 2007 2/17/2019 Inheritance

Inheritance of Constructors A derived-class constructor will automatically invoke the base-class default constructor, unless instructed otherwise. The base-class constructor is invoked before the derived-class constructor is executed. The derived-class constructor may invoke a specific base-class constructor. 2/17/2019 Inheritance

Inheritance of Constructors Suppose we create a List class and then derive an ArrayList class and a LinkedList class from it. List ArrayList LinkedList 2/17/2019 Inheritance

Inheritance of Constructors Since ArrayList and LinkedList have mSize in common, we could put mSize in the List class. class List {int mSize;} class ArrayList {int* element;} class LinkedList {Node* head;} 2/17/2019 Inheritance

Inheritance of Constructors When we construct an ArrayList, what happens? The ArrayList constructor will first call a List constructor which will initialize mSize. Then it will initialize element. 2/17/2019 Inheritance

Inheritance of Constructors Which List constructor will it call? Unless we specify otherwise, it will call the default List constructor. 2/17/2019 Inheritance

Invoking the Base Class Constructor How do we specify a different constructor? We do it through an initializer. ArrayList(parameters) : List(parameters) {body of the ArrayList constructor} 2/17/2019 Inheritance

Invoking the Base Class Constructor ArrayList(int sz, int value) : List(sz) { if (mSize == 0) element = NULL; else element = new int[mSize]; for (int i = 0; i < mSize; i++) element[i] = value; } return; 2/17/2019 Inheritance

Inheritance of Destructors The derived-class destructor automatically invokes the base-class destructor. The base-class destructor is invoked after the derived-class destructor is executed. 2/17/2019 Inheritance

Inheritance of the Assignment Operator The automatic assignment operator invokes the assignment operator for the base class. A programmer-defined assignment operator must copy the base-class members. This is a problem if the base-class members are private. 2/17/2019 Inheritance

Inheritance of the Assignment Operator One can use the scope operator :: to invoke the assignment operator of the base class. class A {private: int a;}; class B : class A {private: int b;}; B& B::operator=(const B& x) { a = x.a // Illegal A::operator=(x); // Legal b = x.b; } 2/17/2019 Inheritance

Inheritance of the Assignment Operator See the tip about the assignment operator. 2/17/2019 Inheritance

Inheritance of the Assignment Operator Suppose we create a class ArrayListwPos, which is an ArrayList that keeps track of a current position. ArrayListwPos has one additional data member int currPos – the current position in the list. 2/17/2019 Inheritance

Inheritance of the Assignment Operator ArrayListwPos& operator=(const ArrayListwPos& list) { if (this != &list) ArrayList::operator=(list); currPos = list.currPos; } return *this; 2/17/2019 Inheritance

Example: Inheritance Header files person.h man.h woman.h father.h mother.h marriedman.h marriedwoman.h 2/17/2019 Inheritance

Example: Inheritance Programs DefaultPeople.cpp FamilyTree.cpp TheFirstFamily.cpp ChangingPlaces.cpp 2/17/2019 Inheritance