Sie sind auf Seite 1von 33

Inheritance

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

is that in which a Class inherits the


features from two Base Classes.
In multiple inheritance, there may be
possibility that a class may inherit
member functions with same name from
two or more base classes . It leads to
two ambiguities

Identical Member in more than one class.


Diamond-shape inheritance.

Solution for MI Ambiguity


Using

Scope resolution Operator.


Using :: we can address which base class we are pointing to.
Using Virtual Class.
This solution is effective for the Diamond shape problem and
this will be happen in case of Hybrid inheritance .
For example if we have a child class which is inheriting
properties from two parent classes which them self are
derived from the Grand parent class.
The child inherits the traits of grandparent from two separate
paths.
All the public and protected member of grandparent are
inherited twice.
Grandparent class act as the Indirect Base class.
This Problem can be solved by making the Indirect base class
as virtual.

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

class is consider as Abstract Class if it has at


least one pure virtual function.
It is the one which are not used to create objects.
It is created or designed only to act as a base
class.
It is a design concepts in program development
and provide a base upon which other class may
be built.

Constructors

They are the special member function used


to initialize the data variable of class.
Constructor of base class cant be inherited
but the derived class can use it.
It no base class constructors takes any
arguments the derived class need not to
have constructor function.
If base class constructor contain a
constructor with one or more argument
then in derived class constructor has to
pass the argument to base class.

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;
}
};

Class C: public A, public B


{
Int m,n;
Public:
C(int a, float b, int c,int d): A(a),B(b)
{
m= c;
n = d;
}
Void show_mn(void)
{
Cout<<m = << m<<\n;
Cout<<n = << n<<\n;
}
};
Int Main()
{
C c(5,10.75,20,30);
Cout<<\n;
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

Initialization List in Constructor


Class xyz
{
Int a;
Int b;
Public:
xyz(int I, int j):a(i),b(2*j + i){}
};
Void Main()
{
Xyz x(2,3);
}

Overloading

Overloaded functions have the same name but


different signatures ( different parameters and
return values).

In reality Operator Overloading is just a function


call using special notation.

Operator overloading is done for the purpose of


making the use of familiar notation in your
program. For instance, let us say you wanted to
add two matrices. You could overload the +
operator to accomplish this.

Why Operator Overloading?


Readable

code
Extension of language to include userdefined types
I.e., classes
Make

operators sensitive to context

Generalization

of function overloading

General Format
returnType operator*(parameters);

any type

Return

keyword

operator symbol

type may be whatever the operator

returns

Including a reference to the object of the operand

Operator

symbol may be any overloadable


operator from the list.

Either

a non-static member function definition

or
a global function definition

Usually a friend of the class

C++ Philosophy

All operators have context

Even the simple built-in operators of basic


types

E.g., '+', '-', '*', '/' for numerical


types
Compiler generators different code
depending upon type of operands

Operator overloading is a generalization


of this feature to non-built-in types

E.g., '<<', '>>' for bit-shift operations and


also for stream operations

Continued
Operators

retain their precedence and


associativity, even when overloaded

Operators
Cannot
Not

retain their number of operands

redefine operators on built-in types

possible to define new operators

Only

(a subset of) the built-in C++ operators


can be overloaded

Operators that Can and


Cannot be Overloaded *

Operator Functions as Class


Members*
Leftmost

operand must be of same class as


operator function.
Use this keyword to implicitly get left
operand argument.
Operators (), [], -> or any assignment
operator must be overloaded as a class
member function.
Called when
Left operand of binary operator is of this class.
Single operand of unary operator is of this class.

Operator Functions as Global


Members
Need

parameters for both operands.

Can

have object of different class than


operator.

Can

be made a friend to access


private or protected data.

Overloading Unary Operator


#include <iostream.h>
Using namespace std;
Class space
{
Int x;
Int y;
Int z;
Public:
Void getdata(int a, int b, int c);
Void display(void);
Void operator ();
};
Void space :: getdata(int a,int b, int c)
{
x= a;
y= b;
z= c;
}
Void space :: display(void)
{
Cout<x= << x<< ;

Cout<x= << x<< ;


Cout<x= << x<< ;
}
Void space :: operator-()
{
x= -x;
y = -y;
z= -z;
}
Int main()
{
space S;
S.getdata(10,-20,30);
Cout<< S: ;
S.display();
-S;
Cout<< -S : ;
S.display();
Return 0;
}

Using Friend Function


friend void operator (space &s);
Declaration
Void operator (space &s)
{
s.x = = s.x;
s.y = = s.y;
s.z = = s.z;
}
Definition

Overloading of Binary Operator *


#include <iostream.h>
Using namespace std;
Class complex
{
Float x;
float y;
public:
complex(){}
complex(float real, float img)
{ x= real; y= img;
}
complex operator +(complex);
void display(void)
};
complex complex:: operator +
(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y= y + c.y;
Return (temp);

Void complex:: display(void)


{
Cout<< x<< +j <<y<<\n;
}
Int main ()
{
complex c1,c2,c3;
c1 = complex(2.5,3.5);
c2= complex(1.6,2.7);
c3= c1 +c2;
cout<< c1 = ;
c1.display();
cout<< c2 = ;
c2.display();
cout<< c3 = ;
c3.display();
Return 0;
}

Overloading of Binary Operator


using Friend function
friend complex operator +(complex,
complex);
Declaration
complex operator (complex a, complex b)
{
Return complex((a.x + b.x),(a.y +b.y))
}
Definition

If we are using the friend function then we have to


pass two arguments, then why and in which
situation we need to used a friend function for
operator overloading??

Manipulation of string using


operators.
ANSI

C implement string using character


array, pointer and string functions.
There are no operator for manipulating
the strings
C++ committee has added a new class
called strings to the C++ class library
that support all types of string
manipulations.

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);
}

Int operator<=(const string &s, const string &t)


{
Int m = strlen(s.p);
Int n = strlen(t.p);
if(m<=n) return(1) else return(0);
}
Void show(const string s)
{
Cout<<s.p;
}
Int main()
{ string s1 = new; s2 = york; s3=delhi;
String st1,st2,st3;
St1 = s1;
St2 = s2;
St3 = s1+s3;
Cout<< \n st1 =<< show(st1);
Cout<< \n st2 =<< show(st2);
Cout<< \n st3 =<< show(st3;
If(st1<= st3)
{
Show(st1);
Cout<<Smaller then;
Show(st3);
}
Else
{
Show(st3);
Cout<<Smaller then;
Show(st1);
}
Return 0;
}

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

conversion in C: (typename) expression


Type conversion in C++: typename(expression)
Assignment operator cause automatic type
conversion.( as long as we are talking about builtin types)
Three types of situations

Conversion of basic type to class type


Conversion of class type to base type
Conversion of class type to class type

Base to Class Type


It

is most simple and can be done with the help of constructors


Class time

{
int hrs;
int mins;
public:
time(int t)
{
hrs = t/60;
mins= t%60; }
};
void main()
{
Time t1;
Int duration = 85;
t1= duration
}

Class to Base type


Overloaded

casting operator
Operator typename()
{}
Satisfy following conditions:

It must be a class member


It must not specify a return type
It must not have any argument.

Class to class type


Can

be done using a casting operator


function or a constructor.
Conversion from class y to class x take
place then y is the source class and x is
the destination class
Depending upon location of the casting
operator function decision between the
constructor and CO function is made

=
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

Das könnte Ihnen auch gefallen