0% found this document useful (0 votes)
225 views26 pages

OOPM Lab Manual 2019-20

The document describes a laboratory manual for an Object Oriented Programming and Methodology course. It includes an index listing sections on the course scheme and syllabus, lab outcomes, and a list of experiments. The lab outcomes section lists eight outcomes related to object-oriented problem solving, programming fundamentals, and applying concepts like classes, methods, constructors, inheritance, and polymorphism. The list of experiments section describes 18 experiments covering topics like control structures, structures, function calls, classes, inheritance, exceptions, and sample projects.

Uploaded by

ankita saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views26 pages

OOPM Lab Manual 2019-20

The document describes a laboratory manual for an Object Oriented Programming and Methodology course. It includes an index listing sections on the course scheme and syllabus, lab outcomes, and a list of experiments. The lab outcomes section lists eight outcomes related to object-oriented problem solving, programming fundamentals, and applying concepts like classes, methods, constructors, inheritance, and polymorphism. The list of experiments section describes 18 experiments covering topics like control structures, structures, function calls, classes, inheritance, exceptions, and sample projects.

Uploaded by

ankita saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

Object Oriented Programming &


Methodology (CS-305)
LABORATORY MANUAL

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 1


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

INDEX
1. Scheme & Syllabus
2. Lab Outcomes
3. List of Experiments
4. Lab Manual

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 2


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

Lab Outcomes

 Articulate the principles of object-oriented problem solving and programming.


 Outline the essential features and elements of the C++ programming language.
 Explain programming fundamentals, including statement and control flow and
recursion.
 Apply the concepts of class, method, constructor, instance, data abstraction, function
abstraction, inheritance, overriding, overloading, and polymorphism.
 Program using objects and data abstraction, class, and methods in function
abstraction.
 Analyze, write, debug, and test basic C++ codes using the approaches introduced in
the course.
 Analyze problems and implement simple C++ applications using an object-oriented
software engineering approach.

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 3


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

List of Experiments

1. Simple C++ Programs to Implement Various Control Structures.


a. If statement
b. Switch case statement and do while loop

2. Program to Understand Structure.

3. Program to Understand Different Function Call Mechanism.


Call by reference and Call by value

4. Write a program Illustrating Class Declarations, Definition, and Accessing Class


Members.

5. Sample Program on Class.

6. Program on Constructors & Destructors.

7. Write a program to access members of a STUDENT class using pointer to object


members.

8. Write a C++ Program that illustrates single inheritance.

9. Programs to Implement Inheritance and Function Overriding.


a. Multiple inheritances –Access Specifier
b. Hierarchical inheritance – Function Overriding /Virtual Function

10. Programs to Overload Unary Operators as Member Function.

11. Write a C++ program containing a possible exception. Use a try block to throw it
and a catch block to handle it properly.

12. Demonstration of Class & Object concept in Java.

13. Demonstration of Constructor overloading in Java.


14. Demonstration of Interface In java.

15. Demonstration of Abstract Class in Java.

16. Demonstration of Inheritance in Java.

17. Sample Project 1

18. Sample Project 2

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 4


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

1. Simple C++ Programs to Implement Various Control Structures.


a. If statement
b. Switch case statement and do while loop
Ex 1A: if .. else statement
An electricity board charges the following rates to domestic users to discourage large
consumption of energy:
FOR the first 100 units - 60P per unit
For next 200 units - 80P per unit
Beyond 300 units - 90P per unit
All users are charged a minimum of Rs.50.00. if the total amount is more than Rs.300.00 than
an additional surcharge of 15% is added
Write a C++ program to read the names of users and number of units consumed and print out
the charges with names.
Ex 1B: switch.. case statements and do .. while loop
An election is contested by five candidates. The candidates are numbered 1 to 5 and a voting is
done by marking the candidate number in a ballot paper. Write a C++ program to read the
ballot and count the votes cast for each candidate using an array variable count. In case, a
number read is outside the range 1 to 5 the ballot should be considered as a ‘spoilt ballot’, and
the program should also count the number of spoilt ballots.
//EX1A: if..else statement
#include<iostream.h>
#include<conio.h>
int main()
{
int no_unit;
float charge,scharge;
char name[20];
cout<<”\n enter name and number of units consumed”;
cin>>name;
cin>>no_unit;
if(no_unit<=100)
charge=(0.60*no_unit);
else if(no_unit>100&& no_unit<=300)
{
charge=(100*0.60);
charge+=((no_unit-100)*0.80);
}
elseif(no_unit>=300)
{
charge=(100*0.60);
charge+=(200*0.80);
charge+=((no_unit-300)*0.90);
}
if(charge<50)
charge=50;

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 5


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 6


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

2. Programs to Understand Structure.


Create a Structure called employee with the following details as variables within it.
1. Name of the employee
2. Age
3. Designation
4. Salary
Write a C++ program to create array of objects for the structure to access these and print the
name, age, designation and salary
//Ex 2A: Structure:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct bio
{
char name[20];
int age;
float sal;
char designation[20];
};
void main()
{
struct bio info[2];
for(int i=0;i<2;i++)
{
cout<<"Enter the data : name, age, salary, designation"<<endl;
cin>>info[i].name>>info[i].age>>info[i].sal>>info[i].designation;
}
for(i=0;i<2;i++)
{
cout<<"Given Data for obj["<<i<<"]:- "<<endl;
cout<<"+++++++++++++++++++++++++++\n";
cout<<"Name : "<<info[i].name<<endl;
cout<<"Age : "<<info[i].age<<endl;
cout<<"Salary : "<<info[i].sal<<endl;
cout<<"Address : "<<info[i].designation<<endl;
}
cout<<"Size of the structure Student is :"<<sizeof(info[0]);
getch();
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 7


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

3. Programs to Understand Different Function Call Mechanism.


Call by reference and Call by value
Write a C++ program to swap two number by both call by value and call by reference mechanism,
using two functions swap_value() and swap_reference respectively , by getting the choice from the
user and executing the user’s choice by switch-case.
Call by reference & Call by value
#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
int x,y,n;
void swap1(int,int);
void swap2(int*x,int*y);
cout<<"enter two values to be swapped\n";
cin>>x>>y;
cout<<"\nfor CALL BY VALUE press1";
cout<<"\nfor CALL BY REFERENCE press2";
cin>>n;
switch(n)
{
case 1:
cout<<"\nCALL BY VALUE";
cout<<"\nvalues before swap()"<<x<<"\t"<<y;
swap1(x,y);
cout<<"\nafter swap outside of swap1()";
cout<<"\nx="<<x<<"\ny="<<y;
break;
case 2:
cout<<"\nCALL BY REFERENCE";
cout<<"\nvalue before swap"<<x<<y;
swap2(&x,&y);
cout<<"\nafter swap(outside of swap2)"<<x<<y;
break;
}
getch();
}
void swap1(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\nswapped values(inside swap1())"<<x<<"\t"<<y;
}
void swap2(int *x,int *y)
{int temp;temp=*x;*x=*y;*y=temp;
cout<<"\nswapped value(inside swap2())"<<*x<<"\t"<<*y;}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 8


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 9


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 10


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

6.Program on Constructors & Destructors.


Create a class for counting the number of objects created and destroyed within various block
using constructor and destructors.
#include<iostream.h>
int count=0;
class contdest
{
public:
contdest()
{
cout<<"\nObject Created ="<<count++;
}
~contdest()
{
cout<<"\nObject Destroyed = "<<--count;
}
};
void main()
{
cout<<"\nMain Block\n";
contdest a,b,c;
{
cout<<"\nBlock 1\n";
contdest d;
}
cout<<"\nAgain main Block\n";
contdest e;
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 11


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 12


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

9. Programs to Implement Inheritance and Function Overriding.


a. Multiple inheritance –Access Specifier
b. Hierarchical inheritance – Function Overriding /Virtual Function
a. Write a C++ program with different class related through multiple inheritance and demonstrate
the use of different access specifiers by means of member variables and member functions.
b. Write a C++ program to explain virtual function (polymorphism) by creating a base class
c_polygon which has virtual function area(). Two classes c_rectangle and c_traingle derived from
c_polygon and they have area() to calculate and return the area of rectangle and triangle
respectively.
Multiple inheritance –Access Specifiers
#include<iostream.h>
#include<conio.h>
class base1
{
protected:
int z;
private:
int y;
public:
int x;
void set_value(int xx,int yy, int zz)
{
x=xx;
y=yy;
z=zz;
}
void display1()
{
cout<<"\nX="<<x;
cout<<"\ny="<<y;
cout<<"\nZ="<<z;
}
};
class base2
{
protected:
int a;
private:
int b;
public:
int c;
void set_values(int aa,int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
void display2()

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 13


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

{
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";

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 14


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 15


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

10. Programs to Overload Unary Operators as Member Function.


Write a C++ program to count the number of persons inside a bank, by increasing count whenever a
person enters a bank, using an increment(++) operator overloading function, and decrease the
count whenever a person leaves the bank using a decrement(--) operator overloading function
inside a class.
#include<iostream.h>
#include<conio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
counter operator++(int)
{
count++;
}
int get_count()
{
return count;
}
counter operator--(int)
{
count--;
}
};
void main()
{
clrscr();
counter c1;
cout<<"Initial No Of People "<<c1.get_count()<<endl;
c1++;
c1++;
c1++;
cout<<"Present No Of People "<<c1.get_count()<<endl;
c1--;
c1--;
cout<<"Present No Of People "<<c1.get_count()<<endl;
getch();
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 16


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

b. Write a C++ program to demonstrate the catching of all exceptions.

#include<iostream.h> #include<conio.h> void test(int x)


{
try
{
if(x>0) throw x;
else
throw 'x';
}
catch(int x)
{
cout<<"Catch a integer and that integer is:"<<x;
}
catch(char x)
{
cout<<"Catch a character and that character is:"<<x;
}
}
void main()
{
clrscr();
cout<<"Testing multiple catches\n:"; test(10);
test(0);
getch();
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 17


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

12. Demonstration of Class & Object concept in Java.


class Lamp {
boolean isOn;

void turnOn() {
isOn = true;
}

void turnOff() {
isOn = false;
}

void displayLightStatus() {

System.out.println("Light on? " + isOn);


}
}

class ClassObjectsExample {
public static void main(String[] args) {

Lamp l1 = new Lamp(), l2 = new Lamp();

l1.turnOn();
l2.turnOff();

l1.displayLightStatus();
l2.displayLightStatus();
}
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 18


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

13 . Demonstration of Constructor overloading in Java.

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 19


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

14. Demonstration of Interface In java.


// Java program to demonstrate working of
// interface.
import java.io.*;

// A simple interface
interface in1
{
// public, static and final
final int a = 10;

// public and abstract


void display();
}

// A class that implements interface.


class testClass implements in1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}

// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 20


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

15. Demonstration of Abstract Class in Java.


abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 21


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

16. Demonstration of Inheritance in Java.

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
}
}

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 22


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 23


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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++)
{

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 24


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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
{

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 25


SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL

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

SIRT/CSE/OOPM/LAB MANUAL/ER. BRIJESH SHARMA Page 26

You might also like