Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

FACULTY OF SCIENCE AND HUMANITIES

Ramapuram Campus

Department of Computer Science & Applications (B.Sc)

PRACTICAL RECORD

NAME :

REGISTER NO :

COURSE : B.Sc – COMPUTER SCIENCE

SEMESTER / YEAR : II / I

SUBJECT CODE : USA20201J

SUBJECT NAME : OBJECT ORIENTED PROGRAMMING LAB

APRIL 2023
FACULTY OF SCIENCE AND HUMANITIES
Ramapuram Campus

Department of Computer Science & Applications (B.Sc)

REGISTER NUMBER:

BONAFIDE CERTIFICATE

This is to certify that the bonafide work done by _____________________________in the subject

OBJECT ORIENTED PROGRAMMING LAB [USA20201J] at, SRM Institute of Science and

Technology, Ramapuram Campus in April 2023.

STAFF IN-CHARGE HEAD OF THE DEPARTMENT

Submitted for the University Practical Examination held at SRM Institute of Science and Technology,
Ramapuram Campus on ____________.

INTERNAL EXAMINER 1 INTERNAL EXAMINER 2


INDEX

PAGE
SNO DATE NAME OF THE PROGRAM SIGN
NO
1 I/O OPERATIONS AND OPERATORS

CHECK THE STRING IS


2
PALINDROME

DISPLAY STUDENT DETAILS USING


3
CLASS

4 CONSTRUCTOR OVERLOADING
5 FUNCTION OVERLOADING
6 UNARY OPERATOR OVERLOADING
7 SINGLE INHERITANCE
8A MULTILEVEL INHERITANCE
8B MULTIPLE INHERITANCE

ABSTRACT CLASS AND VIRTUAL


9
FUNCTIONS

10 SIMPLE FILE PROGRAM


11 WORKING WITH FILES
12 RANDOM ACCESS UPDATING
13A FUNCTION TEMPLATE
13B CLASS TEMPLATE
14 EXCEPTION HANDLING
15 USER DEFINED EXCEPTION

16 SORTING NUMBERS
EXP NO: 1 REGNO:

DATE: NAME:

I/O OPERATIONS AND OPERATORS

AIM:

To write a C++ program for performing arithmetic operations using arithmetic operators.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class arith with the data members a and b as integer type.

Step 3: Define the function, get() to get the input from the user.

Step 4: Define the function, display() to print the final output.

Step 5: Call the get() and display() functions from the main function by creating an instance
of the class arith.

Step 6: Stop the program.

PROGRAM:

#include <iostream>

using namespace std;

class Arith

public:

int a,b;

void get();

void display();
};

void Arith::get()

cout << "Enter the value of a"<<endl;

cin>>a;

cout << "Enter the value of b"<<endl;

cin>>b;

void Arith::display()

// printing the sum of a and b

cout << "a + b = " << (a + b) << endl;

// printing the difference of a and b

cout << "a - b = " << (a - b) << endl;

// printing the product of a and b cout

<< "a * b = " << (a * b) << endl;

// printing the division of a by b

cout << "a / b = " << (a / b) << endl;

// printing the modulo of a by b

cout << "a % b = " << (a % b) << endl;

int main()

Arith ob;//object is intialization

ob.get();
ob.display();

return 0;

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 2 REGNO:

DATE: NAME:

CHECK THE STRING IS PALINDROME

AIM:

To write a C++ program to check the string is palindrome or not using conditional control
statements.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the character array as string1 and integer variables as I, length and set flag
=0.

Step 3: Read the input for string1 and calculate the length of the string.

Step 4: Execute the for loop and check the condition string1[i] != string1[length-i-1].

Step 5: If the condition is false, set flag =1 and break the looping statement.

Step 6: Print the string is not palindrome else print the string is palindrome.

Step 7: Stop the program.

PROGRAM:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char string1[20];
int i, length;
int flag = 0;

cout << "Enter a string: ";


cin >> string1;

length = strlen(string1);

for(i=0;i < length ;i++)

{
if(string1[i] != string1[length-i-1])
{flag = 1;
break;
}
}

if (flag)
{
cout << string1 << " is not a palindrome" << endl;
}
else
{
cout << string1 << " is a palindrome" << endl;
}
return 0;
}

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 3 NAME:
DATE: REGNO:

DISPLAY STUDENT DETAILS USING CLASS

AIM:

To write a C++ program to calculate the total and average marks of student using class.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class student with name as character array and rollno,m1,m2,m3 as
integer type and total, avg as float type.

Step 3: Define the function, getDetails() to get the input from the user.

Step 4: Define the function, putDetails() to calculate the total and average and to print the
final output.

Step 5: Call the getDetails() and putDetails() functions from the main function by creating
an instance of the class student.

Step 6: Stop the program.

PROGRAM:

#include <iostream>
using namespace std;
class student
{
char name[30];
int rollNo,m1,m2,m3;
float total,avg;
public:
//member function to get student's details
void getDetails();
//member function to print student's details
void putDetails();
};
//member function definition, outside of the
class void student::getDetails() {
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter mark1: ";
cin >> m1;
cout << "Enter mark2: ";
cin >> m2;
cout << "Enter mark3: ";
cin >>m3;
cout << "total marks: "<<endl;
total=m1+m2+m3;
cout<<total;
avg=total/3;
}
//member function definition, outside of the
class void student::putDetails() {
cout << "Student details:\n";
cout << "Name:"<< name <<endl;
cout <<",Roll Number:" << rollNo << ",Total:" << total <<endl<< ", Average marks:"
<<avg;
}
int main()
{
student st;
st.getDetails();
st.putDetails();
return 0;
}

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 4 REGNO:

DATE: 01.03.21 NAME:

CONSTRUCTOR OVERLOADING

AIM:

To write C++ program to implement parameterized constructor, copy constructor, default


constructor and destructor.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class with the class name integer with a data members m & n.

Step 3: Define the constructor functions, such as default, parameterized, copy constructors and
destructor.

Step 4: Create instances and invoke all the constructors.

Step 5: The destructor function is invoked automatically to destroy the memory.

Step 6: Stop the program.

PROGRAM:

#include <iostream>

using namespace std;

class integer

int m,n;

public:

integer() //default constructor


{m=0;n=0;}

integer(int,int); //parameterized constructor

integer(integer &i) //copy constructor

m=i.m;

n=i.n;

~integer() //destructor function

{}

void display();

};

integer::integer(int a, int b)

m=a;

n=b;

void integer::display()

cout<<"m : "<<m<<endl;

cout<<"n : "<<n<<endl;

int main()

integer obj1;

integer obj2(5,8);
integer obj3(obj2);

cout<<"First object : "<<endl;

obj1.display();

cout<<"Second object : "<<endl;

obj2.display();

cout<<"Third object : "<<endl;

obj3.display();

return 0;

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 5 REGNO:

DATE: NAME:

FUNCTION OVERLOADING

AIM:

To Calculate the area of the Rectangle, Triangle and Circle using function
Overloading concept

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class with class name calarea and define the member variables
i,b,m,n,tarea,r.

Step 3: Define the function, area() is overloaded to find the area of different

Shapes such as rectangle, triangle and circle.

Step 4: The function area() is defined with return type float and with 2 arguments int, float for
calculating area of rectangle.

Step 5: The function area() is defined without argument and return type for calculating the
area of triangle. The area is calculated and the result is printed in the same function.

Step 6: The function area() is again defined with one argument and return type double for
calculating the area of the circle.

Step 7: In the main(), call all the functions using the instance of class calarea.

Step 8: Stop the program.


PROGRAM:

#include <iostream>

using namespace std;

class calarea

int l;

float b,m,n,tarea;

double r;

public:

float area(int,float); // function with argument and different type

void area(); // function with no argument and no return type

double area(double); // function with argument and return type

};

float calarea::area(int x,float y )


{
l=x;
b=y;
return(l*b);
}

void calarea::area()
{
cout<<"enter the value of m and n";
cin>>m>>n;
tarea=(0.5*m*n);
cout<<"area of the triangle:"<<tarea<<endl;
}
double calarea::area(double c)
{
r=c;
return(3.14*c*c);
}

int main()
{
float res;
calarea ob;
res=ob.area(5,6.5);
cout<<"area of the rectangle:"<<res<<endl;
ob.area();
cout<<"area of the circle:"<<endl;
cout<<ob.area(2.25);
return 0;
}

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 6 REGNO:

DATE: NAME:

UNARY OPERATOR OVERLOADING

AIM:

To write C++ program to change the sign of an operands using unary operator
overloading concept.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class with the class name space and three data members x,y and z of
integer type.

Step 3: Define the operator ‘–‘ to be overloaded using the keyword ‘operator’ in

the public area of the class.

Step 4: Define getvalues() and display() functions to get the data and to display results to
user.

Step 5: Create an instance and call the getvalues() from main()

Step 6: Change the sign of the object by calling the operator overloaded

function.

Step 7: Call the display() to display the manipulated data.

Step 8: Stop the program.


PROGRAM:

#include <iostream>

using namespace std;

class space

int x,y,z;

public:

void getvalues (int a,int b,int c);

void display();

void operator-();

};

void space::getvalues (int a,int b,int c)

x=a;

y=b;

z=c;

void space::display()

cout<<"x : "<<x<<endl;

cout<<"y : "<<y<<endl;

cout<<"z : "<<z<<endl;

void space::operator-()

{
x=-x;

y=-y;

z=-z;

int main()

space s;

s.getvalues(10,-20,30);

s.display();

-s;

cout<<endl<<"s:"<<endl;

s.display();

return 0;

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 7 REGNO:

DATE: NAME:

SINGLE INHERITANCE

AIM:

To write C++ program for calculating the agent commission based on the sales value
using single inheritance concept.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class with the class name commcal (base class) and define the member
function getsalesval() to get the input for svalue.

Step 3: Declare another class with class name agentcomm (derived class) which derives publicly
from class commcal and define the functions calcommision() to calculate the commission
and define a function display() to print the total sales and commission.

Step 4: In the main(), create an instance of derived class agentcomm.

Step 5: Invoke the getsalesval(), calcommision() and display() of class base and derived
respectively.

Step 6: Stop the program.

PROGRAM:

#include <iostream>

using namespace std;

class commcal

public:
long int svalue;

float commission;

void getsalesval()

cout << "Enter the total sale value :"<< endl;

cin>>svalue;

};

class agentcomm: public commcal

public:

void calcommision();

void display();

};

void agentcomm::calcommision()

if(svalue<=10000)

commission=svalue*5/100;

else if(svalue<=25000)

commission=svalue*10/100;

else if(svalue>25000)

commission=svalue*20/100;

void agentcomm::display()

{
cout<< "For a total sale value of $" <<svalue<<endl;

cout<< "the agent's commission is $"

<<commission; }

int main()

agentcomm ob;

ob.getsalesval();// base class

ob.calcommision();

ob.display();

return 0;

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 8 A REGNO:
DATE: NAME:
MULTILEVEL INHERITANCE

AIM:

To write the C++ program for displaying the student results along with sports marks using
multilevel inheritance.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the base class student.

Step 3: Declare and define the function get() to get the student details.

Step 4: Declare the other class sports.

Step 5: Declare and define the function getsm() to read the sports mark.

Step 6: Define the function totcal() to find the total marks.

Step 7: Create the class result derived from student and sports.

Step 8: Declare and define the function display() to find the average.

Step 9: Declare the derived class object,call the functions get(),getsm(),totcal() and display().

Step 10: Stop the program.

PROGRAM:

#include<iostream>
using namespace std;
class student
{
protected:
int rno,m1,m2;
public:void get()

{ cout<<"Enter the Roll no :"<<endl;


cin>>rno;
cout<<"Enter the mark1:"<<endl;
cin>>m1;
cout<<"Enter the mark2:"<<endl;
cin>>m2;
}
};
class sports:public student

{ protected:

int tot,sm;

public:

// sm = Sports mark
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
void totcal()
{
tot=m1+m2+sm;
}
};

class result:public sports


{ouble avg;
public:
void display()
{
avg=tot/3;
cout<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}

};

int main()

{result obj; obj.get();

//base class

obj.getsm(); //

obj.totcal();

obj.display();
return 0;
}
OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 8 B REGNO:

DATE: NAME:

MULTIPLE INHERITANCE

AIM:

To write a C++ program to calculate the income tax based on the gross salary using
multiple inheritance concept.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class with the class name- taxcal with variables empno, empname. Define
getdata() to get the input for empno, empname.

Step 3: Declare the class with class name, incometax with variables yearsal and finaltax. Define
getsalary() to get the input for salary and define taxcalculation() to calculate tax.

Step 4: Declare the class with the class name- result which publicly inherits classes taxcal and
incometax. Define display() to print the salary and calculated tax.

Step 5: Create an instance for the derived class result and invoke the functions.

Step 6: Stop the program.

PROGRAM:

#include <iostream>

using namespace std;

class taxcal

protected:

int empno;
char empname[25];

public:

void getdata()

cout<<"Enter Empno : "<<endl;

cin>>empno;

cout<<"Enter Name : "<<endl;

cin>>empname;

};

class incometax

protected:

long int yearsal;

double finaltax;

public:

void getsalary()

cout << "Enter the annual income after the deductions :"<<

endl; cin>>yearsal;

void taxcalculation();

};

void incometax::taxcalculation()

{
if(yearsal<200000)

finaltax=0;

cout<<"Nil tax"<<endl;

else if(yearsal>=200000 && yearsal<=500000)

finaltax=(yearsal-200000)*0.10;

else if(yearsal>500000 && yearsal<=1000000)

finaltax=(yearsal-200000)*0.20;

else if(yearsal>1000000)

finaltax=(yearsal-200000)*30/100;

class result:public taxcal, public incometax

public:

void display();

};

void result::display()

cout<< "For a annual income of $"

<<yearsal<<endl; cout<< "Final tax is $" <<finaltax;

int main()

result ob;
ob.getdata();

ob.getsalary();

ob.taxcalculation();

ob.display();

return 0;

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 9 REGNO:

DATE: NAME:

ABSTRACT CLASS AND VIRTUAL FUNCTIONS

AIM:

To write a C++ program to display the employee details using abstract class and virtual
function concept.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare a class with one pure virtual function.

Step 3: The function emp_info() is declared as public function.

Step 4: The function emp_info() is again defined in derived class. Here the empno, ename and
dob are declared.

Step 5: The inputs are readed and the output is printed using emp_info() function.

Step 6: In the main function object is created for derived class and pointer object is created for
base class. The address of derived class is assigned to Base object and emp_info() is
invoked.

Step 7: Stop the program.

PROGRAM:

#include<iostream>

using namespace std;

class Base

public:
virtual void emp_info() = 0; // pure virtual

function };

class Derived:public Base

public:

int eno;

char ename[20],dob[10];

void emp_info()

cout <<"Enter Employee ID:"<<endl;

cin>>eno;

cout<<"Enter Employee Name:"<<endl;

cin>>ename;

cout<<"Enter Employee DOB:"<<endl;

cin>>dob;

cout<<"EMPLOYEE DETAILS"<<endl;

cout<<"EMPLOYEE ID"<< eno<<endl;

cout<<"NAME:"<<ename<<endl;

cout<<"DOB:"<<dob<<endl;

};

int main()

Base *b;
Derived d;

b = &d;

b->emp_info();

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 10 REGNO:

DATE: NAME:

SIMPLE FILE PROGRAM

AIM:

To write a C++ program for perform simple file operations using ofstream and ifstream.

ALGORITHM:

Step 1: Start The program.

Step 2: Create a filestream object os using ofstream to perform write operation.

Step 3: Create a text file for performing write operation.

Step 4: Read the value for name and age from user and write it into the file using os object.

Step 5: Close the file using os.close()..

Step 6: Open the file using ifstream object is for reading purpose.

Step 7: Read the characters from the file using getline function until it reaches eof().

Step 8: Close the file using is.close().

Step 9: Stop the program.

PROGRAM:

#include <fstream>

#include <iostream>

using namespace std;

int main ()

char input[75];

ofstream os;
os.open("testout.txt");

cout <<"Writing to a text file:" << endl;

cout << "Please Enter your name: ";


cin.getline(input, 100);
os << input << endl;
cout << "Please Enter your age: ";
cin >> input;
cin.ignore();
os << input << endl;
os.close();
ifstream is;
string line;
is.open("testout.txt");
cout << "Reading from a text file:" << endl;
while (getline (is,line))
{
cout << line << endl;
}
is.close();
return 0;
}

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 11 REGNO:

DATE: NAME:

WORKING WITH FILES

AIM:

To write a C++ program for perform file operations using tellg() and tellp() functions.

ALGORITHM:

Step 1: Start The program.

Step 2: Create a filestream object file using fstream to perform write/read operations.

Step 3: Create a text file for performing write operation.

Step 4: Pass a string and write it into the file using file object.

Step 5: Get the current position of the file using file.tellp() function.

Step 5: Close the file using file.close().

Step 6: Open the file in read mode and check not eof() condition.

Step 7: Read the characters from the file and print it until it reaches eof().

Step 8: Get the position of the characters readed using file.tellg() function

Step 8: Close the file using file.close().

Step 9: Stop the program.

PROGRAM:

#include <iostream>

#include <fstream>

using namespace std;


int main()
{
fstream file;
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
//write A to Z
file<<"WELCOME TO INDIA !! STAY SAFE!! STAY OME!!";

//print the position

cout<<"Current position is: <<file.tellp()<<endl;

file.close();

//again open file in read mode


file.open("sample.txt",ios::in);
if(!file)
{
cout<<"Error in opening file!!!";
return 0;
}
cout<<"After opening file position is: "<<file.tellg()<<endl;
//read characters untill end of file is not found

char ch;

while(!file.eof())

cout<<"At position : "<<file.tellg(); //current position

file>>ch; //read character from file cout<<" Character

\""<<ch<<"\""<<endl;

//close the file

file.close();

return 0;

}
OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 12 REGNO:

DATE: NAME:

RANDOM ACCESS UPDATING

AIM:

To write a C++ program for perform file operations using random access file functions.

ALGORITHM:

Step 1: Start The program.

Step 2: Create a filestream object myfile using ofstream to perform write operations.

Step 3: Create a text file for performing write operation.

Step 4: Pass a string as input and write it into the file using the myfile object.

Step 5: Close the file using myfile.close().

Step 6: Open the file in read mode and check not eof() condition

Step7: move the file pointer 7 bytes from beginning of the file using the seekg function

Step 7:Again move the file pointer 2bytes backwards from current location using the seekg
function.

Step 8: Read the all the characters from the file and print it until it reaches eof().

Step 8: Close the file using file.close().

Step 9: Stop the program.


PROGRAM:

#include<fstream>//required for file operation:

#include<iostream>//required for input/output:

using namespace std;

main()

ofstream myFile; //Object for Writing:

ifstream file; //Object for reading:

char ch[30]; //Arguments, Name & size of Array.

char text; //character variable to read data from file:

cout<<"Enter some Text here: "<<endl;

cin.getline(ch,30); //getline function

//opening file for Writing:

myFile.open("RANDOM.txt", ios::out);

if(myFile) //Error Checker:

myFile<<ch;

cout<<"Data store Successfully: \n\n"<<endl;

else cout<<" Error while opening file: "<<endl;


myFile.close(); //file close:

//opening file for reading:

file.open("RANDOM.txt", ios::in);

if(file) //Error Checker:

file.seekg(7, ios::beg); //skip first 7 bytes from beginning:

file.seekg(-2, ios::cur); //then move back 2 bytes from current position:

cout<<" The output (after skipping first 7 bytes and then move 2 bytes back) is: ";

while(!file.eof()) //read data from file till end of file: {

file.get(text); //read data:

cout<<text; //display data:

else cout<<" Error while Opening File: "<<endl;

file.close(); //file close:

return 0;

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 13 A REGNO:
DATE: NAME:
FUNCTION TEMPLATE
AIM:

To write a program to swap two values using function templates.


ALGORITHM:

Step1: Start the program.


Step2: Create the template using the function name swap() to exchange any two values using
generic type name T.
Step 3: In the main function define the variables i1,i2 of integer type,f1,f2 of float type and
c1,c2 of char type.
Step 4: Print the values of i1,i2,f1,f2,c1,c2 before calling the swap() function.
Step 5: The function swap(i1,i2),swap(f1,f2) and swap(c1,c2) is called respectively and output
is printed.
Step 6: Stop the program.

PROGRAM:

template <typename T>


void Swap(T &n1, T &n2)
{
T temp;

temp = n1;

n1 = n2;

n2 = temp;

}
int main()
{
int i1 = 1, i2 = 2;
float f1 = 1.1, f2 = 2.2;
char c1 = 'a', c2 = 'b';
cout << "Before passing data to function template.\n";

cout << "i1 = " << i1 << "\ni2 = " << i2; cout <<

"\nf1 = " << f1 << "\nf2 = " << f2;


cout << "\nc1 = " << c1 << "\nc2 = " << c2;
Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);
cout << "\n\nAfter passing data to function

template.\n"; cout << "i1 = " << i1 << "\ni2 = " << i2;

cout << "\nf1 = " << f1 << "\nf2 = " << f2;

cout << "\nc1 = " << c1 << "\nc2 = " << c2;
return 0;
}

OUTPUT:

Before passing data to function template.

i1 = 1

i2 = 2

f1 = 1.1

f2 = 2.2

c1 = a

c2 = b

After passing data to function template.

i1 = 2

i2 = 1

f1 = 2.2

f2 = 1.1

c1 = b

c2 = a

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 13 B REGNO:

DATE: NAME:

CLASS TEMPLATE
AIM:

To write a C++ program to display the calculator operations using class templates.

ALGORITHM:

Step1: Start the program.

Step2: Create the template using the class as generic type name T.

Step 3: Create the class name as calculator and declare the variables num1,num2as generic type
T.

Step 4: Define a function calculator with arguments as generic type T.

Step 5: Define the function add(),subtract(),multiply() and divide() with return type as T.

Step 6: Print the results in the displayResult() function.

Step 7: In the main() function pass the data type int, float during object creation and call
the displayResult().

Step 8: Stop the program.

PROGRAM:

#include <iostream>

using namespace std;

template <class T>

class Calculator
{

private:

T num1, num2;

public:

Calculator(T n1, T n2)

num1 = n1;

num2 = n2;

void displayResult()

cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;

cout << "Addition is: " << add() << endl;

cout << "Subtraction is: " << subtract() << endl;

cout << "Product is: " << multiply() << endl;

cout << "Division is: " << divide() << endl;

T add() { return num1 + num2; }

T subtract() { return num1 - num2; }

T multiply() { return num1 * num2; }

T divide() { return num1 / num2; }

};

int main()

{
Calculator<int> intCalc(2, 1);

Calculator<float> floatCalc(2.4, 1.2);

cout << "Int results:" << endl;

intCalc.displayResult();

cout << endl << "Float results:" << endl;

floatCalc.displayResult();

return 0;

OUTPUT:

Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 14 REGNO:

DATE: NAME:

EXCEPTION HANDLING
AIM :

To write a C++ program to demonstrate exception handling using try and catch block.

ALGORITHM:

Step 1: Declare the main()

Step 2: Declare the variables a and b;

Step3: Inside the try block check whether the value of b not equal to 0.

Step 4: Divide the value of a/b and assign to div and type cast to float and check div less than 0.
If the condition is true throw the exception else print the div value.

Step 5: In the Catch block – catch the exception and display the message “Exception: Division
by zero, Exception: Division is less than 1, unknown exception”.

PROGRAM:

#include <iostream>

#include <conio.h>

using namespace std;

int main()

int a,b;

cout << "Enter 2 numbers: ";


cin >> a >> b;

try

if (b != 0)

float div = (float)a/b;

if (div < 0)

throw 'e';

cout << "a/b = " << div;

else

throw b;

catch (int e)

{
cout << "Exception: Division by zero";
}

catch (char st)

{
cout << "Exception: Division is less than 1";
}
catch(...)
{
cout << "Exception: Unknown";
}

getch();
return 0;
}

OUTPUT:

RESULT:

The above program is executed successfully and the output is verified.


EXP NO: 15 REGNO:

DATE: NAME:

USER DEFINED EXCEPTION


AIM :

To write a C++ program to display the error message to the user when the car
crossing the speed limit using user defined exception handling .

ALGORITHM:

Step 1: start the program.

Step 2: Create a class with class name overspeed which inherits from exception class.

Step3: Declare the variable speed define the user defined function what().

Step 4: The what() function return the error message to the user.

Step 5: In the main() function check the speed of the car. If the carspeed greater than 100, if true
then throw the exception.

Step 6: Catch function handles the exception and print the error message.

Step 7: Stop the Program.

PROGRAM:

#include <iostream>
#include <exception>
using namespace std;

class OverSpeed : public exception{


int speed;
public :
const char* what()
{
return "CHECK OUT UR CAR SPEED OF THE CAR!! CROSSING THE SPEED LIMIT ";
}
};

int main()
{
int carspeed=0;
try
{
while(1)
{
carspeed+=10;
if(carspeed>100)
{
OverSpeed s;
throw s;
}
cout<<"Carspeed: "<<carspeed<<endl;
}

}
catch(OverSpeed ex)
{
cout<<ex.what();
}

return 0;
}
OUTPUT:

RESULT: The above program is executed successfully and the output is verified.

You might also like