MAITRAYEE MUKERJI Object Oriented Programming in C++

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
1 CSC241: Object Oriented Programming Lecture No 07.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
1 CSC241: Object Oriented Programming Lecture No 13.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
1 CSC241: Object Oriented Programming Lecture No 25.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Overview of C++ Templates
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
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
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Overview of C++ Polymorphism
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CSC241: Object Oriented Programming Lecture No 08.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Data Structure Lecture 1.  The Logical Organization of Data is called data structures  organize data  more efficient programs.  More powerful computers.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Programming with ANSI C ++
C++ Templates.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generic programming – Function template
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
ADT Implementations: Templates and Standard Containers
Name: Rubaisha Rajpoot
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
Templates I CMSC 202.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class: Special Topics Overloading (methods) Copy Constructors
TOPIC: FUNCTION OVERLOADING
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

MAITRAYEE MUKERJI Object Oriented Programming in C++

Polymorphism Poly: Multiple, Many Morphism: Forms, Behavior Polymorphism  One thing with several distinct forms or behavior  In C++ refers to way in which operators or functions can behave depending on what they are operating on:- standard data types, user defined data types, same function but with different signatures (number and type of arguments), objects belonging to base or derived classes

Polymorphism Compile Time Polymorphism  Operator Overloading  Function Overloading Run Time Polymorphism  Function Overriding Templates

Operator Overloading Operator overloading refers to giving the normal C++ operators (+, *, <=, ++ etc.) additional meaning when they are applied to user defined data types. Example: MyObject d1, d2, d3; d3 = d1 + d2; Extending C++  Using classes to create new kinds of variable  Using operator overloading to create new definitions for operators

Overloading Unary Operator Counter is a variable that counts things  Nodes, persons, file access etc. Each time an event takes place, the counter is incremented by 1 The Counter can be accessed to find the current count

// object represents a counter variable Class Counter { private: unsigned int count; public: Counter () : count (0); {} int get_count() { return count;} void inc_count() { count++; } } Constructor and Initialization to zero Since count is always positive

Main () {Counter C1; cout << “\n C1=“ << C1.getCount(); C1.inc_count (); cout << “\n C1=“ << C1.getCount(); cout << endl; return 0; } ++C1;

// object represents a counter variable Class Counter { private: unsigned int count; public: Counter () : count (0); {} int get_count() { return count;} void inc_count() {count++;} } void operator ++ () {++ count; } The keyword operator is used for overloading the ++ operator

Overloading Binary Operators Adding and Subtracting Two Complex Numbers C1 = 4 + 3i C2 = 3 + 6i CAdd = C1+C2 CSub = C1-C2

struct Complex { floatreal; floatimag; }; int main() { Complex C1, C2,C3; C1.real = 4; C1.imag = 2; C2.real = 5; C2.imag = 4; C3.real = C1.real + C2.real; C3.imag = C1.imag + C2.imag; cout << C3.real << " " << C3.imag ; C3.real = C1.real - C2.real; C3.imag = C1.imag - C2.imag; cout << C3.real << " " << C3.imag ; return 0; } Complex operator+ (const Complex &C1, const Complex &C2) {Complex C3; C3.real = C1.real + C2.real; C3.imag = C1.imag + C2.imag; return C3; } Complex operator- (const Complex &C1, const Complex &C2) {Complex C3; C3.real = C1.real - C2.real; C3.imag = C1.imag - C2.imag; return C3; } C3 = C1 + C2 C3 = C1 - C2

Function Overloading An overloaded function can perform different activities depending on the number and type of data sent to it. More than one function with the same name but  Different type of input argument  Different number of input arguments  Different number and type of input arguments

// Example: Different Type of Input Arguments #include using namespace std; void printNumber (int x) { cout << “I am printing an Integer” << x << endl; } void printNumber (float x) { cout << “I am printing a Float” << x << endl; } int main() { int a= 54; float b = ; printNumber (a); printNumber (b); }

// Different number of input arguments // Printing a line of characters void printChar(); //prints “*”, 45 times in a row void printChar(char); // prints the specified character, 45 times in a row void printChar (char, int); // prints the specified character, #times specified by user int main( ) { printChar (); printChar (‘=‘); printChar (‘+’, 15); return 0; } void printChar( ) { for (int j = 0; j<45; j++) cout << “*”; cout << endl; }

void printChar( char ch) { for (int j = 0; j<45; j++) cout << ch; cout << endl; } void printChar( char ch, int n) { for (int j = 0; j<n; j++) cout << ch; cout << endl; }

Inheritance & Polymorphism “many form” : ability of a variable to take different types In C++, applied mainly on pointer variables  S: Base Class  T: Derived Class  p: Pointer to class S -> p can point to any object belonging to any derived class T of S  a( ): Virtual function in both S and T  Function Call: p->a() invokes  S::a() if p points to object of class S  T:: a () if dynamic binding is used and p points to object type T – T is said to overide function a() from S

Example class BaseA { public: int a; void printData () { cout << "Printing Data In Base Class: " << a << endl; } void printMsg () { cout << "Printing Msg in Base Class: " << "Hello World" << endl;} }; class DerivedA : public BaseA { public: void printData (){ cout << "Printing Data In Derived Class: " << a << endl;} };

Example …contd Code Fragment 1Code Fragment 2Code Fragment 3 BaseA* pBase; pBase = new (BaseA); pBase->a = 100; pBase->printData(); pBase->printMsg(); BaseA* pBase; pBase = new (DerivedA); pBase->a = 200; pBase->printData(); pBase->printMsg(); DerivedA* pDerived pDerived = new (DerivedA); pDerived->a = 300; pDerived->printData(); pDerived->printMsg(); Printing Data In Base Class: 100 Printing Msg in Base Class: Hello World Printing Data In Base Class: 200 Printing Msg in Base Class: Hello World Printing Data In Derived Class: 300 Printing Msg in Base Class: Hello World

Example class BaseA { public: int a; virtual void printData () { cout << "Printing Data In Base Class: " << a << endl; } void printMsg () { cout << "Printing Msg in Base Class: " << "Hello World" << endl;} }; class DerivedA : public BaseA { public: virtual void printData (){ cout << "Printing Data In Derived Class: " << a << endl;} };

Example …contd Code Fragment 1Code Fragment 2Code Fragment 3 BaseA* pBase; pBase = new (BaseA); pBase->a = 100; pBase->printData(); pBase->printMsg(); BaseA* pBase; pBase = new (DerivedA); pBase->a = 200; pBase->printData(); pBase->printMsg(); DerivedA* pDerived pDerived = new (DerivedA); pDerived->a = 300; pDerived->printData(); pDerived->printMsg(); Printing Data In Base Class: 100 Printing Msg in Base Class: Hello World Printing Data In Derived Class: 200 Printing Msg in Base Class: Hello World Printing Data In Derived Class: 300 Printing Msg in Base Class: Hello World

MAITRAYEE MUKERJI Object Oriented Programming in C++: Templates

Templates Templates make it possible to use one function or class to handle many different data types Another way of implementing polymorphism  Function Template  Class Templates  Standard Template Libraries

Function Overloading - Recap Function that returns absolute values of two numbers. int abs (int n) {return (n<0) ? -n: n; } float abs (float n) {return (n<0) ? -n: n;} double abs (double n) {return (n<0) ? -n: n;} Repetitive, time-consuming, long code Error, if found, needs to be corrected in all codes

Function Template Function Template: an automatic mechanism provided by C++, to produce a generic function for an arbitrary type T. A function template provides a well-defined pattern from which a concrete function may later be formally defined or instantiated.

Function Template - Example template anyType abs (anyType n) {return (n<0) ? –n: n; } anyType abs (anyType n) {return (n<0) ? –n: n; } anyType abs (anyType n) {return (n<0) ? –n: n; } int float template parameter declaration

Instantiation C++ does not compile the template function directly. Instead, at compile time, when the compiler encounters a call to a template function, it replicates the template function and replaces the template type parameters with actual types! The function with actual types is called a function template instance.

Function Templates – example Function to find the minimum of two numbers int intMin(int a, int b) // returns the minimum of a and b { return (a < b ? a : b); } template T genMin(T a, T b) { // returns the minimum of a and b return (a < b ? a : b); }

Example..contd main () { int a = genMin (2,10); cout << a << endl; double b = genMin (4.6, 6.4); cout << b << endl; return 0; }

Function Template Template functions for swapping two values Template function for searching an array for a specific value. The function returns the array index for the value if it finds it, otherwise -1

Function Template // Swapping two numbers template void myswap (T& tmp1, T& tmp2) {T temp; temp = tmp1;tmp1 = tmp2;tmp2 = temp; return 0;}

Function Template – Array Search // searching for a value in an array Template int find(atype* array, atype value, int size) { for (int j=0; j<size; j++) if (array[j] == value) return j; return -1; }

Class Template In C++ we can have class templates also It allows us to provide one data structure declaration / data storage that can be applied to many different types. Standard Template Library uses class templates extensively.

Class Template Example class Stack {private: int st[MAX]; int top; public: Stack() { top = -1;} void push (int var); {st[++top] = var;} int pop(); {return st[top--]; } } T T T template

Class Template Example template Stack :: Stack() {top = -1} template Stack :: Push(Type var) { st[++top] = var; } template Type Stack :: Pop() { return st[top --];}

Instantiation Classes are instantiated by defining an object using the template argument Stack s1; Stack s2;

Example Class Node { public: Node(string s); private: string data; Node* previous; Node* next; };

Contd. template Class Node { public: Node(T s); private: T data; Node * previous; Node * next; };

Reference Lafore Horstmann