OOPM Lab Manual 2019-20
OOPM Lab Manual 2019-20
INDEX
1. Scheme & Syllabus
2. Lab Outcomes
3. List of Experiments
4. Lab Manual
Lab Outcomes
List of Experiments
11. Write a C++ program containing a possible exception. Use a try block to throw it
and a catch block to handle it properly.
if(charge>300)
{
scharge=(0.15*charge);
charge+=scharge;
}
cout<<”electricity bill \n”;
cout<<name<<” : : rs”<<charge;
getch();
return(0);
}
switch case, do.. while
#include<iostream.h>
int main()
{
int i, can[5],ballot, count[6];
char ch;
for(i=0;i<=5;i++)
{
count[i]=0;
}
do
{
cout<<"\nEnter the ballot number :";
cin>>ballot;
switch(ballot){
case 1: count[1]++;
break;
case 2: count[2]++;
break;
case 3: count[3]++;
break;
case 4: count[4]++;
break;
case 5: count[5]++;
break;
default: count[0]++;
}
cout<<" \nWant to vote again\n";
cin>> ch;
}
while(ch=='y');
for(i=1;i<=5;i++)
{
cout<<"\nNo:of votes for candidate "<< i <<"="<<count[i];
}
cout<<"\n Sploit ballots are "<<count[0];return 0;}
4. Write a program Illustrating Class Declarations, Definition, and Accessing Class Members.
#include<iostream.h>
class sample
{
private:
public:
int a; char b; float c;
voidget_data()
{
cout<<"Enter an integer value:"; cin>>a;
cout<<"Enter a character:"; cin>>b;
cout<<"Enter a float value:"; cin>>c;
}
voidprint_data()
{
};
int main()
{
cout<<"Values read from keyboard are\n"; cout<<"Integer value:"<<a<<endl; cout<<"character is
:"<<b<<endl; cout<<"float value is :"<<c<<endl; cin>>c;
}
sample s;//creation of object
s.get_data(); s.print_data();
}
5. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent
toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track
of the number of cars that have gone by, and of the total amount of money collected. Model
this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold
the total number of cars, and a type double to hold the total amount of money collected. A
constructor initializes both of these to 0. A member function called payingCar() increments
the car total and adds 0.50 to the cash total. Another function, called nopayCar(), increments
the car total but adds nothing to the cash total. Finally, a member function called display()
displays the two totals. Make appropriate member functions const.
Include a program to test this class. This program should allow the user to push one key to
count a paying car, and another to count a nonpaying car. Pushing the Esc key should cause
the program to print out the total cars and total cash and then exit.
#include <iostream>
using namespace std;
const char ESC = 27; //escape key ASCII code
const double TOLL = 0.5; //toll is 50 cents
class tollBooth
{
private:
unsigned int totalCars; //total cars passed today
double totalCash; //total money collected today
public: //constructor
tollBooth() : totalCars(0), totalCash(0.0)
{}
void payingCar() //a car paid
{ totalCars++; totalCash += TOLL; }
void nopayCar() //a car didn’t pay
{ totalCars++; }
void display() const //display totals
{ cout << “\nCars=” << totalCars
<< “, cash=” << totalCash
<< endl; }
};
int main()
{
tollBooth booth1; //create a toll booth
char ch;
cout << “\nPress 0 for each non-paying car,”
<< “\n 1 for each paying car,”
<< “\n Esc to exit the program.\n”;
do {
ch = getche(); //get character
if( ch == ‘0’ ) //if it’s 0, car didn’t pay
booth1.nopayCar();
if( ch == ‘1’ ) //if it’s 1, car paid
booth1.payingCar();
} while( ch != ESC ); //exit loop on Esc key
booth1.display(); //display totals return 0;}
7. Write a program to access members of a STUDENT class using pointer to object members.
#include<iostream.h>
class student
{
introllno;
char name[50]; public: voidgetdata(); void print();
};
void student::getdata()
{
cout<<"Enter roll number"<<endl; cin>>rollno;
cout<<"Enter Name "; cin>>name;
}
void student::print()
{
cout<<"Name :"<<name<<endl; cout<<"Roll no:"<<rollno<<endl;
}
int main()
{
student a; a.getdata();
a.print();
cout<<"Pointer to class\n"; student *ptr;
ptr=&a;
ptr->print();
}
8 Write a C++ Program that illustrates single inheritance.
#include<iostream>
using namespace std;
class A
{
protected:
inta,b; public:
void get()
{
cout<<"Enter any two integer values"; cin>>a>>b;
}
};
class B:public A
{
int c; public:
void add()
{
c=a+b; cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
B b; b.get();
b.add(); }
{
cout<<"\nA="<<a;
cout<<"\nB="<<b;
cout<<"\nC="<<c;
}
};
class derived:public base1,private base2
{
public:
void display()
{
a=100;
c=200;
cout<<"\nValue of X in derived class="<<x;
//cout<<"\nValue of Y in derived class="<<y; base1::y not accessible
cout<<"\nValue of Z in derived class="<<z;
cout<<"\nValue of A in derived class="<<a;
//cout<<"\nValue of B in derived class="<<b;base2::b not accessible
cout<<"\nValue of C in derived class="<<c;
}
};
void main()
{
clrscr();
derived d;
d.set_value(10,20,30);
//d.set_values(100,200,300);not accessible when derived as private
d.display1();
//d.display2(); not accessible when derived as private
d.display();
cout<<"\nValue of X in main="<<d.x;
//cout<<"\nValue of Y in main="<<d.y; base1::y not accessible
//cout<<"\nValue of Z in main="<<d.z; base1::z not accessible
//cout<<"\nValue of C in main="<<d.a; base2::a not accessible
//cout<<"\nValue of B in main="<<d.b; base2::b not accessible
//cout<<"\nValue of C in main="<<d.c; base2::c not accessible
getch();}
Hierarchical inheritance – Function Overriding /Virtual Function
#include<iostream.h>
#include<conio.h>
class c_polygon
{
protected:
float a,b;
public:
void get_data()
{
cout<<"\nEnter any two floating values:\n";
cin>>a>>b;
}
virtual float area()
{
return 0;
}
};
class c_rectangle:public c_polygon
{
public:
float area()
{
return (a*b);
}
};
class c_triangle:public c_polygon
{
public:
float area()
{
return (b*a/2);
}
};
void main()
{
clrscr();
c_rectangle r;
c_triangle t;
c_polygon *p;
p=&r;
p->get_data();
cout<<"\nArea of rectangle is "<<p->area();
p=&t;
p->get_data();
cout<<"\nArea of triangle is "<<p->area();
getch();
}
11. Write a C++ program containing a possible exception. use a try block to throw it and a
catch block to handle it properly.
#include <iostream>
using namespace std;
int main()
{
int x = -1;
cout<< "Before try \n"; try
{
cout<< "Inside try \n"; if (x < 0)
{
throw x;
cout<< "After throw (Never executed) \n";
}
}
catch (int x )
{
cout<< "Exception Caught \n";
}
cout<< "After catch (Will be executed) \n"; return 0;
}
void turnOn() {
isOn = true;
}
void turnOff() {
isOn = false;
}
void displayLightStatus() {
class ClassObjectsExample {
public static void main(String[] args) {
l1.turnOn();
l2.turnOff();
l1.displayLightStatus();
l2.displayLightStatus();
}
}
class Box
{ double width, height, depth;
// constructor used when all dimensions specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d; }
// constructor used when no dimensions specified
Box()
{
width = height = depth = 0;
}
//constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
public class Test
{
public static void main(String args[])
{ // create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
// A simple interface
interface in1
{
// public, static and final
final int a = 10;
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}
17. Write a Program to Implement a Class STUDENT having following members: (Sample
Project )
#include<iostream.h>
#include<string>
class student
{
public:
charsname[50]; float marks[6]; float total; floatmax_marks;
student(); void assign();
void compute(); void display();};
student::student()
{ strcpy(sname," "); for(int i=0;i<6;i++) marks[i]=0;total=0; max_marks=0;
}
void student::assign()
{
cout<<endl<<"Enter Student Name :"; cin>>sname;
for(int i=0;i<6;i++)
{
cout<<"Enter marks of"<<i+1<<" subject:"; cin>>marks[i];
}
cout<<"Enter Maximum total marks"; cin>>max_marks;
}
void student::compute()
{
total=0;
for(int i=0;i<6;i++) total+=marks[i];
}
void student::display()
{
cout<<"Student Name:"<<sname<<endl; cout<<"Marks are\n";
for(int i=0;i<6;i++)
cout<<"Subject "<<i+1<<": "<<marks[i]<<endl; cout<<" -------------------\n";
cout<<"Total :"<<total<<endl; cout<<" -------------------\n";
float per; per=(total/max_marks)*100;
cout<<"Percentage:"<<per;
}
int main()
{
Student obj;
obj.assign();
obj.compute();
obj.display();return 0; }
18. Write a c++ program to implement the matrix ADT using a class. The operations supported by
this ADT are:
a) Reading a matrix b)addition of matrices c)printing a matrix d)subtraction of matrices
e)multiplication of matrices
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class matrix
{
protected: inti,j,a[10][10],b[10][10],c[10][10];
int m1,n1,m2,n2; public:
virtual void read()=0; virtual void display()=0; virtual void sum()=0; virtual void sub()=0; virtual void
mult()=0;
};
classresult:public matrix
{
public:
void read(); void sum(); void sub(); voidmult(); void display();
};
void result::read()
{
cout<<"\nenter the order of matrix A "; cin>>m1>>n1;
cout<<"\nenter the elements of matrix A ";
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cin>>a[i][j];
}
}
cout<<"\nenter the order of matrix B "; cin>>m2>>n2;
cout<<"\nenter the matrix B "; for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
cin>>b[i][j];
}
}
}
void result::display()
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cout.width(3); cout<<c[i][j];
}
cout<<"\n";
}
}
void result::sum()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for addition";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
}
void result::sub()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for subtraction ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]-b[i][j];
//cout<<a[i][j];
}
}
}
}
void result::mult(void)
{
if(n2!=m2)
{
cout<<"Invalid order limit ";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(int k=0;k<n1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
}
void main()
{
intch;
class matrix *p; class result r;
p=&r; clrscr(); while(1)
{
cout<<"\n1. Addition of matrices "; cout<<"\n2. Subtraction of matrices "; cout<<"\n3. Multipication of
matrices "; cout<<"\n4. Exit";
cout<<"Enter your choice "; cin>>ch;
switch(ch)
{
case 1:
p->read();
p->sum();
p->display(); break;
case 2:
(p)->read();
p->sub();
p->display(); break;
case 3:
p->read();
p->mult();
p->display(); break;
case 4:
exit(0);
}
}
}