Popular Posts

Wednesday, October 3, 2012

Programs of C++



                                                                         INDEX

Sr.No
                                 Topic 
Signature
1.
Program for sorting list of array elements

2.
Program for searching list of elements

3.
Program for call by reference

4.
Program for arithmetic operator overloading

5.
Program for postfix increment operator overloading

6.
Program for function overloading

7.
Program for constructor with explicit call and implicit call

8.
Program to implement class and object

9.
Program for polymorphism

10.
Program for copy constructor


 Programs of C++ 



//Program for sorting list of elements

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[40],n,i,j,k,temp,min,p;
cout<<"Enter number of elements:";
cin>>n;
cout<<"Enter the elements one by one:";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"list of array is:\n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<"\n";
}
for(k=1;k<=n-1;k++)
{
  min=a[k];
  p=k;
  for(i=k+1;i<=n;i++)
  {
  if(min>a[i])
   {
   min=a[i];
   p=i;
   }
  }
  if(p!=k)
  {
  temp=a[k];
  a[k]=a[p];
  a[p]=temp;
  }
}
cout<<"Sorted array is:";

for(i=1;i<=n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}
Output:
Enter number of elements : 5
Enter the elements one by one : 23
34
54
655
67
List of array is:
23
34
54
655
67
Sorted array is:
23
34
54
67
655

 2.
//Program for searching list of array elements

#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,a[10],search,loc;
clrscr();
cout<<"Enter number of elements:\n";
cin>>n;
cout<<"Enter array elements:\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"Enter the element to be searched:";
cin>>search;
i=1;
while(i<=n)
{
 if(a[i]==search)
 {
 loc=i;
 cout<<"Element is found at position:"<<loc;
 goto end;
 }
 else
 {
 i=i+1;
 }
 }
 cout<<"Element not found";
 end:
 getch();
 }

Output:
Enter number of elements:
4
Enter array elements:
3
4
76
7
Enter the element to be searched:3
Element is found at position:1
 
3.

//Program for call by reference

#include<iostream.h>
#include<conio.h>
void swap(int &x, int &y);
void main()
{
int a,b;
clrscr();
cout<<"Enter two values:\n" ;
cin>>a>>b;
cout<<"value before exchange:";
cout<<"\nvalue of A is:"<<a;
cout<<"\nvalue of B is:"<<b;
swap(a,b);
cout<<"\nThe exchanged value are:";
cout<<"\nvalue of A is:"<<a;
cout<<"\nvalue of B is:"<<b;
getch();
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}

Output:
Enter two values:
34
56
Value before exchange:
Value of A is: 34
Value of B is: 56
The exchanged value are:
Value of A is: 56
Value of B is: 34


4.

//Program for arithmetic operator overloading

#include<iostream.h>
#include<conio.h>
class sample
{
float a,b;
public:
sample() {};
sample(float x, float y)
{
a=x;
b=y;
}
sample operator+(sample);
void display(void)
{
cout<<"a="<<a<<"\n";
cout<<"b="<<b<<"\n";
}
};
sample sample :: operator+(sample c)
{
sample temp;
temp.a = a+c.a;
temp.b = b+c.b;
return(temp);
}
void main()
{
sample x,y,z;
x = sample(1.5 ,4.5);
y = sample(6.25 ,3.5);
clrscr();
z = x+y;
cout<<"first object \n";
x.display();
cout<<"second object\n";
y.display();
cout<<"sum of objects\n";
z.display();
getch();
}




Output:
first object
a = 1.5
b = 4.5
second object
a = 6.25
b = 3.5
sum of objects
a = 7.75
b = 8

 

5.

//Program for postfix increment operator

#include<iostream.h>
#include<conio.h>
class complex
{
int real,img;
public:
complex(){};
complex(int a,int b)
{
real=a;
img=b;
}
void display()
{
cout<<real<<"+"<<img<<"i\n";
}
complex operator++(int);
};
complex complex::operator++(int x)
{
real++;
img++;
return *this;
}
void main()
{
clrscr();
complex c(5,10);
c++;
c.display();
c++;
c.display();
getch();
}


Output:
6+11i
7+12i

6.

//Program to function overloading

#include<iostream.h>
#include<conio.h>
void mul(int);
void mul(int,int);
void main()
{
clrscr();
mul(10);
mul(10,20);
getch();
}
void mul(int a)
{
int i;
cout<<"table of 10 is:\n";
for (i=1;i<=10;i++)
{
cout<<i*a<<"\n";
}
}
void mul(int a,int b)
{
int z;
z=a*b;
cout<<"multiplication of 10 and 20 is:\n"<<z;
}


Output:
table of 10 is:
10
20
30
40
50
60
70
80
90
100
multiplication of 10 and 20 is:
200
 

7.

//Program to implement class and object

#include<iostream.h>
#include<conio.h>
class sample
{
 int a,b,c;
 public:
 void getdata()
 {
 cout<<"enter two numbers:";
 cin>>a>>b;
 }
 void sum()
 {
 c=a+b;
 }
 void putdata()
 {
 cout<<"\na="<<a;
 cout<<"\nb="<<b;
 cout<<"\nc="<<c;
 }
};
void main()
{
sample s1;
clrscr();
s1.getdata();
s1.sum();
s1.putdata();
getch();
}


Output:
Enter two number:23
43

a=23
b=43
c=66

8.

//Program for copy constructor

#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
       sample()
       {
       a=0;
       b=0;
       }
       sample(sample &i);
       void display(void)
       {
       cout<<"a= " <<a<<"b= "<<b<<"\n";
       }
 };
 sample::sample(sample &i)
 {
 a=i.a;
 b=i.b;
 }
 void main()
 {
 sample s1,s2(s1);
 clrscr();
 cout<<"First object\n";
 s1.display();
 cout<<"Second object\n";
 s2.display();
 cout<<"Third object\n" ;
 sample s3=s2;
 s3.display();
 getch();
 }


Output:
First object
a=0 b=0
Second object
a=0 b=0
Third object
a=0 b=0
 

9.

//Program for polymorphism

#include<iostream.h>
#include<conio.h>
class sample
{
  int a;
 public:
  sample()
  {
  a=10;
  }
  virtual void display()
  {
   cout<<"a="<<a<<"\n";
  }
};
class derived :public sample
{
 int b;
public:
 derived()
 {
 b=20;
 }
 void display()
 {
  cout<<"b="<<b<<"\n";
 }
};
void main()
{
 sample s;
 derived*ptr;
 ptr=(derived*)&s;
 clrscr();
 ptr->display();

 derived d;
 ptr=&d;
 ptr->display();
 getch();
}

Output:
a=10
b=20

10.

//Program for constructor with implicit call and explicit call

#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
sample(int i,int j);
void display(void);
}
sample :: sample(int i,int j)
{
a=i;
b=j;
}
void sample::display()
{
cout<<"a=" <<a<<" b="<<b<<"\n";
}
 void main()
 {
 sample c(5,10);
 clrscr();
 c.display();
 sample d=sample(3,9);
 d.display();
 getch();
}


Output:
a=5 b=10
a=3 b=9