You are on page 1of 28

LIST OF EXPERIMENT __

1. WAP to swap two integers without using a third


variable. The swapping must be done in a different
method in a different class .
2. WAP that uses a class where the member
functions are defined outside a class.
3. WAP to find the greater of two given numbers in two
different classes using friend function.
4. Create an abstract class Shape which has a field PI=3.14
as final and it has an abstract method Volume. Make two
subclasses Cone and Sphere from this class and they print
their volume.

5. Create a class called LIST with two pure virtual function


store() and retrieve().To store a value call store and to retrieve
call retrieve function. Derive two classes stack and queue
from it and override store and retrieve.

6. WAP to define the function template for calculating the


square of given numbers with different data types.
7. Design a class to represent a bank account. Which
include Contains account number, name of the depositor,
type of the account, balance amount in the account. Define
Methods, to assign initial values, to Deposit an amount, to
Withdraw amount after checking balance, to display Name
And balance.
8. Create an inheritance hierarchy of Rodent, Mouse, Gerbil,
Hamster etc. In the base class Provide methods that are
common to all Rodents and override these in the derived
classes to perform different behaviors, depending on the
specific type of Rodent. Create an array of rodent, fill it with
different specific types of Rodents and call your base class
methods.

9. WAP Containing a Possible Exception. Use a Try Block to


Throw it and a Catch Block to Handle it Properly.

10. WAP to raise an exception if any attempt is made to


refer to an element whose index is beyond the array size.

-------------------------------------------------------
EXPERIMENT - 1

PROGRAM :
1. WAP to swap two integers without using a third variable. The

swapping must be done in a different method in a different class .

#include <iostream>

using namespace std;


class c2;

class c1{
int val1;
friend void exchange(c1 &,c2 &);
public:
void intdata(int a){
val1 = a;

}
void display(void){
cout<< val1 <<endl;
}
};
class c2{
int val2;
friend void exchange(c1 &,c2 &);
public:
void intdata(int a){
val2 = a;

}
void display(void){
cout<< val2 <<endl;
}

};
/*trick to swap 2 number a and b;
temp = a;
a = b;
b = temp;
*/

void exchange(c1 & x,c2 & y){


int temp = x.val1;
x.val1 = y.val2;
y.val2 = temp;

}
int main()
{
c1 oc1;
c2 oc2;

oc1.intdata(48);
oc2.intdata(978);
exchange(oc1,oc2);

cout<<"the value of oc1 after exchanging become: ";


oc1.display();
cout<<"the value of oc2 after exchanging become: ";
oc2.display();

return 0;
}

OUTPUT:

the value of oc1 after exchanging become: 978


the value of oc2 after exchanging become: 48

...Program finished with exit code 0


Press ENTER to exit console.
EXPERIMENT - 2

PROGRAM:

2. WAP that uses a class where the member functions are


defined outside a class .

#include <iostream>

using namespace std;

class Box {
public:
double length;
double breadth;
double height;

double getVolume(void);
void setLength( double len );
void setBreadth( double bre );
void setHeight( double hei );
};

// Member functions definitions


double Box::getVolume(void) {
return length * breadth * height;
}

void Box::setLength( double len ) {


length = len;
}
void Box::setBreadth( double bre ) {
breadth = bre;
}
void Box::setHeight( double hei ) {
height = hei;
}

int main() {
Box Box1;
Box Box2;
double volume = 0.0;

Box1.setLength(32.0);
Box1.setBreadth(5.0);
Box1.setHeight(5.0);

Box2.setLength(12.0);
Box2.setBreadth(78.0);
Box2.setHeight(10.0);

volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;

volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}

OUTPUT:
Volume of Box1 : 800
Volume of Box2 : 9360

...Program finished with exit code 0


Press ENTER to exit console.

—------------------------------------------------------------------------------------------
------------------------

EXPERIMENT - 3

3. WAP to find the greater of two given


numbers in two different classes using friend
function.

PROGRAM:

#include <iostream>
using namespace std;
class y;

class x{
int data;
public:
void setvalue(int value){
data = value;

}
friend void add(x,y);
};
class y{
int num;
public:
void setvalue(int value){
num = value;

}
friend void add(x,y);

};
void add(x o1,y o2){
cout<<"suming data of x and y objects gives
me "<<o1.data + o2.num;
}

int main(){
x a1;
a1.setvalue(14);

y b1;
b1.setvalue(95);

add(a1,b1);
return 0;
}

OUTPUT:

suming data of x and y objects gives me 109

...Program finished with exit code 0


Press ENTER to exit console.

—---------------------------------------------------------------

EXPERIMENT - 4

4. Create an abstract class Shape which has a field


PI=3.14 as final and it has an abstract method
Volume. Make two subclasses Cone and Sphere
from this class and they print their volume.

PROGRAM:

#include <iostream>
#include <cmath>
#include <iomanip>

const float PI = 3.14;


class shape{
float radii;
float height;
public:
shape() { }
virtual float area() = 0;
virtual float volume() = 0;
virtual float getRadius() = 0;
virtual float getHeight() = 0;
virtual ~shape() { }
};

class Sphere : public shape {


float radii;
public:
Sphere(float r = 0): radii(r) { }
float area() { return 4 * PI * pow(radii, 2); }
float volume() { return (4 * PI / 3) * pow(radii,
3); }
float getRadius() { return radii; }
float getHeight() { }
virtual ~Sphere() { }
};
class Cone : public shape {
float radii;
float height;
public:
Cone(float r = 0, float h = 0): radii(r),
height(h) { }
float area() { return PI * radii * (
sqrt(pow(radii,2 ) + pow(height, 2)) + radii ); }
float volume() { return (PI / 3.0) * pow(radii,
4) * height;}
float getRadius() { return radii; }
float getHeight() { return height; }
virtual ~Cone() { }
};

int main()
{
Sphere sphere(9.0);

Cone cone(7.0, 7.0);

std::cout << "Sphere :\nRadius = " <<


sphere.getRadius()
<< std::endl;
std::cout << "Area of Sphere = " <<
sphere.area()
<< std::endl;
std::cout << "Volume of Sphere = " <<
sphere.volume()
<< std::endl;
std::cout << "Cone :\nRadius = " <<
cone.getRadius()
<< ", Height = " << cone.getHeight()
<< std::endl;
std::cout << "Area of Cone = " <<
cone.area()
<< std::endl;
std::cout << "Volume of Cone = " <<
cone.volume()
<< std::endl;
}

OUTPUT:
Sphere :
Radius = 9
Area of Sphere = 1017.36
Volume of Sphere = 3052.08
Cone :
Radius = 7, Height = 7
Area of Cone = 371.451
Volume of Cone = 17591.3

...Program finished with exit code 0


Press ENTER to exit console.

—----------------------------------------
---------------------------------

EXPERIMENT - 5

5. Create a class called LIST with two pure


virtual function store() and retrieve().To store a
value call store and to retrieve call retrieve
function. Derive two classes stack and queue
from it and override store and retrieve.

Program:

#include<iostream>
using namespace std;

class LIST{
public:
virtual void store(int) = 0;
virtual int retrieve() = 0;
};

class stack : public LIST{


public:
void store(int x){
top++;
arr[top] = x;
}
int retrieve(){
return arr[top--];
}
int arr[100];
int top = -1;
};

class queue : public LIST{


public:
void store(int x){
rear++;
arr[rear] = x;
}
int retrieve(){
return arr[front++];
}
int arr[100];
int front = 0;
int rear = -1;
};
int main(){
stack s;
s.store(1);
s.store(2);
s.store(3);
cout << s.retrieve() << endl;
cout << s.retrieve() << endl;
cout << s.retrieve() << endl;
queue q;
q.store(1);
q.store(2);
q.store(3);
cout << q.retrieve() << endl;
cout << q.retrieve() << endl;
cout << q.retrieve() << endl;
return 0;
}

OUTPUT:
3
2
1
1
2
3

...Program finished with exit code 0


Press ENTER to exit console.

—------------------------------------------------------------
--------------------------------------------------------

EXPERIMENT - 7

7. Design a class to represent a bank account. Which


include Contains account number, name of the depositor,
type of the account, balance amount in the account. Define
Methods, to assign initial values, to Deposit an amount, to
Withdraw amount after checking balance, to display Name
And balance.

PROGRAM:

#include<iostream>
using namespace std;
class bank
{
private:
string name;
long int accno;
string acc_type ;
long int balance;
public:
bank(){};
bank(string n,long int ano ,long int
bal,string sa)
{
name=n;
accno=ano;
balance=bal;
acc_type = sa;
}
void setdata()
{
acc_type = "saving";
cout<<"enter the name of account
holder : "<<endl;
cin>>name;
cout<<"enter the account number
of candidate : "<<endl;
cin>>accno;
cout<<"enter the balance :
"<<endl;
cin>>balance;
}
void check();
void deposit();
void display();

};

void bank :: deposit()


{
long int amount;
cout<<"how much money do you want to
deposit : "<<endl;
cin>>amount;
balance=balance+amount;
}

void bank :: check()


{
long int amnt;
cout<<"how much money do you want
to withdraw : "<<endl;
cin>>amnt;
if (balance>amnt)
{
balance=balance-amnt;
}
else
cout<<"you have unsufficient balance
that you cant withdraw money :"<<endl;
}

void bank :: display()


{
cout<<"name of candidate is
"<<name<<endl;
cout<<"account no. :"<<accno<<endl;
cout<<"account type :
"<<acc_type<<endl;
cout<<"total balance in your account is
Rs. "<<balance<<endl;
}

int main()
{
bank candidate;
candidate.setdata();
candidate.deposit();
candidate.check();
candidate.display();
}

OUTPUT:

enter the name of account holder :


aashu
enter the account number of candidate :
2136
enter the balance :
10000
how much money do you want to deposit :
2000
how much money do you want to withdraw :
3000
name of candidate is aashu
account no. :2136
account type : saving
total balance in your account is Rs. 9000

...Program finished with exit code 0


Press ENTER to exit console.

—-------------------------------------------------------
----------------------------------------------------------
EXPERIMENT - 8

8. Create an inheritance hierarchy of Rodent, Mouse, Gerbil,


Hamster etc. In the base class Provide methods that are
common to all Rodents and override these in the derived
classes to perform different behaviors, depending on the
specific type of Rodent. Create an array of rodent, fill it with
different specific types of Rodents and call your base class
methods.

PROGRAM:

You might also like