Rakesh Cs
Rakesh Cs
Rakesh Cs
ANS :
#include<iostream.h>
#include<conio.h>
class add{
private : int a,b;
public : void test()
{
cout<< “\n Enter the 1st number : ”;
cin>>a;
cout<<“\n Enter the 2nd number : ”;
cin>>b;
cout<<“\n Sum of the 2 numbers is : ” <<a+b;
}
};
void main()
{
add x;
clrscr();
x.test();
getch();
}
void input()
{
int x;
cout << "Enter the values of p, q, n: ";
cin >> p >> q >> n;
x = (p * q) - n + 2;
cout << "The calculation is = " << x;
}
};
void main()
{
Calculate k;
clrscr();
k.input();
getch();
}
OUTPUT : Enter l, b = 4 5
The area of rectangle = 20
ASSIGNMENT NO-6 : : Write a program to find the area of a circle using
a member function outside the class definition.
ANS :
#include<iostream.h>
#include<conio.h>
class Circle
{
private : float r;
public : void cir();
};
void Circle::cir()
{
float pi = 3.14;
float area;
cout << "Enter the radius = ";
cin >> r;
area = pi * r * r;
cout << "Area of the circle = " << area;
}
void main()
{
Circle obj;
clrscr();
obj.cir();
getch();
}
OUTPUT : Enter the radius = 5
Area of the circle = 78.5
ASSIGNMENT NO-7 : Write a program to find whether a given number
is odd or even.
ANS :
#include<iostream.h>
#include<conio.h>
class Check {
private : int n;
public: void test();
Check()
{
n = 0;
}
};
void Check::test()
{
cout << "Enter any number: ";
cin >> n;
if (n % 2 == 0)
cout << "The number is even";
else
cout << "The number is odd";
}
void main()
{
Check obj;
clrscr();
obj.test();
getch();
}
void calc()
{
float SI;
SI = (p * n * r) / 100;
cout << "Simple Interest = " << SI;
}
};
void main()
{
Interest x;
clrscr();
x.calc();
getch();
}
OUTPUT : Simple Interest = 45
ASSIGNMENT NO-13 : Write a program to swap two numbers using
object-oriented concepts.
ANS :
#include<iostream.h>
#include<conio.h>
class Swap {
private : int a, b;
public: void read()
{
cout << "Enter two numbers a, b : ";
cin >> a >> b;
}
void swapping()
{
int temp = a;
a = b;
b = temp;
}
void display()
{
cout << "After swapping a=" << a << ", b=" << b;
}
};
void main()
{
Swap obj;
clrscr();
obj.read();
obj.swapping();
obj.display();
getch();
}
OUTPUT : Enter two numbers a, b : 4 7
After swapping a=7, b=4
ASSIGNMENT NO-14 : :Write a program to copy one object to another
object using a constructor.
ANS :
#include<iostream.h>
#include<conio.h>
class Contest {
private : int x, y;
public : Contest(contest & p)
{
x = p.x;
y = p.y;
}
Contest()
{
x = 10;
y = 20;
}
void disp()
{
cout << "x=" << x << endl;
cout << "y=" << y << endl;
}
};
void main()
{
Contest a;
Contest b(a);
clrscr();
cout << "Object a:" << endl;
a.disp();
cout << "Object b (copied from a):" << endl;
b.disp();
getch();
}
OUTPUT : Object a:
x=10
y=20
Object b (copied from a):
x=10
y=20