C++ Assignment PDF
C++ Assignment PDF
class Distance
private:
int feet,inches;
public:
void readDistance(void)
cin >>feet;
cin >>inches;
void dispDistance(void)
cout << "Feet:" << feet << "\t" << "Inches:" << inches << endl;
}
Distance operator+(Distance &dist1)
Distance tempD;
tempD.inches=tempD.inches%12;
return tempD;
};
int main( )
Distance D1,D2,D3;
D1.readDistance( );
D2.readDistance( );
D3=D1+D2;
D3.dispDistance( );
return 0;
}
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO ADD TWO MATRICES
USING OPERATOR OVERLOADING.
#include<iostream>
class Matrix
int a[3][3];
public:
void accept( );
void display( );
};
void Matrix::accept( )
cout<<" ";
cin>>a[i][j];
}
}
void Matrix::display( )
cout<<" ";
cout<<a[i][j]<<"\t";
cout<<"\n";
int mat[3][3];
mat[i][j]=a[i][j]+x.a[i][j];
}
cout<<"\n Addition of Matrix : \n\n";
cout<<" ";
cout<<mat[i][j]<<"\t";
cout<<"\n";
int main( )
Matrix m,n;
m.accept( );
n.accept( );
m.display( );
n.display( );
m+n;
return 0;
}
OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO ADD AND SUBTRACT
TWO COMPLEX NUMBER USING
OPERATOR OVERLOADING.
#include <iostream>
class Complex{
public:
float real;
float img;
Complex( ){
real = r;
img = i;
Complex temp;
return temp;
return temp;
void display( ){
cout << this->real << " + " << this->img << "i" << endl;
};
int main( )
Complex a, b, c;
cout << "Enter real and complex coefficient of First Complex number" << endl;
cout << "Enter real and complex coefficient of Second Complex number"<<
endl;
c = a+b;
c.display( );
c = a-b;
c.display( );
return 0;
}
OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO INCREMENT A DATE
USING OPERATOR OVERLOADING.
#include <iostream>
class Time {
private:
int hours;
int minutes;
public:
Time( ) {
hours = 0;
minutes = 0;
Time(int h, int m) {
hours = h;
minutes = m;
void displayTime( ) {
cout << "H: " << hours << " M:" << minutes <<endl;
Time operator++ ( ) {
++minutes;
minutes -= 60;
++minutes;
++hours;
minutes -= 60;
return T;
};
int main( ) {
++T1;
T1.displayTime( );
++T1;
T1.displayTime( );
T2++;
T2.displayTime( );
T2++;
T2.displayTime( );
return 0;
}
OUTPUT
QUESTION 5 :- WRITE A PROGRAM
IN ‘C++’ TO OVERLOAD DIFFERENT
OPERATORS .
#include<iostream>
#include<cstring>
class my_string{
private:
char str[30];
public:
void getdata( );
void display( );
void palindrome( );
};
void my_string::getdata( )
cin>>str;
}
void my_string::display( )
cout<<"\n"<<str;
strcpy(str1.str,str);
if(strcmp(str,str1.str)==0)
return 1;
return 0;
strcat(str,str1.str);
int i;
cout<<"\n\t--The string after reversing is : ";
for(i=strlen(str1.str);i>=0;i--)
cout<<str1.str[i];
int flag=0,k,i,j,len=strlen(str),len1=strlen(str1.str)-1;
for(i=0;i<len;i++)
if(str[i]==str1.str[0])
if(str[i+len1]==str1.str[len1])
for(j=i,k=0;j<i+len1+1,k<len1;j++,k++)
if(str[j]==str1.str[k])
flag=1;
else
flag=0;
break;
if(flag==0)
return 0;
return 1;
void my_string::palindrome( )
int i,j,flag=0;
for(i=0,j=strlen(str)-1;i<=strlen(str),j>=0;j--,i++)
if(str[i]!=str[j])
flag=1;
cout<<"\n\t--Not a palindrome--";
break;
else
flag=0;
if(flag==0)
cout<<"\n\t--Palindrome--";
int main( )
int opt,c,opt1=1;
my_string a,b;
{
cout<<"\n\t\t\t---Main Menu---\n\t1.Equality\n\t2.String
Copy\n\t3.Concat";
cout<<"\n\t4.Display\n\t5.Reverse\n\t6.Palindrome\n\t7.Sub String";
cin>>opt;
switch(opt)
case 1:
a.getdata();
b.getdata();
c=a=b;
if(c==1)
else
break;
case 2:
a.getdata( );
a==b;
break;
case 3:
a.getdata( );
b.getdata( );
a+b;
break;
case 4:
a.getdata( );
b<<a;
break;
case 5:
a.getdata( );
b>>a;
break;
case 6:
a.getdata( );
a.palindrome( );
break;
case 7:
a.getdata( );
b.getdata( );
c=a/b;
if(c==1)
cout<<"\n\t---Substring---\n";
else
cout<<"\n\t---Not a substring---\n";
break;
case 8: return 0;
if(opt!=8){
cin>>opt1;}
return 0;
OUTPUT
TYPE CONVERSION
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW TYPE
CONVERSION .
#include <iostream>
class stock2 ;
class stock1{
float price ;
public :
code = a ;
item = b ;
price = c ;
};
void disp ( ) {
cout << " code " << code << " \n " ;
cout << " items " << item << " \n " ;
cout << " price per item Rs. " << price << " \n " ;
};
int getcode ( )
{ return code; };
int getitem ( )
{ return item ; };
int getprice ( )
{ return price ; };
operator float ( ) {
return ( item*price ) ;
};
};
class stock2{
int code ;
float val ;
public :
stock2 ( ) {
code = 0;
val = 0 ;
};
code = x ;
val = y ;
};
void disp ( ) {
cout << " code " << code << " \n " ;
cout << " total value Rs. " << val << " \n " ;
};
stock2( stock1 p ) {
code = p.getcode() ;
};
};
int main( )
{
stock2 i2 ;
i2 = i1 ;
cout << " Stock Details : Stock 1 type " << " \n " ;
i1.disp ( );
cout << " Stock Details : Stock 2 type " << " \n " ;
i2.disp ( ) ;
return 0 ;
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO CONVERT FEETS AND
INCHES TO METRES.
#include <iostream>
class Distance
int feet;
float inches;
public:
Distance( )
feet=0;
inches=0.0;
feet=ft;
inches=in;
operator float( ) {
float feetinfractions=inches/12;
feetinfractions+=float(feet);
return (feetinfractions/MeterToFloat);
}
};
int main( )
int feet;
float inches;
cout<<"\nFeet:";
cin>>feet;
cout<<"Inches:";
cin>>inches;
float meters=dist;
return 0;
OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW TYPE
CONVERSION .
#include <iostream>
#include <iomanip>
class Time
private:
int seconds;
int hh,mm,ss;
public:
void getTime(void);
void convertIntoSeconds(void);
void displayTime(void);
};
void Time::getTime(void)
void Time::convertIntoSeconds(void)
{
seconds = hh*3600 + mm*60 + ss;
void Time::displayTime(void)
cout << "The time is = " << setw(2) << setfill('0') << hh << ":"
int main( )
Time T;
T.getTime( );
T.convertIntoSeconds( );
T.displayTime( );
return 0;
OUTPUT
INHERITANCE
QUESTION 1 :- WRITE A PROGRAM IN
‘C++’ TO SHOW SINGLE LEVEL
INHERITANCE.
#include<iostream.h>
#include<conio.h>
class emp {
public:
int eno;
void get( ) {
cin>>eno;
cin>>name;
cin>>des;
};
public:
void get1( ) {
cin>>bp;
cin>>hra;
cin>>da;
cin>>pf;
void calculate( ) {
np = bp + hra + da - pf;
void display( ) {
cout << eno << "\t" << name << "\t" << des << "\t" << bp << "\t" << hra << "\t" <<
da << "\t" << pf << "\t" << np << "\n";
};
void main( ) {
int i, n;
char ch;
salary s[10];
clrscr( );
s[i].get( );
s[i].get1( );
s[i].calculate( );
s[i].display( );
getch();
OUTPUT
Enter the Roll no: 100
90
80
Total : 260
Average: 86.66
QUESTION 2 :- WRITE A PROGRAM IN
‘C++’ TO SHOW MULTIPLE
INHERITANCE.
#include<iostream.h>
#include<conio.h>
class student {
protected:
public:
void get( ) {
cin>>rno;
};
class sports {
protected:
int sm;
public:
void getsm( ) {
cout << "\nEnter the sports mark :";
cin>>sm;
};
public:
void display( ) {
avg = tot / 3;
cout << "\n\n\tRoll No : " << rno << "\n\tTotal : " << tot;
};
void main( ) {
clrscr( );
statement obj;
obj.get( );
obj.getsm( );
obj.display( );
getch( );
}
OUTPUT
90
80
Total : 260
Average: 86.66
QUESTION 3 :- WRITE A PROGRAM IN
‘C++’ TO SHOW MULTILEVEL
INHERITANCE.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Employee {
int eno;
public:
void getEmpDetails( ) {
cin>>eno;
cin>>name;
cin>>des;
void employee_display( ) {
}
};
public:
void getPayDetails( ) {
getEmpDetails( );
cin>>bp;
cin>>hra;
cin>>da;
cin>>pf;
calculate( );
void calculate( ) {
np = bp + hra + da - pf;
void salary_display( ) {
employee_display( );
}
};
int account_number;
public:
void getBankDetails( ) {
getPayDetails( );
cin>>bank;
cin>>ifsc_code;
cin>>account_number;
void display( ) {
salary_display( );
};
int main( ) {
int i, n;
char ch;
BankCredit s[10];
cout << "Simple Multi Level Inheritance Example Program : Payroll System \n";
s[i].getBankDetails();
s[i].display( );
getch( );
return 0;
}
OUTPUT
Employee Details # 1 :
Employee Details # 2 :
Employee number:101
Employee name:MASTE
Employee designation:Er
Employee IFSC:ISFC001
Employee number:102
Employee name:FORGE
Employee designation:Dr
Employee IFSC:ISFC0004
#include<stdio.h>
#include<conio.h>
class Person {
int eno;
public:
void getPersonDetails( ) {
cin>>eno;
cin>>name;
cin>>des;
void person_display( ) {
}
};
public:
void getEmployeeDetails( ) {
getPersonDetails( );
cin>>bp;
cin>>hra;
cin>>da;
cin>>pf;
calculate( );
void calculate( ) {
np = bp + hra + da - pf;
void employee_display( ) {
person_display( );
}
};
public:
void getStudentDetails( ) {
getPersonDetails( );
cin>>college;
cin>>course;
void student_display( ) {
person_display( );
};
int main( ) {
int i, n;
char ch;
Student s[10];
Employee e[10];
cin>>n;
s[i].getStudentDetails();
s[i].student_display();
cin>>n;
e[i].getEmployeeDetails();
e[i].employee_display();
getch();
return 0;
}
OUTPUT
Student Details # 1 :
Student Details # 2 :
Person number:101
Person name:Booke
Person designation:10th
Student IFSC:BSC
Person number:102
Person name:Moste
Person designation:12th
Student IFSC:BE
Employee Details # 1 :
Employee Details # 2 :
Person name:ASHK
Person designation:Er
Person number:203
Person name:ROK
Person designation:Dr
#include<iostream.h>
int a,b,c,d,e;
class A
protected:
public:
void getab( )
cin>>a>>b;
};
class B:public A {
protected:
public:
void getc( )
cout<<"Enter c value:";
cin>>c;
};
class C
protected:
public:
void getd( )
cout<<"Enter d value:";
cin>>d;
};
protected:
public:
void result( )
getab( );
getc( );
getd( );
e=a+b+c+d;
};
int main( )
D d1;
d1.result( );
return 0;
OUTPUT
Total Objects: 1
Enter c value: 15
Enter d value: 20
Addition is :50
VIRTUAL BASE CLASS
QUESTION :- WRITE A PROGRAM IN
‘C++’ TO SHOW THE FOLLOWING
IMPLEMENTATION .
EXAM SPORTS
RESULT
#include<iostream.h>
class student
protected:
int roll_no;
char name[40];
char dept_name[40];
char college[40];
char dob[12];
public:
void get_input( );
void print_data( );
};
void student::get_input( )
cout<<"\nEnter roll-no:";
cin>>roll_no;
cout<<"\nEnter name:";
cin.ignore( );
cin.getline(name,100);
cin.getline(dept_name,20);
cin.getline(college,20);
cin.getline(dob,12);
void student::print_data( )
cout<<"\n\nRoll:"<<roll_no;
cout<<"\n\nName:"<<name;
cout<<"\n\nDate of birth:"<<dob;
cout<<"\n\nDepartment:"<<dept_name;
cout<<"\n\nCollege:"<<college;
protected:
int marks[5];
public:
void get_marks( );
void print_marks( );
};
void exam::get_marks( )
cout<<"\nEnter Marks\n-------------------\n";
for(int i=0;i<6;i++)
while(true)
cout<<"\nSubject"<<i+1<<":";
cin>>marks[i];
if(marks[i]>=0&&marks[i]<=100)
break;
void exam::print_marks( )
{
cout<<"\n\nMarks obtained in theory\n---------------------------\n";
for(int i=0;i<6;i++)
cout<<"Subject"<<i+1<<": "<<marks[i]<<"\n";
protected:
char p_g;
public:
void get_grade( );
void print_grade( );
};
void sports::get_grade( )
while(true)
cin>>p_g;
if(p_g=='A'||p_g=='B'||p_g=='C'||p_g=='D')
break;
void sports::print_grade( )
}
class result:public exam,public sports
private:
int grand_total;
public:
void get_info( );
void display( );
};
void result::get_info( )
get_input( );
get_marks( );
get_grade( );
void result::display( )
grand_total=0;
for(int i=0;i<6;i++)
grand_total+=marks[i];
if(p_g=='A')
grand_total+=50;
if(p_g=='B')
grand_total+=40;
if(p_g=='C')
grand_total+=30;
if(p_g=='D')
grand_total+=20;
print_data( );
print_marks( );
print_grade( );
int main( )
int num,i;
while(true)
cin>>num;
if(num>0)
break;
for(i=0;i<num;i++)
stu[i].get_info();
for(i=0;i<num;i++)
{
cout<<"\n\n\nRecord for student no:"<<i+1<<"\n\n------------------------\n--------
----------------";
stu[i].display();
cout<<"\n";
delete[]stu;
return 0;
OUTPUT
Enter number of students:2
-------------------------
-------------------------
Enter roll-no:122
Enter Marks
-------------------
Subject1:55
Subject2:67
Subject3:84
Subject4:45
Subject5:90
Subject6:34
-------------------------
-------------------------
Enter roll-no:167
Enter Marks
-------------------
Subject1:56
Subject2:67
Subject3:44
Subject4:37
Subject5:89
Subject6:90
--------------------------------------------------------
------------------------
------------------------
Roll:122
Name:Rohit Sharma
Date of birth:07/05/1988
Department:Mathematics
College:Scottish Church
---------------------------
Subject1: 55
Subject2: 67
Subject3: 84
Subject4: 45
Subject5: 90
Subject6: 34
------------------------
------------------------
Roll:167
Name:Pritam Biswas
Date of birth:12/12/1989
Department:Computer Sc.
College:Vidyasagar
---------------------------
Subject1: 56
Subject2: 67
Subject3: 44
Subject4: 37
Subject5: 89
Subject6: 90
#include <iostream.h>
#include <stdlib.h>
class person
protected:
char name[20];
int code;
public:
void getdetail(void)
cin>>name;
cin>>code;
void dispdetail(void)
cout<<"\n\nNAME :- "<<name;
cout<<"\nCODE :- "<<code;
}
};
protected:
float pay;
public:
void getpay(void)
cin>>pay;
void dispay(void)
cout<<"\nPAY :- "<<pay;
};
protected:
int experience;
public:
void getexpr(void)
cin>>experience;
}
void dispexpr(void)
cout<<"\nEXPERIENCE:- "<<experience;
};
public:
void create(void)
cout<<"\n\n=====GETDATA IN=====\n";
getdetail( );
getpay( );
getexpr( );
void display(void)
cout<<"\n\n=====DISPLAY DETAILS=====\n";
dispdetail( );
dispay( );
dispexpr( );
void update(void)
cout<<"\n\n=====UPDATE DETAILS=====\n";
cout<<"1) NAME\n";
cout<<"2) CODE\n";
cout<<"3) EXPERIENCE\n";
cout<<"4) PAY\n";
int choice;
cin>>choice;
switch(choice)
cin>>name;
break;
cin>>code;
break;
cin>>pay;
break;
cin>>experience;
break;
};
int main( )
{
master ob1;
int choice;
while(1)
cout<<"\n\n=====EMPLOYE DATABASE=====\n\n";
cout<<"4) Exit\n";
cin>>choice;
switch(choice)
case 1 : ob1.create( );
break;
case 2 : ob1.display( );
break;
case 3 : ob1.update( );
break;
case 4 : exit(1);
return 0;
}
OUTPUT
=====EMPLOYE DATABASE=====
1) Create Record
2) Display Record
3) Update Record
4) Exit
=====GETDATA IN=====
Enter code :- 01
=====EMPLOYE DATABASE=====
1) Create Record
2) Display Record
3) Update Record
4) Exit
NAME :- Codez
CODE :- 1
PAY :- 40000
EXPERIENCE:- 3
=====EMPLOYE DATABASE=====
1) Create Record
2) Display Record
3) Update Record
4) Exit
=====UPDATE DETAILS=====
1) NAME
2) CODE
3) EXPERIENCE
4) PAY
1) Create Record
2) Display Record
3) Update Record
4) Exit
=====DISPLAY DETAILS=====
NAME :- CodezClub
CODE :- 1
PAY :- 40000
EXPERIENCE:- 3
=====EMPLOYE DATABASE=====
1) Create Record
2) Display Record
3) Update Record
4) Exit
Process returned 1
VIRTUAL FUNCTION
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
IMPLEMENTATION OF PURE
VIRTUAL FUNCTION .
#include <iostream>
class Employee
};
int salary;
public:
Employee_1(int s){
salary = s;
int getSalary( ){
return salary;
};
int salary;
public:
Employee_2(int t){
salary = t;
int getSalary( ){
return salary;
};
int main( )
Employee_1 e1(5000);
Employee_2 e2(3000);
int a, b;
a = e1.getSalary( );
b = e2.getSalary( );
return 0;
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
IMPLEMENTATION OF VIRTUAL
FUNCTION .
#include <iostream>
class Weapon {
public:
};
public:
void features( ) {
this->Weapon::features( );
};
public:
void features( ) {
this->Weapon::features( );
};
class Loader {
public:
weapon->features();
};
int main( ) {
Weapon *w;
Bomb b;
Gun g;
w = &b;
l->loadFeatures(w);
w = &g;
l->loadFeatures(w);
return 0;
OUTPUT
FUNCTION OVERLOADING
& FUNCTION OVERRIDING
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
FUNCTION OVERLOADING .
#include <iostream>
class Rectangle
public:
void printArea(int x)
void printArea(double x)
}
};
int main( )
Rectangle rt;
rt.printArea(2,4);
rt.printArea(2,5.1);
rt.printArea(10);
rt.printArea(2.3);
return 0;
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW FUNCTION
OVERRIDING .
#include <iostream>
class Animals{
public:
void sound( )
};
public:
void sound( )
};
public:
void sound( )
};
public:
void sound( )
};
int main( )
Dogs d;
Cats c;
Pigs p;
d.sound( );
c.sound( );
p.sound( );
return 0;
OUTPUT
GENERIC PROGRAMMING
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW GENERIC
PROGRAMMING USING
TEMPLATE.
#include<iostream>
template<class T>
class matrix
T m[r][c];
public:
void get_value()
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>m[i][j];
}
}
T p[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
p[i][j]=m[i][j]+ob.m[i][j];
cout<<"\n";
T p[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
p[i][j]=m[i][j]-ob.m[i][j];
cout<<"\n";
}
void operator *(matrix ob)
T p[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
p[i][j]=0;
for(int k=0;k<c;k++)
p[i][j]+=(m[i][k] * ob.m[k][j]);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<"\n";
void transpose( )
T p[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
p[j][i]=m[i][j];
}for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<"\n";
void display( )
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cout<<"\n";
cout<<"\n\n";
};
int main( )
matrix<int> m1,m2;
int ch;
m1.get_value();
m2.get_value();
while(1)
system("cls");
cout<<"\n----------MATRIX OPERATIONS-----------\n\n";
cout<<"\n 1. Sum";
cout<<"\n 2. Difference";
cout<<"\n 3. Product";
cout<<"\n 4. Transpose";
cout<<"\n 5. Display";
cout<<"\n 0. EXIT\n";
cin>>ch;
switch(ch)
m1 + m2;
break;
case 2: cout<<"\n\n Matrices Subtraction \n\n";
m1-m2;
break;
m1*m2;
break;
m1.display( );
m1.transpose( );
m2.display( );
m2.transpose( );
break;
m1.display( );
m2.display( );
break;
case 0: exit(0);
system("pause");
};
}
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ SWAP TWO VALUES USING
TEMPLATE .
#include <iostream>
T temp;
temp = n1;
n1 = n2;
n2 = temp;
int main()
int i1 = 1, i2 = 2;
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 << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;
return 0;
OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO FIND THE LARGEST
NUMBER AMONGST THE GIVEN
NUMBER .
#include <iostream>
int main( )
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO MAKE A SIMPLE
CALCULATOR USING TEMPLATE .
#include <iostream>
class Calculator
private:
T num1, num2;
public:
num1 = n1;
num2 = n2;
void displayResult( )
cout << "Numbers are: " << num1 << " and " << num2 << "." <<endl;
}
T add() { return num1 + num2; }
};
int main( )
intCalc.displayResult( );
floatCalc.displayResult( );
return 0;
OUTPUT
EXCEPTION HANDLING
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO CHECK THE CORRECT
FORMAT OF PASSWORD USING
TRY AND CATCH STATEMENT .
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
int main( )
char uname[50];
char pass[20];
gets(uname);
gets(pass);
try
if(strlen(pass)<6)
{
cout<<"\n Password must be at least 6 Characters Long..."<<endl;
throw 'c';
bool digit_yes=false;
bool valid;
if (isdigit(pass[count]))
digit_yes=true;
if (!digit_yes)
valid=false;
cout <<"\n Password must have at least One Digit (0-9)"<< endl;
throw 'c';
else
valid=true;
catch(char c)
catch(...)
return 0;
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO TO PERFORM
ARITHMETIC OPERATIONS ON
TWO NUMBERS AND THROW AN
EXCEPTION IF THE DIVIDEND IS
ZERO OR DOES NOT CONTAIN AN
OPERATOR .
#include<iostream>
#include<string>
int main( )
char Operator;
cout<<"\n --------------------------------------------";
try
cin>>num1;
if(num1==0)
throw 0;
cin>>Operator;
throw Operator;
cin>>num2;
cout<<"\n --------------------------------------------";
switch(Operator)
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
if(num2 == 0)
throw 0;
cout<<"\n Answer : "<<num1<<" "<<Operator<<" "<<num2<<" = "<<ans;
catch(const char c)
catch(const int n)
return 0;
OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW SIMPLE TRY,
THROW, CATCH STATEMENT .
#include <iostream>
return (a/b); }
int main ( ) {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
return 0;
OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW MULTIPLE
CATCH STATEMENTS .
#include <iostream>
int main( )
int choice;
try
cin>>choice;
catch(int a)
}
catch(char b)
catch(float c)
return 0;
OUTPUT
QUESTION 5 :- WRITE A PROGRAM
IN ‘C++’ TO CATCH ALL
EXCEPTIONS .
#include <iostream>
#include<conio.h>
int main( ) {
int var = 0;
cout << "Simple C++ Program for Catch All or Default Exception Handling\n";
try {
if (var == 0) {
throw var; }
catch (float ex) { cout << "Float Exception catch : Value :" << ex; }
getch( );
return 0;
OUTPUT
QUESTION 6 :- WRITE A PROGRAM
IN ‘C++’ TO RETHROW AN
EXCEPTION .
#include <iostream>
#include<conio.h>
void exceptionFunction( ) {
try { throw 0;
} catch (int i) { cout << "\nIn Function : Wrong Input :" << i;
throw; } }
int main( ) {
int var = 0;
try { exceptionFunction( ); }
catch (int ex) { cout << "\nIn Main : Wrong Input :" << ex; }
getch( );
return 0;
OUTPUT
EXEPTION HANDLING
QUIZ
QUESTION 01
#include <iostream>
using namespace std;
int main( )
{
int x = -1;
try {
cout << "Inside try n";
if (x < 0)
{
throw x;
cout << "After throw n";
}
}
catch (int x ) {
cout << "Exception Caught n";
}
➢ Inside try
Exception Caught
After throw
After catch
➢ Inside try
Exception Caught
After catch
➢ Inside try
Exception Caught
Inside try
➢ After throw
After catch
QUESTION 02
What is the advantage of exception handling?
1) Remove error-handling code from the software's main line of code.
2) A method writer can chose to handle certain exceptions and
delegate others to the caller.
3) An exception that occurs in a function can be handled anywhere in
the function call stack.
➢ Only 1
➢ 1, 2 and 3
➢ 1 and 3
➢ 1 and 2
QUESTION 03
What should be put in a try block?
1. Statements that might cause exceptions
2. Statements that should be skipped in case of an exception
➢ Only 1
➢ Only 2
➢ Both 1 and 2
QUESTION 04
Output of following program
#include<iostream>
int main()
Derived d;
try {
throw d;
catch(Base b) {
catch(Derived d) {
return 0;
➢ Compiler Error
QUESTION 05
#include <iostream>
using namespace std;
int main()
{
try
{
throw 'a';
}
catch (int param)
{
cout << "int exceptionn";
}
catch (...)
{
cout << "default exceptionn";
}
cout << "After Exception";
return 0;
}
➢ default exception
After Exception
➢ int exception
After Exception
➢ int exception
➢ default exception
QUESTION 06
#include <iostream>
using namespace std;
int main()
{
try
{
throw 10;
}
catch (...)
{
cout << "default exceptionn";
}
catch (int param)
{
cout << "int exceptionn";
}
return 0;
}
➢ default exception
➢ int exception
➢ Compiler Error
QUESTION 07
#include <iostream>
using namespace std;
int main()
{
try
{
try
{
throw 20;
}
catch (int n)
{
cout << "Inner Catchn";
throw;
}
}
catch (int x)
{
cout << "Outer Catchn";
}
return 0;
}
➢ Outer Catch
➢ Inner Catch
➢ Inner Catch
Outer Catch
➢ Compiler Error
QUESTION 08
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "Constructing an object of Test " << endl; }
~Test() { cout << "Destructing an object of Test " << endl; }
};
int main() {
try {
Test t1;
throw 10;
} catch(int i) {
cout << "Caught " << i << endl;
}
}
➢ Caught 10
Caught 10
Caught 10
➢ Compiler Errror
QUESTION 09
#include <iostream>
using namespace std;
class Test {
static int count;
int id;
public:
Test() {
count++;
id = count;
cout << "Constructing object number " << id << endl;
if(id == 4)
throw 4;
}
~Test() { cout << "Destructing object number " << id << endl; }
};
int Test::count = 0;
int main() {
try {
Test array[5];
} catch(int i) {
cout << "Caught " << i << endl;
}
}
Java.
➢ 1 and 3
➢ 1, 2 and 3
➢ 1 and 2
➢ 2 and 3
STRING MANIPULATION
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO CONCAT TWO
STRINGS.
#include <string>
#include <iostream>
int main() {
fullname += '.';
return 0;
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
IMPLEMENTATION OF STRING
FUNCTIONS .
#include <iostream>
#include <string>
int main( )
string str;
getline(cin,str);
else
str1.append(".COM");
str.clear( );
return 0;
OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO DELETE A WORD FROM
A STRING .
#include<iostream>
#include<string.h>
#include<stdio.h>
int main( )
int i, j = 0, k = 0;
gets(str);
{ if (str[i]==' ') {
str1[k][j] = '\0';
k++;
j=0;
else {
str1[k][j]=str[i];
j++;
str1[k][j] = '\0';
cin>>word;
if (strcmp(str1[i], word) == 0) {
k--; }
{ cout<<" ";
cout<<str1[i]<<" "; }
return 0;
OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO FIND NO. OF VOWELS
AND CONSONANTS IN A STRING.
#include <iostream>
int main( )
char str[100];
cin.getline(str, 150);
str[i]=='U')
vowelCounter++;
}
consonantCounter++;
return 0;
OUTPUT
QUESTION 5 :- WRITE A PROGRAM
IN ‘C++’ TO REMOVE DUPLICATE
CHARACTERS FROM A STRING .
#include <iostream>
#include <string>
#include <algorithm>
if(currChar == str[j])
return str;
}
int main( )
std::string str;
std::getline(std::cin, str);
str = removeDuplicate(str);
OUTPUT