You are on page 1of 71

1

PROGRAM 1: Enter any string and sort it. (Use string class for string handling)
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
int main( )
{
char swap;
string s;
cin>>s;
int len=s.length();
for(int i=0;i<len;i++)
{
for(int j=i;j<len;j++)
{
if(s[i]>s[j])
{
swap=s[i];
s[i]=s[j];
s[j]=swap;
}
}
}
cout<<s;
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


2

PROGRAM 2: Create an array of ten names and sort them. (Use string class for string
handling).
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
int main( )
{
char name[10][20],swap[20];
cout<<"ENter 10 names: ";
for(int i=0;i<10;i++)
cin>>name[i];
for(int i=0;i<10;i++)
{
for(int j=i+1;j<10;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(swap,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],swap);
}
}
}
cout<<"Names after sorted: \n";
for(int i=0;i<10;i++)
cout<<name[i]<<'\n';
}

Name:- Dev verma Roll No. :- 30 Section:- E


3

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


4

PROGRAM 3: Enter 10 numbers and do the following operations using functions: a) sorting
b) searching c) deletion d) updation
SOURCE CODE:
#include<iostream>
using namespace std;
void sorting(int arr[10])
{
int swap;
for(int i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
if(arr[i]>arr[j])
{
swap=arr[i];
arr[i]=arr[j];
arr[j]=swap;
}
}
}
for(int i=0;i<10;i++)
printf("%d ",arr[i]);
}
int searching(int arr[10],int value)
{
for(int i=0;i<10;i++)
{
if(arr[i]==value)
{
cout<<"Value find at position: "<<i+1<<'\n';

Name:- Dev verma Roll No. :- 30 Section:- E


5

return 0;
}
}
cout<<"Value does not found\n";
return 0;
}
void Deletion(int arr[10],int value)
{
int i;
for(i=value-1;i<9;i++)
{
arr[i]=arr[i+1];
}
cout<<"Elements after deletion are: ";
for(int i=0;i<9;i++)
cout<<arr[i]<<' ';
cout<<'\n';
}
void update(int arr[10],int index,int value)
{
arr[index-1]=value;
cout<<"Elements after updation: ";
for(int i=0;i<10;i++)
printf("%d ",arr[i]);
}
int main( )
{
int arr[10],ch,value,index;
cout<<"Enter Elements:
"; for(int i=0;i<10;i++)

Name:- Dev verma Roll No. :- 30 Section:- E


6

cin>>arr[i];
do
{
printf("*** MAIN MENU ***\n1. Sorting\n2. Searching\n3. Deletion\n4.
Updation\nEnter your choice: ");
cin>>ch;
switch(ch)
{
case 1: sorting(arr);
cout<<'\n';
break;
case 2: cout<<"Enter number for searching: ";
cin>>value;
searching(arr,value);
break;
case 3: cout<<("Enter index for deletion: ");
cin>>value;
Deletion(arr,value);
break;
case 4: cout<<("Enter index where you want to update value and value: ");
cin>>index>>value;
update(arr,index,value);
cout<<'\n';
break;
}
}while(ch<5);
}

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


7

Name:- Dev verma Roll No. :- 30 Section:- E


8

PROGRAM 4: Write a program to check whether a given string is a palindrome?


SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
int main( )
{
int i=0,count=0;
string str;
cout<<"Enter string: ";
cin>>str;
int len=str.length( );
int a=len;
for(i=0;i<=len/2;i++)
{
if(str[i]!=str[a-1])
count++;
a--;
}
if(count>=1)
printf("Not palindrome");
else
printf("Palindrome");
}

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


9

PROGRAM 5: Print a given matrix in spiral form. Example: Input: {{1, 2, 3, 4}, {5, 6, 7, 8},
{9, 10, 11, 12}, {13, 14, 15, 16 }} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Name:- Dev verma Roll No. :- 30 Section:- E


10

SOURCE CODE:
#include<iostream>
using namespace std;
int main( )
{
int n,i,j;
cout<<"Enter rows and column of array: ";
cin>>n;
int arr[n][n];
cout<<"Enter elements of matrix: \n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cin>>arr[i][j];
}
int s=0,end=n-1;
while(s<=end)
{
for(i=s;i<=end;i++)
cout<<arr[s][i]<<' ';
for(i=s+1;i<=end;i++)
cout<<arr[i][end]<<' ';
for(i=end-1;i>=s;i--)
cout<<arr[end][i]<<' ';
for(i=end-1;i>=s+1;i--)
cout<<arr[i][s]<<' ';
s++,end--;
}
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


11

PROGRAM 6: An array of integers is given, both +ve and -ve. You need to find the two
elements such that their sum is closest to zero. Example: int arr[] = {1, 60, -10, 70, -80, 85};
For the above array, program should print -80 and 85

Name:- Dev verma Roll No. :- 30 Section:- E


12

SOURCE CODE:
#include<iostream>
#include<limits.h>
#include<math.h>
using namespace std;
int main( )
{
int n,sum,ans=INT_MAX,first,second,j;
cout<<"Enter size of array: ";
cin>>n;
int arr[n];
cout<<"Enter elements of array: ";
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
sum=arr[i]+arr[j];
if(abs(sum)<abs(ans))
{
ans=sum;
first=arr[i];
second=arr[j];
}
}
}
}

Name:- Dev verma Roll No. :- 30 Section:- E


13

cout<<first<<" "<<second;
}

OUTPUT:

PROGRAM 7: Write a class for the following scenario and run it. The employees have four
attributes: id, name, salary, and company name. Initialize id, name, and salary at the time of
object creation. The list of methods is given as void setID(int) - to set the employee’s ID.

Name:- Dev verma Roll No. :- 30 Section:- E


14

void setName(string) - to set the employee’s name void setSalary(int) - to set the employee’s
salary. void setCompany_name(string)- to set the employee’s company name int getId()-to
get the employee’s id string getName()-to get the employee’s name. int getSalary()-to get the
employee’s salary. string getCompany_name()-to get the employee’s company name. If
employees are considered from the same company, then use an efficient memory
management technique to handle company name.
SOURCE CODE:
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class employee
{
private:
int id;
string name;
int salary;
string company_name;
public:

employee ( ) //constructor
{
id=1234;
name="xyz";
salary=5000;
company_name="xyz";
}
void setID(int x)
{
id=x;
}
void setName(string y)
{

Name:- Dev verma Roll No. :- 30 Section:- E


15

name=y;
}
void setSalary(int s)
{
salary=s;
}
void setCompany_name(string cmpny)
{
company_name=cmpny;
}
void getId()
{
cout<<"Id is: "<<id<<"\n";
}
void getName()
{
cout<<"Employee name: "<<name<<"\n";
}
void getSalary()
{
cout<<"Salary is: "<<salary<<"\n";
}
void getCompany_name()
{
cout<<"Comapny name: "<<company_name<<"\n";
}
};

int main( )
{

Name:- Dev verma Roll No. :- 30 Section:- E


16

employee abc;
int x,s;
string name,company;
cout<<"Enter name: ";
getline(cin,name);
cout<<"Enter company name: ";
getline(cin,company);
cout<<"Enter id: ";
cin>>x;
cout<<"Enter salary: ";
cin>>s;
abc.setID(x);
abc.setName(name);
abc.setSalary(s);
abc.setCompany_name(company);
abc.getId();
abc.getName();
abc.getSalary();
abc.getCompany_name();
}
OUTPUT:

PROGRAM 8: . Write a program to construct objects of a class with provided initial values.
Also, demonstrate the copy constructor.
SOURCE CODE:

Name:- Dev verma Roll No. :- 30 Section:- E


17

#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class employee
{
private:
int id;
string name;
int salary;
string company_name;
public:

employee ( ) //default constructor


{
id=1234;
name="xyz";
salary=5000;
company_name="abc";
}
employee(int id,string name,int salary,string company) //parameter
constructor
{
this->id=id;
this->name=name;
this->salary=salary;
this->company_name=company;
}
employee(employee &temp) //copy constructor
{
this->id=temp.id;
this->name=temp.name;

Name:- Dev verma Roll No. :- 30 Section:- E


18

this->salary=temp.salary;
this->company_name=temp.company_name;
}
void getId()
{
cout<<"Id is: "<<id<<"\n";
}
void getName()
{
cout<<"Employee name: "<<name<<"\n";
}
void getSalary()
{
cout<<"Salary is: "<<salary<<"\n";
}
void getCompany_name()
{
cout<<"Comapny name: "<<company_name<<"\n";
}
};
int main( )
{
employee abc; //default constructor
employee a1(500,"ashish",10000,"ltd"); //Parameter constructor
cout<<"Values of ABC object: \n";
abc.getId();
abc.getName();
abc.getSalary();
abc.getCompany_name();
cout<<"\nValues of A1 object: \n";

Name:- Dev verma Roll No. :- 30 Section:- E


19

a1.getId();
a1.getName();
a1.getSalary();
a1.getCompany_name();
// copy values
cout<<"\nValues of OBJ object: \n";
employee obj(a1);
obj.getId();
obj.getName();
obj.getSalary();
obj.getCompany_name();
}
OUTPUT:

PROGRAM 9: Write a program that counts the total number of objects created for any given
class.
SOURCE CODE:

Name:- Dev verma Roll No. :- 30 Section:- E


20

#include <iostream>
using namespace std;
static int count= 0;
class A
{
int x;
public:
A()
{
count++;
x = 10;
}
};
int main()
{
A obj1, obj2 , obj3;
cout <<"Number of objects created : "<< count<<endl;
}
OUTPUT:

PROGRAM 10: If you want to access private members of a class, then what are the options?
Use examples to demonstrate.
SOURCE CODE:

Name:- Dev verma Roll No. :- 30 Section:- E


21

#include<iostream>
using namespace std;
class Student
{
private:
int id;
public:

void setId(int id1)


{
id=id1;
}
int getId()
{
return id;
}
friend void show(Student);
};
void show(Student s)
{
cout<<s.id;
}
int main()
{
Student s1;
s1.setId(50);
show(s1);
}

Name:- Dev verma Roll No. :- 30 Section:- E


22

OUTPUT:

By use of friend function we can access the private member methods.

Name:- Dev verma Roll No. :- 30 Section:- E


23

PROGRAM 11: Write a program to create objects at the time of run. Also, write code to
delete objects dynamically.
SOURCE CODE:
#include<iostream>
using namespace std;
class abc
{
private:
int a,b;
public:
abc(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"a=: "<<a<<"\n"<<"b=: "<<b<<endl;
}
~abc()
{
cout<<"The object is deleted\n";
}
};
int main()
{
abc a(30,17);
a.show( );

Name:- Dev verma Roll No. :- 30 Section:- E


24

abc *t=new
abc(15,20); t->show();
delete t; }

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


25

PROGRAM 12: Write a program to overload the ++ operator for the following scenario.
Class A Attributes are as follows: int a=1, b=2, c=3. Overload the ++ operator (for both
prefix and postfix) so that when you apply it to an object of class A, it increases the values of
all attributes by one. For this example, the answer will be a=2, b=3 and c=4
SOURCE CODE:
#include<iostream>
using namespace std;
class increase
{
private:
int a,b,c;
public:

increase(int x,int y,int z) //constructor


{
a=x;
b=y;
c=z;
}
void show()
{
cout<<"a= "<<a<<"\t"<<"b= "<<b<<"\t"<<"c= "<<c<<endl;
}
void operator ++()
{
a++;
b++;
c++;
}

Name:- Dev verma Roll No. :- 30 Section:- E


26

};

int main()
{
increase d(1,2,3);
d.show();
++d; // d.operator++();
d.show( );
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


27

PROGRAM 13: Overload the binary operator + to add the integer attribute values of two
objects.
SOURCE CODE:
#include<iostream>
using namespace std;
class abc
{
private:
int x;
public:
void getdata( )
{
cout<<"ENter number: ";
cin>>x;
}
void show()
{
cout<<"Sum of x field= "<<x<<endl;
}
abc operator + (abc d)
{
abc d3;
d3.x=x+d.x;
return d3;
}
};
int main()
{

Name:- Dev verma Roll No. :- 30 Section:- E


28

abc a,b,c;
a.getdata( );
b.getdata( );
c=a+b;
c.show( );
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


29

PROGRAM 14: Overload the binary operator + to add two strings.


SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
class abc
{
private:
string x;
public:
void getdata( )
{
cout<<"Enter String: ";
getline(cin,x);
}
void show()
{
cout<<"Sum of two string: "<<x<<endl;
}
abc operator + (abc d)
{
abc d3;
d3.x=x+d.x;
return d3;
}
};
int main()

Name:- Dev verma Roll No. :- 30 Section:- E


30

{
abc a,b,c;
a.getdata( );
b.getdata( );
c=a+b;
c.show( );
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


31

PROGRAM 15: Create base class with attributes name, age and roll_number. Consider name
as private, age as protected, and roll_number as public. Write various child programs that
inherit base class in different access modes like private protected and public and show the
accessibility of various attributes.
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
class A
{
private:
string name;
public:
int roll_no;
protected:
int age;
};
class B:public A
{
public:
/*void setName(string x1)
{
name=x1; //Error as name is a priavte member in class A
} */
void setRollNo(int y1)
{
roll_no=y1;
}

Name:- Dev verma Roll No. :- 30 Section:- E


32

void setAge(int z1)


{
age=z1;
}
void show()
{
//cout<<"The value of x= "<<x<<endl; //Error, since x is private in class A hence not
inherited
cout<<"Roll No: "<<roll_no<<endl;
cout<<"Age: "<<age<<endl;
}
};
int main()
{
B abc;
// abc.setName("ashish"); //can-not be accessed as it is not member of class B
abc.setRollNo(20);
abc.setAge(30);
abc.show();
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


33

PROGRAM 16: Write a Base class having two attributes int x, int y. Write child class having
void sum(int x, int y) function to add two provided values. You have to initialize base class’s
attributes with the help of child class object and then use sum method to add the values. Also
write a show() method in child to show the result.
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
class A
{
protected:
int x,y,ans;
};
class B:public A
{
public:

void setValue(int x1,int y1)


{
x=x1;
y=y1;
}
void sum( )
{

ans=x+y;
}
void show()
{
cout<<"Sum: "<<ans;
}

Name:- Dev verma Roll No. :- 30 Section:- E


34

};

int main()
{
B abc;
abc.setValue(20,30);
abc.sum();
abc.show();
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


35

PROGRAM 17: . Write a program to copy the value of one object to another.
SOURCE CODE:
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class employee
{
public:

int id;
string name;
int salary;
string company_name;
employee(int id,string name,int salary,string company) //parameter
constructor

{
this->id=id;
this->name=name;
this->salary=salary;
this->company_name=company;
}
employee(employee &temp) //copy constructor
{
this->id=temp.id;
this->name=temp.name;
this->salary=temp.salary;
this->company_name=temp.company_name;
}

Name:- Dev verma Roll No. :- 30 Section:- E


36

void getId()
{
cout<<"Id is: "<<id<<"\n";
}
void getName()
{
cout<<"Employee name: "<<name<<"\n";
}
void getSalary()
{
cout<<"Salary is: "<<salary<<"\n";
}
void getCompany_name()
{
cout<<"Comapny name: "<<company_name<<"\n";
}
};
int main( )
{
employee a1(500,"ashish",10000,"ltd"); //Parameter constructor
cout<<"Values of A1 object: \n";
a1.getId();
a1.getName();
a1.getSalary();
a1.getCompany_name();
cout<<"\nValues of OBJ object: \n"; // copy values
employee obj(a1);
obj.getId();
obj.getName();
obj.getSalary();

Name:- Dev verma Roll No. :- 30 Section:- E


37

obj.getCompany_name();
}

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


38

PROGRAM 18: Write a program to demonstrate diamond’s problem. Also update the same
program to resolve it.
SOURCE CODE:
#include<iostream>
using namespace std;
int main( )
{
int rows,i;
cout<<"Enter no of rows: ";
cin>>rows;
int a=rows;
int b=rows;
for(i=1;i<=rows;i++)
{
for(int j=1;j<=(rows*2);j++)
{
if(j==a || j==b)
cout<<"*";
else
cout<<" ";
}
a--;
b++;
cout<<"\n";
}
a=3;
b=(rows*2)-1;
for(i=rows-1;i>0;i--)

Name:- Dev verma Roll No. :- 30 Section:- E


39

{
for(int j=(rows*2);j>=0;j--)
{
if(j==a || j==b)
cout<<"*";
else
cout<<" ";
}
a++;
b--;
cout<<"\n";
}
}
OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


40

PROGRAM 19: Write a program having a number of sum () methods which are used to sum
the provided arguments. Also write a show() method to show the result.
SOURCE CODE:
#include <iostream>
using namespace std;
class overloading
{
private:
int sum;
public:
void addition(int a,int b)
{
sum=a + b;
}
void addition(float a, float b, float c)
{
sum=a+b+c;
}
void show( )
{
cout<<"Sum is: "<<sum<<endl;
}
};
int main()
{
overloading a;
a.addition(50, 50);
a.show( );

Name:- Dev verma Roll No. :- 30 Section:- E


41

a.addition(25,26,60); //using same function but changing arguments


a.show( );
}

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


42

PROGRAM 20: Write a program to demonstrate late binding.


SOURCE CODE:
#include<iostream>
using namespace std;
class Base
{
public:
virtual void display()
{
cout<<"Base class\n";
}
};
class Derived: public Base
{
public:
void display()
{
cout<<"Derived class\n";
}
};

Name:- Dev verma Roll No. :- 30 Section:- E


43

int main()
{
Base *base_pointer = new
Derived; base_pointer->display();
return 0;
}
OUTPUT:

PROGRAM 21: Write a program to demonstrate pure virtual functions and abstract classes.
SOURCE CODE:
#include <iostream>
using namespace std;
class Shape // Abstract base class with a pure virtual function
{
public:
virtual double area() = 0; // Pure virtual function
};
class Square : public Shape // Derived class that implements the pure virtual function
{
private:
double length, width;
public:
Square(double length, double width):length(length), width(width) {}
double area()
{
return length * width;
}
};
int main()

Name:- Dev verma Roll No. :- 30 Section:- E


44

{
Square s(10,10); // Create a square object
cout<<"Area of square: "<<s.area()<<endl; // Call the area method to calculate the
rectangle's area
// Cannot create an object of the abstract base class Shape
// Shape r; // error: cannot declare variable 'r' to be of abstract type 'Shape'
return 0;
}

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


45

Name:- Dev verma Roll No. :- 30 Section:- E


46

PROGRAM 22: If Parent and child classes are there and pointer of parent is referring to the
object of child then how virtual destructor of parent plays an important role? Write a program
to demonstrate the scenario.
SOURCE CODE:
When a parent class has a virtual destructor and a pointer to the parent class refers to an
object of a derived class, the correct destructor is called for the derived class when the object
is deleted. This is important because the derived class may have additional resources that
need to be properly deallocated when the object is destroyed.

Here's an example of how you can demonstrate this scenario in C++:


#include <iostream>
using namespace std;
class Base // Base class with a virtual destructor
{
public:
virtual ~Base()
{
cout<<"Base::~Base()"<< endl;
}
};
class Derived:public Base // Derived class with a custom destructor
{

Name:- Dev verma Roll No. :- 30 Section:- E


47

private:
int *ptr;
public:
Derived()
{
ptr=new int;
}
~Derived()
{
delete ptr;
cout<<"Derived::~Derived()"<<endl;
}
};
int main()
{
Base *b; // Pointer to the base class
// Create a derived object and assign its address to the base pointer
Derived d;
b=&d;
// When the object is deleted, the correct destructor is called
delete b; // prints "Derived::~Derived()" and "Base::~Base()"
return 0;
}

Name:- Dev verma Roll No. :- 30 Section:- E


48

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


49

PROGRAM 23: Write a program to read text from keyboard then write to some text file like
“Sample.txt”. Then read this content and write back to console.
SOURCE CODE:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string text;
cout<<"Enter text: ";
getline(cin,text);
ofstream f1("Sample.txt");
f1<<text;
f1.close();
ifstream f2("Sample.txt");
string output;
getline(f2,output);
f2.close();
cout<<"Text from file: "<<output<<endl;
}

Name:- Dev verma Roll No. :- 30 Section:- E


50

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


51

PROGRAM 24: Write a program to search any given word in a given file. Also show its
frequency.
SOURCE CODE:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string text;
cout<<"Enter text: ";
getline(cin,text);
ofstream f1("Sample.txt");
f1<<text;
f1.close();
cout<<"Enter a word to search for: ";
string input_word,filename;
cin>>input_word;
ifstream f2;
cout<<"Enter file name: ";
cin>>filename;

Name:- Dev verma Roll No. :- 30 Section:- E


52

f2.open(filename.c_str( ));
int count=0;
string word;
while(f2>>word) {
if(word==input_word)
count++;
}
f2.close();
cout<<"No of words present in file: "<<count<<endl;
}

Name:- Dev verma Roll No. :- 30 Section:- E


53

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


54

PROGRAM 25: Write a program to open any existing file like sample.txt then write
something in it and then read from start and display on console.
SOURCE CODE:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("Sample.txt");
out<<"Hello, world!"<<endl;
out.close();
ifstream in("Sample.txt");
string contents((istreambuf_iterator<char>(in)),istreambuf_iterator<char>());
in.close();
cout<<contents<<endl;
}

Name:- Dev verma Roll No. :- 30 Section:- E


55

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


56

PROGRAM 26: Write a function sum(argument 1, argument 2) that can take any type of two
arguments and add them. Write a program also to demonstrate.
SOURCE CODE:
#include<iostream>
using namespace std;
template <class T1,class T2> T1 sum(T1 x,T2 y)
{
return (x+y);
}

int main( )
{
int a=10;
int b=20;
cout<<"Sum of 2 integer: "<<sum(a,b)<<endl;
float c=10.5;
float d=10.7;
cout<<"Sum of 2 float: "<<sum(c,d)<<endl;

Name:- Dev verma Roll No. :- 30 Section:- E


57

float e=10.7;
int g=10;
cout<<"Sum of float and integer: "<<sum(e,g)<<endl;
}

Name:- Dev verma Roll No. :- 30 Section:- E


58

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


59

PROGRAM 27: Overload generic function sum() as given in program no. 26 with three
arguments.
SOURCE CODE:
#include<iostream>
using namespace std;
template <class T1,class T2> T1 sum(T1 x,T2 y)
{
return (x+y);
}
template <class T1,class T2,class T3> T1 sum(T1 x,T2 y,T3 z)
{
return (x+y+z);
}
int main( )
{
int a=10;
int b=20;
cout<<"Sum of 2 integer: "<<sum(a,b)<<endl;
float c=10.5;

Name:- Dev verma Roll No. :- 30 Section:- E


60

float d=10.7;
cout<<"Sum of 2 float:
"<<sum(c,d)<<endl; float e=10.7;
int g=10;
cout<<"Sum of float and integer: "<<sum(e,g)<<endl;
cout<<"Sum of 3 integer: "<<sum(a,b,g)<<endl;
}

Name:- Dev verma Roll No. :- 30 Section:- E


61

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


62

PROGRAM 28: Write a program and handle exception that occurs during division and
denominator is zero.
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"Enter value of x and y: ";
cin>>x>>y;
try
{
if(y==0
) throw
1; else
{
z=x/y;
cout<<z<<endl;
}

Name:- Dev verma Roll No. :- 30 Section:- E


63

}
catch(int a)
{
cout<<a<<endl;
}
}

Name:- Dev verma Roll No. :- 30 Section:- E


64

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


65

PROGRAM 29: . Create your own exception “MyException” that occurs at the same
condition as mentioned in program no. 28.
SOURCE CODE:
#include <iostream>
#include <exception>
using namespace std;
class MyException : public exception
{
public:
const char * what() const throw()
{
return "Enter valid value of denominator!\n";
}
};

int main()
{
try
{

Name:- Dev verma Roll No. :- 30 Section:- E


66

int x,y;
cout<<"Enter the two numbers: ";
cin>>x>>y;
if(y==0)
{
MyException z;
throw z;
}
else
{
cout<<x/y<<endl;
}
}
catch(MyException e)
{
cout<<e.what();
}
}

Name:- Dev verma Roll No. :- 30 Section:- E


67

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


68

PROGRAM 30: Create a vector and add five elements. Display all the elements, add a new
element in last, show the capacity, remove from last, show the size, add element at start and
3rd position, remove all elements and then show the capacity again.
SOURCE CODE:
#include<iostream>
#include<vector>
using namespace std;
int main( )
{
int val;
vector<int>v1;
// enter and displaying Elements
for(int i=0;i<5;i++)
{
cout<<"Enter element: ";
cin>>val;
v1.push_back(val);
}
cout<<"Elements are: ";
for(int i=0;i<v1.size();i++)

Name:- Dev verma Roll No. :- 30 Section:- E


69

cout<<v1[i]<<" ";
cout<<"\nEnter an element to add at last postion: ";
cin>>val;
v1.push_back(val);
for(int i=0;i<v1.size();i++)
cout<<v1[i]<<" ";
cout<<"\nCapacity: "<<v1.capacity( );
v1.pop_back( );
cout<<"\nElements after popping: ";
for(int i=0;i<v1.size( );i++)
cout<<v1[i]<<" ";
cout<<"\nSize: "<<v1.size( );
cout<<"\nEnter an element: ";
cin>>val;
v1.insert(v1.begin(),val);
cout<<"Enter an element: ";
cin>>val; v1.insert(v1.begin()
+3,val);
cout<<"Elements after adding elements are: ";
for(int i=0;i<v1.size();i++)
cout<<v1[i]<<" ";
v1.clear( ); //delete all elements cout<<"\
nSize: "<<v1.size( ); cout<<"\nCapacity:
"<<v1.capacity( );
}

Name:- Dev verma Roll No. :- 30 Section:- E


70

OUTPUT:

Name:- Dev verma Roll No. :- 30 Section:- E


71

Name:- Dev verma Roll No. :- 30 Section:- E

You might also like