Oops
Oops
INDEX
No. Practical Name Sign
1
A Write a C++ program to create a simple calculator.
B Write a C++ program to convert seconds into hours, minutes and seconds.
C Write a C++ program to find the volume of a square, cone, and rectangle.
2
A Write a C++ program to find the greatest of three numbers.
B Write a C++ program to find the sum of even and odd n natural numbers
C Write a C++ program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.
3
A Write a C++ program using classes and object Student to print name of the
student, roll_no. Display the same.
B Write a Program to find Maximum out of Two Numbers using friend
function.
Note: Here one number is a member of one class and the other number is
member of some other class.
C Write a C++ Program using copy constructor to copy data of an object to
another object.
D Write a C++ Program to allocate memory dynamically for an object of a
given class using class’s constructor.
4
A Write a C++ program to design a class representing complex numbers and
having the functionality of performing addition & multiplication of two
complex numbers using operator overloading.
B Write a C++ program to overload new/delete operators in a class.
C Write a C++ program to access members of a STUDENT class using pointer
to object members
D Write a C++ Program to generate Fibonacci Series by using Constructor to
initialize the Data Members.
E Write a C++ program that Illustrate all types of inheritance.
5
A Write a C++ Program to design a student class representing student roll no.
and a test class (derived class of student) representing the scores of the
student in various subjects and sports class representing the score in sports.
1|Page
Name: Niraj Vishwakarma Roll No: 87
The sports and test class should be inherited by a result class having the
functionality to add the scores and display the final result for a student.
6
A Write a C++ program to maintain the records of person with details (Name
and Age) and find the eldest among them. The program must use this
pointer to return the result.
7
A Write a C++ program illustrating the use of virtual functions in class.
B Write a C++ program to design a class representing the information
regarding digital library (books, tape: book & tape should be separate
classes having the base class as media). The class should have the
functionality for adding new item, issuing, deposit etc. the program should
use the runtime polymorphism.
8
A Write a C++ program to show conversion from string to int and vice-versa.
B Write a C++ program implementing basic operation of class ios i.e. setf,
unsetf, precision etc.
C Write a C++ program to implement I/O operations on characters. I/O
operations includes inputting a string, Calculating length of the string,
Storing the String in a file, fetching the stored characters from it, etc.
D Write a C++ program to copy the contents of one file to another.
E Write a C++ program to perform read/write binary I/O operation on a file
(i.e. write the object of a structure/class to file).
9
A Write a C++ program to implement the exception handling with multiple
catch statements.
B Write a C++ program to implement the exception handling with rethrowing
in Exception.
10
A Write a C++ Program to create Simple calculator using Class template.
B Write a C++ Program to get maximum of two number using Class template.
2|Page
Name: Niraj Vishwakarma Roll No: 87
Practical-1
a) Write a C++ program to create a simple calculator.
Code-
# include <iostream.h>
#include<conio.h>
int main() {
clrscr();
char op;
float num1, num2;
cout << "Enter operator: +, -, *, /,% : ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
3|Page
Name: Niraj Vishwakarma Roll No: 87
break;
default:
cout << "Error! operator is not correct";
break;
}
getch();
return 0;
}
OUTPUT-
4|Page
Name: Niraj Vishwakarma Roll No: 87
Practical-1
b) Write a C++ program to convert seconds into hours, minutes and seconds.
Code-
#include<iostream.h>
#include<conio.h>
int main ()
{
clrscr();
float hrs, min, sec = 0;
cout << “Enter seconds : “;
cin >> sec;
min = sec / 60;
hrs = min / 60;
cout << “\n” << hrs << “ hours = “ << min << “ minutes = “ << sec << “ seconds”;
getch();
return 0;
}
OUTPUT-
5|Page
Name: Niraj Vishwakarma Roll No: 87
Practical-1
c) Write a C++ program to find the volume of a square, cone, and rectangle.
Code-
#include<iostream.h>
#include<conio.h>
int main ()
{
clrscr();
const float pi=3.14159;
float s,r,h,l,b,vs,vc,vr;
cout<<"Input Square's side: ";
cin>>s;
//Square's volume.
vs= s*s*s;
cout<<"The volume of a square is: "<<vs<<endl;
cout<<"\n\n";
cout<<"Input Cone's radius: ";
cin>>r;
cout<<"Input Cone's height: ";
cin>>h;
// Cone's volume.
vc=(1.0/3.0)*pi*(r*r)*h;
cout<<"The volume of the cone is: "<<vc<<endl;
cout<<"\n\n";
cout<<"Input Rectangle's length: ";
cin>>l;
cout<<"Input Rectangle's breadth: ";
6|Page
Name: Niraj Vishwakarma Roll No: 87
cin>>b;
cout<<"Input Rectangle's height: ";
cin>>h;
//Rectangle's volume
vr=l*b*h;
cout<<"The volume of a rectangle is: "<<vr;
getch();
return 0;
}
OUTPUT-
7|Page
Name: Niraj Vishwakarma Roll No: 87
Practical-2
a) Write a C++ program to find the greatest of three numbers.
Code-
#include <iostream.h>
#include<conio.h>
int main() {
clrscr();
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
// check if n1 is the largest number
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
// check if n2 is the largest number
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
// if neither n1 nor n2 are the largest, n3 is the largest
else
cout << "Largest number: " << n3;
getch();
return 0;
}
OUTPUT-
8|Page
Name: Niraj Vishwakarma Roll No: 87
Practical-2
b) Write a C++ program to find the sum of even and odd n natural numbers.
Code-
#include <iostream.h>
#include<conio.h>
int main() {
clrscr();
int n, sum_even = 0, sum_odd = 0;
cout << "Enter the value of n: ";
cin >> n;
for(int i = 1; i <= n; i++) {
if(i % 2 == 0) {
sum_even += i;
} else {
sum_odd += i;
}}
cout << "Sum of even numbers from 1 to " << n << " is " << sum_even << endl;
cout << "Sum of odd numbers from 1 to " << n << " is " << sum_odd << endl;
getch();
return 0;
}
OUTPUT-
9|Page
Name: Niraj Vishwakarma Roll No: 87
Practical-2
c) Write a C++ program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.
Code-
#include <iostream.h>
#include<conio.h>
int main() {
clrscr();
int n;
cout << "Enter a number: ";
cin >> n;
// Check if i is prime
for(int j = 2; j <= i/2; j++) {
if(i % j == 0) {
count++;
break;
}
}
// Print i if it is prime
if(count == 0) {
cout << i << " ";
}
10 | P a g e
Name: Niraj Vishwakarma Roll No: 87
}
getch();
return 0;
}
OUTPUT-
11 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-3
a) Write a C++ program using classes and object Student to print the name of
the student, roll_no. Display the same.
Code-
#include<iostream.h>
#include<string.h>
#include<conio.h>
class student
{
char sname[10];
int sage;
char sclass[10];
public:
void getdata (char[], int, char[]);
void showdata();
};
void student::getdata(char sn[10], int a, char sc[10])
{
strcpy(sname, sn);
sage =a;
strcpy(sclass, sc);
}
void student ::showdata()
{
cout<<"Name=\t"<<sname<<endl;
cout<<"Age=\t"<<sage<<endl;
cout<<"Class=\t"<<sclass<<endl;
12 | P a g e
Name: Niraj Vishwakarma Roll No: 87
}
void main()
{
student s1;
char st[10];
int sa;
char scls[10];
clrscr( );
cout<<"Enter data for student 1: Name, Age and Class\n";
cin>>st>>sa>>scls;
s1.getdata(st,sa,scls);
s1.showdata( );
getch( );
}
OUTPUT-
13 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-3
b) Write a C++ Program using a copy constructor to copy data of an object to
another object.
Code-
#include<iostream.h>
#include<conio.h>
class demo
{
int data;
public :
demo()
{
data =200;
cout<<"Default constructor is called \n";
}
demo(int x)
{
data = x;
}
demo(demo &d)
{
data= d.data;
cout<<"copy constructor is called \n";
}
void show()
{
cout<<"data="<<data<<endl;
14 | P a g e
Name: Niraj Vishwakarma Roll No: 87
}
};
void main()
{
clrscr();
demo d1(120);
demo d2=d1;
demo d3;
d3=d2;
demo d4=demo(d3);
d1.show();
d2.show();
d3.show();
d4.show();
getch();
}
OUTPUT-
15 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-3
c) Write a C++ Program to allocate memory dynamically for an object of a given
class using class’s constructor.
Code-
#include<iostream.h>
#include<conio.h>
class Dyn_arr
{
int *ptr;
int size;
public :
Dyn_arr(int s)
{
ptr= new int[size =s];
}
void input();
void sort();
void show();
};
void Dyn_arr::input()
{
int i;
for(i=0;i<size;i++)
{
cout<<"\nENTER PTR["<<i<<"]ELEMENT :=";
cin>>ptr[i];
}
16 | P a g e
Name: Niraj Vishwakarma Roll No: 87
}
void Dyn_arr::sort()
{
int i,j,t;
for(i=0;i<size;i++)
for(j=i+1;j<size;j++)
if(ptr[i]>ptr[j])
{
t=ptr[i];
ptr[i]=ptr[j];
ptr[j]=t;
}
}
void Dyn_arr::show()
{
for(int i=0;i<size;i++)
cout<<ptr[i]<<" ";
}
void main()
{
Dyn_arr obj(5);
clrscr();
obj.input();
cout<<"ORIGINAL ARRAY"<<endl;
obj.show();
obj.sort();
cout<<"\n SORTED ARRAY"<<endl;
17 | P a g e
Name: Niraj Vishwakarma Roll No: 87
obj.show();
getch();
}
OUTPUT-
18 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-3
d) Write a Program to find Maximum out of Two Numbers using the friend
function.
Code-
#include<iostream.h>
#include<conio.h>
class second;
class first
{
int fx;
public :
void inputf(int x)
{
fx=x;
}
friend void findmax(first,second);
};
class second
{
int sx;
public :
void inputs(int x)
{
sx = x;
}
friend void findmax(first,second);
};
19 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
20 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-4
a) Write a C++ program to design a class representing complex numbers and
having the functionality of performing addition & multiplication of two complex
numbers using operator overloading.
Code-
#include<iostream.h>
#include<conio.h>
class Complex{
public:
float real;
float img;
Complex(){
this->real = 0.0;
this->img = 0.0;
}
Complex(float real, float img){
this->real = real;
this->img = img;
}
//overloading + operator
Complex operator+(const Complex &obj){
Complex temp;
temp.img = this->img + obj.img;
temp.real = this->real + obj.real;
return temp;
}
//overloading * operator
21 | P a g e
Name: Niraj Vishwakarma Roll No: 87
22 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
23 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-4
b) Write a C++ program to overload new/delete operators in a class.
Code-
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Dummy
{
public:
Dummy()
{
cout<<"Dummy :: Constructor"<<endl;
}
~Dummy()
{
cout<<"Dummy :: Destructor"<<endl;
}
// Overloading Class specific new operator
static void *operator new(size_t sz)
{
void *m = malloc(sz);
cout<<"Dummy :: Operator new"<<endl;
return m;
}
// Overloading Class specific delete operator
static void operator delete(void *m)
{
24 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
25 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-4
26 | P a g e
Name: Niraj Vishwakarma Roll No: 87
ptr->display();
getch();
}
OUTPUT-
27 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-4
d) Write a C++ Program to generate Fibonacci Series by using Constructor to
initialize the Data Members.
Code-
#include <iostream.h>
#include <conio.h>
class Fibonacci {
private:
int a, b;
public:
Fibonacci() {
a = 0;
b = 1;
}
void generate_series(int n) {
cout << "Fibonacci Series: ";
for(int i = 1; i <= n; i++) {
cout << a << " ";
int next_term = a + b;
a = b;
b = next_term;
}
}
};
int main() {
int n;
clrscr(); // clear screen
28 | P a g e
Name: Niraj Vishwakarma Roll No: 87
cout << "Enter the number of terms to generate in the Fibonacci Series: ";
cin >> n;
Fibonacci obj;
obj.generate_series(n);
getch(); // wait for user input
return 0;
}
OUTPUT-
29 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-4
e) Write a C++ Program that illustrates different types of inheritance.
Code-
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "I can eat." << endl;
}
};
// Single level inheritance
class Cat : public Animal {
public:
void meow() {
cout << "I can meow." << endl;
}
};
// Multilevel inheritance
class Kitten : public Cat {
public:
void play() {
cout << "I can play." << endl;
}
};
// Multiple inheritance
30 | P a g e
Name: Niraj Vishwakarma Roll No: 87
class FlyingAnimal {
public:
void fly() {
cout << "I can fly." << endl;
}
};
class Bat : public Animal, public FlyingAnimal {
public:
void sleep() {
cout << "I can sleep upside down." << endl;
}
};
// Hierarchical inheritance
class Dog : public Animal {
public:
void bark() {
cout << "I can bark." << endl;
}
};
class Puppy : public Dog {
public:
void wagTail() {
cout << "I can wag my tail." << endl;
}
};
// Hybrid inheritance
class Bird {
31 | P a g e
Name: Niraj Vishwakarma Roll No: 87
public:
void chirp() {
cout << "I can chirp." << endl;
}
};
class Parrot : public Bird, public Animal {
public:
void repeat() {
cout << "I can repeat what you say." << endl;
}
};
int main() {
// Single level inheritance
Cat c;
c.eat();
c.meow();
// Multilevel inheritance
Kitten k;
k.eat();
k.meow();
k.play();
// Multiple inheritance
Bat b;
b.eat();
b.fly();
b.sleep();
32 | P a g e
Name: Niraj Vishwakarma Roll No: 87
// Hierarchical inheritance
Puppy p;
p.eat();
p.bark();
p.wagTail();
// Hybrid inheritance
Parrot pa;
pa.eat();
pa.chirp();
pa.repeat();
return 0;
}
OUTPUT-
33 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-5
a) Write a C++ Program to design a student class representing student roll no.
and a test class (derived class of student) representing the scores of the student
in various subjects and sports class representing the score in sports. The sports
and test class should be inherited by a result class having the functionality to
add the scores and display the final result for a student .
Code-
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_num;
public:
void get_number(int a)
{
roll_num = a;
}
void put_number(void)
{
cout<<"Roll No:"<<roll_num<<"\n";
}
};
class test : public student
{
protected:
float sem1, sem2;
34 | P a g e
Name: Niraj Vishwakarma Roll No: 87
public:
void get_marks(float x, float y)
{
sem1 = x;
sem2 = y;
}
void put_marks(void)
{
cout<<"Marks obtained"<<"\n"<<"sem1 ="<<sem1<<"\n"<<"sem2 ="<<sem2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score(void)
{
cout<<"Sports wt:"<<score<<"\n\n";}
};
class result : public test, public sports
{
float total;
35 | P a g e
Name: Niraj Vishwakarma Roll No: 87
public:
void display(void);
};
void result ::display(void){
total = (sem1 + sem2 + score)/2;
put_number();
put_marks();
put_score();
cout<<"Total Pointer:"<<total<<"\n";}
void main(){
clrscr();
result s1;
s1.get_number (1234);
s1.get_marks (7.5,6.8);
s1.get_score (0.6);
s1.display ();
getch();
}
OUTPUT-
36 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-6
a) Write a C++ program to maintain the records of a person with details (Name
and Age and find the eldest among them. The program must use this pointer to
return the result.
Code-
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Person
{
char Name[20];
int Age;
public:
void getData()
{
cout<<"Enter Person Name:";
gets(Name);
cout<<"Enter Person Age:";
cin>>Age;
}
void putData()
{
cout<<"\n\nDetails About Eldest Person as follows:";
cout<<"\nName="<<Name<<"\nAge="<<Age;
}
Person &Compare(Person &p1)
{
37 | P a g e
Name: Niraj Vishwakarma Roll No: 87
if(p1.Age>this->Age)
return p1;
return *this;
}
};
void main()
{
clrscr(); Person x,y,z;
x.getData();
y.getData();
z=x.Compare(y);
z.putData();
getch();
}
OUTPUT-
38 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-7
a) Write a C++ program illustrating the use of virtual functions in class.
Code-
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
void main()
{
clrscr();
A* a; //pointer of base class
B b; //object of derived class
a = &b;
39 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
40 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-7
b) Write a C++ program to design a class representing the information regarding
digital library (books, tape: book & tape should be separate classes having the
base class as media). The class should have the functionality for adding new
item, issuing, deposit etc. the program should use the runtime polymorphism.
Code-
#include<iostream.h>
#include<string.h>
#include<conio.h>
class media
{
protected:
char title[50];
float price;
public:
media(char *s, float a)
{
strcpy(title, s);
price = a;
}
virtual void display(){}
};
class book : public media
{
int pages;
public:
book(char *s, float a, int p) : media(s,a)
41 | P a g e
Name: Niraj Vishwakarma Roll No: 87
{
pages = p;
}
void display();
};
class tape : public media
{
float time;
public:
tape(char * s, float a, float t):media(s,a)
{
time =t;
}
void display();
};
void book ::display()
{
cout<<"\n Title:"<<title;
cout<<"\n Pages:"<<pages;
cout<<"\n Price:"<<price;
}
void tape ::display ()
{
cout<<"\n Title:"<<title;
cout<<"\n Play Time:"<<time<<"mins";
cout<<"\n Price:"<<price;
}
42 | P a g e
Name: Niraj Vishwakarma Roll No: 87
void main()
{
clrscr();
char *title = new char[30];
float price, time;
int pages;
cout<<"\n Enter Book Details \n";
cout<<"\n Title:";
cin>>title;
cout<<"\n Price:";
cin>>price;
cout<<"\n Pages:";
cin>>pages;
book book1(title, price, pages);
cout<<"\n Enter Tape Details";
cout<<"\n Title:";
cin>>title;
cout<<"\n Price:";
cin>>price;
cout<<"\n Play Times(mins):";
cin>>time;
tape tape1(title, price, time);
media* list[2];
list[0] = &book1;
list[1] = &tape1;
cout<<"\n Media Details";
cout<<"\n.......Book.....";
43 | P a g e
Name: Niraj Vishwakarma Roll No: 87
list[0]->display ();
cout<<"\n.......Tape.....";
list[1]->display ();
getch();
}
OUTPUT-
44 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-8
a) Write a C++ program to show conversion from string to int and vice-versa.
Code-
#include<iostream>
#include<string>
using namespace std;
int main(){
int i_val = 20;
//Converting integer to string
std::string stri = std::to_string(i_val);
// Displaying the converted strings
cout << "The integer to string is : "<< stri << endl;
cout <<"Data type of variable "<<typeid(stri).name()<<endl;
std::string str = "123";
int num; // using stoi() to store the value of str1 to x
num = std::stoi(str);
cout <<"The string to integer is : "<<num<<endl;
cout <<"Data type of variable "<<typeid(num).name();
return 0;}
OUTPUT-
45 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-8
b) Write a C++ program implementing basic operation of class ios i.e. setf,
unsetf, precision etc.
Code-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout.width(8);
cout.precision(4);
cout<<125<<endl;
cout.width(8);
cout<<23.0<<endl;
cout.width(8);
cout<<34.5<<endl;
cout.setf(ios::hex,ios::basefield);
cout.setf(ios::uppercase);
cout<<0x34f<<endl;
getch();
}
46 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
47 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-8
c) Write a C++ program to implement I/O operations on characters. I/O
operations include inputting a string, Calculating length of the string, Storing the
String in a file, fetching the stored characters from it, etc.
Code-
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char fname[20], ch;
ifstream fin; // create an input stream
cout << "Enter the name of the file: ";
cin.get(fname, 20);
cin.get(ch);
fin.open(fname,ios::in); // open file
if(!fin) // if fin stores zero, i.e., a false value
{
cout << "An error occurred in opening the file.\n";
}
while(fin) // When eof is reached, fin equals 0
{
fin.get(ch); // Read a character
cout << ch; // Display the character
}
getch();
48 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
49 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-8
d) Write a C++ program to copy the contents of one file to another.
Code-
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
clrscr();
ifstream fs;
ofstream ft;
char ch, fname1[20], fname2[20];
cout<<"Enter source file name with extension (like files.txt) : ";
cin>>fname1;
fs.open(fname1);
if(!fs)
{
cout<<"Error in opening source file..!!";
exit(1);
}
cout<<"Enter target file name with extension (like files.txt) : ";
cin>>fname2;
ft.open(fname2);
if(!ft)
{
50 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
51 | P a g e
Name: Niraj Vishwakarma Roll No: 87
52 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-9
a) Write a C++ program to implement the exception handling with multiple
catch statements.
Code-
#include<iostream>
using namespace std;
int main()
{
int x= 3;
// Some code
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;
}
53 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
54 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-9
b) Write a C++ program to implement the exception handling with rethrowing in
an Exception.
Code-
#include<iostream>
using namespace std;
void sub(int i,int j)
{
try
{
if(i==0)
{
throw i;
}
else
cout<<"Subtraction result is: "<<i-j<<endl;
}
catch(int i)
{
cout<<"Exception caught inside sub()\n";
throw;
}
};
int main()
{
try
{
55 | P a g e
Name: Niraj Vishwakarma Roll No: 87
sub(8,4);
sub(0,8);
}
catch(int k)
{
cout<<"Exception caught inside main()\n";
}
return 0;
}
OUTPUT-
56 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-10
a) Write a C++ Program to create Simple calculator using a Class template.
Code-
#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; }
57 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
58 | P a g e
Name: Niraj Vishwakarma Roll No: 87
Practical-10
b) Write a C++ Program to get a maximum of two number using Class template.
Code-
#include<iostream.h>
#include<conio.h>
template<class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
void main()
{
clrscr();
int i1, i2;
float f1, f2;
char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
getch();
}
59 | P a g e
Name: Niraj Vishwakarma Roll No: 87
OUTPUT-
60 | P a g e