Lab Programs of C++


  • Important programs of c++ language 
Cleck here for out puts๐Ÿ‘‰:Out puts of this programs
Note:
1.some program are not their in blog those will be posted soon.
2.some program not run in phone and some are show different out puts in phone .So, those type programs try in system..
3.For Those who writing record : comment section may or may not be written in record.
                                                                          
♠️๐Ÿ™Please do follow, share and subscribe  ,any mistake in blog comment ๐Ÿ™♠️
                                                                          

1.write a c++ program to find the add two interfere by class concept.
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b,c;
public:
void accept()
{
cout<<"Enter a,b value\n";
cin>>a>>b;
}
void add()
{
c=a+b;
}
void display()
{
cout<<c;
}
};
int main()
{
abc a;
clrscr();
a.accept();
a.add();
a.display();
getch();
}
                                                                                

2.program on scope resolution operator.

#include<iostream.h>
#include<conio.h>
int s=10;
int main()
{
int s=50;
{
int s=40;
cout<<"inner block ";
cout<<"s="<<s;
cout<<"::s="<<::s;
}
cout<<"\nouter block ";
cout<<"s="<<s;
cout<<"::s="<<::s;
getch();
}

                                                                                

3.Program on Manipulate setw,endl.

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a=100,b=20,c;
cout<<"a="<<setw(4)<<a<<endl;
cout<<"b="<<setw(4)<<b<<endl;
c=a+b;
cout<<"c="<<setw(4)<<c;
return 0;
}
                                                                                


4.Program on inline function with class without class
i】without class
#include<iostream>
#include<iomanip>
using namespace std;
inline float mul(float a,float b)
{
return(a*b);
}
inline float div(double a,double b)
{
return(a/b);
}
int main()
{
float a=12,b=4;
cout<<mul(a,b)<<"\n"<<div(a,b)<<endl;
return 0;
}
ii】with class
#include<iostream>
#include<iomanip>
using namespace std;
class inl
{
public:
inline float mul(float a,float b)
{
return(a*b);
}
//by default all member functions of class is inline function
float div(double a,double b)
{
return(a/b);
}
};
int main()
{
inl i;
float a=12,b=4;
cout<<i.mul(a,b)<<"\n"<<i.div(a,b)<<endl;
return 0;
}
                                                                                

5.Nesting of member functions .
#include<iostream>
#include<iomanip>
using namespace std;
class demo
{
public:
void dis() //all member functions are user define
{
cout<<"Hello ";
}
void show()
{
dis();
cout<<"world";
}
};
int main()
{
demo d;
d.show();
return 0;
}
                                                                                

6.WAP on Private member function.
#include<iostream>
#include<iomanip>
using namespace std;
class demo
{
private: //by default all member of class a are private
int a,b;
void acc()
{
cout<<"enter a,b values\n";
cin>>a>>b;
}
public:
void dis()
{
acc();
cout<<"a+b="<<(a+b);
}
};
int main()
{
demo d;
d.dis();
return 0;
}
                                                                                

7.WAP on i】Static data member ii】Static member function .
i】Static data member
#include<iostream>
#include<iomanip>
using namespace std;
class demo
{
static int count;
int n;
public:
void getdata(int a)
{
n=a;
count++;
cout<<"a="<<a;
}
void getcount()
{
cout<<"\ncount="<<count<<endl;
}
};
int demo::count=7;
int main()
{
demo x,y;
x.getcount();
x.getdata(5);
y.getcount();
y.getdata(7);
return 0;
}
ii】Static member function
#include<iostream>
#include<iomanip>
using namespace std;
class demo
{
static int count;
int c;
public:
void setcode()
{
++count;
++c;
}
void show()
{
cout<<"object no ="<<c<<endl;
}
//static member function allows only static data members
static void showcount()
{
cout<<"count="<<count<<endl;
}
};
int demo::count;
int main()
{
demo x;
x.setcode();
x.show();
demo::showcount();
return 0;
}
                                                                                


8.WAP on array of object
#include<iostream.h>
#include<conio.h>
class shashi
{
int a,b,c;
public:
void accept()
{
cout<<"Enter a,b value\n";
cin>>a>>b;
c=a+b;
}
void display()
{
cout<<"a+b="<<(a+b)<<endl;
}
};
int main()
{
shashi s[3];
int i;
clrscr();
for(i=0;i<3;i++)
s[i].accept();
for(i=0;i<3;i++)
s[i].display();
getch();
}
                                                                                

9.WAP on bubble sort in class
#include<iostream.h>
#include<conio.h>
class shashi
{
int i,j,t,n,a[10];
public:
void acc();
void dis();
void sort();
};
void shashi::acc()
{
cout<<"enter array value\n";
for(i=0;i<5;i++)
cin>>a[i];
}
void shashi::dis()
{
cout<<"array values"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<endl;
}
void shashi::sort()
{
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
void main()
{
shashi s;
clrscr();
s.acc();
cout<<"before sort"<<endl;
s.dis();
s.sort();
cout<<"after sort"<<endl;
s.dis();
getch();
}
                                                                                

10.WAP on linear search in class
#include<iostream.h>
#include<conio.h>
class leaner
{
    int n,i,a[10],se,t=1;
    public:
    void accept();
    void display();
    void search();
};
void leaner::accept()
{
cout<<"enter array values"<<endl;
    for(i=0 ; i<5; i++)
    cin>>a[i];
}
void leaner::display()
{
cout<<"array values"<<endl;
    for(i=0;i<5;i++)
    cout<<a[i]<<endl;
}
void leaner::search()
{
cout<<"enter serch element"<<endl;
cin>>se;
    for(i=0;i<5;i++)
    {
     if(a[i]==se)
     {
     cout<<"location of the serch element="<<i<<endl;
     t++;
     }
    }
    if(t==1)
   cout<<"serch element is not found"<<endl; 
}
void main()
{
    leaner s ;
    clrscr();
    s.accept();
    s.display();
    s.search();
    getch();
}
                                                                                


11.WAP on minimum,maximum of array in class
#include<iostream.h>
#include<conio.h>
int main ()
{
    int a[10], n, i, max, min;
    cout << "Enter array size :\n";
    cin >> n;
    cout << "Enter array elements : \n";
    for (i = 0; i < n; i++)
        cin >> a[i];
    max = a[0];
    for (i = 0; i < n; i++)
    {
        if (max < a[i])
            max = a[i];
    }
    min = a[0];
    for (i = 0; i < n; i++)
    {
        if (min > a[i])
            min = a[i];
    }
  cout<<"maximam value :"<<max<< "\nminimam value :" << min;
    getch();
}
                                                                                

12.WAP on transpose of matrix in class
#include <iostream.h>
#include <conio.h>
class arr
{
int a[4][4], b[4][4], i, j,nr,nc;
  public:
void acc();
void dis();
void trans();
};
void arr::acc()
{
cout<<"enter no of roos and colomus \n";
cin>>nr>>nc;
cout << "enter array values of a\n";
for (i = 0; i < nr; i++)
{
for (j = 0; j < nc; j++)
cin >> a[i][j];
}
}
void arr::dis()
{
cout << "array values of a\n";
for (i = 0; i < nr; i++)
{
for (j = 0; j < nc; j++)
cout << a[i][j] <<"\t";
cout<<endl;
}
}
void arr::trans()
{
cout<<"\ntranspose of matrix\n";
for(i=0;i<nr;i++)
{
for(j=0;j<nc;j++)
cout<<a[j][i]<<"\t";
cout<<endl;
}
}
void main()
{
arr a;
clrscr();
a.acc();
a.dis();
a.trans();
getch();
}


                                                                                


13.WAP on matrix addition of class
#include <iostream.h>
#include <conio.h>
class arr
{
int a[10][10],b[10][10],i, j,nr,nc;
  public:
void acc();
void add();
};
void arr::acc()
{
cout<<"enter no of roos and colomus \n";
cin>>nr>>nc;
cout << "enter array values of a\n";
for (i = 0; i < nr; i++)
{
for (j = 0; j < nc; j++)
cin >> a[i][j];
}
cout << "enter Array values of b\n";
for (i = 0; i < nr; i++)
{
for (j = 0; j < nc; j++)
cin >> b[i][j];
}
}
void arr::add()
{
cout<<"\naddition of matrix\n";
for(i=0;i<nr;i++)
{
for(j=0;j<nc;j++)
cout<<(a[i][j]+b[i][j])<<"\t";
cout<<endl;
}
}
void main()
{
arr a;
clrscr();
a.acc();
a.add();
getch();
}
                                                                                


14.WAP on matrix multiplication
#include <iostream.h>
#include <conio.h>
class arr
{
int a[10][10],b[10][10],c[10][10],i, j,k,nr,nc,nr1,nc1;
  public:
void acc();
void mul();
};
void arr::acc()
{
cout<<"enter nr,nr1,nc and nc1\n";
cin>>nr>>nr1>>nc>>nc1;
cout << "enter array values of a\n";
for (i = 0; i < nr; i++)
{
for (j = 0; j < nc; j++)
cin >> a[i][j];
}
cout << "enter Array values of b\n";
for (i = 0; i < nr1; i++)
{
for (j = 0; j < nc1; j++)
cin >> b[i][j];
}
}

void arr::mul()
{
for(i=0;i<nr;i++)
{
for(j=0;j<nc1;j++)
{
c[i][j]=0;
for(k=0;k<nc;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
         
}}
for(i=0;i<nr;i++)
{
for(j=0;j<nc1;j++)
cout<<c[i][j]<<"\t";
cout<<endl;
}
}
void main()
{
arr a;
clrscr();
a.acc();

cout<<"mul matrix\n";
a.mul();
getch();
}
                                                                                


15.WAP on multiplication matrix with checking condition.
#include <iostream.h>
#include <conio.h>
#include<process.h>
class arr
{
int a[10][10],b[10][10],c[10][10],i, j,k,nr,nc,nr1,nc1;
  public:
void acc();
void mul();
};
void arr::acc()
{
cout<<"enter nr,nr1,nc and nc1\n";
cin>>nr>>nr1>>nc>>nc1;
if(nc!=nr1)
{
cout<<"matrix multipilication is not possable";
exit(0);
}
cout << "enter array values of a\n";
for (i = 0; i < nr; i++)
{
for (j = 0; j < nc; j++)
cin >> a[i][j];
}
cout << "enter Array values of b\n";
for (i = 0; i < nr1; i++)
{
for (j = 0; j < nc1; j++)
cin >> b[i][j];
}
}

void arr::mul()
{
for(i=0;i<nr;i++)
{
for(j=0;j<nc1;j++)
{
c[i][j]=0;
for(k=0;k<nc;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
         
}}
for(i=0;i<nr;i++)
{
for(j=0;j<nc1;j++)
cout<<c[i][j]<<"\t";
cout<<endl;
}
}
void main()
{
arr a;
clrscr();
a.acc();

cout<<"mul matrix\n";
a.mul();
getch();
}
                                                                                


16.WAP on sorting strings
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[6][20], t[20];
int i, j;
cout<<"Enter six name\n";
for(i=0; i<5; i++)
cin>>str[i];
for(i=1; i<5; i++)
{
for(j=i-1; j<5; j++)
{
if(strcmp(str[j], str[i])>0)
{
strcpy(t, str[j]);
strcpy(str[j], str[i]);
strcpy(str[i], t);
}
}
}
cout<<"Names in alphabetical order \n";
for(i=0; i<5; i++)
cout<<str[i]<<endl;
getch();
}
                                                                                

17.WAP on multi dimensional array
#include <iostream.h>
#include<conio.h>
class arr
{
int i,j,k,test[2][3][2];
public:
void acc();
void dis();
};
void arr::acc()
{
    cout << "Enter 12 values: \n";
    for(i = 0; i < 2; ++i)
    {
        for ( j = 0; j < 3; ++j)
        {
            for( k = 0; k < 2; ++k )
                cin >> test[i][j][k];
            }
        }
    }
void arr::dis()
{
    cout<<"\n3d array values:"<<endl;
    for( i = 0; i < 2; ++i)
    {
        for (j = 0; j < 3; ++j)
        {
            for( k = 0; k < 2; ++k)
             cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
            }
        }
    }
    int main()
    {
    arr a;
    clrscr();
    a.acc();
    a.dis();
    getch();
    }
                                                                                

18.Program on function proto type
#include<iostream.h>
#include<conio.h>
class proto
{
public:
int add(int x, int y)  
{
int res;
res = x + y;
return res;
}
int sub(int x, int y)   
{
int res;
res = x - y;
return res;
}
};
void main()
{
clrscr();
int a, b;
proto p;
cout<<"Enter a,b values:\n";
cin>>a>>b;
cout<<"\naddition= "<<p.add(a, b);
cout<<"\nSubtraction = "<<p.sub(a, b);
getch();
}
                                                                                

19.WAP on swapping of two numbers
a】pass by value
#include<iostream.h>
#include<conio.h>
class abc
{
public:
void add(int a,int b)
{
int c;
c=a+b;
cout<<"c="<<c;
}
};
int main()
{
abc s;
clrscr();
s.add(4,5);
getch();
}
b】pass by reference
#include<iostream.h>
class test
{
int a;
public:
void change(int &a)
{
a=6;
cout<<"a="<<a<<endl;
}
};
int main()
{
test t;
int a=9;
t.change(a);
cout<<"a="<<a<<endl;
return 0;
}
c】call by reference
#include<iostream.h>
class test
{
int a,b,t;
public:
void change(int *a,int *b)
{
    t=*a;
    *a=*b;
    *b=t;
}
};
int main()
{
test s;
int a=9,b=7;
  cout<<"Before swap\na="<<a<<"\nb="<<b;
s.change(&a,&b);
cout<<"\nafter swap\na="<<a<<"\nb="<<b;
return 0;
}
                                                                                

20.Swapping of two classes private data using friend function .
#include<iostream.h>
#include<conio.h>
class y;
class x
{
int a;
public:
void geta()
{
cout<<"Enter a value\n";
   cin>>a;
}
friend void swap(x,y);
};
class y
{
int b;
public:
void getb()
{
cout<<"Enter b value\n";
cin>>b;
}
friend void swap(x,y);
};
void swap(x i,y j)
{
int t;
cout<<"before swap\na="<<i.a<<"\nb="<<j.b<<endl;
t=i.a;
i.a=j.b;
j.b=t;
cout<<"after swap\na="<<i.a<<"\nb="<<j.b<<endl;
}
int main()
{
clrscr();
x obj1;
y obj2;
obj1.geta();
obj2.getb();
swap(obj1,obj2);
getch();
}
                                                                                


21.Program on object as function argument.
#include<iostream.h>
class test
{
int a,b;
public:
void dis(test);
};
void test::dis(test t)
{
t.a=5;
t.b=6;
cout<<"a="<<t.a<<"\nb="<<t.b;
}
int main()
{
test obj;
obj.dis(obj);
    return 0;
}
                                                                                

22.Program on Returning object .
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b,c;
public:
demo acc();
void show(demo);
};
demo demo::acc()
{
demo d;
d.a=9;
d.b=4;
d.c=d.a+d.b;
return d;
}
demo demo::show(demo d)
{
cout<<"a="<<d.a<<"\nb="<<d.b<<"\nc="<<d.c;
}
int main()
{
demo obj;
obj.acc();
obj.show(obj);
}
                                                                                

23.Programs on types of Constructor
A】Default
#include<iostream.h>
class x
{
public:
x()
{
cout<<"Hi shashi";
}};
int main()
{
x a;
return 0;
}
B】Parameterized
#include<iostream.h>
class area
{
public:
area(double r)
{
cout<<"area of circle="<<(r*r*3.14)<<endl;
}};
int main()
{
area a(3.5);
return 0;
}
C】Overloaded
#include<iostream.h>
class area
{
public:
area(double r);
area(int l,int b);
};
area::area(double r)
{
cout<<"area of circle="<<(r*r*3.14)<<endl;
}
area::area(int l, int b)
{
cout<<"area of rectangle="<<(l*b);
}
int main()
{
area a(3.5);
area b(4,5);
return 0;
}
                                                                                

24.Program on copy constructor
#include<iostream.h>
class emp
{
int id;
public:
emp();
emp(int s)
{
id=s;  
}
emp(emp &a)
{
id=a.id;
}
void show()
{cout<<id; }
};
int main()
{
  emp x(50);
  emp y(x);
  emp z=x;
  emp t=x;
  cout<<"id of x=";
   x.show();
  cout<<"\nid of y=";
    y.show();
  cout<<"\nid of z=";
    z.show();
  cout<<"\nid of t=";
    t.show();
return 0;
}
                                                                                

25.Program on constructor & destructor
#include<iostream.h>
int a=0;
class dig
{
public:
dig()
{
a++;
cout<<"\n no.of obj created :"<<a;
}
~dig()
{
cout<<"\n no.of obj destroyed :"<<a;
a--;
}
};
int main()
{
cout<<" In main function";
dig x,y,z;
{
cout<<"\n block1";
dig b;
}
{
cout<<"\n block2";
dig c;
}
return 0;
}
                                                                                

26.Program on on new, delete operator.
#include <iostream.h>
class demo
{
    public:
demo()
{
cout<<"Hi ";
}
~demo()
{
cout<<"\nBye ";
}
};
int main()
{
demo *d = new demo();
delete(d);
return 0;
}
                                                                          

๐Ÿ’ฅ๐Ÿ’ฅ27-50{will be uploaded soon}๐Ÿ’ฅ๐Ÿ’ฅ
                                                                          

                                   ๐Ÿ’
**๐Ÿ™...Thanks for watching...๐Ÿ™**



                                                                          

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat