You are on page 1of 28

Programming using C++ lab

#include <iostream>
using namespace std;
int main()
{
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "Number is Prime."<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int num1,num2,num3;
cout<<" Enter value for first number";
cin>>num1;
cout<<" Enter value for second number";
cin>>num2;
cout<<" Enter value for third number";
cin>>num3;
if(num1>num2&&num1>num3) {
cout<<" First number is greatest:"<<endl<<"which is= "<<num1;
} else if(num2>num1&&num2>num3) {
cout<<" Second number is greatest"<<endl<<"which is= "<<num2;
} else {
cout<<" Third number is greatest"<<endl<<"which is= "<<num3;
}
return 0;
}
• Q) WAP in c++ to create a class with a private data
member and two member function get() and show().
ANS-
#include<iostream.h>/#include<iostream>
#include<conio.h>/ using namespace std;
Class A
{
int n;
Private:
void get()
{
cout<< “enter the number”<<endl;
cin>>n;
}
void show()
{
cout<< “the number is”<< n<<endl;
}
Public:
Void disp()
{
Void get();
Void show();
}
};
void main()
{
A a;
a.disp();
getch();
}
• Q) WAP in c++ to create a class with a private
data member and two member function get()
and show() defined outside the class.
• Q) WAP in c++ to create a class with a private
data member and private member function.
Q) WAP with multiple constructors for the single
class.
Q) WAP to pass an object with reference to
constructor. Declare and initialize other
objects.
Q)WAP to create an object and release them
using destructor.
• WAP with multiple constructors for the single
class.
• Ans :-
#include<iostream.h>/#include<iostream>
#include<conio.h>/ using namespace std;
Class num
{
Private:
int a;
float b; char c;
Public:
num(int m, float j, char k);
num(int m, float j);
num();
void show()
{
Cout<<“\n\t a=“<<a<<“b=“<<b<<“c=“<<c;
}
};
num::num(int m, float j,char k)
{
Cout<<“\n constructor with 3 arguments”;
a=m;
b=j;
c=k;
}
num::num(int m, float j)
{
Cout<<“\n constructor with 2 arguments”;
a=m;
b=j;
c=‘ ‘;
}
num::num()
{
Cout<<“\n constructor without arguments”;
a=b=c=NULL;
}
main()
{
Clrscr();
Class num x(4,5.5,’A’);
x.show();
Class num y(1,2.2);
y.show();
Class num z;
z.show();
return 0;
}
• Q) WAP to pass an object with reference to constructor.
Declare and initialize other objects.
#include<iostream.h>/#include<iostream>
#include<conio.h>/ using namespace std;
Class num
{
int n;
Public:
num() {}
num(int k) {n=k;}
num()(num &j)
{
n=j.n;
}
void show(void)
{
Cout<<n;
}
main()
Clrscr()
num j(50);
num k(j);
num l=j;
num m;
m=j;
Cout<<“\n object j value of n:”;
j.show();
Cout<<“\n object k value of n:”;
k.show();
Cout<<“\n object l value of n:”;
l.show();
Cout<<“\n object m value of n:”;
m.show();
Return 0;
}
• Q) Write a program to redefine a virtual base
class function in the derived class. Also add new
member in the derived class.
• Ans:-
#include<iostream.h>/#include<iostream>
#include<conio.h>/ using namespace std;
Class A
{
Public:
Virtual void joy()
{ cout<<endl<<“in joy of class A”;}
};
Class B: public A
{
Public:
Void joy(){cout<<endl<<“in joy of class B”;}
Void virtual joy(){cout<<endl<<“in joy2 of class B”;}
};
Void main()
{
Clrscr();
A *a1,*a2;
A a3;
B b;
a1=&a3;
a2=&b;
a1->joy();
a2->joy();
}
• Q) WAP to increment member variables of object.
Overload unary ++ operator.
• Ans:-
#include<iostream.h>/#include<iostream>
#include<conio.h>/ using namespace std;
Class num
{
Private:
Int a,b,c,d;
Public:
num (int j, int k, int m,int l )
{
a=j; b=k; c=m; d=l;}
Void show void(void);
Void operator ++();
};
Void num:: show()
{
Cout<<“A=“<<a<<“B=“<<b<<“C=“<<c<<“D=“<<d;
}
Void num:: operator++()
{
++a; ++b; ++c; ++d;
}
main()
{
Clrscr();
num x(3,4,5,7);
Cout<<“\n before increment of x:”;
x.show();
++x;
Cout<<“\n after increment of x:”;
x.show();
return 0;
}
• Q) WAP using c++ to overload – operator.
• Q)WAP to overload ++ and – for prefix and
postfix use.
• Q) WAP to overload + binary operator.
C++ Program to Subtract Complex Number
Using Operator Overloading
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input()
{
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag; }
// Operator overloading
Complex operator - (Complex c2)
{
Complex temp;
temp.real = real - c2.real;
temp.imag = imag - c2.imag;
return temp;
}
void output()
{
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
} };
int main()
{
Complex c1, c2, result;
cout<<"Enter first complex number:\n";
c1.input();
cout<<"Enter second complex number:\n";
c2.input(); // In case of operator overloading of binary operators in C++ programming,
// the object on right hand side of operator is always assumed as argument by
compiler.
result = c1 - c2;

result.output();
return 0; }
Function Overloading using Different Types of
Parameter
// Program to compute absolute value
// Works for both int and float
#include <iostream>
using namespace std;
// function with float type parameter
float absolute(float var)
{
if (var < 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var)
{
if (var < 0)
var = -var;
return var;
}
int main()
{
// call function with int type parameter
cout << "Absolute value of -5 = " << absolute(-5) << endl;
// call function with float type parameter
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
}
Function Overloading using Different Number
of Parameters
#include <iostream>
using namespace std;
// function with 2 parameters
void display(int var1, double var2)
{
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}
// function with double type single parameter
void display(double var)
{
cout << "Double number: " << var << endl; }
// function with int type single parameter
void display(int var)
{
cout << "Integer number: " << var << endl;
}
int main()
{
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
• Q) WAP to calculate square of an integer and
float number. Define function sqr(). Use
function overloading concept.
• Q) WAP to find the area of rectangle, triangle
and sphere. Use function overloading.
WAP to calculate square of an integer and float number. Define
function sqr(). Use function overloading concept .
#include<iostream.h>/#include<iostream>
#include<conio.h>/ using namespace std;
int sqr(int);
float sqr(float);
main()
{
clrscr();
int a=15;
float b=2.5;
Cout<<“square=“<<sqr(a)<<“\n”;
Cout<<“square=“<<sqr(a)<<“\n”;
return 0;
}
int sqr(int a)
{
return(s*s);
}
float sqr(float j)
{
return(j*j);
{

You might also like