Professional Documents
Culture Documents
Unit 3 New
Unit 3 New
Polymorphism : 43
functions overloading,
overloading unary operators,
overloading binary operators,
virtual base classes,
abstract classes,
pointer to object,
this pointer,
pointer to derived class,
virtual function,
pure virtual function
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
OBJECT ORIENTED PROGRAMMING USING C++
• #include <iostream>
• using namespace std;
class className
{ ... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};
}
Syntax2 :
returntype classname : : operator operatortobeoverloaded
(argument_list)
{
// body of the function.
}
operator(x)
X .operator( y ) or x+y
Operartor (x,y)
• class Unary {
• public:
• int value=9;
• // Overload ++ when used as prefix
• void operator ++ () {
• value++;
• }
• void display() {
• cout << "Count: " << value << endl;
• }
• };
• int main() {
• Unary count1;
• count1.display();
• return 0;
• }
• class Unary {
• public:
• int value=6;
• // Overload ++ when used as prefix
• void operator ++ () {
• ++value;
• }
• void display() {
• cout << "Count: " << value << endl;
• }
• };
• int main() {
• Unary count1;
• count1.display();
• return 0;
• }
• KUNDAN
• #include <iostream>
• using namespace std;
• class Unary {
• public:
• int value=4;
• // Overload ++ when used as prefix
• void operator -- () {
• --value;
• }
• void display() {
• cout << "Count: " << value << endl;
• }
• };
• int main() {
• Unary count1;
• count1.display();
• return 0;
• }
• void lpu::operator-(){
• x=-x;
• y=-y;
• };
• int main()
• {
• lpu obj;
• obj.getdata(10,-20);
• obj.display();
• -obj; //or obj.operator-();
• obj.display();
• }
•
OBJECT ORIENTED PROGRAMMING USING C++
• // BINARY OPERATOR OVERLOADING
• #include<iostream>
• using namespace std;
• class A
• {
• public:
• int x;
• A(int i)
• {
• x=i;
• }
• void operator+(A a)
• {
• int m = x+a.x; // a represents obj 2
• cout<<"The result of the addition of two objects is : "<<m;
•
• }
•
• };
•
•
• int main()
• {
• A a1(4);
• A a2(5);
• a1+a2; //a1.operator(a2)
• return 0;
• }
operator(x)
X .operator( y ) or x+y
Operartor (x,y)
• class Test
• {
• private:
• int a;
• public:
• void set_a();
• void get_a();
• friend Test operator -(Test);
• };
• void Test :: set_a()
• {
• a = 10;
• }
converting one data type into another data type is called type casting .
Converting smaller data type into a larger one is also called as type promotion.
#include<iostream>
using namespace std;
int main()
{
int x=1;
char y='a';
cout<<x+y;
OR
#include <iostream>
using namespace std;
int main ()
{
// declaration of the variables
int a=97;
regNo, Name
regNo, Name studentDetails()
studentDetails()
ambiguity arises as to which
Result data/function member would
OBJECT ORIENTED PROGRAMMING USING C++ be called?
virtual base class introduce
The function to be invoked is known at the compile time. The function to be invoked is known at the run time.
It is also known as overloading, early binding and static binding. It is also known as overriding, Dynamic binding and late binding.
Overloading is a compile time polymorphism where more than one method is having the same Overriding is a run time polymorphism where more than one method is having the same name, number of parameters and the type of
name but with the different number of parameters or the type of the parameters. the parameters.
It is achieved by function overloading and operator overloading. It is achieved by virtual functions and pointers.
It provides fast execution as it is known at the compile time. It provides slow execution as it is known at the run time.
Created By:
Kumar Vishal
It is less flexible as mainly all the things execute at the compile time. It is more flexible as all the things execute at the run time.
(SCA), LPU
OBJECT ORIENTED PROGRAMMING USING C++
virtual base classes
• It means we are making base class as virtual
• But why ???
• In situation??? A int a
int b int c
int a
D int d
getArea()
getArea()
getArea()
Area = w × h
w = width Area = π × r2
h = height
OBJECT ORIENTED PROGRAMMING USING C++
Important points:
class base{};
class derive: public base{};
base b1,*b2;
derive d1;
b2=&d1;