Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.
C++ fundamentals.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Chapter -6 Polymorphism
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Exception Handling How to handle the runtime errors.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
LESSON 2 Basic of C++.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
A Sample Program #include using namespace std; int main(void) { cout
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Basic concepts of C++ Presented by Prof. Satyajit De
MT262A Review.
Class and Objects UNIT II.
Command Line Arguments
Learning Objectives Pointers Pointer in function call
Introduction to C++ October 2, 2017.
Programming Fundamentals
C++ Arrays.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
1. Structures 2. Abstract Data Types
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.
CS150 Introduction to Computer Science 1
Counting Loops.
Lecture 12 Oct 16, 02.
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Introduction to Programming
Testing with OO OO has several key concepts:
Wednesday 09/23/13.
CS150 Introduction to Computer Science 1
Introduction to Programming
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
TOPIC: FUNCTION OVERLOADING
CS150 Introduction to Computer Science 1
Presentation transcript:

Lecture 20 Polymorphism

Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a variety of representations. –Entities which exhibit Polymorphism are Type 1: Variables Type 2: Functions Type 3: Objects

Polymorphism Type 1 Definition: The concept of dynamic binding allows a variable to take different types of values EG: int input; : input = 100 ; // same variable used to store integer and character values : : input = ‘Hello’;

Polymorphism Type 2  Definition: If a function is defined by the combination of its name and its parameters then it is called polymorphism. Examples: Next slide….

Polymorphism Type 2 : Sample program 1 #include int add (int a, int b) { int c; c = a + b; return c; } int add (int d, int e, int f) {int g; g = d + e + f; return g; } void main() {int i,j,k,l,m,n,p; i = 4;j = 6; k=add(i,j); cout<<"Sum of two no. is ” << k << endl; l=5;m=6;n=7; p=add(l,m,n); cout << "Sum of three no. is ” << p << endl; } Output: Sum of two no. is 10 Sum of three no. is 18 Different Number of Arguments

Polymorphism Type 2 : Sample program 2 #include int add (int a, int b) { int c; c = a + b; return c; } void add (float d, float e) { float g; g = d + e; cout << "Sum of two float no. is ”<< g << endl; } void main() { int i, j, k; float l, m; i = 4; j = 6; k=add(i, j); cout << "Sum of two int no. is ”<< k <<endl; l=5.2;m=6.4; add(l, m); } Output: Sum of two int no. is 10 Sum of two float no. is 18 Same Number of arguments but different data types

Polymorphism Type 2 : Sample program 3 #include class Patient { private: int IdNumber; char Name; public: Patient (); Patient(int,char); void Displaydetails(); }; Patient :: Patient() {cout<<”Enter number and name: "; cin >> IdNumber >> Name; } Patient::Patient(int id, char nam) { IdNumber = id; Name = nam; } void Patient::Displaydetails() { cout <<“Patient No: “<<IdNumber <<endl; cout<<“Patient Name: “<<Name<<endl<<endl; } void main() { Patient p1(267,’B'); Patient p2; p1.Displaydetails(); p2.Displaydetails(); } Output: Enter number and name: 8678 H Patient No: 267 Patient Name: B Patient No: 8678 Patient Name: H Member functions name are same in the class with different number of arguments

Polymorphism Type 3  The method (function) to be invoked can depend on the object.  EG : Class A { add( ) } Class B { add( ) } Class C { add( ) } Main( ) A firstobject B secondsobject firstobject.add( ); Secondbject.add( );

Polymorphism Type 3 #include class Patient { public: int IdNumber; char Name; void Setdetails (int I, char N) { IdNumber = I; Name = N;} void Displaydetails() { cout<<endl<<"Patient:"<<IdNumber <<Name; } }; // end class Patient class InPatient : public Patient { private: int Wardnumber; int Daysinward; public: void Setdetails (int I, char N, int W, int D) { IdNumber = I;Name = N; Wardnumber = W; Daysinward = D;}; void Displaydetails() { cout<<endl<<"Inpatient:"<<IdNumber<< Name<<Wardnumber<<Daysinward; } }; class OutPatient : public Patient { private : int BP; public : void Setdetails(int I, char N, int B) {IdNumber = I; Name = N; BP = B; } void Displaydetails() {cout<<endl<<"Outpatient:"<<IdNumber <<Name<<BP; } }; void main() { Patient p1; p1.Setdetails(111,’A'); p1.Displaydetails(); InPatient p2; p2.Setdetails(333,’Z',12,14); p2.Displaydetails(); OutPatient p3; p3.Setdetails(444,’Y',140); p3.Displaydetails(); }