Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.

Similar presentations


Presentation on theme: "1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh."— Presentation transcript:

1 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

2 2

3 Function Overloading Multiple definitions for same function in same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. 3

4 Function Overloading (fo_print.cpp) #include class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; pd.print(5); // print integer pd.print(500.263); // print float pd.print("Hello C++"); //print character } 4 Program: Write a program of function overloading, which uses one function with different arguments.

5 Function Overloading (fo_print2.cpp) #include class printData { public: void print(int i, int j) { cout << "Printing int: " << i*j << endl; } void print(double d, double d2) { cout << "Printing float: " << d*d2<< endl; } void print(char* c, char* d) { cout << "Printing character: " << c<<d << endl; } }; int main(void) { printData pd; pd.print(5,10); pd.print(10.01,20.034); pd.print("Hello C++","Good Bye"); } 5 Program: Write a program of function overloading, which uses one function with different arguments, also perform multiplication of arguments.

6 Function Overloading (fo_data.cpp) #include class printData { public: void print(int i, int j) { cout << "Printing int: " << i*j << endl; } void print(int i, double d) { cout << "Printing int and double" << i*f<< endl; } void print(int i, char* c) { cout << "Printing "<< i<<c << endl; } }; int main(void) { printData pd; pd.print(5,10); pd.print(10,20.034); pd.print(99, " Hello C++"); } 6 Program: Write a program of function overloading, which used different data types.

7 7

8 Types of Operators There are two types of operators to overload: Unary Binary Unary operators: Type of Operators works on only one operand. Operand is a variable acted on by an operator. Prefix Unary Operator: Unary operators can appear before operand. i-e: ++a; --a; -3; !(3) Postfix Unary Operator: Unary operator can also appear after operand. i-e: a++; a--; Binary Operators: Type of Operators works on two operands i-e: a+b; a/b; (+, -, *, /, %, =, ) 8

9 Prefix & Post Fix Example Pre_post.cpp #include int main() { int a=5,b=5; cout<<"Post fix a: "<<a++<<endl; cout<<"After post fix a: "<<a<<endl; cout<<"Pre fix b: "<<++b<<endl; } 9

10 Operator Overloading  Definition Operator overloading is to allow the same operator for multiple implementations.  Syntax return-type operator operator-symbol (parameter-list) {} //operator is keyword  Example void operator ++ () { // Body of function } 10

11 Restrictions on Operator Overloading C++ operators that can be overloaded C++ Operators that cannot be overloaded

12 Restrictions on Operator Overloading Overloading restrictions Precedence of an operator cannot be changed Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately No new operators can be created Use only existing operators

13 13

14 Overloading Unary Operator (op_count.cpp) #include class Counter { private: unsigned int count; public: Counter(): count(0) {/*emptly body*/} unsigned int get_count() {return count;} void operator++() { ++count; } }; //Counter =constructor. Count=variable int main() { Counter c1,c2; //Define & Initialize cout<<"\n c1= "<<c1.get_count(); //display cout<<"\n c2= "<<c2.get_count(); ++c1;//increment c1 //c1.inc_count(); ++c2;//increment c2 cout<<"\n c1= "<<c1.get_count(); //display again cout<<"\n c2= "<<c2.get_count(); cout<<endl; } 14 Program: Write a program using class and constructor, which uses overloaded operator ++.

15 The operator Keyword (op_count.cpp) void operator++(); The declaratory syntax tells the compiler to call this member function whenever the ++ operator is encountered. Compiler can distinguish between overloaded operator by looking at the data type of operands. 15

16 Operator Arguments In main function ++ operator is applied to specific object. Operator ++ takes no arguments Operator ++ only increments the count data in the object of which it is member. 16 (op_count.cpp)

17 Operator Return Values The ++ operator has a return type void in the operator ++() function, any try to get its values to another variable will generate error. c1=++c2; This is wrong because counter objects must always appear standalone with its operand. 17 (op_count.cpp)

18 18

19 Overloading Binary Operators Binary Operators: Type of Operators works on two operands i-e: a+b; a/b; (+, -, *, /, %, =, ) You can overload the Binary operators to compare two objects. 19

20 Overloading Binary Operator (oo_binary.cpp) #include class Distance { private: int feet; // 0 to infinite int inches; // 0 to 12 public: // required constructors Distance() { feet = 0; inches = 0; } Distance(int f, int i) { feet = f; inches = I; } // method to display distance void displayDistance() { cout << "F: " << feet << " I:" << inches <<endl; } // overloaded minus (-) operator Distance operator- () { feet = -feet; inches = -inches; return Distance(feet, inches); } }; int main() { Distance D1(10, 15), D2(-20, 25); -D1; // apply negation D1.displayDistance(); // display D1 -D2; // apply negation D2.displayDistance(); // display D2 } 20 Program: Write a program using class and constructor, which uses overloaded operator negation(-).

21 Overloading Binary Operator (oo_plus2.cpp) #include class Distance { private: int feet; int inches; public: Distance():feet(0),inches(0.0) { } Distance(int ft, float in): feet(ft),inches(in) { } void getdist() { cout >feet; cout >inches; } void showdist()const { cout<<feet<<"\'-"<<inches<<'\"'; } Distance operator+(Distance)const; }; // Function Distance Distance::operator+(Distance d2)const { int f=feet+d2.feet; float i=inches +d2.inches; if(i>=12.0) { i-=12.0; f++; } return Distance(f, i); } 21

22 Overloading Binary Operator (oo_plus2.cpp) int main() { Distance dist1,dist3,dist4; dist1.getdist(); Distance dist2(11,6.25); dist3 = dist1+dist2; dist4 = dist1+dist2+dist3; cout<<"dist1 ="; dist1.showdist();cout<<endl; cout<<"dist2 ="; dist2.showdist();cout<<endl; cout<<"dist3 ="; dist3.showdist();cout<<endl; cout<<"dist4 ="; dist4.showdist();cout<<endl; return 0; } 22

23 Overloading Binary Operator (oo_plus2.cpp) 23

24 Overloading Binary Operator (oo_plus2.cpp) Scope resolution operator(::) is used to define a function outside a class or when we want to use a global variable but also has a local variable with same name. () const member function can not modify the data. 24


Download ppt "1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh."

Similar presentations


Ads by Google