Object Oriented Programming Elhanan Borenstein Lecture #6.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
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.
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #8 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein.
Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
C++ data types. Structs vs. Classes C++ Classes.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Abstract Data Types and Encapsulation Concepts
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Object Oriented Programming Elhanan Borenstein Lecture #4.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Object Oriented Programming Elhanan Borenstein Lecture #5.
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.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Overview of C++ Polymorphism
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Object-Oriented Programming (OOP) Lecture No. 45
Operator overloading Conversions friend inline
Inheritance & Polymorphism
11.1 The Concept of Abstraction
Object Oriented Programming
Memberwise Assignment / Initialization
Understanding Inheritance
Virtual Functions Department of CSE, BUET Chapter 10.
Chapter 8: Class Relationships
Overview of C++ Polymorphism
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C++ data types.
SPL – PS3 C++ Classes.
11.1 The Concept of Abstraction
SPL – PS4 C++ Advanced OOP.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Object Oriented Programming Elhanan Borenstein Lecture #6

Agenda Operator Overloading – Cont’ Inheritance – Part 1

Operators Overloading

Most operators cannot be automatically applied to objects though. (which can?) However… we can define (overload) all available operators for classes too. For example: Operator Overloading Example class CPoint { int m_x, m_y; public: CPoint(int ax, int ay):m_x(ax), m_y(ay){} CPoint operator+(const CPoint& p) const; }; CPoint CPoint::operator+(const CPoint& p) const { return CPoint(m_x + p.m_x, m_y + p.m_y); }

Operators Overloading An operator should be overloaded only when its behavior is obvious. Otherwise, a function should be used. An overloaded operator should work in an analogous manner to the fundamental types. It should also have the same behavior. Efficiency is essential:  Object parameters will pass ByRef  Object parameters that should not be changed will be defined as const  An operator that returns a local object should return it ByVal (preferably as a temp object within the return command)  Operators that returns the activating object or an object that was passed as a parameter should return it ByRef Guidelines

Operators Overloading Operators can be overloaded in two manners: 1) As a method (member function) – An activating object is mandatory (parameters are optional)  The assignment operator can be overloaded only as a method. 2) As a global function - Only parameters  We will usually prefer to define the function as a friend function Access to private data members Declaration within the class  In some cases, we must use global function rather than a method. When using a global function, there is always an additional parameter. (the first parameter will substitute for the activating object) Methods (Member Functions) vs. Friend Functions

Operators Overloading The CArray class. Example

Operators Overloading The ->, * Operators for Smart Pointer class Data { … public: void Init(){…} }; class DataPtr { Data* pd; public: DataPtr(Data* p) : pd(p){ } Data* operator->() { return pd; } Data& operator*() { return *pd; } operator Data*() { return pd; } }; void func1(const Data* p); void func2(const Data& d); void main() { Data d; DataPtr p = &d; p->Init(); func1(p); func2(*p); }

Inheritance

Inheritance is one of the fundamental features of OOP. In many cases we wish to extend an existing class (without replicating its source code), but … We may be unable (or unwilling) to change the original base class:  The existing base class is still in use in its original form.  The original base class works fine (and we do not want to introduce new bugs)  There is no access to the original class source code (only the.obj and header files exists)  There are similar entities (with a common base) we wish to represent. (  polymorphism) The resulting code is easier to read and maintain and does not include redundant code Motivation

Inheritance Example class Base1 { int var1; public: Base1() {…} // constructor void OldFunc() {…} void Init() {…} }; class Derived1 : [public|protected|private] Base1 { int var2; public: Derived1() {…} // constructor void Init() {…} // override void Do(){…} };

Inheritance When inheriting a class, we inherit all its members and they will be included in the derived class: Somewhat similar to object data members (embedded): To differentiate, we can check the logics behind the implemented entities:  Inheritance: The derived class is a type of the base class.  Inner class: The inner class is a part of the external class. The Logic behind Inheritance Base (father) Additions Derived (son)  (SIZE?) Additions embedding class inner object

Inheritance Understanding Inheritance - Example class GraphicItem { int color; public: GraphicItem() {…} // constructor void Draw() {…} void ChangeColor(int n_clr) {…} }; class CPoint : public GraphicItem { int m_x, m_y; public: CPoint() {…} // constructor void Draw() {…} // override void Align(){…} }; … GraphicItem G, *pG; CPoint P, *pP; … G = P; P = G; pG = pP; pP = pG; …

Inheritance Inheritance is a logical specification (narrowing) of an entity, but… usually a physical extension of it. The derived class can override methods that were inherited from the base class. In this lecture, we will focus on public inheritance. When inheriting a class, we inherit all its members - partial inheritance is not possible. …However, we may not be able to access all of them. Inheritance Concepts

Inheritance All the data members of the base class also exists in the derived class. But…:  There are cases where we cannot access these members.  They will still be included in the derived object size. When won’t we be able to access the base class members:  Hiding – The derived class has defined a data member with the same name. We can still access the hidden data member using its full name (base_class::member)  Private Permissions – Any private member of the base class cannot be accessed in the derived class. Note: the private data member is still included in the derived class (and can be accessed using a public method of the base class). A good design, will include in the base class only data members that are common (and can be accessed) by all derived classes. Accessing the Base Class Data Members

Inheritance The derived class also inherit all the base class methods. The derived class can override inherited methods. When activating a method of an object from the derived class: > If the inherited method was not overridden the base method (from the base class) will be activated. > If the method was overridden in the derived class, the method of the derived class will be activated.  We can still activate the base method using its full name.  It is very common to include a call to the base method from the derived method. C’tor, D’tor and C.C. are the only methods that have a different behavior when inherited. Using the Base Class Member Functions

Inheritance C’tor and D’tor are not inherited (WHY?). Constructing an object of the derived class, always activates first the C’tor of the base class (to create the base part of the object). The order is thus as follows:  The C’tor of the base class is activated.  The additional data members of the derived class are created.  If some of the data members are objects  the C’tor of each such object is activated.  The C’tor of the derived class is activated The order of the D’tor’s activation is of course reversed. What if the C’tor of the base class has parameters? C’tor and D’tor in Inherited Classes

Inheritance If the C’tor of the base class has parameters we must use the initialization line (in an analogous manner to object data members): C’tor and D’tor in Inherited Classes – Cont’ class CRect { CPoint pnt1, pnt2; public: CRect(int x1, int y1, …) : pnt1(x1,y1), pnt2(0,0) {…} // c’tor }; class CGraphicRect : public GraphicItem { … public: CGraphicRect(int color) : GraphicItem(color) {…} // c’tor }; Embedded Objects Inheritance

The assignment operator in not inherited. If the derived class did not implement the assignment operator, when copying, the base class assignment operator will not be activated but rather the default assignment operator of the derived class. But… as part of the default assignment operator action, (when the base class members should be copied), the base class assignment operator will be activated. Example: Stack Assignment Operator (=) in Inherited Classes

Inheritance Rule of thumb: A derived class should implement an assignment operator only if it performs dynamic allocation. The same is true for the copy constructor and the destructor. Remember the triplet:  Copy Constructor  Destructor  Assignment Operator Example: AB.cpp Assignment Operator (=) in Inherited Classes

Questions?