Professional Documents
Culture Documents
Poly C++
Poly C++
polymorphism.
situations.
Dogs, Birds - And they also have their own implementation of an animal
Example
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n" ;
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n" ;
}
};
// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n" ;
}
};
Remember from the Inheritance chapter that we use the : symbol to
Example
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n" ;
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n" ;
}
};
// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n" ;
}
};
int main() {
Animal myAnimal;
Pig myPig;
Dog myDog;
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
return 0;
}
Try it Yourself »
Oriented Programming.
Runtime Polymorphism
There are two types of polymorphism in C++:
time polymorphism.
I. Function Overloading
C++, a single function is used to perform many tasks with the same
of compile-time polymorphism.
1. /*datatype functionname(<parameters>)
2. {
3. ...
4. }
5.
6. datatype samefunctionname(< different parameters/different number of
parameters >)
7. {
8. ...
9. }
10. */
In the below example, A function ADD() is used to perform two tasks.
The two asks would be to add two integer values and add two strings
(concatenate).
Source Code:
1 #include <iostream>
2 class Addition {
3 public:
4 int ADD(int X,int Y) // Function with parameter
5 {
6 return X+Y; // this function is performing addition of two Integer value
7 }
8 int ADD() { // Function with same name but without parameter
9 string a= "HELLO";
10 string b="SAM"; // in this function concatenation is performed
11 string c= a+b;
12 cout<<c<<endl;
13
14 }
15 };
16 int main(void) {
17 Addition obj; // Object is created
18 cout<<obj.ADD(128, 15)<<endl; //first method is called
19 obj.ADD(); // second method is called
20 return 0;
21 }
22
Output
143
Operator Overloading
function.
class className {
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};
Here,
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
Run Code
Output
Count: 6
Here, when we use ++count1; , the void operator ++ () is called. This increases
the value attribute for the object count1 by 1.
Note: When we overload operators, we can use it to work in any way we like.
For example, we could have used ++ to increase value by 100.
However, this makes our code confusing and difficult to understand. It's our
job as a programmer to use operator overloading properly and in a consistent
and intuitive way.
The above example works only when ++ is used as a prefix. To make ++ work
as a postfix we use this syntax.
Notice the int inside the parentheses. It's the syntax used for using unary
operators as postfix; it's not a function parameter.
The advantage of Operators overloading is to perform different
binding.
overriding, more than one method has the same name with different
function in the derived class. At that time, we can say the base
function, one in the superclass and one in the derived class. The
Source code
1 #include <iostream>
3 class Animal {
4 public:
5 void function(){
6 cout<<"Eating..."<<endl;
7 }
8 };
10 {
11 public:
12 void function()
13 {
14 cout<<"Walking ..."<<endl;
15 }
16 };
17 int main(void) {
18 Animal A =Animal();
19 A.function(); //parent class object
20 Man m = Man();
22
23 return 0;
24 }
Output
Eating …..
Walking……
II. Virtual Function
But, when the base class pointer contains the derived class address,
the object always executes the base class function. For resolving this
Let‟s see the below example for understanding how the program
Source code
3 #include <iostream>
5 class Add
6 {
8 public:
10 {
12 }
13 };
16 int y = 10,z=30;
17 public:
19 {
21 }
22 };
23 int main()
24 {
25 Add *m; //base class pointer .it can only access the base class members
27 m = &s;
28 m->display(); // Accessing the function by using base class pointer
29 return 0;
30 }
Output
Value of x is: 25
Virtual Function used to invoke the derived class in a program.
Source Code
1 #include<iostream>
4 class Add
5 {
6 public:
10 }
11
12 void show ()
14 };
15
17 {
18 public:
19 void print () //print () is already virtual function in derived class, we could als
20 { int x=20,y=10;
21
23
24 void show ()
25 { cout<< "show derived class" <<endl; }
26 };
27
28 //main function
29 int main()
30 {
31 Add *aptr;
32 Sub s;
33 aptr = &s;
34
36 aptr->print();
37
38 // Non-virtual function, binded at compile time
39 aptr->show();
40
41 return 0;
42 }
Output
Source Code
1 #include <iostream>
3 class Animal
4 {
5 public:
7 };
9 {
10 public:
11 void show()
12 {
13 cout << "Man is the part of animal husbandry " << endl;
14 }
15 };
16 int main()
17 {
19 //Animal a;
21 aptr = &m;
22 aptr->show();
23 return 0;
24 }
Output
Run-time
1)Run time is the time period where the executable code is running.
3)Errors that occur during the execution of a program are called run-time errors. Run