CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.

Slides:



Advertisements
Similar presentations
1 Object-Oriented Programming Concepts. 2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result.
Advertisements

OBJECT-ORIENTED PROGRAMMING CONCEPTS (Review). What is an Object? What is an Object? Objects have states and behaviors. Example: A dog has states - color,
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
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.
EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Object-oriented Programming Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
LECTURE 07 Programming using C# Inheritance
1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Module 7: Essentials of Object-Oriented Programming.
Chapter 4 Objects and Classes.
Introduction to Object-oriented programming and software development Lecture 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Information System Design (IT60105) Lecture 26 Object-Oriented System Testing.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts.
: Maha Sabri Altememe Lecturer : Maha Sabri Altememe Lecture :1 1.
Java Object Oriented Programming Encapsulation, Inheritance, Polymorphism (Java: An Eventful Approach - Ch. 17, 21.7), Slides Credit: Bruce, Danyluk and.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Object Oriented Programming in Java Habib Rostami Lecture 2.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
Polymorphism.
Polymorphism.
Object Oriented Analysis and Design
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Object-Oriented Programming
Presentation transcript:

CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

Object-Oriented Programming Concepts The Object Model –Object, Attributes, Methods, Messages –Encapsulation –Class & Instance Inheritance –Base class, Derived class Polymorphism –Dynamic binding –Static binding

The Object Model

Object = Attributes + Methods message attrib utes method

How OO Programs Work - Based on message passing among objects - In C++, sending/receiving a message corresponds to a member function call.

Example 1: Bicycle object change_gears change_cadence brake - speed - cadence - gear (cadence: how frequent you pump the pedal.)

class Bicycle { private: float speed; float cadence; int gear; public: void change_gears(int gear); void break(); void chage_cadence(float cadence); }; In C++,... member variables (attributes) member functions (methods)

Example 2: CD Player Object (from the lab assignment) CD player object bool powerOn; bool trayOpen; bool mediaLoaded; bool playing; void pushPower(void); void pushEject(void); void pushPlay(void); void pushStop(void); void loadUnloadMedia(void); messages interface attributes methods

In C++,... member variables (attributes) member functions (methods) class CDPlayer { private: bool powerOn; bool trayOpen; bool mediaLoaded; bool playing; public: void pushPower(); void pushEject(); void pushPlay(); void pushStop(); void loadUnloadMedia(); };

Attributes : data that describe the internal status of an object –“member variables” in C++ –inaccessible from outside  Encapsulation –also known as “state”, etc. Methods : functions which can access the internal status of an object –“member functions” in C++ –accessible from outside –manipulates attributes –also known as “behavior”, etc. The Object Model Object = Attributes + Methods

Encapsulation change gears change cadence brake - speed - cadence - gear User Users should use methods to manipulate objects. Users should not access attributes directly. Access to the attributes is done by methods. Methods can keep the consistency of the attributes. OK NO! OK

Class & Instance change gears brake - speed - cadence - gear change gears brake - speed = 15 [mph] - cadence = 60 [rpm] - gear = 3rd change cadence change cadence Class Instance -A blueprint of an object -No occupied memory space (just a declaration) -An actually created object -Requires memory space to store attributes Instantiate

Instance and member variables instance 1 - cadence = 60 [rpm] - gear = 3rd Different instances can have different values of member variables - speed = 4 [mph] - cadence = 12 [rpm] - gear = 1st - speed = 35 [mph] - cadence = 80 [rpm] - gear = 5th instance 2 instance 3 - speed = 10 [mph] class Bicycle

Inheritance

MountainBike We can make “specialized” version of Bicycles by adding more features (attributes, methods) Base class (Super class) RacingBike TandemBike Bicycle Derived classes (Subclasses) (this means inheritance)

Benefits from Inheritance Enhances extensibility of programs –class library, application frameworks (Ex. MFC etc.) provides “base” classes to create new classes specific to new problem domain you can create new classes by adding new features to existing classes Simplifies programs –enables generic programming

Inheritance Ex. 1: Telephone Objects class Telephone hangupStatus pickUp() dial() hangUp() class SpeakerTelephone hangupStatus pickUp() dial() hangUp() speakerVolume volumeUp() volumeDown() class WirelessTelephone hangupStatus pickUp() dial() hangUp() antennaStatus extendAntenna() shrinkAntenna() class Voic Telephone hangupStatus pickUp() dial() hangUp() preservedMassages listenMessage() deleteMessage() Superclass (Base Class) Subclasses (Derived Classes) attributes methods

General or Specific Base classes - general - less members - smaller object size Derived Classes - specific - more members - larger object size class Telephone class SpeakerTelephone class WirelessTelephone class Voic Telephone class SpeakerRedialTelephone Inheritance is called “is-a” relationship. (cf. “has-a” relationship) Ex. “ WirelessTelephone is a kind of Telephone.”

Inheritance Ex. 2: Graphics Objects Shape RectangleCrossEllipse virtual void Shape::draw(const Canvas*); void Rectangle::draw(const Canvas*); void Cross ::draw(const Canvas*); void Ellipse ::draw(const Canvas*); extensible...

Polymorphism

Polymorphism Ex. 1: Drawing Tool RectangleTriangle Ellipse

What we want to do ? Shape* shapes[10] Put them in a “generic” container, and... RectangleTriangle Ellipse

What we want to do ? Shape* shapes[10]... manipulate them by sending the same message “draw” CharScreen s(20,10); for (i = 0; i < numShapes; i++) { shapes[i]->draw(s); } “draw”

What we want to do ? Shape* shapes[10] But, how to draw() each object should be different... “draw” void Rectangle::draw() { // draw a rectangle... } void Triangle::draw() { // draw a triangle... } void Ellipse::draw() { // draw an ellipse... } “draw” So, a proper draw() function for each object needs to be automatically chosen in runtime (dynamic binding).

Polymorphism Ex. 2:Operator ‘ = ‘ and ‘ + ’ int x = 1; int y = 2; int z; What should happen if you say z = x + y; for each case? String x=”abc”; String y=”def”; String r; double x = 1.2; double y = 3.4; double z; Complex x(1.0, 2.0); Complex y(2.0, 3.0); Complex z; Different types of ‘ + ’ and ‘ = ‘ operations should be executed based on the data type. Which function is used can be determined in compile time (static binding).

Polymorphism For a given (same) message, to respond in a different way based on the receiver’s object type. dynamic binding static binding to appear in the later lecture…