I Bca CPP Record Programs Using Dev CPP
I Bca CPP Record Programs Using Dev CPP
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>
OUTPUT:
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>
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:
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;
}
};
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:
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>
class circle
{
int radius;
public:
void get()
{
cout << "Enter the radius of Circle : ";
cin >> radius;
}
friend float calculate(circle ob);
};
int main()
{
circle c;
cout<<"To find the perimeter of circle using Friend function\n";
c.get();
cout<<"\nPerimeter of Circle : "<<calculate(c);
}
OUTPUT:
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:
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>
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:
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;
}
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>
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 D4:public B1
{
public:
int c;
void show();
};
void B1::get()
{
cout<<"\nEnter 2 values : \n";
cin>>a>>b;
}
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:
Enter 2 values : 4 5
Result is : 20
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";
}
};
OUTPUT:
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>
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:
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();
}
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:
PROGRAM:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
OUTPUT:
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>
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:
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>
template<class t>
OUTPUT:
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>
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.