OOP using C++ 1. 2 Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Cpt S 122 – Data Structures Polymorphism
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
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.
Chapter -6 Polymorphism
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Object-Oriented Programming (OOP) and C++
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Review: Two Programming Paradigms
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
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:

OOP using C++ 1

2

Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input an output 3

Encapsulation Class. Object. Private. Public. Inside a class : variable and operations - – Variables (data) are called data members. – Functions (operations ) are called member functions, function members or methods. This combining of data and operations is called Encapsulation. 4

Introduction to Classes An “object” is an instantiation of a class. Information Hiding: Private members can only be accessed by methods in its class. Public members can be accessed by methods in any class. Development issues: What the program should do? How it could be done? 5

Example 1 class Laptop { private: int cpu_speed; public: int display () { return cpu_speed; } void setup (int new_speed) { cpu_speed = x; } }; 6

Constructor class Laptop { private: int cpu_speed; public: Laptop () // Zero-parameter constructor { cpu_speed = 0; } Laptop (int initial_speed) // One-parameter { cpu_speed = initial_speed; } int display () { return cpu_speed; } void setup (int new_speed) { cpu_speed = new_speed; } }; 7

Example 2 #include using namespace std; class C { public: char getchar [20]; int intval; C (char *s="", int i=0) { strcpy(getchar,s); intval=i;} void fun(int i, char *s) {cout<<i<<" "<<s<<endl;} }; int main () { C obj1("IN", 10); obj1.fun(50,”DATA”); return 0; } 8 cout<<&s<<endl; cout<<*s<<endl;

Inheritance Objects do not have to be instantiations of a single class. 9

Pointer / new / delete 10 A pointer is an address in memory. The & operator (ampersand) changes a thing into a pointer (the address of the thing). The * operator changes a pointer into a thing. { int x;int *x_ptr; x = 5; cout<<x<<endl; cout<<&x<<endl; x_ptr = &x; *x_ptr = 10; cout<<x<<endl; cout<<&x<<endl; cout<<*x_ptr<<endl; cout<<&x_ptr<<endl;} { int x;int *x_ptr; x = 5; cout<<x<<endl; cout<<&x<<endl; x_ptr = &x; *x_ptr = 10; cout<<x<<endl; cout<<&x<<endl; cout<<*x_ptr<<endl; cout<<&x_ptr<<endl;} { int x;int *x_ptr; x = 5; cout<<x<<endl; cout<<&x<<endl; x_ptr = &x; cout<<x<<endl; cout<<&x<<endl; *x_ptr = 10; cout<<*x_ptr<<endl; cout<<&x_ptr<<endl;} { int x;int *x_ptr; x = 5; cout<<x<<endl; cout<<&x<<endl; x_ptr = &x; cout<<x<<endl; cout<<&x<<endl; *x_ptr = 10; cout<<*x_ptr<<endl; cout<<&x_ptr<<endl;}

11

12

Pointers an Arrays Pointer refers to a block of memory that holds on integer Array “is a pointer” refers to a block of memory that holds more than one integer 13

Pointers an copy constructors (Discussion) 14

Pointers to Objects Dell is a pointer to a Laptop. To allocate space, a “new” operator must be used. To reclaim the space use the “delete” operator. Use the “→ ” operator to access the methods. 15 class Laptop { private: int cpu_speed; public: Laptop (int initial_speed = 0) { cpu_speed = initial_speed; } int display () { return cpu_speed; } void setup (int new_speed) { cpu_speed = new_speed; } }; int main() { Laptop *dell = NULL; dell = new Laptop(2); cout display(); dell->setup(3); cout display(); delete dell; return 0; } class Laptop { private: int cpu_speed; public: Laptop (int initial_speed = 0) { cpu_speed = initial_speed; } int display () { return cpu_speed; } void setup (int new_speed) { cpu_speed = new_speed; } }; int main() { Laptop *dell = NULL; dell = new Laptop(2); cout display(); dell->setup(3); cout display(); delete dell; return 0; }

Pointer to functions (self study) 16

Polymorphism different objects 17

18 Pointers to base class

Virtual members A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual: class Base-class { protected: public: virtual type-in-derived-class function-in-derived-class() { return (0); } }; 19

Virtual members cont’d cout << object-to-derived-class.function-in- derived-class() << endl; Rewriting cout Virtual-function-in- derived-or-base-class() << endl; 20

21 Example

Note The member function area() has been declared as virtual in the base class because it will be redefined in each derived class. If the virtual keyword is removed from the declaration of area() within base class, and then the program is implemented, the result will be 0 for the three polygons instead of 20, 10 and 0. A class that declared or inherited a virtual function is called a polymorphic class. 22

Abstract Classes and Pure virtual Functions A class is made abstract by declaring one or more of its virtual functions to be "pure." A pure virtual function is specified by placing "= 0" in its declaration, as in: Example: virtual void draw() const = 0; // pure virtual function The "=0" is known as a pure specifier. Pure virtual functions do not provide implementations. 23

Abstract Classes and Pure virtual Functions (cont’d) The difference between a virtual function and a pure virtual function is that a virtual function has an implementation and gives the derived class the option of overriding the function; by contrast, a pure virtual function does not provide an implementation and requires the derived class to override the function. Pure virtual functions are used when it does not make sense for the base class to have an implementation of a function, but the programmer wants all concrete derived classes to implement the function. 24

Abstract base classes cont’d // abstract class Cpolygon class Cpolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area () =0; }; This type of function is called a pure virtual function, and all classes that contain at least one pure virtual function are abstract base classes 25

Abstract base classes cont’d But a class that cannot instantiate objects is not totally useless. We can create pointers to it and take advantage of all its polymorphic abilities. Therefore a declaration like: CPolygon poly; would not be valid for the abstract base class we have just declared, because tries to instantiate an object. Nevertheless, the following pointers: CPolygon * ppoly1; CPolygon * ppoly2; would be perfectly valid. 26 In main function

27 Example

Dynamic binding or Late binding (cont’d) If the program invokes a virtual function through a base-class pointer to a derived-class object (e.g., shapePtr->draw ()), the program will choose the correct derived-class draw function dynamically (i.e., at execution time) based on the object type not the pointer type. Choosing the appropriate function to call at execution time (rather than at compile time) is known as dynamic binding or late binding. 28

Dynamic binding or Late binding (cont’d) When a virtual function is called by referencing a specific object by name and using the dot member-selection operator (e.g., squareObject.draw()), the function invocation is resolved at compile time (this is called static binding) and the virtual function that is called is the one defined for (or inherited by) the class of that particular object this is not polymorphic behavior. Thus, dynamic binding with virtual functions occurs only off pointer handles. 29

The Standard Template Library Containers Iterates Algorithms 30

CONTAINER is a class, a data structure, or an abstract data type (ADT) whose instances are collections of other objects. In other words; they are used for storing objects in an organized way following specific access rules. a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently 31

stack data structure could be defined by three operations: – push, that inserts some data item onto the structure – pop, that extracts an item from it – peek, that allows data on top of the structure to be examined without removal. 32

ITERATES is an object used to reference an element stored in container. 33

ALGORITHMS The STL provides 70 generic functions, caled algorithms, that can be applied to the STL containers and to arrays example: Indicating the position of element e1 in the rang i1 up to excluding i2. 34

Read only 35