0% found this document useful (0 votes)
148 views

I Bca CPP Record Programs Using Dev CPP

Uploaded by

kavi333567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

I Bca CPP Record Programs Using Dev CPP

Uploaded by

kavi333567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

23UCAP02 - C++ PROGRAMMING LAB

1. Write a C++ program to demonstrate function overloading, Default Arguments and Inline
function.
2. Write a C++ program to demonstrate Class and Objects
3. Write a C++ program to demonstrate the concept of Passing Objects to Functions
4. Write a C++ program to demonstrate the Friend Functions
5. Write a C++ program to demonstrate the concept of Passing Objects to Functions
6. Write a C++program to demonstrate Constructor and Destructor
7. Write a C++program to demonstrate Unary Operator Overloading
8. Write a C++program to demonstrate Binary Operator Overloading
9. Write a C++program to demonstrate
 Single Inheritance
 Multilevel Inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
10. Write a C++ program to demonstrate Virtual Functions.
11. Write a C++ program to manipulate a Text File.
12. Write a C++ program to perform Sequential I/O Operations on a file.
13. Write a C++program to find the Biggest Number using Command Line Arguments.
14. Write a C++program to demonstrate Class Template.
15. Write a C++program to demonstrate Function Template.
16. Write a C++program to demonstrate Exception Handling.
1. Program to demonstrate function overloading, Default Arguments and Inline function

AIM:
To write a C++ program to demonstrate function overloading, Default Arguments and Inline
function.

ALGORITHM:
Step1 : Start the program.
Step 2: Define the functions volume( ) with different arguments for calculating the area of
different shapes.
Step 3: Define the function sum( ) with two default argument and one formal argument, Inside the
function sum( ) do the addition of argument inputs and return the result.
Step 4: Define the inline function area(int r) , Inside the function return the (3.14 * r * r) calculation
result.
Step 5: Create a main function with required input and output variables.
Step 6: Read the input values x , y , z and pass this input argument values to the required functions.
Step 7: Print the output values.
Step 8: Stop the program.

PROGRAM:

#include<iostream>

using namespace std;

int volume(int l,int b,int h)


{
return(l*b*h);
}
int volume(double r,int h)
{
return(3.14*r*r*h);
}
int volume(int a)
{
return(a*a*a);
}

int sum(int a,int b=10,int c=20)


{
cout<<"Using functions with 3 arguments"<<endl;
return a+b+c;
}
inline float area( int r)
{
return(3.14*r*r);
}
int main()
{
int x,y,z;
cout<<"Enter the x,y,z input values"<<"\n";
cin>>x>>y>>z;
cout<<"Demonstration of Function Overloading"<<"\n";
cout<<"the volume of rectangle is= "<<volume(x,y,z)<<"\n";
cout<<"the volume of clinder is= "<<volume(x,y)<<"\n";
cout<<"the volume of cube is= "<<volume(x)<<"\n";
cout<<"Demonstration of Functions with default arguments"<<"\n";
cout<<"The sum1 value (using two default arguments) is= "<<sum(x)<<"\n";
cout<<"The sum2 value (using one default argument) is ="<<sum(x,y)<<"\n";
cout<<"The sum3 value (using No default arguments) is ="<<sum(x,y,z)<<"\n";
cout<<"Demonstration of Inline Function"<<"\n";
cout<<"The area of circle is= "<<area(x)<<"\n";
}

OUTPUT:

Enter the x,y,z input values


4
2
3
Demonstration of Function Overloading
The volume of rectangle is= 24
The volume of clinder is= 100
The volume of cube is= 64
Demonstration of Functions with default arguments
Using function with 3 arguments
The sum1 value (using two default arguments) is = 34
Using function with 3 arguments
The sum1 value (using one default arguments) is = 26
Using function with 3 arguments
The sum1 value (using No default arguments) is = 9
Demonstration of Inline Function
The area of circle is= 50.240002

RESULT:
Thus the above c++ program has to be created successfully and the outputs are verified.
2. Program to demonstrate Class and Objects

AIM :
To write a C++ program to demonstrate Class and Objects.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the class as room and inside the class declare the required input variables.
Step 3: For inside the class define the member function as calculateArea( ) , calculateVolume( ) with
required calculations.
Step 4: Create a main function , for inside the main function create a object r1 from room class.
Step 5: For inside the main function declare the input variables and read the input values from the user.
Step 6: Assign the input values to the function argument variable through using the object.
Step 7: Print the function output results using object reference.
Step 8: Stop the program.

PROGRAM:

#include<iostream>

using namespace std;

class Room
{
public:
double length;
double breadth;
double height;
double calculateArea()
{
return length * breadth;
}

double calculateVolume()
{
return length * breadth * height;
}
};
int main()
{
Room r1;
float l,b,h;
cout<<"Area and Volume calculation of Room using class and objects\n";
cout<<"Enter the length, breadth and height values of Room\n";
cin>>l>>b>>h;
r1.length = l;
r1.breadth = b;
r1.height = h;
cout << "Area of Room = " << r1.calculateArea() << endl;
cout << "Volume of Room = " << r1.calculateVolume() << endl;
}

OUTPUT:

Area and Volume calculation of Room using class and objects


Enter the length, breadth and height values of Room
42.5
30.8
19.2
Area of Room = 1308.999968
Volume of Room = 25132.8000376

RESULT:
Thus the above c++ program has to be created successfully and the outputs are verified.
3&5. Program to demonstrate the concept of Passing Objects to Functions

AIM:
To write a C++ program to demonstrate the concept of Passing Objects to Functions.

ALGORITHM:
Step 1: Start the program.
Step 2: Crate a class student, for inside the student class declare the required input variable and
constructor student (double m).
Step 3: Define the function calculateAverage( ) , Inside the function find the average of two marks
with may passing from objects to arguments.
Step 4: Create a main function , For inside the main function declare the input variable m1 , m2 and
read the input values.
Step 5: Pass the input values to the function arguments through the object reference.
Step 6: Print the function result.
Step 7: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;

class student
{
public:
double marks;
student(double m)
{
marks = m;
}
};

void calculateAverage(student s1, student s2)


{
double average = (s1.marks + s2.marks) / 2;
cout << "Average Marks = " << average << endl;
}

int main()
{
int m1,m2;
cout<<"Enter the student1 mark\n";
cin>>m1;
cout<<"Enter the student2 mark\n";
cin>>m2;
cout<<"To calculate the average marks of two students\n";
student s1(m1),s2(m2);
calculateAverage(s1, s2);
}
OUTPUT:

Enter the syudent1 mark


58
Enter the student2 mark
74
To calculate the average marks of two students
Average Marks = 66

RESULT:
Thus the above c++ program has to be created successfully and the outputs are verified.
4. Program to demonstrate the Friend Functions

AIM:
To write a C++ program to demonstrate the Friend Functions.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class circle , For inside the class declare the input variable radius , and define the
function get( ) , Inside the function reading the input radius value.
Step 3: For Inside the class declare the friend function calculate(circle ob).
Step 4: For outside the class declaration define the function calculate( ) , Inside the function return
the 2 * 3.14 * ob.radius result.
Step 5: Create a main function , For inside the main function create a object c for circle class.
Step 6: To using the object c pass the input value to the get( ) function and print the circumference
of circle output.
Step 7: Stop the program.

PROGRAM:

#include<iostream>

using namespace std;

class circle
{
int radius;
public:
void get()
{
cout << "Enter the radius of Circle : ";
cin >> radius;
}
friend float calculate(circle ob);
};

float calculate(circle ob)


{
return 2 * 3.14 * ob.radius;
}

int main()
{
circle c;
cout<<"To find the perimeter of circle using Friend function\n";
c.get();
cout<<"\nPerimeter of Circle : "<<calculate(c);
}
OUTPUT:

To find the perimeter of circle using Friend function


Enter the radius of Circle : 5

Perimeter of Circle : 31.4

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
6. Program to demonstrate Constructor and Destructor

AIM:
To write a C++ program to demonstrate Constructor and Destructor.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class student , For inside the class declare the required input variables and constructor ,
destructor and member functions.
Step 3: For outside the class define the constructor student( ).
Step 4: Define the read( ) function for reading the input values for student Name , regno ,
address , Zipcode.
Step 5: Define the disp( ) function for printing the student details.
Step 6: Define the destructor ~student( ).
Step 7: Crate a main function , For inside the main function create a object for student class and call the
member functions using object reference and print the functions result.
Step 8: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;

class student
{
private:
char name[20],add[20],regno[10];
double zip;
public:
student();
~student();
void read();
void disp();
};
student :: student()
{
cout<<"Student class constructor called."<<endl;
}
void student :: read()
{
cout<<"Enter the student Name: ";
cin>>name;
cout<<"Enter the student roll no: ";
cin>>regno;
cout<<"Enter the student address: ";
cin>>add;
cout<<"Enter the Zipcode: ";
cin>>zip;
}
void student :: disp()
{
cout<<"Studet details"<<endl;
cout<<"Student Name :"<<name<<endl;
cout<<"Register no is :"<<regno<<endl;
cout<<"Address is :"<<add<<endl;
cout<<"Zipcode is :"<<zip<<endl;
}
student :: ~student()
{
cout<<"Student class destructor called.";
}

int main()
{
cout<<"Student Information handling using constructor and destructor\n";
student s;
s.read();
s.disp();
}

OUTPUT:

Student Information handling using constructor and destructor


Student class constructor called.
Enter the student Name:S.MUTHAMIL
Enter the student register no:C23UG151CAP035
Enter the student address: SAMANTHAMALAI-KRISHNAGIRI
Enter the Zipcode:635115

Studet details
Student Name : S.MUTHAMIL
Register no is : C23UG151CAP035
Address is : SAMANTHAMALAI-KRISHNAGIRI
Zipcode is : 635115
Student class destructor called.

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
7. Program to demonstrate Unary Operator Overloading

AIM:
To write a C++ Program to demonstrate Unary Operator Overloading.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class IncDec , For inside the class declare the member variable a, b and define the
member functions.
Step 3: For inside the accept function definition to read the input values a, b.
Step 4: Define the operator—( ) function to the overloaded and decrease the a , b values.
Step 5: Define the operator++( ) function to overload and increase the a , b values.
Step 6: Define the display( ) function to print a , b output values.
Step 7: Create a main function , For inside the main function create a object id for IncDec class.
Step 8: For using id object call the member functions and overloading outputs for decrementing and
incrementing a , b values.
Step 9: Stop the program.

PROGRAM:
#include<iostream>

using namespace std;

class IncDec
{
int a, b;
public:
void accept()
{
cout<<"\n Enter Two Numbers : \n";
cin>>a>>b;
}
void operator--()
{
a--;
b--;
}
void operator++()
{
a++;
b++;
}
void display()
{
cout<<"\n A : "<<a;
cout<<"\n B : "<<b;
}
};
int main()
{
cout<<"Increment and Decrement the value using Unary operator overloading\n";
IncDec id;
id.accept();
--id;
cout<<"\n After Decrementing : ";
id.display();
++id;
++id;
cout<<"\n\n After Incrementing : ";
id.display();
}

OUTPUT:

Increment and Decrement the value using Unary Operator Overloading


Enter Two Numbers :
15
32

After Decrementing :
A : 14
B : 31

After Incrementing :
A : 16
B : 33

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
8. Program to demonstrate Binary Operator Overloading

AIM:
To write a C++ Program to demonstrate Binary Operator Overloading.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class complex , For inside the class declare the member variables a, b and define the
member functions getvalue( ), complex operator+(complex ob), complex operator- (complex ob).
Step 3: For inside the operator overloading functions add and subtract the input values using
object reference.
Step 4: For inside the display( ) function definition print the output results of a, b.
Step 5: Create a main function , For inside the main function create a objects c1 , c2 , r1 ,r2 for
complex class.
Step 6: For using object call the getvalue( ) function to read the input values a, b.
Step 7: Do the overloading calculations for addition & subtraction of input values.
Step 8: Call the display( ) function to print the output values using c1 , c2 , r1 ,r2 objects.
Step 9: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;

class complex
{
int a, b;
public:
void getvalue()
{
cout << "Enter the value of Complex Numbers a,b:\n";
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()
{
cout << a << "+" << b << "i" << "\n";
}
};

int main()
{
cout<<"Addition & Subtraction of complex Numbers using Binary operator overloading\n";
complex c1, c2, r1, r2;
c1.getvalue();
c2.getvalue();
r1 = c1 + c2;
r2 = c1 - c2;
cout << "Input Values:\n";
c1.display();
c2.display();
cout << "Result:\n";
r1.display();
r2.display();
}

OUTPUT:
Addition & Subtraction of complex Numbers using Binary operator overloading
Enter the value of Complex Numbers a, b:
15
12
Enter the value of Complex Numbers a ,b:
10
5
Input Values:
15+12i
10+5i
Result:
25+17i
5+7i

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
9. Program to demonstrate all types of Inheritance

AIM:
To write a C++ program to demonstrate all types of Inheritance.

ALGORITHM:
Step 1: Start the program.
Step2: Create a Base class B1 and inside the base class B1 declare the member variable a, b and
member function get( ).
Step 3: Create a subclass D1 from B1 , For inside the class declare the member variable c and
member function mul( ).
Step 4: Create a subclass D2 from D1 , For inside the class declare the member variable d and
member function product( ).
Step 5: Create a base class B2 , For inside the B2 class declare the show function.
Step 6: Create a sub class D3 from D2 & B2 classes , For inside the D3 class declare member
variable e and member function multiply( ).
Step 7: Create a class D4 from B1 , For inside the D4 class declare the member variable c and
member function show( ).
Step 8: define get( ) and mul( ) functions for reading the inputs and processing the calculations to
inside the functions.
Step 9: Define the product ( ) , multiply( ) , show( ) functions for reading the inputs and processing
the calculations to inside the functions.
Step 10 : Create a main function , For inside the main function ,to create a required objects d , d1
from D3 , D4 class and call the member functions from the variables base and derived
class to processing the various calculations using the object reference.
Step 11: Print the all function outputs from the various base and derived classes.
Step 12: Stop the program.

PROGRAM:
#include<iostream>

using namespace std;

class B1
{
public:
int a,b;
void get();
};

class D1:public B1
{
public:
int c;
void mul();
};
class D2:public D1
{
public:
int d;
void product();
};

class B2
{
public:
void show();
};

class D3:public D2,public B2


{
public:
int e;
void multiply();
};

class D4:public B1
{
public:
int c;
void show();
};

void B1::get()
{
cout<<"\nEnter 2 values : \n";
cin>>a>>b;
}

void D1:: mul()


{
c=a*b;
cout<<"\nResult is : "<<c;
}

void D2::product()
{
cout<<"\n\n Enter 2 values: ";
cin>>b>>c;
d=b*c;
cout<<"\nResult is : "<<d;
}
void D3::multiply()
{
cout<<"\n\n Enter 2 values : ";
cin>>c>>d;
e=c*d;
cout<<"\nResult is : "<<e;
}

void D4::show()
{
cout<<"\n\nEnter 2 values : ";
cin>>a>>b;
c=a*b;
cout<<"\nResult is : "<<c;
}

int main()
{
cout<<"\n\n\t IMPLEMENTATION OF HYBRID INHERITANCE ";
cout<<"\n\n\n\n";
cout<<"\n\t **** SINGLE INHERITANCE ***\n";
D3 d;
d.get();
d.mul();
cout<<"\n\n\t **** MULTILEVEL INHERITANCE ****\n";
d.product();
cout<<"\n\n\t **** MULTIPLE INHERITANCE ****\n";
d.multiply();
D4 d1;
cout<<"\n\n\t **** HIERARCHICAL INHERITANCE ****";
d1.show();
}
OUTPUT:

IMPLEMENTATION OF HYBRID INHERITANCE


**** SINGLE INHERITANCE ***

Enter 2 values : 4 5
Result is : 20

**** MULTILEVEL INHERITANCE ****


Enter 2 values: 7 3
Result is : 21

**** MULTIPLE INHERITANCE ****


Enter 2 values : 8 6
Result is : 48

**** HIERARCHICAL INHERITANCE ****


Enter 2 values : 10 8
Result is : 80

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
10. Program to demonstrate Virtual Functions

AIM:
To write a C++ program to demonstrate Virtual Functions.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class as first, For inside the class define the virtual function void calculate( ).
Step 3: Create a sub class second derived from base class first , For inside the second class declare the
required input and output variables and calculate( ) function.
Step 4: For inside the calculate( ) function read the input values and do the area , perimeter calculations
then print the results.
Step 5: Create a main function , For inside the main function create a pointer object *f for first base
class then s for second base class.
Step 6: Assign the reference address to the objects.
Step 7: For calling calculate ( ) virtual function then do the calculations and print the outputs for using
the f object reference.
Step 8: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class first
{
public:
virtual void calculate()
{
cout << "Area Perimeter of a Rectangle";
}
};

class second : public first


{
public:
int width, height, area, perimeter;
void calculate()
{
cout << "Enter Width of Rectangle:";
cin>>width;
cout << "Enter Height of Rectangle:";
cin>>height;
area = height*width;
cout << "Area of Rectangle:" << area;
perimeter = 2 * (height + width);
cout << "\nPerimeter of Rectangle:" << perimeter;
}
};
int main()
{
cout<<"Area and Perimeter of Rectangle using Virtual Functions\n";
first *f;
second s;
f = &s;
f->calculate();
}

OUTPUT:

Area and Perimeter of Rectangle using Virtual Functions


Enter Width of Rectangle:4
Enter Height of Rectangle:3
Area of Rectangle:12
Perimeter of Rectangle:14

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
11. Program to manipulate a Text File
AIM:
To write a C++ program to manipulate a Text File.

ALGORITHM:
Step 1: Start the program.
Step 2: Create the main function , For inside the main function declare the input variable c , u ,
fname[10] and ofstream out object.
Step 3: Read the input text file name
Step 4: Open the input file and write the content to the input file using while loop.
Step 5: close the input file.
Step 6: To use ifstream in(fname) file to print the content to the standard output screen for using
while loop.
Step 7: Stop the program.

PROGRAM:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<ctype.h>
#include<string.h>

using namespace std;

int main()
{
char c, u;
char fname[10];
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;
}
}
OUTPUT:

Manipulating a Text File


Enter File Name: file1.txt
Enter the text(Enter # at end)
Welcome to bca in arignar anna college at krishnagiri #

The File contains

WELCOME TO BCA IN ARIGNAR ANNA COLLEGE AT KRISHNAGIRI

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
12. Program to perform Sequential I/O Operations on a file.

AIM:
To write a C++ program to perform Sequential I/O Operations on a file.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class student , for inside the class declare the member variable name[2] , grade , mark
and member functions as getdata( ) and display( ).
Step 3: For outside the class define the getdata( ) function , For inside this function read the input values.
Step 4: Define the display( ) function to print the student details.
Step 5: Create a main function , For inside the function declare the required input variables fname[20]
and create a file object fil for using fstream class.
Step 6: Read the input file name and open the input file , if the file name is not found in present
directory then error message will be printed.
Step 7: Enter the student details to the input file using for loop and write statement.
Step 8: Print the file information’s using for loop and read statement.
Step 9: Close the file using file object.
Step 10 : Stop the program.

PROGRAM:
#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;

class student
{
char name[20];
char grade;
float marks;
public:
void getdata(void);
void display(void);
};

void student::getdata(void)
{
char ch;
cin.get(ch);
cout << "Enter the name: ";
cin>>name;
cout << "Enter the grade: ";
cin >> grade;
cout << "Enter the mark: ";
cin >> marks;
cout << "\n";
}
void student::display(void)
{
cout << "Name: " << name << "\tGrade: " << grade << "\tMarks: " << marks << "\n";
}

int main()
{
int i;
char fname[20];
cout<<"To perform Sequential I/O operations on a file\n";
student cse[3];
fstream fio;
cout << "Enter file name: ";
cin.get(fname, 20);
fio.open(fname, ios::in | ios::out);
if (!fio)
{
cout << "Error opening the file!\n";
getch();
}

cout << "Enter details for the three students:\n\n";


for (i = 0; i < 3; i++)
{
cse[i].getdata();
fio.write((char *)&cse[i], sizeof(cse[i]));
}
fio.seekg(0);
cout << "The contents of the " << fname << " file are shown below:\n";
for (i = 0; i < 3; i++)
{
fio.read((char *)&cse[i], sizeof(cse[i]));
cse[i].display();
}
fio.close();
getch();
}
OUTPUT:

To perform Sequential I/O operations on a file


Enter file name: student.txt
Enter details for the three students:

Enter the name: Farhan.A


Enter the grade: A
Enter the mark: 95

Enter the name: Arun.K


Enter the grade: B
Enter the mark: 76

Enter the name:Ranjith.M


Enter the grade: C
Enter the mark: 58

The contents of the student.txt file are shown below:


Name: Farhan.A Grade: A Marks: 95
Name: Arun.K Grade: A Marks: 76
Name: Ranjith.M Grade: A Marks: 58

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
13. Program to find the Biggest Number using Command Line Arguments

AIM:
To write a C++ program to find the Biggest Number using Command Line Arguments.

ALGORITHM:

Step 1: Start the program.


Step 2: Create the main function with command line arguments as argc , *argv[ ].
Step 3: For inside the main function , Declare the input variables a, b, c.
Step 4: Read the input data’s using command line arguments.
Step 5: Compare the a, b, c input values using else if statements.
Step 6: If (a>b && a> c) it’s true means “ a Largest “ will be printed other wise go to next step.
Step 7: If (b>a && b> c) it’s true means “ b Largest “ will be printed other wise go to next step.
Step 8: If (c>a && c>b) it’s true means “ c Largest “ will be printed.
Step 9: Stop the program.

PROGRAM:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

using namespace std;

int main(int argc, char *argv[])


{
int a, b, c;
cout<<"Bigest among the given 3 Numbers using command line arguments\n";
if (argc < 4 || argc > 5)
{
cout<<"enter 3 arguments only eg.\filename arg1 arg2 arg3!!\n";
}
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
if (a < 0 || b < 0 || c < 0)
{
cout<<"enter only positive values in arguments !!";
}
if (!(a != b && b != c && a != c))
{
cout<<"please enter three different value ";
}
else
{
if (a > b && a > c)
cout<<" Largest is:\n"<<a;
else if (b > c && b > a)
cout<<" Largest is:\n"<<b;
else if (c > a && c > b)
cout<<"Largest is: \n"<<c;
}
getch();
}

OUTPUT:

1. Enter search tab in start menu --> type cmd


2.Type proper path “Ex:C:\Users\NareshKumar\Desktop\I BCA CPP”
3.C:\Users\NareshKumar\Desktop\I BCA CPP\cline.exe 10 50 30 press enter key
Largest is : 50

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
14. Program to demonstrate Class Template

AIM:
To write a C++ program to demonstrate Class Template.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class Template as Tclassmax.
Step 3: For inside the Template class assign x , y is the Template variables and define the
Tclassmax( ) constructor.
Step 4: Define the Template function getmaximum( ) , For inside the function compare x > y then
return the result.
Step 5: To create a main function , For inside the main function create a imax & fmax object for
Tclassmax template class.
Step 6: Read first integer values from user then pass integer inputs to template function through
template class using object.
Step 7: Read the float inputs from user and pass this input to template function through template
class using object.
Step 8: Print the outputs for both integer and float inputs.
Step 9: Stop the program.

PROGRAM:
#include<iostream>
#include<stdio.h>

using namespace std;

template<class T>

class TClassMax
{
T x, y;
public:
TClassMax()
{
}
TClassMax(T first, T second)
{
x = first;
y = second;
}
T getMaximun()
{
if (x > y)
return x;
else
return y;
}
};

int main()
{
TClassMax <int> iMax;
int a, b, i;
TClassMax <float> fMax;
float c, d, j;
cout << "Class Template Program : Get Maximum Number \n";
cout << "Enter A,B values(integer):\n";
cin >> a>>b;
iMax = TClassMax<int>(a, b);
i = iMax.getMaximun();
cout << "Result Max Int : " << i;
cout << "\n\nEnter C,D values(float):\n";
cin >> c>>d;
fMax = TClassMax<float>(c, d);
j = fMax.getMaximun();
cout << "Result Max Float : " << j;
}

OUTPUT:

Class Template Program : Get Maximum Number


Enter A,B values(integer):
54
48
Result Max Int : 54

Enter C,D values(float):


40.23
40.32
Result Max Float : 40.32

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
15. Program to demonstrate Function Template

AIM:
To write a C++ program to demonstrate Function Template.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a template class t , For inside the class template create a function swap( ) with
Template arguments.
Step 3: For inside the swap( ) function interchange the variable values.
Step 4: Define the fun( ) function , For inside the function read integer , float data input’s from
the user and pass this input parameter values to swap function.
Step 5: Create a main function , For inside the main function declare the input variables and
read the input values as like integer and float types.
Step 6: Pass this input values to swap( ) function and print swapping result to the screen.
Step 7: Stop the program.

PROGRAM:

#include<iostream>

using namespace std;

template<class t>

int swapme(t &x, t &y)


{
t temp = x;
x = y;
y = temp;
}

int fun(int a, int b, float c, float d)


{
cout << "\na and b before swaping :" << a << "\t" << b;
swapme(a, b);
cout << "\na and b after swaping :" << a << "\t" << b;
cout << "\n\nc and d before swaping :" << c << "\t" << d;
swapme(c, d);
cout << "\nc and d after swaping :" << c << "\t" << d;
}
int main()
{
int a, b;
float c, d;
cout<<"Swaping of Two Numbers using Function Template\n";
cout << "Enter A,B values(integer):";
cin >> a>>b;
cout << "Enter C,D values(float):";
cin >> c>>d;
fun(a, b, c, d);
}

OUTPUT:

Swapping of Two Numbers using Function Template


Enter A,B values(integer):
60
80
Enter C,D values(float):
50.53
70.55
a and b before swapping : 60 80
a and b after swapping : 80 60

a and d before swapping : 50.529999 70.550003


a and d after swapping : 70.550003 50.529999

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.
16. Program to demonstrate Exception Handling.
AIM:
To write a C++ program to demonstrate Exception Handling.

ALGORITHM:
Step 1: Start the program.
Step 2: Create the main function , For inside the main function declare the required input and
output variables.
Step 3: Read the input values through numerator , and denominator variables.
Step 4: Create a try block , For inside the try block check the condition if( denominator ==0 ) then
throw 0, otherwise go to the next step.
Step 5: Do the calculation divide = numerator / denominator.
Step 6: Print the calculation result.
Step 7: If any number divide by zero then exception will be raised and print the exception
statement to the output screen otherwise calculation will be performed.
Step 8: Stop the program.

PROGRAM :

#include<iostream>

using namespace std;

int main()
{
double numerator, denominator, divide;
cout<<"Exception Handling for Division by zero errors";
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
try
{
if (denominator == 0)
throw 0;
divide = numerator / denominator;
cout << numerator << " / " << denominator << " = " << divide << endl;
}
catch (int num_exception)
{
cout << "Error: Cannot divide by " << num_exception << endl;
}
}
OUTPUT:

Enter numerator: 72
Enter denominator: 0
Error: Cannot divide by 0

Enter numerator: 72
Enter denominator: 3
72 / 3 = 24

RESULT:

Thus the above c++ program has to be created successfully and the outputs are verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy