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

ASSIGNMENT NAME OF THE DATE OF THE

NUMBER ASSIGNMENT ASSIGNMENT

01. OPERATOR OVERLOADING 01-04-2020


AND INHERITANCE
MCQs .
02. WAP TO DEMONSTRATE 04-04-2020
OPERATOR
OVERLOADING .
03. INHERITANCE . 07-04-2020

04. TYPE CONVERSION . 09-04-2020

05. WAP TO SHOW THE 11-04-2020


IMPLEMENTATION OF VIRTUAL
BASE CLASS .
06. WAP TO MAINTAIN EMPLOYEES 13-04-2020
DATABASE USING
INHERITANCE .
07. WAP TO SHOW THE 16-04-2020
IMPLEMENTATION OF VIRTUAL
FUNCTIONS .
08. WAP TO SHOW FUNCTION 18-04-2020
OVERLOADING AND FUNCTION
OVERRIDDING .
09. GENERIC PROGRAMMING USING 20-04-2020
TEMPLATE .

10. EXCEPTION 22-04-2020


HANDLING .

11. STRING 24-04-2020


MANIPULATION .
OPERATOR OVERLOADING
AND INHERITANCE MCQ’S
OPERATOR OVERLOADING
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO ADD TWO DISTANCES
USING OPERATOR OVERLOADING.
#include<iostream>

using namespace std;

class Distance

private:

int feet,inches;

public:

void readDistance(void)

cout << "Enter feet: ";

cin >>feet;

cout << "Enter inches: ";

cin >>inches;

void dispDistance(void)

cout << "Feet:" << feet << "\t" << "Inches:" << inches << endl;

}
Distance operator+(Distance &dist1)

Distance tempD;

tempD.inches= inches + dist1.inches;

tempD.feet = feet + dist1.feet + (tempD.inches/12);

tempD.inches=tempD.inches%12;

return tempD;

};

int main( )

Distance D1,D2,D3;

cout << "Enter first distance:" << endl;

D1.readDistance( );

cout << endl;

cout << "Enter second distance:" << endl;

D2.readDistance( );

cout << endl;

D3=D1+D2;

cout << "Total Distance:" << endl;

D3.dispDistance( );

cout << endl;

return 0;

}
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO ADD TWO MATRICES
USING OPERATOR OVERLOADING.

#include<iostream>

using namespace std;

class Matrix

int a[3][3];

public:

void accept( );

void display( );

void operator +(Matrix x);

};

void Matrix::accept( )

cout<<"\n Enter Matrix Element (3 X 3) : \n";

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

cout<<" ";

cin>>a[i][j];

}
}

void Matrix::display( )

for(int i=0; i<3; i++)

cout<<" ";

for(int j=0; j<3; j++)

cout<<a[i][j]<<"\t";

cout<<"\n";

void Matrix::operator +(Matrix x)

int mat[3][3];

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

mat[i][j]=a[i][j]+x.a[i][j];

}
cout<<"\n Addition of Matrix : \n\n";

for(int i=0; i<3; i++)

cout<<" ";

for(int j=0; j<3; j++)

cout<<mat[i][j]<<"\t";

cout<<"\n";

int main( )

Matrix m,n;

m.accept( );

n.accept( );

cout<<"\n First Matrix : \n\n";

m.display( );

cout<<"\n Second Matrix : \n\n";

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>

using namespace std;

class Complex{

public:

float real;

float img;

Complex( ){

real = img =0;

Complex(float r, float i){

real = r;

img = i;

Complex operator+(const Complex &obj){

Complex temp;

temp.img = this->img + obj.img;

temp.real = this->real + obj.real;

return temp;

Complex operator-(const Complex &obj){


Complex temp;

temp.img = this->img - obj.img;

temp.real = this->real - obj.real;

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;

cin >> a.real;

cin >> a.img;

cout << "Enter real and complex coefficient of Second Complex number"<<
endl;

cin >> b.real;

cin >> b.img;

cout << "Adding Complex Numbers" << endl;

c = a+b;

c.display( );

cout << "Subtracting Complex Numbers" << endl;

c = a-b;

c.display( );

return 0;

}
OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO INCREMENT A DATE
USING OPERATOR OVERLOADING.
#include <iostream>

using namespace std;

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;

if(minutes >= 60) {


++hours;

minutes -= 60;

return Time(hours, minutes);

Time operator++( int ) {

Time T(hours, minutes);

++minutes;

if(minutes >= 60) {

++hours;

minutes -= 60;

return T;

};

int main( ) {

Time T1(11, 59), T2(10,40);

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

using namespace std;

class my_string{

private:

char str[30];

public:

void getdata( );

void display( );

void operator== (my_string str1);

int operator= (my_string str1);

void operator+ (my_string str1);

void operator<< (my_string str1);

void operator>> (my_string str1);

int operator/ (my_string str1);

void palindrome( );

};

void my_string::getdata( )

cout<<"\nEnter the string : ";

cin>>str;

}
void my_string::display( )

cout<<"\n"<<str;

void my_string::operator== (my_string str1)

strcpy(str1.str,str);

cout<<"\n\tCopied String is : "<<str1.str;

int my_string::operator= (my_string str1)

if(strcmp(str,str1.str)==0)

return 1;

return 0;

void my_string::operator+ (my_string str1)

strcat(str,str1.str);

cout<<"\n\t--String After Concat is : "<<str;

void my_string::operator<< (my_string str1)

cout<<"\n\t--The string you entered is :"<<str1.str;

void my_string::operator>> (my_string str1)

int i;
cout<<"\n\t--The string after reversing is : ";

for(i=strlen(str1.str);i>=0;i--)

cout<<str1.str[i];

int my_string::operator/ (my_string str1)

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;

while(opt1==1 && opt!=8)

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

cout<<"\n\t8.Exit\n\t\t--Enter your choice-->";

cin>>opt;

switch(opt)

case 1:

cout<<"\nEnter the 1st string-\n";

a.getdata();

cout<<"\nEnter the 2nd string-\n";

b.getdata();

c=a=b;

if(c==1)

cout<<"\n\t---Strings are Equal---\n";

else

cout<<"\n\t---Strings are not Equal---\n";

break;

case 2:

a.getdata( );

a==b;

break;

case 3:

cout<<"\nEnter the 1st string-\n";

a.getdata( );

cout<<"\nEnter the 2nd string-\n";

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:

cout<<"\nEnter the Main string-\n";

a.getdata( );

cout<<"\nEnter the other string-\n";

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;

default: cout<<"Invalid choice..try again\n";


}

if(opt!=8){

cout<<"\n\n\tDo you want to continue(Press 1 to continue)";

cin>>opt1;}

return 0;

OUTPUT
TYPE CONVERSION
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW TYPE
CONVERSION .
#include <iostream>

using namespace std;

class stock2 ;

class stock1{

int code , item ;

float price ;

public :

stock1 ( int a , int b , int c ) {

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 ;

};

stock2( int x , float y ) {

code = x ;

val = y ;

};

void disp ( ) {

cout << " code " << code << " \n " ;

cout << " total value Rs. " << val << " \n " ;

};

stock2( stock1 p ) {

code = p.getcode() ;

val = p.getitem() * p.getprice() ;

};

};

int main( )
{

stock1 i1 ( 101 , 10 ,125.0 ) ;

stock2 i2 ;

float tot_val = i1;

i2 = i1 ;

cout << " Stock Details : Stock 1 type " << " \n " ;

i1.disp ( );

cout << " Stock Value " << " - " ;

cout << tot_val << " \n " ;

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>

using namespace std;

const float MeterToFloat=3.280833;

class Distance

int feet;

float inches;

public:

Distance( )

feet=0;

inches=0.0;

Distance(int ft, float in)

feet=ft;

inches=in;

operator float( ) {

float feetinfractions=inches/12;

feetinfractions+=float(feet);

return (feetinfractions/MeterToFloat);
}

};

int main( )

int feet;

float inches;

cout <<"Enter distance in Feet and Inches.";

cout<<"\nFeet:";

cin>>feet;

cout<<"Inches:";

cin>>inches;

Distance dist(feet, inches);

float meters=dist;

cout<<"Converted Distance in Meters is: "<< meters;

return 0;

OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW TYPE
CONVERSION .
#include <iostream>

#include <iomanip>

using namespace std;

class Time

private:

int seconds;

int hh,mm,ss;

public:

void getTime(void);

void convertIntoSeconds(void);

void displayTime(void);

};

void Time::getTime(void)

cout << "Enter time:" << endl;

cout << "Hours? "; cin >> hh;

cout << "Minutes? "; cin >> mm;

cout << "Seconds? "; cin >> ss;

void Time::convertIntoSeconds(void)

{
seconds = hh*3600 + mm*60 + ss;

void Time::displayTime(void)

cout << "The time is = " << setw(2) << setfill('0') << hh << ":"

<< setw(2) << setfill('0') << mm << ":"

<< setw(2) << setfill('0') << ss << endl;

cout << "Time in total seconds: " << seconds;

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;

char name[20], des[20];

void get( ) {

cout << "Enter the employee number:";

cin>>eno;

cout << "Enter the employee name:";

cin>>name;

cout << "Enter the designation:";

cin>>des;

};

class salary : public emp {

float bp, hra, da, pf, np;

public:
void get1( ) {

cout << "Enter the basic pay:";

cin>>bp;

cout << "Enter the Humen Resource Allowance:";

cin>>hra;

cout << "Enter the Dearness Allowance :";

cin>>da;

cout << "Enter the Profitablity Fund:";

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

cout << "Enter the number of employee:";


cin>>n;

for (i = 0; i < n; i++) {

s[i].get( );

s[i].get1( );

s[i].calculate( );

cout << "\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";

for (i = 0; i < n; i++) {

s[i].display( );

getch();

OUTPUT
Enter the Roll no: 100

Enter two marks

90

80

Enter the Sports Mark: 90

Roll No: 100

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:

int rno, m1, m2;

public:

void get( ) {

cout << "Enter the Roll no :";

cin>>rno;

cout << "Enter the two marks :";

cin >> m1>>m2;

};

class sports {

protected:

int sm;

public:

void getsm( ) {
cout << "\nEnter the sports mark :";

cin>>sm;

};

class statement : public student, public sports {

int tot, avg;

public:

void display( ) {

tot = (m1 + m2 + sm);

avg = tot / 3;

cout << "\n\n\tRoll No : " << rno << "\n\tTotal : " << tot;

cout << "\n\tAverage : " << avg;

};

void main( ) {

clrscr( );

statement obj;

obj.get( );

obj.getsm( );

obj.display( );

getch( );

}
OUTPUT

Enter the Roll no: 100

Enter two marks

90

80

Enter the Sports Mark: 90

Roll No: 100

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>

using namespace std;

class Employee {

int eno;

char name[20], des[20];

public:

void getEmpDetails( ) {

cout << "\nEnter the Employee number:";

cin>>eno;

cout << "Enter the Employee name:";

cin>>name;

cout << "Enter the Employee designation:";

cin>>des;

void employee_display( ) {

cout <<"\nEmployee number:"<<eno;

cout <<"\nEmployee name:"<<name;

cout <<"\nEmployee designation:"<<des;

}
};

class Salary : private Employee {

float bp, hra, da, pf, np;

public:

void getPayDetails( ) {

getEmpDetails( );

cout << "Enter the Basic pay:";

cin>>bp;

cout << "Enter the Humen Resource Allowance:";

cin>>hra;

cout << "Enter the Dearness Allowance :";

cin>>da;

cout << "Enter the Profitablity Fund:";

cin>>pf;

calculate( );

void calculate( ) {

np = bp + hra + da - pf;

void salary_display( ) {

employee_display( );

cout <<"\nEmployee Basic pay:"<<bp;

cout <<"\nEmployee Humen Resource Allowance:"<<hra;

cout <<"\nEmployee Dearness Allowance:"<<da;

cout <<"\nEmployee Profitablity Fund:"<<pf;

cout <<"\nEmployee Net Pay:"<<np;

}
};

class BankCredit : private Salary {

char bank[20], ifsc_code[20];

int account_number;

public:

void getBankDetails( ) {

getPayDetails( );

cout << "Enter the Bank Name:";

cin>>bank;

cout << "Enter the IFSC:";

cin>>ifsc_code;

cout << "Enter the Account Number :";

cin>>account_number;

void display( ) {

salary_display( );

cout <<"\nEmployee Bank Name:"<<bank;

cout <<"\nEmployee IFSC:"<<ifsc_code;

cout <<"\nEmployee Account Number:"<<account_number<<endl;

};

int main( ) {

int i, n;

char ch;

BankCredit s[10];

cout << "Simple Multi Level Inheritance Example Program : Payroll System \n";

cout << "Enter the number of employee:";


cin>>n;

for (i = 0; i < n; i++) {

cout << "\nEmployee Details # "<<(i+1)<<" : ";

s[i].getBankDetails();

for (i = 0; i < n; i++) {

s[i].display( );

getch( );

return 0;

}
OUTPUT

Simple Multi Level Inheritance Example Program : Payroll System

Enter the number of employee:2

Employee Details # 1 :

Enter the Employee number:101

Enter the Employee name:MASTE

Enter the Employee designation:Er

Enter the Basic pay:26000

Enter the Humen Resource Allowance:1300

Enter the Dearness Allowance :1200

Enter the Profitablity Fund:500

Enter the Bank Name:SBI

Enter the IFSC:ISFC001

Enter the Account Number :10001

Employee Details # 2 :

Enter the Employee number:102

Enter the Employee name:FORGE

Enter the Employee designation:Dr

Enter the Basic pay:32000

Enter the Humen Resource Allowance:2000

Enter the Dearness Allowance :300

Enter the Profitablity Fund:500


Enter the Bank Name:CITI

Enter the IFSC:ISFC0004

Enter the Account Number :20001

Employee number:101

Employee name:MASTE

Employee designation:Er

Employee Basic pay:26000

Employee Humen Resource Allowance:1300

Employee Dearness Allowance:1200

Employee Profitablity Fund:500

Employee Net Pay:28000

Employee Bank Name:SBI

Employee IFSC:ISFC001

Employee Account Number:10001

Employee number:102

Employee name:FORGE

Employee designation:Dr

Employee Basic pay:32000

Employee Humen Resource Allowance:2000

Employee Dearness Allowance:300

Employee Profitablity Fund:500

Employee Net Pay:33800

Employee Bank Name:CITI

Employee IFSC:ISFC0004

Employee Account Number:20001


QUESTION 4 :- WRITE A PROGRAM IN
‘C++’ TO SHOW HIERARCHICAL
INHERITANCE.
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

using namespace std;

class Person {

int eno;

char name[20], des[20];

public:

void getPersonDetails( ) {

cout << "\nEnter the Person number:";

cin>>eno;

cout << "Enter the Person name:";

cin>>name;

cout << "Enter the Person designation:";

cin>>des;

void person_display( ) {

cout <<"\nPerson number:"<<eno;

cout <<"\nPerson name:"<<name;

cout <<"\nPerson designation:"<<des;

}
};

class Employee : private Person {

float bp, hra, da, pf, np;

public:

void getEmployeeDetails( ) {

getPersonDetails( );

cout << "Enter the Basic pay:";

cin>>bp;

cout << "Enter the Human Resource Allowance:";

cin>>hra;

cout << "Enter the Dearness Allowance :";

cin>>da;

cout << "Enter the Profitablity Fund:";

cin>>pf;

calculate( );

void calculate( ) {

np = bp + hra + da - pf;

void employee_display( ) {

person_display( );

cout <<"\nEmployee Basic pay:"<<bp;

cout <<"\nEmployee Humen Resource Allowance:"<<hra;

cout <<"\nEmployee Dearness Allowance:"<<da;

cout <<"\nEmployee Profitablity Fund:"<<pf;

cout <<"\nEmployee Net Pay:"<<np;

}
};

class Student : private Person {

char college[20], course[20];

public:

void getStudentDetails( ) {

getPersonDetails( );

cout << "Enter the Student college Name:";

cin>>college;

cout << "Enter the Student course Name:";

cin>>course;

void student_display( ) {

person_display( );

cout <<"\nStudent college Name:"<<college;

cout <<"\nStudent IFSC:"<<course<<endl;

};

int main( ) {

int i, n;

char ch;

Student s[10];

Employee e[10];

cout << "Simple Hierarchical Inheritance Example Program : Payroll System


\n";

cout << "Enter the number of Student:";

cin>>n;

for (i = 0; i < n; i++) {


cout << "\nStudent Details # "<<(i+1)<<" : ";

s[i].getStudentDetails();

for (i = 0; i < n; i++) {

s[i].student_display();

cout << "\n\nEnter the number of Employee:";

cin>>n;

for (i = 0; i < n; i++) {

cout << "\nEmployee Details # "<<(i+1)<<" : ";

e[i].getEmployeeDetails();

for (i = 0; i < n; i++) {

e[i].employee_display();

getch();

return 0;

}
OUTPUT

Simple Hierarchical Inheritance Example Program : Payroll System

Enter the number of Student:2

Student Details # 1 :

Enter the Person number:101

Enter the Person name:Booke

Enter the Person designation:10th

Enter the Student college Name:CGR

Enter the Student course Name:BSC

Student Details # 2 :

Enter the Person number:102

Enter the Person name:Moste

Enter the Person designation:12th

Enter the Student college Name:IIT

Enter the Student course Name:BE

Person number:101

Person name:Booke

Person designation:10th

Student college Name:CGR

Student IFSC:BSC
Person number:102

Person name:Moste

Person designation:12th

Student college Name:IIT

Student IFSC:BE

Enter the number of Employee:2

Employee Details # 1 :

Enter the Person number:201

Enter the Person name:ASHK

Enter the Person designation:Er

Enter the Basic pay:12000

Enter the Human Resource Allowance:1200

Enter the Dearness Allowance :1100

Enter the Profitablity Fund:900

Employee Details # 2 :

Enter the Person number:203

Enter the Person name:ROK

Enter the Person designation:Dr

Enter the Basic pay:13000

Enter the Human Resource Allowance:1300

Enter the Dearness Allowance :800

Enter the Profitablity Fund:700


Person number:201

Person name:ASHK

Person designation:Er

Employee Basic pay:12000

Employee Human Resource Allowance:1200

Employee Dearness Allowance:1100

Employee Profitablity Fund:900

Employee Net Pay:13400

Person number:203

Person name:ROK

Person designation:Dr

Employee Basic pay:13000

Employee Human Resource Allowance:1300

Employee Dearness Allowance:800

Employee Profitablity Fund:700

Employee Net Pay:14400


QUESTION 5 :- WRITE A PROGRAM IN
‘C++’ TO SHOW HYBRID INHERITANCE.

#include<iostream.h>

using namespace std;

int a,b,c,d,e;

class A

protected:

public:

void getab( )

cout<<"\nEnter a and b value:";

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;

};

class D:public B,public C

protected:

public:

void result( )

getab( );

getc( );

getd( );

e=a+b+c+d;

cout<<"\n Addition is :"<<e;

};

int main( )

D d1;

d1.result( );
return 0;

OUTPUT

Total Objects: 1

Enter a and b value: 5 10

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 .

VIRTUAL BASE CLASS VIRTUAL BASE CLASS


STUDENT

EXAM SPORTS

RESULT

#include<iostream.h>

using namespace std;

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

cout<<"\nEnter department name:";

cin.getline(dept_name,20);

cout<<"\nEnter college name:";

cin.getline(college,20);

cout<<"\nEnter date of birth of this student(in DD/MM/YY format):";

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;

class exam:virtual public student

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;

cout<<"Invalid marks given!!Enter proper marks..\n";

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

class sports:virtual public student

protected:

char p_g;

public:

void get_grade( );

void print_grade( );

};

void sports::get_grade( )

while(true)

cout<<"\nEnter performance grade in sports for this student(A/B/C/D):";

cin>>p_g;

if(p_g=='A'||p_g=='B'||p_g=='C'||p_g=='D')

break;

cout<<"\nInvalid grade entered!!!";

void sports::print_grade( )

cout<<"\n\nPerformance grade in sports:"<<p_g;

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

cout<<"\n\nAll total marks of "<<name<<":"<<grand_total<<"\n";

int main( )

int num,i;

while(true)

cout<<"Enter number of students:";

cin>>num;

if(num>0)

break;

cout<<"Invalid number given!!\n";

result *stu=new result[num];

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

cout<<"\n\nEnter record for student no:"<<i+1<<"\n\n-------------------------\n---


----------------------";

stu[i].get_info();

cout<<"\n\nDisplaying results of students \n----------------------------------------


----------------\n";

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 record for student no:1

-------------------------

-------------------------

Enter roll-no:122

Enter name:Rohit Sharma

Enter department name:Mathematics

Enter college name:Scottish Church

Enter date of birth of this student(in DD/MM/YY format):07/05/1988

Enter Marks

-------------------

Subject1:55
Subject2:67

Subject3:84

Subject4:45

Subject5:90

Subject6:34

Enter performance grade in sports for this student(A/B/C/D):A

Enter record for student no:2

-------------------------

-------------------------

Enter roll-no:167

Enter name:Pritam Biswas

Enter department name:Computer Sc.

Enter college name:Vidyasagar

Enter date of birth of this student(in DD/MM/YY format):12/12/1989

Enter Marks
-------------------

Subject1:56

Subject2:67

Subject3:44

Subject4:37

Subject5:89

Subject6:90

Enter performance grade in sports for this student(A/B/C/D):D

Displaying results of students

--------------------------------------------------------

Record for student no:1

------------------------

------------------------

Roll:122
Name:Rohit Sharma

Date of birth:07/05/1988

Department:Mathematics

College:Scottish Church

Marks obtained in theory

---------------------------

Subject1: 55

Subject2: 67

Subject3: 84

Subject4: 45

Subject5: 90

Subject6: 34

Performance grade in sports:A

All total marks of Rohit Sharma:425

Record for student no:2

------------------------

------------------------
Roll:167

Name:Pritam Biswas

Date of birth:12/12/1989

Department:Computer Sc.

College:Vidyasagar

Marks obtained in theory

---------------------------

Subject1: 56

Subject2: 67

Subject3: 44

Subject4: 37

Subject5: 89

Subject6: 90

Performance grade in sports:D

All total marks of Pritam Biswas:403


EMPLOYEES DATABASE
QUESTION :- WRITE A PROGRAM IN
‘C++’ TO MAINTAIN EMPLOYEES
DATABASE USING INHERITANCE .

#include <iostream.h>

#include <stdlib.h>

using namespace std;

class person

protected:

char name[20];

int code;

public:

void getdetail(void)

cout<<"\n\nEnter name :- ";

cin>>name;

cout<<"\nEnter code :- ";

cin>>code;

void dispdetail(void)

cout<<"\n\nNAME :- "<<name;

cout<<"\nCODE :- "<<code;
}

};

class account : virtual public person

protected:

float pay;

public:

void getpay(void)

cout<<"\nEnter Pay amount :- ";

cin>>pay;

void dispay(void)

cout<<"\nPAY :- "<<pay;

};

class admin : virtual public person

protected:

int experience;

public:

void getexpr(void)

cout<<"\nEnter Experience in yrs :- ";

cin>>experience;

}
void dispexpr(void)

cout<<"\nEXPERIENCE:- "<<experience;

};

class master : public account, public admin

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<<"\nChoose detail you want to update\n";

cout<<"1) NAME\n";
cout<<"2) CODE\n";

cout<<"3) EXPERIENCE\n";

cout<<"4) PAY\n";

cout<<"Enter your choice:- ";

int choice;

cin>>choice;

switch(choice)

case 1 : cout<<"\n\nEnter name : - ";

cin>>name;

break;

case 2 : cout<<"\n\nEnter code :- ";

cin>>code;

break;

case 3 : cout<<"\n\nEnter pay :- ";

cin>>pay;

break;

case 4 : cout<<"\n\nEnter Expereince :- ";

cin>>experience;

break;

default: cout<<"\n\nInvalid choice\n\n";

};

int main( )

{
master ob1;

int choice;

while(1)

cout<<"\n\n=====EMPLOYE DATABASE=====\n\n";

cout<<"\nChoose Operation you want to perform\n";

cout<<"1) Create Record\n";

cout<<"2) Display Record\n";

cout<<"3) Update Record\n";

cout<<"4) Exit\n";

cout<<"\nEnter your choice:- ";

cin>>choice;

switch(choice)

case 1 : ob1.create( );

break;

case 2 : ob1.display( );

break;

case 3 : ob1.update( );

break;

case 4 : exit(1);

default : cout<<"\n\nInvalid Choice\nTry Again\n\n";

return 0;

}
OUTPUT
=====EMPLOYE DATABASE=====

Choose Operation you want to perform

1) Create Record

2) Display Record

3) Update Record

4) Exit

Enter your choice:- 1

=====GETDATA IN=====

Enter name :- Codez

Enter code :- 01

Enter Pay amount :- 40000

Enter Experience in yrs :- 3

=====EMPLOYE DATABASE=====

Choose Operation you want to perform

1) Create Record

2) Display Record

3) Update Record

4) Exit

Enter your choice:- 2


=====DISPLAY DETAILS=====

NAME :- Codez

CODE :- 1

PAY :- 40000

EXPERIENCE:- 3

=====EMPLOYE DATABASE=====

Choose Operation you want to perform

1) Create Record

2) Display Record

3) Update Record

4) Exit

Enter your choice:- 3

=====UPDATE DETAILS=====

Choose detail you want to update

1) NAME

2) CODE

3) EXPERIENCE

4) PAY

Enter your choice:- 1

Enter name : - CodezClub


=====EMPLOYE DATABASE=====

Choose Operation you want to perform

1) Create Record

2) Display Record

3) Update Record

4) Exit

Enter your choice:- 2

=====DISPLAY DETAILS=====

NAME :- CodezClub

CODE :- 1

PAY :- 40000

EXPERIENCE:- 3

=====EMPLOYE DATABASE=====

Choose Operation you want to perform

1) Create Record

2) Display Record

3) Update Record

4) Exit

Enter your choice:- 4

Process returned 1
VIRTUAL FUNCTION
QUESTION 1 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
IMPLEMENTATION OF PURE
VIRTUAL FUNCTION .
#include <iostream>

using namespace std;

class Employee

{ virtual int getSalary( ) = 0;

};

class Employee_1: public Employee{

int salary;

public:

Employee_1(int s){

salary = s;

int getSalary( ){

return salary;

};

class Employee_2: public Employee

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

cout << "Salary of Developer : " << a << endl;

cout << "Salary of Driver : " << b << endl;

return 0;

OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
IMPLEMENTATION OF VIRTUAL
FUNCTION .
#include <iostream>

using namespace std;

class Weapon {

public:

virtual void features() { cout << "Loading weapon features.\n"; }

};

class Bomb : public Weapon {

public:

void features( ) {

this->Weapon::features( );

cout << "Loading bomb features.\n";

};

class Gun : public Weapon {

public:

void features( ) {

this->Weapon::features( );

cout << "Loading gun features.\n";

};
class Loader {

public:

void loadFeatures(Weapon *weapon) {

weapon->features();

};

int main( ) {

Loader *l = new Loader;

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>

using namespace std;

class Rectangle

public:

void printArea(int x, int y)

cout << x * y << endl;

void printArea(int x)

cout << x * x << endl;

void printArea(int x, double y)

cout << x * y << endl;

void printArea(double x)

cout << x * x << endl;

}
};

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>

using namespace std;

class Animals{

public:

void sound( )

cout << "This is parent class" << endl;

};

class Dogs : public Animals{

public:

void sound( )

cout << "Dogs bark" << endl;

};

class Cats : public Animals{

public:

void sound( )

cout << "Cats meow" << endl;


}

};

class Pigs : public Animals{

public:

void sound( )

cout << "Pigs snort" << endl;

};

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>

using namespace std;

const int r=2,c=2;

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

cout<<"\n M["<<i<<"]["<<j<<"] = ";

cin>>m[i][j];

}
}

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]=m[i][j]+ob.m[i][j];

cout<<" "<<p[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]=m[i][j]-ob.m[i][j];

cout<<" "<<p[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<<" "<<p[i][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<<" "<<p[i][j]<<" ";

cout<<"\n";

void display( )

for(int i=0;i<r;i++)

for(int j=0;j<c;j++)

cout<<" "<<m[i][j]<<" ";

cout<<"\n";

cout<<"\n\n";

};
int main( )

matrix<int> m1,m2;

int ch;

cout<<"\n Enter Elements of Matrix A\n";

m1.get_value();

cout<<"\n Enter Elements of Matrix B\n";

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

cout<<"\n Enter your choice: ";

cin>>ch;

switch(ch)

case 1: cout<<"\n\n Matrices Sum \n\n";

m1 + m2;

break;
case 2: cout<<"\n\n Matrices Subtraction \n\n";

m1-m2;

break;

case 3: cout<<"\n\n Matrices Product \n\n";

m1*m2;

break;

case 4: cout<<"\n\n MATRIX A\n";

m1.display( );

cout<<"\n\n Transposed Matrix\n";

m1.transpose( );

cout<<"\n\n MATRIX B\n";

m2.display( );

cout<<"\n\n Transposed Matrix\n";

m2.transpose( );

break;

case 5: cout<<"\n\n MATRIX A\n";

m1.display( );

cout<<"\n\n MATRIX B\n";

m2.display( );

break;

case 0: exit(0);

default: cout<<"\n\n Invalid choice";

system("pause");

};

}
OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ SWAP TWO VALUES USING
TEMPLATE .
#include <iostream>

using namespace std;

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
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO FIND THE LARGEST
NUMBER AMONGST THE GIVEN
NUMBER .

#include <iostream>

using namespace std;

template <class T>

T Large(T n1, T n2)

return (n1 > n2) ? n1 : n2;

int main( )

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

return 0;

OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO MAKE A SIMPLE
CALCULATOR USING TEMPLATE .
#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
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>

using namespace std;

int main( )

char uname[50];

char pass[20];

cout<<"\n Enter User Name : ";

gets(uname);

cout<<"\n Enter Password : ";

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;

int len = strlen(pass);

for (int count = 0; count < len; count++)

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;

cout<<"\n Password is Correct";

catch(char c)

cout<<"\n Invalid Password Format!!!";


}

catch(...)

cout<<"\n Default Exception";

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>

using namespace std;

int main( )

float num1, num2, ans;

char Operator;

cout<<"\n Perform Arithmetic Operations on Two Numbers";

cout<<"\n --------------------------------------------";

try

cout<<"\n Enter First Number : ";

cin>>num1;
if(num1==0)

throw 0;

cout<<"\n Enter Operator : ";

cin>>Operator;

if(Operator != '+' && Operator != '-' &&

Operator != '*' && Operator != '/')

throw Operator;

cout<<"\n Enter Second Number : ";

cin>>num2;

cout<<"\n --------------------------------------------";

switch(Operator)

case '+':

ans = num1 + num2;

break;

case '-':

ans = num1 - num2;

break;

case '*':

ans = num1 * num2;

break;

case '/':

ans = num1 / num2;

break;

if(num2 == 0)

throw 0;
cout<<"\n Answer : "<<num1<<" "<<Operator<<" "<<num2<<" = "<<ans;

catch(const char c)

cout<<"\n Exception Caught... \n Bad Operator : "<<c<<" is not a Valid


Operator";

catch(const int n)

cout<<"\n Error : Bad Operation...";

return 0;

OUTPUT
QUESTION 3 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW SIMPLE TRY,
THROW, CATCH STATEMENT .
#include <iostream>

using namespace std;

double division(int a, int b) {

if( b == 0 ) { throw "Division by zero condition!"; }

return (a/b); }

int main ( ) {

int x = 50;

int y = 0;

double z = 0;

try {

z = division(x, y);

cout << z << endl;

} catch (const char* msg)

{ cerr << msg << endl; }

return 0;

OUTPUT
QUESTION 4 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW MULTIPLE
CATCH STATEMENTS .
#include <iostream>

using namespace std;

int main( )

int choice;

try

cout<<"Enter any choice: ";

cin>>choice;

if(choice == 0) cout<<"Hello Baby!"<<endl;

else if(choice == 1) throw (100);

else if(choice == 2) throw ('x');

else if(choice == 3) throw (1.23f);

else cout<<"Bye Bye !!!"<<endl;

catch(int a)

cout<<"Integer Exception Block, value is: "<<a<<endl;

}
catch(char b)

cout<<"Character Exception Block, value is: "<<b<<endl;

catch(float c)

cout<<"Float Exception Block, value is: "<<c<<endl;

return 0;

OUTPUT
QUESTION 5 :- WRITE A PROGRAM
IN ‘C++’ TO CATCH ALL
EXCEPTIONS .
#include <iostream>

#include<conio.h>

using namespace std;

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

catch (...) { cout << "Default Exception Catch"; }

getch( );

return 0;

OUTPUT
QUESTION 6 :- WRITE A PROGRAM
IN ‘C++’ TO RETHROW AN
EXCEPTION .
#include <iostream>

#include<conio.h>

using namespace std;

void exceptionFunction( ) {

try { throw 0;

} catch (int i) { cout << "\nIn Function : Wrong Input :" << i;

throw; } }

int main( ) {

int var = 0;

cout << "Simple C++ Program for Rethrowing Exception Handling : In


Function\n";

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

cout << "After catch n";


return 0;
}

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

using namespace std;

class Base {};

class Derived: public Base {};

int main()

Derived d;

try {

throw d;

catch(Base b) {

cout<<"Caught Base Exception";

catch(Derived d) {

cout<<"Caught Derived Exception";

return 0;

➢ Caught Derived Exception

➢ Caught Base Exception

➢ 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

➢ Constructing an object of Test

Caught 10

➢ Constructing an object of Test

Destructing an object of Test

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

➢ Constructing object number 1


Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 1
Destructing object number 2
Destructing object number 3
Destructing object number 4
Caught
➢ Constructing object number 1
Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 3
Destructing object number 2
Destructing object number 1
Caught 4

➢ Constructing object number 1


Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 4
Destructing object number 3
Destructing object number 2
Destructing object number 1
Caught 4

➢ Constructing object number 1


Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 1
Destructing object number 2
Destructing object number 3
Caught 4
QUESTION 10

Which of the following is true about


exception handling in C++?

1) There is a standard exception class like Exception class in

Java.

2) All exceptions are unchecked in C++, i.e., compiler doesn't

check if the exceptions are caught or not.

3) In C++, a function can specify the list of exceptions that

it can throw using comma separated list like following.

void fun(int a, char b) throw (Exception1, Exception2, ..)

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

using namespace std;

int main() {

string firstname = "MANOJ";

string lastname = " KUMAR";

string fullname = firstname + lastname;

fullname += ", JR";

fullname += '.';

cout << firstname << lastname << endl;

cout << fullname << endl;

return 0;

OUTPUT
QUESTION 2 :- WRITE A PROGRAM
IN ‘C++’ TO SHOW
IMPLEMENTATION OF STRING
FUNCTIONS .
#include <iostream>

#include <string>

using namespace std;

int main( )

string str;

cout<<"Input the string\n";

getline(cin,str);

cout<<"\nString entered is :"<<str;

int len = str.length();

cout<<"\nLength of the string str is :"<<len;

string str1 = "WWW.GOOGLE";


if(str.compare(str1) == 0)

cout<<"\nTwo strings are equal\n";

else

cout<<"\nTwo strings are not equal\n";}

str1.append(".COM");

cout<<"\nNew str1 : "<<str1;

str.clear( );

cout<<"\n\nstr new length : "<<str.length( );

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>

using namespace std;

int main( )

int i, j = 0, k = 0;

char str[100], str1[10][20], word[20];

cout<<"\n Enter String : ";

gets(str);

for (i=0; str[i]!='\0'; i++)

{ if (str[i]==' ') {

str1[k][j] = '\0';

k++;

j=0;

else {

str1[k][j]=str[i];
j++;

str1[k][j] = '\0';

cout<<"\n Which Word You Want to Delete? : ";

cin>>word;

for (i=0; i<k+1; i++) {

if (strcmp(str1[i], word) == 0) {

for (j=i; j<k+1; j++) {

strcpy(str1[j], str1[j + 1]);

k--; }

cout<<"\n New String After Deleting the Word : \n\n";

for (i=0; i<k+1; i++)

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

using namespace std;

int main( )

char str[100];

int vowelCounter = 0, consonantCounter = 0;

cout << "Enter any string: ";

cin.getline(str, 150);

for(int i = 0; str[i]!='\0'; i++)

if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||

str[i]=='o' || str[i]=='u' || str[i]=='A' ||

str[i]=='E' || str[i]=='I' || str[i]=='O' ||

str[i]=='U')

vowelCounter++;
}

else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))

consonantCounter++;

cout << "Vowels in String: " << vowelCounter << endl;

cout << "Consonants in String: " << consonantCounter << endl;

return 0;

OUTPUT
QUESTION 5 :- WRITE A PROGRAM
IN ‘C++’ TO REMOVE DUPLICATE
CHARACTERS FROM A STRING .

#include <iostream>

#include <string>

#include <algorithm>

std::string & removeDuplicate(std::string& str)

int length = str.length( );

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

char currChar = str[i];

for(unsigned int j = i+1; j < length; j++)

if(currChar == str[j])

str.erase (std::remove(str.begin()+j, str.end(), str[j]), str.end());

return str;
}

int main( )

std::string str;

std::cout << "Enter string \n";

std::getline(std::cin, str);

str = removeDuplicate(str);

std::cout <<"New String is " << str << "\n";

OUTPUT

You might also like