Pds - Manual 1 To 11

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 49

1.

a)ADDITION OF TWO NUMBERS USING CLASS

Ai m:

To implement a simple C++ program for addition of two two numbers using class.

Algorithm:

Step 1: Start

Step 2:Create a class called add with data members as a, b and c.

Step 3:Declare and define read() to get two numbers.

Step 4: Declare and define display() to display the two numbers and sum value.

Step 5:Declare and define sum() to add two numbers.

Step6:In main() create object for class add.

Step7:Using the object to call the above functions.

Step 8:Stop.

Program

// Adding two number using class (C++)


#include<iostream.h>
#include<conio.h>
class add
{
private:
int a,b,c;
public:
void read()
{
cout<<"enter Two Number "<<endl;
cin>>a>>b;
}
void display()
{
cout<<" A = "<<a<<endl;
cout<<" B = "<<b<<endl;
cout<<"Sum = "<<c<<endl;
}
void sum()
{
c= a+b;
}
};
void main()
{
add x; // x is a object off add
clrscr();
x.read();
x.sum();
x.display();
getch();
}

Result:

Thus the above Simple C++ program for addition of two numbers using class was created
successfully and verified.

1.b)PROGRAM TO PRINT THE MINIMUM VALUE.

Aim:

To implement a simple C++ program to print the minimum value among the given set of
values.

Algorithm:

Step 1:Start

Step2:Read two Numbers.

Step3:check if (a<=b).
Step 4:Print the Minimum value is a.

Step 5:Else print Minimum value is b.

Step 6:Stop.

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter a and b";
cin>>a>>b;
if(a<=b)
{
cout<<"minimum value is "<<a;
}
else
{
cout<<"minimum value is "<<b;
}
getch();
}

Result:

Thus the above Simple C++ program to print the minimum value was created successfully and
verified.

1.c)PROGRAM TO DISPLAY NAME AND NUMBER.

Aim:
To implement a Simple C++ program to display the name and number.

Algorithm:

Step 1:Include the header files.

Step 2:Declare the class as person with data members and Number.

Step 3:Create an object for the class person.

Step 4:Get the name and number as input.

Step 5:Access the data member using object and display the output.

Step 6:Stop.

Program

#include<iostream.h>
class student
{
private:
char name[20];
int no;
public:
void input();
void display();
};
void student::input()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Reg no.:";
cin>>no;

}
void student::display()
{
cout<<"\nName:"<<name;
cout<<"\nRegno.:"<<no;
}
int main()
{
student s;
s.input();
s.display();
}

Result:

Thus the above simple C++ program to display the name and number was created successfully
and verified.

2.a) PARAMETERIZED CONSTRUCTOR

Aim:

To write a C++ program for Parameterized constructor.

Algorithm:

Step 1:Start.

Step 2:Create a class called Prime with data members as a,k,i.

Step 3:Declare the function Prime(int x),calculate(),show().

Step 4:Create object for the class Prime.

Step 5:Call the function prime(a) with argument as a.

Step 6:Print the result using show().

Step 7:stop.

Program

#include<iostream.h>
#include<conio.h>
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
void calculate()
{
k=1;
for(i=2;i<=a/2;i++){
if(a%2==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<"\n"<<a<<"\t"<<"Prime Number"<<endl;
else
cout<<"\n"<<a<<"\t"<<"Not prime"<<endl;
}
};
void main()
{
int a;
clrscr();
cout<<"Enter the number";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
}

Result:

Thus the above C++ program for Parameterized Constructor was created successfully and
verified.

2.b) DYNAMIC CONSTRUCTOR

Aim:

To write a C++ program for Dynamic Constructor.

Algorithm:

Step 1:start

Step 2:Include the header Files.

Step 3:Create a class called box.

Step 4:Deallocate the memory allocated to newbox

Step 5:Stop.

Program

#include<iostream.h>
#include<conio.h>
class box
{
public:
box(){
cout<<"Constructor called"<<endl;
}
~box(){
cout<<"Destructor called"<<endl;
}
};
int main()
{
clrscr();
box *myboxarray=new box[4];
delete[] myboxarray;
return 0;
}

Result:

Thus the above C++ program for Dynamic Constructor was created successfully and verified.

2.c)COPY CONSTRUCTOR.

Aim:

To write a C++ program to find factorial of a given number using Copy Constructor.

Algorithm:

Step 1:Include the header files.

Step 2: Declare the class name as copy with data members and member functions.

Step 3:The Constructor copy() with argument to assign the value.

Step 4:To call the function calculate() do the following steps.

Step 5:for i=1 to var do.

Step 6: Calculate fact*i to assign to fact.

Step 7: Increment the value as 1.

Step 8:Return the value fact.

Step 10:Print the result.

Program

#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int n;
cout<<"\n\tEnter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
getch();
}
Result:

Thus the above C++ program for to find factorial of a given number using Copy Constructor
was implemented successfully.

2.d) DESTRUCTOR

Aim:

To implement a C++ program to print student details using destructor.

Algorithm:

Step 1: Include the header file.

Step 2: Invoke The Classes

Step 3: Call The Read() Function.

Step 4: Get The Inputs Name ,Roll Number And Address

Step 5: Call The Display() Function

Step 6: Display The Name, Roll Number, And Address Of The Student

Step 7:Stop.

Program

#include<iostream.h>
#include<conio.h>
class stu
{
private:
char name[20],add[20];
int roll,zip;
public:
stu();
~stu();
void read();
void disp();
};
stu::stu(void)
{
cout<<"This is student details"<<endl;
}
void stu::read()
{
cout<<"Enter student name";
cin>>name;
cout<<"Enter student rollno";
cin>>roll;
cout<<"Enter student address";
cin>>add;
cout<<"Enter student zip code"<<endl;
cin>>zip;
}
void stu::disp()
{
cout<<"student name:"<<name<<"\n";
cout<<"student roll no:"<<roll<<"\n";
cout<<"student address:"<<add<<"\n";
cout<<"student zipcode:"<<zip<<"\n";
}
stu::~stu()
{
cout<<"Student details is closed";
}
void main()
{
stu s;
s.read();
s.disp();
getch();
}
Result:
Thus the above C++ program to print student details using destructor was created successfully
and verified.

3.a) FRIEND FUNCTION

Aim:

To implement a C++ program to find the mean value of a given number using friend function.

Algorithm:

Step 1: Include the header files


Step 2: Declare the class name as base with data members and member functions.
Step 3: The function get () is used to read the 2 inputs from the user.
Step 4: Declare the friend function mean (base ob) inside the class.
Step 5: Outside the class to define the friend function and do the following.
Step 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
Step 7:Stop.

Program

#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:"<<"\n";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\nMean value is:"<<mean(obj);
getch();
}

Result:

Thus the above C++ program to find the mean value of a given number using friend function was
created successfully and verified.

3.b) FRIEND CLASS

Aim:

To write a C++ program to find sum using Friend Class.

Algorithm:

Step 1:Start.

Step 2: Create a class named read int with data members as two floating point variables a and b.

Step 3:Define the member function named read() inside read int class that gets the input for the two
variables.

Step 4:Create a new class named sum with data members as float variables C.

Step 5: Now declare class sum as friend of class read int.

Step 6:Define member function named add() inside sum class that adds the data members of read int
class and stores the result in sum class.

Step 7:Create object for both classes inside main() as r and s for read int and sum.

Step 8: Call the r.read() and s.add() and display the result.
Program

#include<iostream.h>
#include<conio.h>
class readint
{
float a,b;
public:
void read()
{
cout<<"\nEnter a and b:"<<endl;
cin>>a>>b;
}
friend class sum;
};
class sum
{
public:
float c;
void add(readint rd)
{
c=rd.a+rd.b;
cout<<"Sum="<<c;
}
};
void main()
{
readint rd;
sum s;
clrscr();
rd.read();
s.add(rd);
getch();
}
Result:
Thus the above program for friend class was created successfully and verified.
4.a) SINGLE INHERITENCE

Aim:

To implement a C++ program to print the student details using single inheritance

Algorithm:

Step 1: Include the header files


Step 2: Declare the base class student.
Step 3: Define and declare the function getfun () to get the student details.
Step 4: Declare the derived class it
Step 5: Declare and define the function getdet () to get the student details.
Step 6: Define the function putdet () to find the details.
Step 7: Define the function display ().
Step 8: Create the derived class object.
Step 9: Read the number of students
Step 10: Call the function getfun(), getdet () and putdet () to each student
Step 11: Call the display ()

Program

#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[10],collname[10],dept[5];
void getfun()
{
cout<<"enter name ,college ,department\n";
cin>>name >>collname>>dept;
}
};
class it :public student {
public :
char sub1[5],sub2[5],sub3[5];
void getdet()
{
getfun();
cout<<"enter subject 1,2,3";
cin>>sub1>>sub2>>sub3;
}
void putdet()
{
cout<<"name\t"<<name<<"\n";
cout<<"college name\n"<<collname<< dept<<"subject1,2,3\t"<<sub1<<sub2<<sub3;
}
};
void main()
{
clrscr();
it obj;
obj.getdet();
obj.putdet();
getch();
}
Result:
Thus a C++ program was print the student details using single inheritance was implemented
successfully.

4b) MULTIPLE INHERITANCE


AIM: To implement a C++ program to find out the student details using multiple inheritance.
ALGORITHM:
Step 1: Include the header files
Step 2: Declare the base class student.
Step 3: Declare and define the function get () to get the student details.
Step 4: Declare the other class subject.
Step 5: Declare and define the function getsm() to read the subject mark
Step 6: Create the class statement derived from student and subject.
Step 7: Declare and define the function display () to find out the total .
Step 8: Declare the derived class object, call the functions get(),getsm() and display().

Program
#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; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
inttot,avg;
13
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();
}

RESULT:
Thus a C++ program to find out the student details using multiple inheritance is implemented
successfully.

4c) MULTILEVEL INHERITANCE


AIM: To implement a C++ program to find out the student details using multilevel inheritance.
ALGORITHM:
Step 1: Include the header files
Step 2: Declare the base class student.
Step 3: Declare and define the function get_ number ( ) and put_number( ).
Step 4: Declare the derived class test for student
Step 5: Declare and define the function get_ marks ( ) and put_marks( ).
Step 6: Declare the derived class result for test
Step 7: Declare and define the function display ( )
Step 8: Create objects for the derived class.
Step 9: Declare the derived class object, call the functions get_ number ( ), get_ marks ( ), display ( ).
Program
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get_number(int a)
{
roll = a;
}
void put_number()
{
cout<<"Roll Number: "<<roll<<"\n";
}
};
class test : public student
{
protected:
float sub1;
float sub2;
public:
void get_marks(float x,float y)
{
sub1 = x;
sub2 = y;
}
void put_marks()
{
cout<<"Marks in Subject 1 = "<<sub1<<"\n";
cout<<"Marks in Subject 2 = "<<sub2<<"\n";
}
};
class result : public test
{
private:
float total;
public:
void display()
{
total = sub1 + sub2;
put_number();
put_marks();
cout<<"Total = "<<total<<"\n";
}
};
void main()
{
clrscr();
result student;
student.get_number(83);
student.get_marks(99.0,98.5);
student.display();
getch();
}
RESULT:
Thus a C++ program to find out the student details using multilevel inheritance is implemented
successfully.

4 d) HIERARCHIAL INHERITANCE

AIM:

To write a c++ program to illustrate area of square and circle by hierarchial inheritance.

ALGORITHM:

Step1: start the program.

Step2: declare base class as base.

Step3: Declare and define virtual function show ().


Step4: Declare and define function display ().

Step5: create the derived class from base class.

Step6: create the base class object and the pointer variable.

Step7: call the functions display () and show () using the base class object and the pointer.

Step8: create the derived class object and call the function display () and show () using the derived
class object and pointer.

Program

#include<iostream.h>
#include<conio.h>
class shape
{
protected:
float l;
public:
void get_data()
{
cin>>l;
}
virtual float area()=0;
};
class square:public shape
{
public:
float area()
{
return l*l;
}
};
class circle:public shape
{
public:
float area()
{
return 3.14*l*l;
}
};
int main()
{
clrscr();
square s;
circle c;
cout<<"Enter length to calculate area of square:"<<endl;
s.get_data();
cout<<"Area of square:"<<s.area()<<endl;
cout<<"Enter radius to calculate the area of circle:";
c.get_data();
cout<<"Area of circle is:"<< c.area();
getch();
return 0;
}
RESULT:
Thus a C++ program to find out the area of the circle and square using hierarchical inheritance
was implemented successfully.

5) FUNCTION OVERLOADING

AIM:

To write a c++ program to find area of circle,rectangle,triangle by fucntion overloading.

ALGORITHM:
STEP1: start the program.

STEP2: Include the header files.

STEP3: Declare class name as function with data members and member functions.

STEP4: Read choice from the user.

STEP5: choice =1 then go the step 5.

STEP6: The function area() to find area of circle with one integer argument.

STEP7: Choice = 2 then go to step 7.

STEP8: The function area() to find area of triangle with three arguments,two as integer and one as
float.

STEP9: choice=4 then exit.

STEP 10: stop.

PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(inta,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,inta,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
intch;
inta,b,r;

23
clrscr();
fnobj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
cout<<”Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
SAMPLE OUTPUT:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4

RESULT:
Thus a C++ program to calculate the area of circle, rectangle and triangle using function overloading is
implemented successfully.
6.) VIRTUAL FUNCTION.

AIM:

To write a c++ program to illustrate the virtual function.

ALGORITHM:

Step1: start the program.

Step2: declare base class as base.

Step3: Declare and define virtual function show ().

Step4: Declare and define function display ().

Step5: create the derived class from base class.

Step6: create the base class object and the pointer variable.

Step7: call the functions display () and show () using the base class object and the pointer.

Step8: create the derived class object and call the function display () and show () using the derived
class object and pointer.

cout<<"\n Base class show:";


}
void display()
{
cout<<"\n Base class display:" ;
}
};
class drive:public base
{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
}
};
void main()
{
clrscr();
base obj1;
25
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to
drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}
SAMPLE OUTPUT:
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show

RESULT:

Thus the c++ program for virtual function was implemented successfully.

7a) UNARY OPERATOR.

AIM:

To implement c++ program to find complex numbers using unary operator overloading.

ALGORITHM:

Step1: Start the program.

Step2: Declare the class.

Step3: Declare variables and its member function.

Step4: Using the get value () to get two numbers.

Step5: Define the function operator ++ to increment values.

Step6: Define function operator - - to decrement values.

Step7: Display function is defined.


Step8: Declare class object.

Step9: Call getvalue ()

Step10: Call function operator ++ () by incrementing the class object and call the function
display.

Step11: Call function operator - - () by decrementing class object and call the function display.

Step12: Stop the program.

Program
#include<iostream.h>
#include<conio.h>
class unary
{
private:
int x;
public:
unary()
{
x=0;
}
void display()
{
cout<<"value of x is"<<x<<endl;
}
void operator ++();
};
void unary::operator ++()
{
++x;
}
void main()
{
clrscr();
unary obj1,obj2;
cout<<"before increment:"<<endl;
cout<<"object.obj1:";
obj1.display();
cout<<"object.obj2:";
obj2.display();
++obj1;
++obj2;
cout<<"after increment:"<<endl;
cout<<"object.obj1:";
obj1.display();
cout<<"object.obj2:";
obj2.display();
getch();
}
RESULT:
Thus the c++ program for unary operator overloading was implemented successfully.

7b) BINARY OPERATOR OVERLOADING.

AIM:

To implement a c++ program to add two complex numbers using binary operator overloading.

ALGORITHM:

Step1:Start the program.


Step2: Declare the class.

Step3: Declare variables and its member function.

Step4:using the function get value () to get two numbers.

Step5: Define function operator + () to add two complex numbers.

Step6: Define function operator – () to subtract two complex numbers.

Step7: Define display function.

Step8: Declare class object obj1, obj2 and result.

Step9: Call the function get value using obj1 and obj2.

Step10:Calculate value for object result by calling function operator + and operator -.

Step11: Call function using obj1 and abj2 and result .

Step12:Return the values .

Step13: Stop the program.

getvalue()
{
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{

29
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
SAMPLE OUTPUT:
Enter the value of Complex Numbers a, b
45
Enter the value of Complex Numbers a, b
22
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i

RESULT:

Thus the c++ program to add two complex numbers using binary operator overloading was
implemented successfully.

8a) CLASS TEMPLATE

AIM:
To write a c++ program to implement class templates.

ALGORITHM:

Step1: start the program.

Step2: create a class name test and write a constructor to the assign the value of parameter
passed.

Step3: create template for the class.

Step4: In main function, create an integer abject, float object ,char object and pass integer ,float ,
char value to constructor.

Step5: For this purpose we need not to write two separate constructor functions instead of we
can use class templates

Step6: stop the program.

RESULT:

Thus the c++ program for class template was implemented successfully.

8b) FUNCTION TEMPLATE

Aim:

To write a c++ program for function template.

ALGORITHM:

Step1: start the program.

Step2: Declare the template for the function.


Step3: Declare and define functions to swap values.

Step4: Declare and define function to get values.

Step5: Read values and call corresponding functions.

Step6: Display the results.

Step7: Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &x,t&y)
{
t temp=x;
x=y;
y=temp;
}
void fun(inta,intb,floatc,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}
void main()
{
inta,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}

SAMPLE OUTPUT:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50

RESULT:

Thus the c++ program for function template was implemented successfully.

9a) TRY-CATCH STATEMENTS

AIM:

To write a c++ program to perform exception handling for divide by zero exception.

ALGORITHM:

Step1: start the program.

Step2: Read two values.

Step3: compute c=a-b.

Step4: Check if (c !=0) within try block. if it is not equal to zero then print a/c value.
Step5: Else throw c value.

Step6: Write divide by zero within catch block.

Step7: Stop the program.

Program
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
cout<<"Enter value for A and B:"<<endl;
cin>>a>>b;
c=a-b;
try
{
if(c!=0)
{
cout<<"The value of a/c is:"<<a/c<<endl;
}
else
{
throw(c);
}
}
catch(int c)
{
cout<<"Divide by zero"<<endl;
}
getch();
}
RESULT:

Thus the c++ program to perform exception handling for divide by zero exception was
implemented successfully.

9b) INVOKE FUNCTION

AIM:

To write a c++ program for invoking a function in exception handling.

ALGORITHM:

Step1: Start the program.

Step2: Declare a function divide (inta,intb).

Step3: within try block declare three functions.

Step4: within divide (), compute c= a-b.

Step5: if (c ! =0) then print value of a/c.

Step6: else throw c value .

Step7: write suitable catches for throw exception.

Step8: stop the program.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void divide(int a,int b);


void divide(int a,int b)
{
int c;
c=a-b;

if(c!=0)
{
cout<<"the value of a/c is "<<a/c<<"\n";
}
else
{
throw(c);
}
}
void main()
{
try
{
divide(20,10);
divide(10,10);
}
catch(int c)
{
cout<<"it is divided by zero \n";
}
getch();
}
RESULT:
Thus the c++ program for invoking a function in exception handling was implemented
successfully.
9c) MULTI-CATCH STATEMENT

AIM:

To write a c++ program for multicatch statement is exception handling.

ALGORITHM:

Step1: start the program .

Step2: create a function called test.

Step3: The number is checked within try block.

Step4: If (x= = 1) throw value of x.

Step5: If (x= = 0) throw character value.

Step6: Else throw y value .

Step7: write suitable catches for each try block.

Step8: stop the program.

Program

#include<iostream.h>
#include<conio.h>

void test(int x);

void main()
{
test(1);
test(0);
test(2);
}
void test(int x)
{
float y;
{
try
{
if(x==1) throw (x);

else if(x==0) throw ('x');

else throw (y);


}

catch(int x)
{
cout<<"catch a int element \n";
}
catch(char x)
{
cout<<"catch a char element \n";
}
catch(float y)
{
cout<<"catch a float element";
}
}
getch();
}
RESULT:
Thus the c++programformulitcatch statement in exception handling was executed successfully
and verified.

10) STANDARD TEMPLATE LIBRARY

AIM: To implement a C++ program to illustrate the concept of standard template library.

ALGORITHM:
Step 1: Include the header files

Step 2: Create a vector to store int

Step 3: Display the original size to store int

Step 4: Push 5 values into the vector using for loop

Step 5: Display extended size of vec

Step 6: Access 5 values from the vector

Step 7: Use iterator to access the values

PROGRAM:
#include <iostream.h>
#include <vector>
using namespace std;
int main()
{
vector<int>vec;
int i;
cout<< "vector size = " <<vec.size() <<endl;
for(i = 0; i < 5; i++)
{
vec.push_back(i);
}
cout<< "extended vector size = " <<vec.size() <<endl;
for(i = 0; i < 5; i++)
{
cout<< "value of vec [" << i << "] = " <<vec[i] <<endl;
}
vector<int>::iterator v = vec.begin();
while( v != vec.end()) {
cout<< "value of v = " << *v <<endl;
v++;
}
return 0;
}

SAMPLE OUTPUT:
vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4

RESULT:
Thus a C++ program to illustrate the concept of standard template libraryis implemented
successfully.

Ex.No 11 a)
FILE STREAM CLASSES

TO READ THE CONTENT OF A FILE

AIM: To implement a C++ program to read the content of a file.


ALGORITHM:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Get the file name to read.
Step 4: Using ifstreamin(filename) check whether the file exist.
Step 5: If the file exist then check for the end of file condition.
Step 6: Read the contents of the file.
Step 7: Print the contents of the file.
Step 8: Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
clrscr();
cout<<"Enter file name:";
cin>>fname;
ifstream in(fname);
if(!in)
{
cout<<"File Does not Exist";
getch();
return;
}
cout<<"\n\n";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}
SAMPLE OUTPUT:
Enter File name: one.txt
Master of Computer Applications
RESULT:
Thus a C++ program to read the content of a file is implemented successfully.
11)b.
TO PERFORM THE WRITE OPERATION WITHIN A FILE

AIM: To implement a C++ program to perform the write operation within a file.

ALGORITHM:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Read the file name.
Step 4: open the file to write the contents.
Step 5: writing the file contents up to reach a particular condition.
PROGRAM:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
ofstream out;
cout<<"Enter File name:";
cin>>fname;
out.open(fname);
cout<<"Enter contents to store in file (Enter # at end):\n";
while((c=getchar())!='#')
{
out<<c;
}
out.close();
getch();
}
SAMPLE OUTPUT:
Enter File name: one.txt
Enter contents to store in file (enter # at end)
Master of Computer Applications#
RESULT:
Thus a C++ program to perform the write operation within a file is implemented successfully

11.c) TO CONVERT LOWERCASE TO UPPERCASE USING FILES

AIM: To implement a C++ program to convert lowercase to uppercase using files


ALGORITHM:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Read the file name.
Step 4: open the file to write the contents.
Step 5: writing the file contents up to reach a particular condition.
Step 6: write the file contents as uppercase.
Step 7: open the file to read the contents.
PROGRAM:
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char c,u;
char fname[10];
clrscr();
ofstream out;
cout<<"Enter File Name:";
cin>>fname;
out.open(fname);
cout<<"Enter the text(Enter # at end)\n"; //write contents to file
while((c=getchar())!='#')
{
u=c-32;
out<<u;
}
out.close();
ifstream in(fname); //read the contents of file
cout<<"\n\n\t\tThe File contains\n\n";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}
SAMPLE OUTPUT:
Enter File Name: two.txt
Enter contents to store in file (enter # at end)
oops programming
The File Contains
OOPS PROGRAMMING
RESULT: Thus a C++ program to convert lowercase to uppercase using files is implemented
successfully

You might also like