4th sem OOPC Write the following programs using CONSTRUCTOR & DESTRUCTOR in CPP language.
1. To enter two operands using constructor and perform arithmetic operations (+,-,*, /) on them and display the results.
#include<iostream>
#include<conio.h>
using namespace std;
class operation
{
public:
int a,b;
operation(int x,int y)
{
a=x;
b=y;
cout<< a<< "+" << b <<"="<< a+b<<"\n";
cout<<a << "-"<<b<<"="<<a-b<<"\n";
cout<<a<<"*"<<b<<"="<<a*b<<"\n";
cout<<a<<"/"<<b<<"="<<a/b<<"\n";
}
};
int main()
{
int m,n;
cout <<"enter the values of oprands "<<endl;
cin>>m>>n;
operation(m,n);
return 0;
}
2. Write a program to generate a Fibonacci series using copy constructor. The range of the series should be user defined.
#include<iostream>
#include<conio.h>
using namespace std;
class code{
int id;
public:
code(){}
code(int a){id =a;}
code(code &x){
id=x.id;
}
void display(){
cout<< id;
}
};
int main(){
code A(100);
code B(A);
code D;
D=A;
cout<<"\n id of A:";A.display();
cout<<"\n id of D:";D.display();
cout<<"\n id of B:";B.display();
return 0;
}
3. To find biggest three numbers using friend function.
#include<iostream>
using namespace std;
class maximum{
int a,b,c;
public:
maximum(){}
maximum(int x,int y,int z){
a=x;
b=y;
c=z;
}
friend int sum(maximum);
};
int sum(maximum l){
if(l.a>l.b){
if(l.a>l.c){
return (l.a);
}else{
return (l.c);
}
}else{
if(l.b>l.c){
return (l.b);
}else{
return (l.c);
}
}
}
int main(){
int m,n,o;
cout<<"Enter the digit"<<endl;
cin>>m>>n>>o;
maximum A(m,n,o);
cout<<"maximum is : "<<sum(A);
return 0;
}
4. Write a program to find the sum of two numbers and display it using a friend function to a class.
#include<iostream>
using namespace std;
class sum{
int a,b;
public:
sum(){}
sum(int x,int y){
a=x;
b=y;
}
friend int addition(sum);
};
int addition(sum l){
return(l.a+l.b);
}
int main(){
int m,n;
cout<<"Enter the Two number"<<endl;
cin>>m>>n;
sum A(m,n);
cout<<addition(A);
}
Comments
Post a Comment