You are on page 1of 10

14.

/*unary operator overloading*/

#include<iostream.h>
#include<conio.h>

class com
{
protected:
float x,y;
public:
com() {}
com(float real, float img)
{
x = real;
y = img;
}

friend com operator –(com);


void operator++();
void disp();
};
com operator –(com z)
{
return com(--z,x,--z,y);
}
void com :: operator++()
{
x++;
y++;
}
void com :: disp()
{
cout<<x<<”+”(“<<y<<”)j\n;
}
int main()
{
clrscr();
com c1,c3,s;
float r1, i 1;
cout<<”\n\t Complex Number \n”;
cout<<”\n Enter the real number: ”;
cin>>r1;
cout<<”\n Enter the imaginary value: ”;
cin>>i1;
c1 = com(r1,i1);
c3 = operator—(c1);
cout<<”\n \t complex number (decrement by 1): “);
c3.disp();
s = com(r1,i1);
++s;
cout<<”\n \t complex number (increment by 1): “;
cout<<”s:”’
s.disp();
getch();
}

OUTPUT:
Complex Number
Enter the real number: 2
Enter the imaginary value: 2
complex number (decrement by 1): 1+(1)j
complex number (decrement by 1): s: 3+(3)j

19. /*function overloading*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

class over
{
public:
int volume(int x)
{
return(x*x*x); //cube
}
float volume(int r, int h)
{
return(3.14*r*r*h); //cylinder
}
float volume(int l, int b, int h)

{
return(l*b*h); //rectangle
}
};

void main()
{
over fo;
int x, l, b, h, r, a, n, con;
float r1, d, d1;
double a2, r2;
clrscr();
cout<<”**********FUNCTION OVERLOADING**********”;
load;
cout<<”\n enter your choice :”;
cout<<”\n 1cube \n 2cylinder \n 3rectangle”;
cin>>n;

switch(n)
{
case 1:
cout<<”\n The volume of cube “;
cout<<”\n \n enter the value of x :”;
cin>>x;
a = fo.volume(x);
cout<<”\n the volume is :”<<a;
break;

case 2:
cout<<”\n The volume of cylinder “;
cout<<”\n \n enter the value of radius & height :”;
cin>>r>>h;
a1 = fo.volume(r,h);
cout<<”\n the volume is :”<<a1;
break;

case 3:
cout<<”\n The volume of rectangle “;
cout<<”\n \n enter the value of l,b & h :”;
cin>>l>>b>>h;
a1 = fo.volume(l,b,h);
cout<<”\n the volume is :”<<a1;
break;

OUTPUT:
**********FUNCTION OVERLOADING**********
enter your choice
1 cube
2 cylinder
3 rectangle 1

The volume of cube


enter the value of x : 5
the volume is : 125

do you want to continue


0 yes 1 no 0

enter your choice


1 cube
2 cylinder
3 rectangle 2

The volume of cylinder


enter the value of radius & height : 2 3
the volume is : 37.68

do you want to continue


0 yes 1 no 0

enter your choice


1 cube
2 cylinder
3 rectangle 3

The volume of rectangle


enter the value of l, b, & h : 2 4 6
the volume is : 48

do you want to continue


0 yes 1 no 0

16 /*employee details using array of objects*/

#include<iostream.h>
#include<conio.h>

class emp

{
char name[10];
int eno,bs;
float pf, hra, gp, net;

public:
void input();
void output();
};

void emp :: input()


{
cout<<”\n enter employee name :”);
cin>>name;

cout<<”\n enter employee number :”);


cin>>eno;

cout<<”\n enter basic salary :”);


cin>>bs;
}

void main()
{
int n;
emp c[10];
clrscr();
cout<<”enter the number of person :”;
cin>>n;
for(int i = 0; i < n; i++);
{
c[i].input();
c[i].output();
}
getch();

};

OUTPUT
enter the number of person :1
enter employee name : santhosh
enter employee number : 230
enter basic salary : 12000

EMPLOYEE DETAILS

employee name : santhosh


employee number : 230
basic salary : 12000
the gross pay : 21600
net pay : 20160
11. /*prime numbers between 1 and 50*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i, j;
clrscr();

cout<<”the prime series is from 1 to 50 \n”;

for(i = 1; i < = 50; i++)


{
for(j = 2; j < i; j++);
{
if(i % j = = 0)
break;
}
if(i = = j)
cout<<i;
}
getch();
}

OUTPUT
Prime Series

2
5
7
11
13
17
19
23
29
31
37
41
43
47
20. /*class template to find maximum of two numbers*/
#include<iostream.h>
#include<conio.h>

template<class T1, class T2>


class Test
{
T1 a;
T2 b;

public:
Test(T1 x, T2 y)
{
a = x;
b = y;
}
if a > b
cout<<”a is maximum \n”;
else
cout<<”b is maximum \n”;
}

int main()

test1.show();
test2.show();

};
7. /*data manipulations on array*/
#include<iostream.h>
#include<conio.h>
int a[5], ctr = 0, sum;

void main()
{
for(; ctr<5; ctr++)
{
cout<<”\n Enter 5 value “);
cin>> a[ctr];
}

//generating sum of all the elements stored in array

for(ctr = 0; sum = 0; ctr<5; ctr++)


cout<<’\t’ << a[ctr];
cout<<”\n Sum of the elements “<<sum;
getch();
}
OUTPUT:
Enter 5 values
5 3 2 1 4
Sum of the elements
15
8. /*reverse a string*/
#include<iostream.h>
#include<conio.h>

void main()
{
int even[3] = {0,2,4};
int reverse[3];
for(int i = 0; int j = 2; i < 3; i++, j - -)
reverse[j] = even [i];
clrscr();
for(i = 0; i < 3; i++)
cout’\t’<< even[i] << ‘\t’ << reverse [i] << ‘\n’;
getch();
}

OUTPUT
0 4
1 2
4 0

8. /*length of a string*/
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
char name[20];
cout<<”Enter the name\n”;
cin>>name;
cout<<”\n The length of the string \t“<<strlen(name);
getch();
}

8. /*copy, concatenate string*/


#include<iostream.h>
#include<conio.h>
#include<string.h>

class strings
{
char s[10];
public:
strings()
{
s[0] = ‘\0’;
}
strings(char *c)
{
strcpy(s,c);
}
char *operator+(strings x1)

{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}
};

You might also like