Anda di halaman 1dari 11

Operator overloading

1. Overloading an operator is t way a programmer redefines a n operator, that is using an operator


in a different way.
2. Overloading cannot be done on built in types
3. Only user defined type
4. Operator are overloaded within a class, or a structure
User defined type
These 4 operations cannot be overloaded
5. Scope resolution operator (::)
6. Dot operator (.)
7. Dot-valued through pointer operator (.*)
8. Terriary operator (?:){for conditions{if ais better then b print b

The operators “=” and “&”


//syntax for overloading an operator
<return-type> operator<operator symbol> ()
{
Statements;
}

//implementation of operator overloading.

#include <iostream>

#include <string>

Using namespace std;

Class Overloading

Public:

String x,y,z;

Void operator +()

{z+x+y;;}

Void display ()}

Cout<<”final string”<<Z;

}};

Int main()

Overloading a;
a.x=ABC;

a.y=”DEFm ”;

+a;

a.display();

return o;

inheritance
involves two classase one will be source/base and the other will be derived

classes
(A) (B)

(Source/base) (derived)

-> Inheritance is when there is a relationship between two classes at least , one can access the
properties of the other.

-> Inheritance involves base classes end derived classes

Note(er database)

Syntax for a simple inheritance

Class<base>

};

Class<derived>:<access-specifier> <Base>

};

// implementing inheritance

Base

Name -talk

Age -walk

Profession -eat

Derived

Teacher musician soccer player

Can teach() music() playsoccer()


// implementing class overloading

Class person{

Public:

Int age;

String name prof;

Void info (){

Cout<<name<<prof<<age;

Walk (); talk ();eat ();

void talk(){cout<<”can talk”;}

void walk (){cout<<can walk”;}

void eat(){cout<<”can eat”;}

};

Class Teacher:public person

{ public:void can teach(){

Cout<<”I can teach\n”;}

Class musician:public person

{void music ()
{cout<<”I can play music\n”;

}};

Class soccer player : public person

{void soccer(){

Cout<<”I can play soccer\n”;}

};

Int main(){

Teacher t;

t.name=”david”;

t.age=26;

t.prof=”teacher”;
t.info();

t.canTeach();

musician m;

m.name=”yves”;

m.age=40;

m.prof=”Musician”;

m.info();

soccerPlayer s;

s.name =”sam”;

s.age=17;

s.prof=”soccerplayer”;

s.info();s.soccer()

return 0;

Types of inheritance
9. There are four types of inheritance`
I. Simple inheritance(1 base ,1 derived)
II. Multilevel inheritance
III. Multiple inheritance(many base class for 1 derived)
IV. Hierarchical inheritance(single base classes, multiple derived

(1) Simple inheritance


Syntax class<base>{ };

Class <derived>:<access-specifeir><base>

};
2) Multiple inheritance
N-base classes, one derived class
Class<Base-1>{ };
Class<Base-2>{ };
Class <Derived> :<access-speciufier>,base-1,
<access-specifier><base-2>{ };

3) Hierarchical inheritance
one bass class ,N derived classes
class <Base>{ };
class <derived-1>:<access-specifier><base>{ };
class<derived-2><access-specifier><base>{ };

4) Multilevel inheritance
The next class is derived of previous ,adjacent class
Lets say we have 3 classes A , B, C
A is the base class of B.
B is the base class of C, and the derived class of A
C is the derived class of b
Class A{ };
Class B:publicA {};
Class C:public B{};

10. Multilevel inheritance


Multi level inheritance comprises of a base class and a derived class in which they are in levels
that one the previous derived class is the base class of the next derived class and so on

A
derived| base
B
derived|base
C
derived|base
D
derived|base
E
Person
Multilevel inheritance

class A
{
public: int a, b;
void inputdata()
{
cout<<”Enter 2 numbers”;
cin>> a>>b;
}
};

class B:public A
{
public: int x, y;
void result()
{
cout<<”Sum is ” << a+b;
}
};

class C:public B
{
public:
void product()
{
cout<<”Result is ” << x*y;
}
};

int main()
{
B n;
n.inputdata();
n.result();

C m;
m.x=12;
m.y=20;
m.product();

return 0;
}
//read and print employee info using public multiple
inheritance
N-base classes, one derived class
Class<Base-1>{ };
Class<Base-2>{ };
Class <Derived> :<access-speciufier>,base-1,
<access-specifier><base-2>{ };
Base employee
Name
Age
Idno

Base 2

Company

Branch

Division()

position

worker()

derived 1

function overriding
class base{

public :void func(){}

};

Clas derived:public base{

Public : void func(){}

};

Function overriding
Overriding a function is nothing but duplicating both in a base class and a derived class.

Concept:suppose we have a software using object oriented programming everytime we don’t want to
use previous values sometimes we can have multiple inmheritance no problem for this we don’t need
function overridi ng now in these case now we have base clkas which has four member and we ant to
duplicate this task and we need to use function overridind to quickly release memory

\ function we have

Int main ()

Derived d;

d.function();return 0;

class base{

public:void input(){

cout<<”data is input\m”

}};

Class derived:public base

\{public:void input()

{cout<<”input function is overridden\n;}

};

Int main()[

Derived;

x.input();return 0

//to display both

In derived insert

Base::input();//function call for base

Cout<< “data Is now overridden\n”

}};

Details serial number price

Book-s.no-title-price

Now we want to add more attributes

In the derived class it will help update information of a book using method/function overriding
#include <cmath>Hybrid inheritance (mixing inheritance)

Class cube

{ public;

Double side;void intro(){cout<<”shape:\n”;}

Void volume(){

Cout<<”volume is\n”;

Cout <<side*side*side;endl,}

};

Class cuboid{

Public:int length,breadth,height;

Void intro(){

Cube::intro();}

Void shape details()

Cout<<”shape volume is”<<length*breadth*height:

}};

Class prism:public cuboid

{public:int width;

Void shapedetails(){

Cout<<”volume of prism is\n”;

Cout<<width*length*height;}}

Int main()

cubec;c.intro();c.volume();c.side=4

cuboid cu

cu.shapedetails()

cu.length+12

cu.height=4

cu.breasdth=2;

Anda mungkin juga menyukai