Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Program 7

Write a program using function overloading, overload in three ways so that it return square root
for int, double and long int.
#include<iostream.h>
#include<conio.h>
#include <math.h>
int squareroot(int s)
{
return sqrt(s);
}
double squareroot(double s1)
{
return sqrt(s1);
}
long squareroot(long s2)
{
return sqrt(s2);
}
void main()
{
clrscr();
cout<<"\nThe square root of is: "<< squareroot(625);
cout<<"\nThe square root of is: "<<squareroot(625.25);
cout<<"\nThe square root of is: "<<squareroot(62500);
getch();
}

OUTPUT

Program 8
Write a program to implement Unary operator overloading.
#include<iostream.h>
#include<conio.h>
class sample
{
int m,n;
public:
void getdata(int a, int b);
void display();
void operator-();
};
void sample::getdata(int a, int b)
{
n=b;
m=a;
}
void sample::display()
{
cout<<m<<"\t ";
cout<<n<<"\t ";
}
void sample::operator-()
{
m=-m;
n=-n;
}
void main()
{
clrscr();
sample ob;
ob.getdata(40,-25);
cout<<"\nvalues of data member before operator overloading: ";
ob.display();
-ob;
cout<<"\nvalues of data member after operator overloading: ";
ob.display();
getch();
}

OUTPUT

Program 9
Write a program to implement Binary operator overloading.
#include<iostream.h>
#include<conio.h>
class sample
{
float m,n;
public:
sample(){};
sample(float a, float b)
{
n=b;
m=a;
}
void display()
{
cout<<"m = "<<m;
cout<<"\nn = "<<n;
}
sample operator+(sample);
};
sample sample::operator+(sample c)
{
sample temp;
temp.m=m+c.m;
temp.n=n+c.n;
return(temp);
}
void main()
{
clrscr();
sample ob,ob1,ob2;
ob=sample(1.5,7.25);
ob1=sample(5.25,2.15);
ob2=ob+ob1;
cout<<"\nfirst object is:\n";
ob.display();
cout<<"\nsecond object is:\n";
ob1.display();
cout<<"\nsum of object is:\n";
ob2.display();
getch();
}

OUTPUT

Program 10
Write a program using friend function.
#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
void set()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return(s.a+s.b)/2.0;
}
void main()
{
clrscr();
sample ob;
ob.set();
cout<<"mean value is "<<mean(ob);
getch();
}

Program 15
Write a program using virtual function, explain polymorphism.
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void vfunc()
{
cout<<"\nthis is base class virtual function";
}
};
class derived:public base
{
public:
void vfunc()
{
cout<<"\nthis is derived class virtual function";
}
};
void main()
{
clrscr();
base *p,b;
derived d;
p=&b;
p->vfunc();
p=&d;
p->vfunc();
getch();
}

Program 17
Write a program to show public inheritance.
#include<iostream.h>
#include<conio.h>
class base
{
int i,j;
public:
void set(int a, int b)
{
i=a;
j=b;
}
void display()
{
cout<<i<<"\t"<<j;
}
};
class derived:public base
{
int k;
public:
void setk(int x)
{
k=x;
}
void show()
{
cout<<"\t"<<k;
}
};
void main()
{
clrscr();
derived ob;
ob.set(10,20);
ob.display();
ob.setk(30);
ob.show();
getch();
}

OUTPUT

Program 18
Write a program to show private inheritance.
#include<iostream.h>
#include<conio.h>
class base
{
protected:
int i,j;
public:
void set(int a, int b)
{
i=a;
j=b;
}
void display()
{
cout<<i<<"\t"<<j;
}
};
class derived:private base
{
int k;
public:
void show()
{
set(10,20);
display();
k=i*j;
cout<<"\t"<<k;
}
};
void main()
{
clrscr();
derived ob;
ob.show();
getch();
}

Program 19
Write a program to show protected inheritance.
#include<iostream.h>
#include<conio.h>
class base
{
protected:
int i,j;
public:
void set(int a, int b)
{
i=a;
j=b;
}
void display()
{
cout<<i<<"\t"<<j;
}
};
class derived:protected base
{
int k;
public:
void show()
{
set(10,20);
display();
k=i*j;
cout<<"\t"<<k;
}
};
void main()
{
clrscr();
derived ob;
ob.show();
getch();
}

OUTPUT

You might also like