Object-Oriented Programming with C++ Yingcai Xiao.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
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.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Inheritance Inheritance Reserved word protected Reserved word super
Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
LECTURE 07 Programming using C# Inheritance
What is Object-Oriented Programming?. Objects – Variables and Logic inside "Objects", not standalone code – Objects contain related variables and functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Inheritance in the Java programming language J. W. Rider.
Polymorphic support in C# Let us now examine third pillar of OOP i.e. Polymorphism.. Recall that the Employee base class defined a method named GiveBonus(),
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.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Lecture 5 : Inheritance & Polymorphism Acknowledgement : courtesy of Prof. Timothy Budd lecture slides.
ISBN Object-Oriented Programming Chapter Chapter
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter - 4 OOP with C# S. Nandagopalan.
CS3340 – OOP and C++ L. Grewe.
Polymorphism.
Inheritance & Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object Oriented Analysis and Design
Chapter 10 Thinking in Objects
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
The Object Paradigm Classes – Templates for creating objects
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Object-Oriented Programming with C++ Yingcai Xiao

Code Reuse Inheritance Encapsulation Polymorphism

Two ways of code reuse: composition & inheritance Composition (“has-a” relationship): An object of a class has an object of another class in it. Use predefined code of a class by creating an object of the class. Inheritance (“is-a” relationship): A child class inherits everything from its parent class. Use predefined code of a class by sub-classing from it.

class CWheel { private: float air-pressure; public: void inflate(); … }; // predefined class class CPassengerCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: … };

class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; // predefined class class CPassengerCar : public CCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: … }; “a passenger car is a car” “a truck is a car” “a race car is a car”

class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; Grouping data and operations (functions/methods) together to facilitate code reuse. Controlling the access of the members. (Private members are hidden and can only be accessed by the member functions of the class. Users can only use the given public functions to alter the private data members).

class CCar { protected: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; Protected members are hidden and can only be accessed by the member functions of the class and its decedent classes.

class CShape { private: int color; public: virtual float area() { }; // a virtual function can be overridden by the children }; One method behaves differently for different objects. class CCircle : public CShape{ public: float area() {return (pi * r * r)}; }; class CRect : public CShape{ public: float area() {return (w*h)}; };

CShape *shapes[4]; CCircle *c1 = new CCircle(5); CCircle *c2 = new CCircle(10); CRect *r1 = new CRect(4,5); CRect *r2 = new CRect(8,5); shapes[0] = (CShape *) c1; // cast to parent allowed shapes[1] = (CShape *) c2; // cast to parent allowed shapes[2] = (CShape *) r1; // cast to parent allowed shapes[3] = (CShape *) r2; // cast to parent allowed for (I=0; I area() << endl; One function behaves differently for different objects.

CObject in MFC is the parent of all classes in an MFC based application. Therefore MFC can use a loop to process the objects in the application without knowing how the classes in the application are defined as long as they are subclassed from CObject and follow the standards.

Polymorphism makes it possible for objects of different child classes to have the same interface. To enforce a standard interface, one can make the parent class an abstract class and subclass from it. An abstract class is a class containing a pure virtual function. One can’t instantiated an abstract class. It is just for setting up standard interfaces. Its child classes must override and implement the pure virtual function. (A regular virtual function is not required to be overridden by the child classes and it is inherited if it is not overridden.) Setting up standard interfaces

class CShape { public: virtual float area() = 0; // a pure virtual function }; // CShape a-shape; not allowed. class CCircle : public CShape{ public: float area(); // must be implemented. }; class CRect : public CShape{ public: float area(); // must be implemented. };

Two types of code reuse: inclusion (has-a) inheritance (is-a) Three pillars of OOP: encapsulation (to facilitate code reuse) inheritance (code reuse) polymorphism (to facilitate code reuse)