Download presentation
Presentation is loading. Please wait.
Published byLionel Wright Modified over 8 years ago
1
MAITRAYEE MUKERJI Object Oriented Programming in C++
2
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
3
Polymorphism Compile Time Polymorphism Operator Overloading Function Overloading Run Time Polymorphism Function Overriding Templates
4
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
5
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
6
// 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
7
Main () {Counter C1; cout << “\n C1=“ << C1.getCount(); C1.inc_count (); cout << “\n C1=“ << C1.getCount(); cout << endl; return 0; } ++C1;
8
// 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
9
Overloading Binary Operators Adding and Subtracting Two Complex Numbers C1 = 4 + 3i C2 = 3 + 6i CAdd = C1+C2 CSub = C1-C2
10
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
11
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
12
// 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 = 34.5678; printNumber (a); printNumber (b); }
13
// 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; }
14
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; }
15
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
16
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;} };
17
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
18
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;} };
19
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
20
MAITRAYEE MUKERJI Object Oriented Programming in C++: Templates
21
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
22
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
23
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.
24
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
25
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.
26
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); }
27
Example..contd main () { int a = genMin (2,10); cout << a << endl; double b = genMin (4.6, 6.4); cout << b << endl; return 0; }
28
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
29
Function Template // Swapping two numbers template void myswap (T& tmp1, T& tmp2) {T temp; temp = tmp1;tmp1 = tmp2;tmp2 = temp; return 0;}
30
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; }
31
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.
32
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
33
Class Template Example template Stack :: Stack() {top = -1} template Stack :: Push(Type var) { st[++top] = var; } template Type Stack :: Pop() { return st[top --];}
34
Instantiation Classes are instantiated by defining an object using the template argument Stack s1; Stack s2;
35
Example Class Node { public: Node(string s); private: string data; Node* previous; Node* next; };
36
Contd. template Class Node { public: Node(T s); private: T data; Node * previous; Node * next; };
37
Reference Lafore Horstmann
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.