HI FRIENDS I AM ADDING HERE 11 PROGRAM OF OOPS IN C++
ALL PROGRAM OF C++TABLE OF CONTENTS
ALL PROGRAM OF C++TABLE OF CONTENTS
SNO
|
PROGRAM NAME
|
PAGE NO
|
SIGN
|
1.
|
Program to find sum, multiplication, subtraction and
division of two numbers
|
1
|
|
2.
|
Program to find the complete no. by
area exchange and
number of a customer
|
4
|
|
3.
|
Program to illustrate the concept
of
this pointer
|
6
|
|
4.
|
Program to implement
simple constructor
|
8
|
|
5.
|
Program to implement default constructor
|
10
|
|
6.
|
Program to implement friend function
|
12
|
|
7.
|
Program for binary operator overloading using friend function
|
14
|
|
8.
|
Program to overload unary (-) operator
|
16
|
|
9.
|
Program for a simple constructor
|
18
|
|
10.
|
Program to find transpose of a matrix
using the concept
of
classes
|
20
|
|
11.
|
Program to implement destructor which destruct the objects one by one
|
22
|
|
1. Program to find sum ,multiplication,subtraction and division of two numbers.
#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();
int a,b,s,m,d,r,ch,e;
char choice;
do
{
cout<<"1. ADDITION \n \n";
cout<<"2.
SUBTRACTION \n \n";
cout<<"3. MULTIPLICATION \n \n";
cout<<"4. DIVISION \n \n";
cout<<"Enter Your Choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter The Value Of First Variable";
cin>>a;
cout<<"Enter The Value Of Second Variable";
cin>>b;
r=a+b;
cout<<"Result After Addition="<<r<<endl;
break;
case 2:
cout<<"Enter The Value Of First Variable";
cin>>a;
cout<<"Enter The Value Of Second Variable";
cin>>b;
s=a-b;
cout<<"Result After Subtraction="<<s<<endl;
break;
case 3:
cout<<"Enter The Value Of First Variable";
cin>>a;
cout<<"Enter The Value Of Second Variable";
cin>>b;
m=a*b;
cout<<"Result After Multiplication="<<m<<endl;
break;
case 4:
1 | P ag e
cout<<"Enter The Value Of Dividend";
cin>>a;
cout<<"Enter The Value Of Divisor";
cin>>b;
d=(a/b);
e=a%b;
cout<<"Result After Division \n \n"<<"Quotient=" <<d<<"\t"<<"Remainder="<<e<<endl;
break;
default:
cout<<"Wrong Choice Entered \n";
}
cout<<"Do Another(Y/N) ? \n";
cin>>choice;
}
while((choice=='Y')||(choice=='y'));
getch();
}
2 | P ag e
3 | P ag e
2. Program to find the complete no. by area exchange and number of a customer.
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct phone
{
int area,xchange,number;
}a,b;
int main()
{
clrscr(); a.area=505; a.xchange=989;
a.number=7806;
cout<<"Enter the Area Code,
Exchange and Number \n";
cin>>b.area;
cin>>b.xchange;
cin>>b.number;
cout<<"My number is
\t"<<a.area<<"-"<<a.xchange<<"-"<<a.number<<"\n";
cout<<"your number is
\t"<<b.area<<"-"<<b.xchange<<"-"<<b.number<<"\n";
getch();
}
4 | P ag e
5 | P ag e
3.Program to illustrate the concept of this
pointer.
#include<iostream.h>
#include<conio.h>
class k
{ int x; public:
k()
{ }
k(int a)
{ x=a;
}
k &show()
{ return *this;
}
void display()
{ cout<<x;
} };
void main()
{ clrscr(); k l,l1(60); l=l1.show(); l.display();
getch();
}
6 | P ag e
7 | P ag e
4. Write a program to implement simple constructor.
#include<iostream.h>
#include<conio.h>
class multiply
{
int a,b,c; public: multiply()
{ a=0;
b=0;
c=0;
}
void input();
int mul();
void output();
};
void multiply::input()
{
cout<<"\nEnter the value of A:";
cin>>a;
cout<<"\nEnter the value of B:";
cin>>b;
}
int multiply::mul()
{
cout<<"\nMultiplying A and B:";
c=a*b;
return c;
}
void multiply::output()
{
cout<<"\nThe value of c after multiplication of A and B is
:";
cout<<c;
}
void main()
{
clrscr();
cout<<"\n\nINPUT THE DATA";
m.input();
cout<<"\n\nMULTIPLICATION OF DATA:";
m.mul();
cout<<"\n\nOUTPUT OF DATA:";
m.output();
getch();
}
8 | P ag e
9 | P ag e
5.Write a program to inplement default constructor.
#include<iostream.h>
#include<conio.h>
class mobile
{
int no;
float price; public: mobile();
void getdata();
};
mobile::mobile()
{
cout<<"ENTER NO";
cin>>no;
cout<<"ENTER PRICE";
cin>>price;
}
void mobile::getdata()
{
cout<<"NO IS = "<<no<<endl;
cout<<"PRICE IS = "<<price<<endl;
}
void main()
{ clrscr(); mobile m; m.getdata();
getch();
}
10 | P ag e
11 | P ag e
6.Write a program to implement friend function.
#include<iostream.h>
#include<conio.h>
class abc;
class
pqr
{
int p;
public:
void input(int k)
{
p=k;
}
friend int large(pqr,abc);
};
class abc
{
int d;
public:
void getdata(int s)
{
d=s;
}
friend int large(pqr,abc);
};
large(pqr l,abc b)
{
if(l.p>b.d)
cout<<"\n\nTHE PROGRAM
IS CORRECT\n"<<l.p<<"is greater than
"<<b.d;
else
cout<<"\n\n THE FUNCTION IS INCORRECT";
return 0;
}
void main()
{ clrscr();
pqr x;
x.input(500); abc y; y.getdata(200); large(x,y);
getch();
}
12 | P ag e
13 | P ag e
7.Write a program for binary operator overloading using friend function.
#include<iostream.h>
#include<conio.h>
class complex
{
float real,imag;
public:
complex()
{ real=0;
imag=0;
}
complex(float r,float i)
{
real=r;
imag=i;
}
friend complex operator-(complex l,complex k)
{
complex d;
d.real=l.real-k.real; d.imag=l.imag-k.imag;
return d;
}
void display()
{
cout<<"\nthe result is\n\t\t"<<real<<"+iota("<<imag<<")";
}
};
void main()
{clrscr();
complex c1(21.2,44.3);
complex c2(14.3,51.4);
complex c3; c3=operator-(c1,c2); c3.display();
getch();
}
14 | P ag e
15 | P ag e
8.Program to overload unary (-) operator.
#include<iostream.h>
#include<conio.h>
class ips
{
int a,b,c;
public:
void enter(int p,int q,int r)
{ a=p; b=q;
c=r;
}
void operator-()
{
a=-a;
b=-b;
c=-c;
}
void test()
{ cout<<a<<endl; cout<<b<<endl;
cout<<c<<endl;
}
};
void main()
{
clrscr(); ips
s; s.enter(50,70,90);
s.test();
-s; s.test(); getch();
}
16 | P ag e
17 | P ag e
9.Program for a simple constructor.
#include<iostream.h>
#include<conio.h>
class XYZ
{
int a,b;
clrscr(); public: XYZ()
{
a=0;
b=0;
}
void show();
};
void XYZ::show()
{
cout<<"Value of a is "<<a<<endl;
cout<<"Value of b is "<<b<<endl;
}
void main()
{
clrscr(); XYZ z1;
z1.show(); getch();
}
18 | P ag e
19 | P ag e
10. Program to find transpose of a matrix using the concept of classes.
#include<iostream.h>
#include<conio.h>
class matrix
{
int m,n,i,j;
int a[5][5];
public:
void input();
void output();
};
void matrix::input()
{
cout<<"enter the order of a matrix"<<endl;
cin>>m>>n;
cout<<"enter the elements
of
a matrix whose transpose is
to be calculated"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void matrix::output()
{
cout<<"transpose is\n" ;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<"\n";
}
}
void main()
{
clrscr(); matrix a; a.input(); a.output();
getch();
}
20 | P ag e
21 | P ag e
11.Program to implement destructor which destruct the objects
one by one.
#include<iostream.h>
#include<conio.h>
int a=0;
class dest
{
public:
dest()
{
cout<<"\nthe no.
of
object created="<<++a;
}
~dest()
{
cout<<"\nthe no.
of
object ruined="<<a--;
}
};
void main()
{
clrscr();
dest a,b;
{
dest c;
}
dest d;
getch();
}
22 | P ag e
23 | P ag e
No comments:
Post a Comment