Sei sulla pagina 1di 74

Features

Over loading
 Functions overloading
 Operators overloading
Functions overloading
 Two or more function having the same name, declared
in the same scope but different parameters are called
function overloading
 The compiler will automatically call the appropriate
function
 It is identified by its signature
#include <iostream.h>

int add(int ival1, int ival2);


float add(float fval1, float favl2);
double add(double dval1, double dval2);

void main()
{

int a = 10, b = 20, isum;


float x = 42.65, y = 112.50, fsum;
double p = 1234.567, q = 6677.999, dsum;

isum = add(a,b);
fsum = add(x,y);
dsum = add(p,q);

cout<<"Sum of two integers="<<isum<<endl;


cout<<"Sum of two floating-point numbers="<<fsum<<endl;
cout<<"Sum of two double-precision numbers="<<dsum<<endl;
}
int add (int ival1, int ival2)
{
return (ival1+ival2);
}

float add (float fval1, float fval2)


{
return (fval1 + fval2);
}

double add (double dval1, double dval2)


{
return (dval1+dval2);
}
Example 2

#include <iostream.h>

int area(int s);


float area(float rad);
float area(float bs, int ht);
float area(int l, int b);
void main()
{

int side, length, breadth, height, sqarea;


float radius, base, circArea, rectArea, tarea;
cout<<"Enter the side of a square"<<endl;
cin >> side;
sqarea = area(side);
cout<<"Area of a square= "<<sqarea<<endl;
cout<<"Enter the radius of the circle"<<endl;
cin >>radius;
circArea = area(radius);
cout<<"Area of a circle= "<<circArea<<endl;
cout<<"Enter the base and heght of a triangle"<<endl;
cin >>base >> height;
tarea = area(base, height);
cout<<"Area of a triangle= "<<tarea<<endl;
cout<<"Enter the length and breadth of a rectangle"<<endl;
cin >>length>> breadth;
rectArea = area(length, breadth);
cout<<"Area of a rectangle= "<<rectArea<<endl;
}
int area(int s)
{
int sa = s*s;
return sa;
}

float area(float rad)


{
float sc = 3.142*rad*rad;
return sc;
}
float area(float bs, int ht)
{
float ta = 0.5* bs * ht;
return ta;
}

float area(int l, int b)


{
float ra = l*b;
return ra;
}
Overloading
 Three steps of overloading resolutions
 Identification
 The overload functions for the call
 The properties of the arguments list is specified in the
function call
 Selection
 It selects the function from the candidates list
 Best match
 The function calls the best one that match, else it will try for
type cast
Operator overloading
 Here we are overloading the operator its self like
 +, -, *, /, and so on…
Over loadable operators
Assignment Arithmetic Relation Logical Bitwise Increment Special
operator operator operator operator operators & operator
decrement
operator
= + < ! & ++ ,
- <= && | -- []
* > || ~ ()
/ >= ^ ->
% == << ->*
+= != >> new
-= <<= new[]
*= >>= delete
/= &= delete[
%= |= ]
^=
Operators that cannot be overloaded
:: Scope resolution operator
. Class member access operator
.* Class member access operator
?: Conditional operator
Sizeof() Size of..
Operators that cannot be overloaded with
friend functions
= Equal Operator
() Function Call operator
[] Subscripting operator
-> Class member access opeartor
Overloading unary operator
 Unary operator
 -,+
 Binary operators
 +, -, / , *
Unary operator
#include<iostream.h>
Class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display();
void operator –();
};
Void space:: getdata(int a,int b,int c)
{
x=a;
y=b;
Z=c;
}
Void space:: display()
{
cout<<x<<y<<z;
}
Void space:: operator-()
{
x=-x;
y=-y;
z=-z;
}
Void main()
{
space s;
s.getdata(10,-20,30);
cout<<s”;
s.display();
-s;
cout<<s”
s.display();
}
Binary operator
 A+B
 A=5
 B=10
#include <iostream.h>
class complex
{
private:
float real;
float img;
public:
Complex() //Constructor
{ real=0; img=0; }
void getvalue( )
{
cout << “\n Enter value for real: “;
cin >> real;
cout << “\n Enter value for imaginary: “;
cin>> img;
}
void displayvalue( ) //Member Function for Outputting Values
{
cout <<”value of real is: “ << x <<”; value of imaginary is: “<<y
}
Complex operator +(complex c2);
};
Complex Complex :: operator + (Complex c2)
{
float x1 = real+ c2.real;
float y1 = img+c2.img;
return Complex(x1,y1);
}
void main( )
{
complex e1,e2,e3; //Objects e1, e2, e3 created
cout<<\n”Enter value for Object e1:”;
e1.getvalue( );
cout<<\n”Enter value for Object e2:”;
e2.getvalue( );
e3= e1+ e2; //Binary Overloaded operator used
cout<< “\nValue of e1 is:”<<e1.displayvalue();
cout<< “\nValue of e2 is:”<<e2.displayvalue();
cout<< “\nValue of e3 is:”<<e3.displayvalue();
}
Using friend functions
 We need to pass explicit the two parameter for binary
operator
#include <iostream.h>
class complex
{
private:
float real;
float img;
public:
Complex() //Constructor
{ real=0; img=0; }
void getvalue( )
{
cout << “\n Enter value for real: “;
cin >> real;
cout << “\n Enter value for imaginary: “;
cin>> img;
}
void displayvalue( ) //Member Function for Outputting
Values
{
cout <<”value of real is: “ << x <<”; value of imaginary is: “<<y
}
Friend Complex operator +(complex c1,complex c2);
};
Complex Complex :: operator + (complex c1,complex c2)
{
float x1 = c1.real+ c2.real;
float y1 = c1.img+c2.img;
return Complex(x1,y1);
}
void main( )
{
Complex e1,e2,e3; //Objects e1, e2, e3 created
cout<<\n”Enter value for Object e1:”;
e1.getvalue( );
cout<<\n”Enter value for Object e2:”;
e2.getvalue( );
e3= e1+ e2; //Binary Overloaded operator used
cout<< “\nValue of e1 is:”<<e1.displayvalue();
cout<< “\nValue of e2 is:”<<e2.displayvalue();
cout<< “\nValue of e3 is:”<<e3.displayvalue();
}
Class templates
 Class templates
 Function templates
 Overloading templates ..
Class templates
 Its just similar to the class expect the keyword
template present in front of the class

 Class name<type> object name
 Class name<type> object name(arglist)
Example
#include <iostream.h>
#include <conio.h>

template <class T>


class Numbers
{
private: T n1, n2, total;

public : void getData();


void sum2();

}; //End of class template

template <class T>


void Numbers <T>:: getData()
{
cout<<"Enter two numbers"<<endl;
cin >> n1 >> n2;
}
template <class T>
void Numbers <T>:: sum2()
{
T total;
total = n1 + n2;
cout<< n1 <<" + " << n2 <<" = " << total <<endl;

}
void main()
{
Numbers <int> iob;
Numbers <float> fob;
cout<<"Integer case..."<<endl;

iob.getData();
iob.sum2();
cout<<"Floating-point case..."<<endl;
fob.getData();
fob.sum2();
}
Class templates with multiple
parameters
 Template <class t1,class t2,…..>
 Class class_name
 {
 //boby
 };
Example
#include<iostream.h>
Template<class T1,class T2)
Class test
{
T1 a;
T2 b;
public:
test(T1 x,T2 y)
{ a=x; b=y; }
void show()
{ cout<<a<<“and”<<b; }
};
Void main()
{
test <float,int> test1(1.23,123);
test <int,char> test1(100,’w’);
test1.show();
test2.show();
}
Function templates
 Like class templates we can also define function
templates that would be used to create a family of
function arguments types
#include <iostream.h>
Template <class T>
Void swap(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
Void main()
{
int m=5,n=6;
float p=5.5,q=6.6;
cout<<“Before swaping m=“<<m<<“n=“<<n<<endl;
swap(m,n);
cout<<“After swaping m=“<<m<<“n=“<<n<<endl;
cout<<“Before swaping p=“<<p<<“q=“<<q<<endl;
swap(p,q);
cout<<“After swaping p=“<<p<<“q=“<<q<<endl;
getch();
}
Function templates with multiple parameter
 Like template class in function we can pass different
parameters to the functions
Example
#include<iostream.h>
#include<conio.h>
Template<class T1,class T2>
Void display(T1 x,T2 y)
{
cout<<x<<“ “<<y<<endl;
}
Void main()
{
clrscr();
display(1999,”NTTF”);
display(12.34,1234);
getch();
}
Overloading of template function
 A template function can be overloaded by template
function or by an ordinary functions
#include<iostream.h> Void display(int x)
#include<conio.h> {
Template<class T1,class T2> cout<<“Explicit display”<<x;
Void display(T1 x,T2 y) }
{
cout<<x<<“ “<<y<<endl;
}

Void main()
{
Template<class T1> clrscr();
Void display(T1 x) display(1999,”NTTF”);
{ display(12.34);
cout<<“Template “<<x<<endl; display(5);
} getch();
}
Member function of class templates
 A function defined & declared is called member
function
 The functions that are defined with in the template
class are called member function of class template
 The syntax for what for normal class is applied its also
applicable for the template
 There must be certain syntax should be followed
#include <iostream.h>
#include <conio.h>

template <class T>


class Numbers
{
private: T n1, n2, total;

public : void getData();


void sum2();

}; //End of class template

template <class T>


void Numbers <T>:: getData()
{
cout<<"Enter two numbers"<<endl;
cin >> n1 >> n2;
}
template <class T>
void Numbers <T>:: sum2()
{
T total;
total = n1 + n2;
cout<< n1 <<" + " << n2 <<" = " << total <<endl;

void main()
{
Numbers <int> iob;
Numbers <float> fob;
cout<<"Integer case..."<<endl;

iob.getData();
iob.sum2();
cout<<"Floating-point case..."<<endl;
fob.getData();
fob.sum2();
}
Concepts
 Polymorphism
 Virtual functions
 Virtual base class
Polymorphism
 The term is derived from Greek word and meaning
“having more than one form”
 We are achieving the polymorphism in two forms
 Compile time polymorphism
 Runtime polymorphism
Polymorphism

Compile time Run time

Function overloading Operator overloading Virtual functions


Pointers to objects
 Pointers are used to access the the class members
 Student s;
 Student *s1=&s;
 s1->getdata();
 s1->putdata();
#include <iostream.h>
#include <conio.h>
#include <string.h>

class student
{
private: int roll_no;
char name[25];
float percentage;
public: void getData(int rn, char nm[], float pt);
void putData(void);
};

void student::getData(int rn, char nm[], float pt)


{
roll_no = rn;
strcpy(name, nm);
percentage = pt;
}
void student::putData(void)
{
cout<<"Roll number = "<<roll_no<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Percentage = "<<percentage<<endl;
}

void main()
{
student s; //s is an object of type student
student *sptr; //sptr is an object poiner

sptr = &s; //initialize the address of s to sptr

sptr->getData(25, "Rama", 85.76);


sptr->putData();
}
Pointers to derived class
#include <iostream.h>
#include <conio.h>
#include <string.h>
class student
{
private: int roll_no;

public: void getData(void);


void putData(void);
};
class Junior_student : public student
{
private: int age;

public: void getData(void);


void putData(void);
};
void student::getData(void)
{
cout<<"Enter the roll number : ";
cin>>roll_no;
}
void student::putData(void)
{
cout<<"\nIN THE BASE CLASS...\n";
cout<<"Roll number = "<<roll_no<<endl;
}
void Junior_student::getData(void)
{
student::getData();
cout<<"Enter the age : ";
cin>>age;
}
void Junior_student::putData(void)
{
student::putData();
cout<<"\nIN THE DERIVED CLASS...\n";
cout<<"Age = "<<age<<endl;
}
void main()
{
student s; //s is an object of type student
student *sptr; //sptr is an object poiner
Junior_student js; //js is an object of type Junior_student
clrscr();
sptr = &s; //initialize the address of s to sptr
sptr->getData();
sptr->putData();
cout<<"\n";
sptr = &js;
sptr->getData();
sptr->putData();
}
Output
Enter the roll number : 123

IN THE BASE CLASS...


Roll number = 123

Enter the roll number : 145

IN THE BASE CLASS...


Roll number = 145
Type casting pointers
 ((Derived_name *) pointer)->getdata();
 ((Derived_name *) pointer)->putdata();
Pointers to derived class
#include <iostream.h>
#include <conio.h>
#include <string.h>
class student
{
private: int roll_no;

public: void getData(void);


void putData(void);
};
class Junior_student : public student
{
private: int age;

public: void getData(void);


void putData(void);
};
void student::getData(void)
{
cout<<"Enter the roll number : ";
cin>>roll_no;
}
void student::putData(void)
{
cout<<"\nIN THE BASE CLASS...\n";
cout<<"Roll number = "<<roll_no<<endl;
}
void Junior_student::getData(void)
{
student::getData();
cout<<"Enter the age : ";
cin>>age;
}
void Junior_student::putData(void)
{
student::putData();
cout<<"\nIN THE DERIVED CLASS...\n";
cout<<"Age = "<<age<<endl;
}
void main()
{
student s; //s is an object of type student
student *sptr; //sptr is an object poiner
Junior_student js; //js is an object of type Junior_student
clrscr();
sptr = &s; //initialize the address of s to sptr
sptr->getData();
sptr->putData();
cout<<"\n";
sptr = &js;
((Junior_student*)sptr)->getData();//type casting
((Junior_student*)sptr)->putData();
}
Output
Enter the roll number : 123

IN THE BASE CLASS...


Roll number = 123

EEter the roll number : 145


Enter the age : 20

IN THE DERIVED CLASS...

IN THE BASE CLASS...


Roll number = 145
Age = 20
Virtual functions
 The virtual functions are used to achieve runtime
polymorphism
 Typecasting
 Virtual function
 The keyword virtual is required for prefixing
 The process of redefining a virtual function in derived
classes is called function overriding
#include <iostream.h>
#include <conio.h>
#include <string.h>
class student
{
private: int roll_no;
char name[25];
public: virtual void getData(void)
{
cout<<"Enter the roll number : ";
cin>>roll_no;
cout<<"Enter the name : ";
cin>>name;
}
virtual void putData(void)
{
cout<<"\nIN THE BASE CLASS...\n";
cout<<"Roll number = "<<roll_no<<endl;
cout<<"Name = "<<name<<endl;
}
};
class Junior_student : public student
{
private: int age;

public: void getData(void);


void putData(void);
};
void Junior_student::getData(void)
{
student::getData();
cout<<"Enter the age : ";
cin>>age;
}
void Junior_student::putData(void)
{
cout<<"\nIN THE DERIVED CLASS...\n";
student::putData();
cout<<"Age = "<<age<<endl;
}
void main()
{
student s; //s is an object of type student
student *sptr; //sptr is an object poiner
Junior_student js; //js is an object of type Junior_student
clrscr();
sptr = &s; //initialize the address of s to sptr
sptr->getData();
sptr->putData();
cout<<"\n";
sptr = &js;
sptr->getData();
sptr->putData();
}
Second example
#include <iostream.h> #include <conio.h>
class GeometricFigure
{
public : float a, b,area;
void getData()
{
cin>>a>>b;
}
virtual void display()
{
}
};
class triangle : public GeometricFigure
{
public: void display()
{
area = a*b/2;
cout<<"The area of the triangle is = "<<area<<endl;
}
};
void main()
{
GeometricFigure *p1;
triangle tri;
char ac;
clrscr();
cout<<"Enter the type of the geometric figure(type t/r)"<<endl;
cin>>ac;
p1 = new GeometricFigure ;
if (ac == 't')
{
cout<<"Your choice is for TRIANGLE..."<<endl;
cout<<"So enter the values of base and height"<<endl;
p1 = &tri;
p1->getData();
p1->display();
}
else
{
cout<<"Not a valid entry for the geometric figure"<<endl;
}
}
Rules for virtual functions
 The virtual function must be defined in a base class
 The virtual function cannot be static members
 The virtual functions are accessed by using object
pointer
 They should be declared in public function
 No constructor is made as virtual
 A base class pointer can be used to address the derived
class object
Pure virtual function
 Normally virtual functions are declared in base class
 A virtual functions which provides an interface for the derived
class functions to override
 Syntax
 Virtual ret_type func_name()=0;
 Pure Virtual Function is a Virtual function with no body

class classname //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};
Example
#include <iostream.h> #include <conio.h>
class BC1
{
private: int i;
public: virtual void getData(void)
{
cout<<"Enter the value of i"<<endl;
cin>>i;
}
virtual void putData(void) = 0; // pure vitual function
};
class DC1 : public BC1
{
private: int j;
public: void getData(void)
{
cout<<"Enter the value of j"<<endl;
cin>>j;
}
void putData(void)
{
cout<<"\nIN THE DERIVED CLASS..."<<endl;
cout<<"j = "<<j<<endl;
}
};
void main()
{
BC1 *bptr;
DC1 d;
bptr = &d;
bptr->getData();
bptr->putData();
}
Virtual base class
class Exforsys class Training:public Exf1,public Exf2
{ {
protected: public:
int x; int example()
}; {
return x;
class Exf1:public Exforsys }
{ }; };

class Exf2:public Exforsys


{ };
class Exforsys
{
protected:
int x;
;

class Exf1:virtual public Exforsys


{ };

class Exf2:virtual public Exforsys


{ };

class Training:public Exf1,public Exf2


{
public:
int example()
{
return x;
}
};

Potrebbero piacerti anche