Inheritance and Operator Overloading
Inheritance and Operator Overloading
Inheritance and Operator Overloading
And
Operator
Overloading
By
Bhagwant Singh
Content
Ambiguity of Multiple inheritance.
Virtual base Classes
Abstract Classes
Constructor in derived Classes
Member Classes: Nesting of Classes
Operator Overloading.
Overloading Unary Operators
Overloading Binary Operators
Overloading Binary Operators using Friends
Manipulation of strings using Operators
Some other Operator Overloading Examples.
Rules for Overloading Operators
Type Conversions.
Multiple Inheritances
It
Class A
{
..
..
};
Class B: virtual public A // Inheriting Class A as Virtual and public
{
..
Hierarchical
..
Inheritance
};
Class B1: virtual public A // Inheriting Class A as Virtual and public
{
..
..
};
Class C: Public B, Public B1
{
ance
..
..
};
Multiple
Inherit
Abstract Classes
A
Constructors
Example
# include < iostream.h>
Using namespace std;
Class A
{
Int x;
Public:
A(int i)
{
X= I;
}
Void show_x(void)
{
Cout<<x = << x<<\n;
}
};
Class B
{
float y;
Public:
B(float j)
{
y= j;
}
Void show_y(void)
{
Cout<<y = << y<<\n;
}
};
Overloading
code
Extension of language to include userdefined types
I.e., classes
Make
Generalization
of function overloading
General Format
returnType operator*(parameters);
any type
Return
keyword
operator symbol
returns
Operator
Either
or
a global function definition
C++ Philosophy
Continued
Operators
Operators
Cannot
Not
Only
Can
Can
C++ Code
# include<iostream.h>
#include<string.h>
Class string
{
Char *p;
Int len;
Public:
string(){len = 0; p=0;}
string (const char *s);
string(const string &s);
~ string (){ delete p;}
Friend string operator + (const string &s, const string &t);
Friend void show(const string s);
};
String:: string (const char *s)
{ len = strlen(s);
p = new char[len+1];
Strcpy(p,s);
}
String:: string (const string &s)
{ len = strlen(s);
p = new char[len+1];
Strcpy(p,s.p);
}
String operator + (const string&s, const string &t)
{
String temp;
Temp.len = s.len + t.len;
Temp.p = new char[temp.len+1];
Strcpy(temp.p,s.p);
Strcat(temp.p, t.p);
Return(temp);
}
Rules of Overloading
Only existing operators can be overloaded
The overloaded operator must have at least one
operand that is user defined type
Overloaded operator follows the syntax rule of
original operator
There are some operator that cant be overloaded.
(. |.*|::|?:|)
There are some operator that cant be overloaded
using friend function.(=|()|[]|->)
Binary arithmetic operator must explicitly return a
value.
Type conversion
Type
{
int hrs;
int mins;
public:
time(int t)
{
hrs = t/60;
mins= t%60; }
};
void main()
{
Time t1;
Int duration = 85;
t1= duration
}
casting operator
Operator typename()
{}
Satisfy following conditions:
=
Obj x
Obj y
Class Y(Source
Class)
Casting Operator
Function
Conversion here
Class X(Destination
Class
Constructor Function
Conversion here
Class Y(Source
Class)
Data assess
function
#include<iostream>
Using namespace std;
Class invert1;
Class invert2;
Class invert1
{
Int code;
Int item;
Float price;
Public: invent1(int a, int b, float c)
{
Code = a;
Item = b;
Price c;
}
Void putdata()
{
Cout<< code: << code <<\n;
Cout<< item: << item <<\n;
Cout<< price: << price <<\n;
}
Int getcode() { return code;}
Int getitem(){ return item;}
Float getprice() {return price;}
Operator float(){ return (item*price);
};
Class invent2
{
Int code ;
Float value;
Public:
Invent2()
{
Code = 0; value 0;
}
Invent2(int x, float y)
{
Code = x;
Value = y;
}
Void putdata()
{
Cout<<code: << code << \n;
Cout<<value: << value << \n;
}
Invent2(invent1 p)
{
Code = p.getcode();
Value =p.getitem();*p.getprice();
}
};
Int main()
{
Invent1 s1(100,5,140.0);
Invent2 d1;
Float total_value;
Total_value = s1;
D1= s1;
Cout<<product details invent1 type<<\n;
S1.putdata();
Cout<<\n stock value<<\n;
Cout<<value= << total_value<<\n\n;
Cout<<product details-invent2 type<<\n;
D1.putdata();
Return 0;
}
Thanks