Rakesh Cs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

ASSIGNMENT NO-1 : WAP to add 2 numbers using class concept.

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();
}

OUTPUT : Enter the 1st number : 2


Enter the 2nd number : 5
Sum of the 2 numbers is : 7
ASSIGNMENT NO-2 : WAP to find the factorial of an integer number
using class concept.
ANS :
#include<iostream.h>
#include<conio.h>
class factorial{
private : int n;
public : void input()
{
cout<<“\n Enter a number : ”;
cin>>n;
}
void output()
{
cout<<“\n The factorial is : ”<<cal(n);
}
double cal(int)
{
if(m==0)
return 0;
else if(m==1)
return 1;
else
return m*cal(m-1);
}
};
void main()
{
factorial y;
clrscr();
y.input();
y.output();
getch();
}

OUTPUT : Enter a number : 5


The factorial is : 120
ASSIGNMENT NO-3 : WAP to find the given series calculation x=pq-n+2
using constructor.
ANS :
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Calculate {
private: int p;
int q;
int n;
public: calculate()
{
p = 0;
q = 0;
n = 0;
}

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 the values of p, q, n: 3 4 1


The calculation is = 13
ASSIGNMENT NO-4 : WAP to check the no of characters or length of a
string using class concept.
ANS :
#include<iostream.h>
#include<conio.h>
class length{
private : char ch[20];
public : void input()
{
cout<<“\n Enter a string : ”;
cin>>ch;
}
void output()
{
cout<<“\n Length : ”<<cal();
}
int calculate()
{
int i,n=0;
for(i=0;ch[i]!=0;i++)
n=n+1;
return n;
}
};
void main()
{
length x;
clrscr();
x.input();
x.output();
getch();
}

OUTPUT : Enter a string : school


Length : 6
ASSIGNMENT NO-5 : Write a program to find the area of a rectangle
using a constructor.
ANS :
#include<iostream.h>
#include<conio.h>
class Rect {
private: int l;
int b;
public: Rect()
{
cout << "Enter l, b = ";
cin >> l >> b;
}
void calc()
{
int area = l * b;
cout << "The area of rectangle = " << area;
}
};
void main()
{
Rect obj;
clrscr();
obj.calc();
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();
}

OUTPUT : Enter any number: 7


The number is odd
ASSIGNMENT NO-8 : Write a program to check the maximum number
among three numbers.
ANS :
#include<iostream.h>
#include<conio.h>
class Max {
private : int a, b, c;
public:
void check()
{
cout << "Enter the three numbers a, b, c= ";
cin >> a >> b >> c;
if (a > b && a > c)
cout << "The number a is maximum";
else if (b > a && b > c)
cout << "The number b is maximum";
else
cout << "The number c is maximum";
}
};
void main()
{
Max obj;
clrscr();
obj.check();
getch();
}
OUTPUT : Enter the three numbers a, b, c =12 8 16
The number c is maximum
ASSIGNMENT NO-9 : Write a C++ program to implement a class called
"Book" with private members: bookID, title, author, and price.
Include public member functions to input and display book details.
Create an object of the class and demonstrate its usage by inputting
book details and displaying them.
ANS :
#include<iostream.h>
#include<conio.h>
class Book{
private : int bookID;
char title[50], author[50];
float price;
public : void inputDetails() {
cout << "Enter Book ID: ";
cin >> bookID;
cout << "Enter Title: ";
cin>>title;
cout << "Enter Author: ";
cin>>author;
cout << "Enter Price: ";
cin >> price;
}
void displayDetails() {
cout << "Book ID: " << bookID << endl;
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: " << price << endl;
}
};
void main() {
clrscr();
Book myBook;
myBook.inputDetails();
myBook.displayDetails();
getch();
}
OUTPUT : Enter Book ID: 123
Enter Title: The Catcher in the Rye
Enter Author: J.D. Salinger
Enter Price: 15.99
Book ID: 123
Title: The Catcher in the Rye
Author: J.D. Salinger
Price: 15.99
ASSIGNMENT NO-10 : : Write a program to find swapping of 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
ASIGNMENT NO-11 : WAP to reverse a number using class concept.
ANS :
#include<iostream.h>
#include<conio.h>
class A{
private : int n,r;
public : void input()
{
cout<<“\n Enter any number : ”;
cin>>n;
}
void show()
{
while(n>0)
cout<<“\n Reverse : ”;
{
r=n%10;
cout<<r;
n=n/10;
}
}
};
void main()
{
A obj;
clrscr();
obj.input();
obj.show();
getch();
}

OUTPUT : Enter any number : 123


Reverse : 321
ASSIGNMENT NO-12 : WAP to find simple interest of rupees 500 for 3
years of rate of interest 3%.
ANS :
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Interest {
private: float p;
int n;
float r;
public:
Interest()
{
p = 500;
n = 3;
r = 3;
}

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

You might also like