1) WAP For Function Overloading
1) WAP For Function Overloading
#include<iostream>
using namespace std;
class A
{
public:
int get()
{
cout<<"Hello"<<endl;
return 0;
}
int get(int x,float y)
{
cout<<"The sum of two numbers is:"<<x+y<<endl;
return 0;
}
int get(int x,float y,int z)
{
cout<<"The sum of three numbers is:"<<x+y+z<<endl;
return 0;
}
};
int main()
{
A c;
c.get();
c.get(3,3.5);
c.get(3,3.5,4);
return 0;
}
OUTPUT:
EXPERIMENT-2
2)WAP for covering inbuilt functions of string
#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char a[15],b[15],c[15];
cout<<"Enter first string:";
cin>>a;
cout<<endl;
cout<<"Enter second string:";
cin>>b;
cout<<endl;
strcpy(c,a);
cout<<"The first string is:"<<a<<endl<<"The copy of first string is:"<<c<<endl;
cout<<"The concatanation first and second string is:"<<strcat(c,b)<<endl;
OUTPUT:
EXPERIMENT-3
3)WAP for usedefined counter part for strcpy(), strcat(), strcmp()
#include<iostream>
using namespace std;
int main()
{
int i,j,temp=0;
char a[15],b[15],c[15];
cout<<"Enter first string:";
cin>>a;
cout<<endl;
cout<<"Enter second string:";
cin>>b;
cout<<endl;
for(i=0;a[i]!='\0';i++)
{
c[i]=a[i];
}
cout<<"The copied string of first string:";
for(i=0;a[i]!='\0';i++)
{
cout<<c[i];
}
cout<<endl;
cout<<"The concatanation of first and second string is:";
for(i=0; a[i]!='\0'; ++i); /* i contains length of string s1. */
for(j=0; b[j]!='\0'; ++j, ++i)
{
a[i]=b[j];
}
a[i]='\0';
cout << "After concatenation: " << a;
cout<<endl;
cout<<"comparing of first and second strings:"<<endl;
for(i=0;a[i]!='\0';i++)
{
if(a[i]==b[i])
temp=1;
else
temp=0;
}
if(temp==1)
cout<<"Both strings are equal"<<endl;
else
cout<<"Both strings are not equal"<<endl;
return 0;
}
}
OUTPUT:
EXPERIMENT-4
4)WAP for constructor and destructor overloading
#include<iostream>
using namespace std;
class hell
{
public:
hell()
{
cout<<"constructor"<<endl;
}
hell(int x,int y)
{
Cout<<”sum is:”<<x+y<<endl;
}
~hell()
{
cout<<"destructor"<<endl;
}
};
int main()
{
hell obj,ob(2,3);
return 0;
OUTPUT:
EXPERIMENT-5
5)WAP to illustrate the concept of friend class
#include <iostream>
using namespace std;
class a
{
public:
void print(int a)
{
cout<<"Interger: "<<a<<endl;
}
void print(int a,int b)
{
cout<<"Integer1: "<<a<<" Integer2: "<<b<<endl;
}
void print(float a)
{
cout<<"float: "<<a<<endl;
}
void print(char a)
{
cout<<"char: "<<a<<endl;
}
};
int main()
{
a obj;
int a=10,d=20;
float b=213.213;
char c='c';
obj.print(a);
obj.print(b);
obj.print(c);
obj.print(a,b);
return 0;
}
OUTPUT:
EXPERIMENT-6
EXPERIMENT-6.1
6)WAP to illustrate multilevel inheritance
#include <iostream>
using namespace std;
class a
{
public:
int english1 , science1 , bcpc1 ;
string uid1,section1,name1;
int getdetails1()
{
cout<<"enter your name:"<<endl;
cin>>name1;
cout<<"enter your uid:"<<endl;
cin>>uid1;
cout<<"enter your section:"<<endl;
cin>>section1;
cout<<"enter marks of english:"<<endl;
cin>>english1;
cout<<"enter marks of science:"<<endl;
cin>>science1;
cout<<"enter marks of bcpc:"<<endl;
cin>>bcpc1;
return 0;
}
};
class b
{
public:
int english2 , science2 , bcpc2 ;
string uid2,section2,name2;
int getdetails2()
{
cout<<"enter your name:"<<endl;
cin>>name2;
cout<<"enter your uid:"<<endl;
cin>>uid2;
cout<<"enter your section:"<<endl;
cin>>section2;
cout<<"enter marks of english:"<<endl;
cin>>english2;
cout<<"enter marks of science:"<<endl;
cin>>science2;
cout<<"enter marks of bcpc:"<<endl;
cin>>bcpc2;
return 0;
}
};
class c
{
public:
int english3 , science3 , bcpc3;
string uid3,section3,name3;
int getdetails3()
{
cout<<"enter your name:"<<endl;
cin>>name3;
cout<<"enter your uid:"<<endl;
cin>>uid3;
cout<<"enter your section:"<<endl;
cin>>section3;
cout<<"enter marks of english:"<<endl;
cin>>english3;
cout<<"enter marks of science:"<<endl;
cin>>science3;
cout<<"enter marks of bcpc:"<<endl;
cin>>bcpc3;
return 0;
}
};
class final : public a,public b,public c
{
public:
int display()
{
cout<<"srno\tuid\tname\tsection\tenglish\tscience\tbcpc"<<endl;
cout<<"1\t"<<uid1<<"\t"<<name1<<"\t"<<section1<<"\t"<<english1<<"\t"<<sci
ence1<<"\t"<<bcpc1<<endl;
cout<<"2\t"<<uid2<<"\t"<<name2<<"\t"<<section2<<"\t"<<english2<<"\t"<<sci
ence2<<"\t"<<bcpc2<<endl;
cout<<"3\t"<<uid3<<"\t"<<name3<<"\t"<<section3<<"\t"<<english3<<"\t"<<sci
ence3<<"\t"<<bcpc3<<endl;
return 0;
}
};
int main()
{
final obj;
obj.getdetails1();
obj.getdetails2();
obj.getdetails3();
obj.display();
return 0;
}
OUTPUT:
EXPERIMENT-7
7) WAP to illustrate inbuilt functions like puts(), gets()
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
char a[20];
cout<<"enter any thing to check for aplhabet"<<endl;
gets(a);
puts(a);
int i;
for (i=0;a[i]!='\0';i++)
{
if (isalpha(a[i])) {
cout<<a[i]<<" is a alphabet"<<endl;
}
else
{
cout<<a[i]<<" is not a alphabet"<<endl;
}
}
for (i=0; a[i]!='\0'; i++) {
if (isdigit(a[i])) {
cout<<a[i]<<" is a digit"<<endl;
}
else
{
cout<<a[i]<<" is not a digit"<<endl;
}
}
for (i=0; a[i]!='\0'; i++) {
if (isupper(a[i])) {
cout<<a[i]<<" is a upper case charecter"<<endl;
}
else
{
cout<<a[i]<<" is not a upper case charecter"<<endl;
}
}
for (i=0; a[i]!='\0'; i++) {
if (islower(a[i])) {
cout<<a[i]<<" is a lower case charecter"<<endl;
}
else
{
cout<<a[i]<<" is not a lower case charecter"<<endl;
}
}
char b[20];
for (i=0; a[i]!='\0'; i++) {
b[i]=tolower(a[i]);
}
puts(b);
char c[20];
for (i=0; a[i]!='\0'; i++) {
c[i]=toupper(a[i]);
}
puts(c);
srand(time(NULL));
for (i=0; i<=10; i++) {
cout<<rand()<<" ";
}
cout<<endl;
random();
for (i=0; i<=10; i++) {
cout<<rand()<<" ";
}
cout<<endl;
return 0;
}
OUTPUT:
EXPERIMENT-8
8) WAP to illustrate operator overloading
#include <iostream>
using namespace std;
class a
{
public:
int b,c;
void getint()
{
cout<<"Enter first coordinate: "<<endl;
cin>>b;
cout<<"Enter second coordinate: "<<endl;
cin>>c;
}
void display()
{
cout<<"Enter first coordinate: "<<b<<endl;
cout<<"Enter second coordinate: "<<c<<endl;
}
};
a operator + (a obj1 , a obj2)
{
a temp;
temp.b= (obj1.b+obj2.b)/2;
temp.c= (obj1.c+obj2.c)/2;
return (temp);
}
int main()
{
a obj1;
a obj2;
a obj3;
obj1.getint();
obj2.getint();
obj3=obj1+obj2;
obj3.display();
return 0;
}
OUTPUT:
EXPERIMENT-9
9) WAP to illustrate new and delete operators
#include<iostream>
using namespace std;
int main()
{
int n,i;
cout<<"Enter the no.of students:";
cin>>n;
int *p;
p=new int [n];
if(p==NULL)
{
cout<<"Memory couldn't be allocated"<<endl;
}
else
{
for(i=0;i<n;i++)
{
cout<<"Enter the marks:";
cin>>*(p+i);
}
}
cout<<"The elements in pointer array are:";
for(i=0;i<n;i++)
{
cout<<*(p+i)<<"\t";
}
delete [] p;
return 0;
}
OUTPUT:
10) WAP to illustrate Exception Handling
#include <iostream>
using namespace std;
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
OUTPUT:
10.1) WAP to illustrate Rethrowing of an exception
#include<iostream>
using namespace std;
void sub(int i,int j)
{
cout<<"Inside sub function"<<endl;
try{
if(i==0)
throw i;
else
cout<<"The substraction is:"<<i-j<<endl;
}
catch(int)
{
cout<<"NULL value"<<endl;
throw 9.5;
}
catch(float)
{
cout<<"Inside main"<<endl;
}
return 0;
}
OUTPUT:
11) WAP to illustrate Function Template
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
return (n1>n2) ? n1:n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
cout<<"Enter two integers: ";
cin>>i1>>i2;
cout<<Large(i1, i2)<<" is larger.";
cout<<"\n\nEnter two floating-point numbers: ";
cin>>f1>>f2;
cout<<Large(f1, f2)<<" is larger.";
cout<<"\n\nEnter two characters: ";
cin>>c1>>c2;
cout<<Large(c1, c2)<<" has larger ASCII value.";
return 0;
}
OUTPUT:
12) WAP to illustrate class Template
// C++ class template example
#include <iostream>
using namespace std;
// destructor
template <class any_data_type>
Test<any_data_type>::~Test()
{cout<<"Destructor, deallocate..."<<endl;}
OUTPUT: