Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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>
void main()
{
isum = add(a,b);
fsum = add(x,y);
dsum = add(p,q);
#include <iostream.h>
}
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>
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
class student
{
private: int roll_no;
char name[25];
float percentage;
public: void getData(int rn, char nm[], float pt);
void putData(void);
};
void main()
{
student s; //s is an object of type student
student *sptr; //sptr is an object poiner
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 }
{ }; };