Professional Documents
Culture Documents
C++ - Record
C++ - Record
Ramapuram Campus
PRACTICAL RECORD
NAME :
REGISTER NO :
SEMESTER / YEAR : II / I
APRIL 2023
FACULTY OF SCIENCE AND HUMANITIES
Ramapuram Campus
REGISTER NUMBER:
BONAFIDE CERTIFICATE
This is to certify that the bonafide work done by _____________________________in the subject
OBJECT ORIENTED PROGRAMMING LAB [USA20201J] at, SRM Institute of Science and
Submitted for the University Practical Examination held at SRM Institute of Science and Technology,
Ramapuram Campus on ____________.
PAGE
SNO DATE NAME OF THE PROGRAM SIGN
NO
1 I/O OPERATIONS AND OPERATORS
4 CONSTRUCTOR OVERLOADING
5 FUNCTION OVERLOADING
6 UNARY OPERATOR OVERLOADING
7 SINGLE INHERITANCE
8A MULTILEVEL INHERITANCE
8B MULTIPLE INHERITANCE
16 SORTING NUMBERS
EXP NO: 1 REGNO:
DATE: NAME:
AIM:
To write a C++ program for performing arithmetic operations using arithmetic operators.
ALGORITHM:
Step 2: Declare the class arith with the data members a and b as integer type.
Step 3: Define the function, get() to get the input from the user.
Step 5: Call the get() and display() functions from the main function by creating an instance
of the class arith.
PROGRAM:
#include <iostream>
class Arith
public:
int a,b;
void get();
void display();
};
void Arith::get()
cin>>a;
cin>>b;
void Arith::display()
int main()
ob.get();
ob.display();
return 0;
OUTPUT:
RESULT:
DATE: NAME:
AIM:
To write a C++ program to check the string is palindrome or not using conditional control
statements.
ALGORITHM:
Step 2: Declare the character array as string1 and integer variables as I, length and set flag
=0.
Step 3: Read the input for string1 and calculate the length of the string.
Step 4: Execute the for loop and check the condition string1[i] != string1[length-i-1].
Step 5: If the condition is false, set flag =1 and break the looping statement.
Step 6: Print the string is not palindrome else print the string is palindrome.
PROGRAM:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char string1[20];
int i, length;
int flag = 0;
length = strlen(string1);
{
if(string1[i] != string1[length-i-1])
{flag = 1;
break;
}
}
if (flag)
{
cout << string1 << " is not a palindrome" << endl;
}
else
{
cout << string1 << " is a palindrome" << endl;
}
return 0;
}
OUTPUT:
RESULT:
AIM:
To write a C++ program to calculate the total and average marks of student using class.
ALGORITHM:
Step 2: Declare the class student with name as character array and rollno,m1,m2,m3 as
integer type and total, avg as float type.
Step 3: Define the function, getDetails() to get the input from the user.
Step 4: Define the function, putDetails() to calculate the total and average and to print the
final output.
Step 5: Call the getDetails() and putDetails() functions from the main function by creating
an instance of the class student.
PROGRAM:
#include <iostream>
using namespace std;
class student
{
char name[30];
int rollNo,m1,m2,m3;
float total,avg;
public:
//member function to get student's details
void getDetails();
//member function to print student's details
void putDetails();
};
//member function definition, outside of the
class void student::getDetails() {
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter mark1: ";
cin >> m1;
cout << "Enter mark2: ";
cin >> m2;
cout << "Enter mark3: ";
cin >>m3;
cout << "total marks: "<<endl;
total=m1+m2+m3;
cout<<total;
avg=total/3;
}
//member function definition, outside of the
class void student::putDetails() {
cout << "Student details:\n";
cout << "Name:"<< name <<endl;
cout <<",Roll Number:" << rollNo << ",Total:" << total <<endl<< ", Average marks:"
<<avg;
}
int main()
{
student st;
st.getDetails();
st.putDetails();
return 0;
}
OUTPUT:
RESULT:
CONSTRUCTOR OVERLOADING
AIM:
ALGORITHM:
Step 2: Declare the class with the class name integer with a data members m & n.
Step 3: Define the constructor functions, such as default, parameterized, copy constructors and
destructor.
PROGRAM:
#include <iostream>
class integer
int m,n;
public:
m=i.m;
n=i.n;
{}
void display();
};
integer::integer(int a, int b)
m=a;
n=b;
void integer::display()
cout<<"m : "<<m<<endl;
cout<<"n : "<<n<<endl;
int main()
integer obj1;
integer obj2(5,8);
integer obj3(obj2);
obj1.display();
obj2.display();
obj3.display();
return 0;
OUTPUT:
RESULT:
DATE: NAME:
FUNCTION OVERLOADING
AIM:
To Calculate the area of the Rectangle, Triangle and Circle using function
Overloading concept
ALGORITHM:
Step 2: Declare the class with class name calarea and define the member variables
i,b,m,n,tarea,r.
Step 3: Define the function, area() is overloaded to find the area of different
Step 4: The function area() is defined with return type float and with 2 arguments int, float for
calculating area of rectangle.
Step 5: The function area() is defined without argument and return type for calculating the
area of triangle. The area is calculated and the result is printed in the same function.
Step 6: The function area() is again defined with one argument and return type double for
calculating the area of the circle.
Step 7: In the main(), call all the functions using the instance of class calarea.
#include <iostream>
class calarea
int l;
float b,m,n,tarea;
double r;
public:
};
void calarea::area()
{
cout<<"enter the value of m and n";
cin>>m>>n;
tarea=(0.5*m*n);
cout<<"area of the triangle:"<<tarea<<endl;
}
double calarea::area(double c)
{
r=c;
return(3.14*c*c);
}
int main()
{
float res;
calarea ob;
res=ob.area(5,6.5);
cout<<"area of the rectangle:"<<res<<endl;
ob.area();
cout<<"area of the circle:"<<endl;
cout<<ob.area(2.25);
return 0;
}
OUTPUT:
RESULT:
DATE: NAME:
AIM:
To write C++ program to change the sign of an operands using unary operator
overloading concept.
ALGORITHM:
Step 2: Declare the class with the class name space and three data members x,y and z of
integer type.
Step 3: Define the operator ‘–‘ to be overloaded using the keyword ‘operator’ in
Step 4: Define getvalues() and display() functions to get the data and to display results to
user.
Step 6: Change the sign of the object by calling the operator overloaded
function.
#include <iostream>
class space
int x,y,z;
public:
void display();
void operator-();
};
x=a;
y=b;
z=c;
void space::display()
cout<<"x : "<<x<<endl;
cout<<"y : "<<y<<endl;
cout<<"z : "<<z<<endl;
void space::operator-()
{
x=-x;
y=-y;
z=-z;
int main()
space s;
s.getvalues(10,-20,30);
s.display();
-s;
cout<<endl<<"s:"<<endl;
s.display();
return 0;
OUTPUT:
RESULT:
DATE: NAME:
SINGLE INHERITANCE
AIM:
To write C++ program for calculating the agent commission based on the sales value
using single inheritance concept.
ALGORITHM:
Step 2: Declare the class with the class name commcal (base class) and define the member
function getsalesval() to get the input for svalue.
Step 3: Declare another class with class name agentcomm (derived class) which derives publicly
from class commcal and define the functions calcommision() to calculate the commission
and define a function display() to print the total sales and commission.
Step 5: Invoke the getsalesval(), calcommision() and display() of class base and derived
respectively.
PROGRAM:
#include <iostream>
class commcal
public:
long int svalue;
float commission;
void getsalesval()
cin>>svalue;
};
public:
void calcommision();
void display();
};
void agentcomm::calcommision()
if(svalue<=10000)
commission=svalue*5/100;
else if(svalue<=25000)
commission=svalue*10/100;
else if(svalue>25000)
commission=svalue*20/100;
void agentcomm::display()
{
cout<< "For a total sale value of $" <<svalue<<endl;
<<commission; }
int main()
agentcomm ob;
ob.calcommision();
ob.display();
return 0;
OUTPUT:
RESULT:
AIM:
To write the C++ program for displaying the student results along with sports marks using
multilevel inheritance.
ALGORITHM:
Step 3: Declare and define the function get() to get the student details.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 7: Create the class result derived from student and sports.
Step 8: Declare and define the function display() to find the average.
Step 9: Declare the derived class object,call the functions get(),getsm(),totcal() and display().
PROGRAM:
#include<iostream>
using namespace std;
class student
{
protected:
int rno,m1,m2;
public:void get()
{ protected:
int tot,sm;
public:
// sm = Sports mark
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
void totcal()
{
tot=m1+m2+sm;
}
};
};
int main()
//base class
obj.getsm(); //
obj.totcal();
obj.display();
return 0;
}
OUTPUT:
RESULT:
DATE: NAME:
MULTIPLE INHERITANCE
AIM:
To write a C++ program to calculate the income tax based on the gross salary using
multiple inheritance concept.
ALGORITHM:
Step 2: Declare the class with the class name- taxcal with variables empno, empname. Define
getdata() to get the input for empno, empname.
Step 3: Declare the class with class name, incometax with variables yearsal and finaltax. Define
getsalary() to get the input for salary and define taxcalculation() to calculate tax.
Step 4: Declare the class with the class name- result which publicly inherits classes taxcal and
incometax. Define display() to print the salary and calculated tax.
Step 5: Create an instance for the derived class result and invoke the functions.
PROGRAM:
#include <iostream>
class taxcal
protected:
int empno;
char empname[25];
public:
void getdata()
cin>>empno;
cin>>empname;
};
class incometax
protected:
double finaltax;
public:
void getsalary()
cout << "Enter the annual income after the deductions :"<<
endl; cin>>yearsal;
void taxcalculation();
};
void incometax::taxcalculation()
{
if(yearsal<200000)
finaltax=0;
cout<<"Nil tax"<<endl;
finaltax=(yearsal-200000)*0.10;
finaltax=(yearsal-200000)*0.20;
else if(yearsal>1000000)
finaltax=(yearsal-200000)*30/100;
public:
void display();
};
void result::display()
int main()
result ob;
ob.getdata();
ob.getsalary();
ob.taxcalculation();
ob.display();
return 0;
OUTPUT:
RESULT:
DATE: NAME:
AIM:
To write a C++ program to display the employee details using abstract class and virtual
function concept.
ALGORITHM:
Step 4: The function emp_info() is again defined in derived class. Here the empno, ename and
dob are declared.
Step 5: The inputs are readed and the output is printed using emp_info() function.
Step 6: In the main function object is created for derived class and pointer object is created for
base class. The address of derived class is assigned to Base object and emp_info() is
invoked.
PROGRAM:
#include<iostream>
class Base
public:
virtual void emp_info() = 0; // pure virtual
function };
public:
int eno;
char ename[20],dob[10];
void emp_info()
cin>>eno;
cin>>ename;
cin>>dob;
cout<<"EMPLOYEE DETAILS"<<endl;
cout<<"NAME:"<<ename<<endl;
cout<<"DOB:"<<dob<<endl;
};
int main()
Base *b;
Derived d;
b = &d;
b->emp_info();
OUTPUT:
RESULT:
DATE: NAME:
AIM:
To write a C++ program for perform simple file operations using ofstream and ifstream.
ALGORITHM:
Step 4: Read the value for name and age from user and write it into the file using os object.
Step 6: Open the file using ifstream object is for reading purpose.
Step 7: Read the characters from the file using getline function until it reaches eof().
PROGRAM:
#include <fstream>
#include <iostream>
int main ()
char input[75];
ofstream os;
os.open("testout.txt");
OUTPUT:
RESULT:
DATE: NAME:
AIM:
To write a C++ program for perform file operations using tellg() and tellp() functions.
ALGORITHM:
Step 2: Create a filestream object file using fstream to perform write/read operations.
Step 4: Pass a string and write it into the file using file object.
Step 5: Get the current position of the file using file.tellp() function.
Step 6: Open the file in read mode and check not eof() condition.
Step 7: Read the characters from the file and print it until it reaches eof().
Step 8: Get the position of the characters readed using file.tellg() function
PROGRAM:
#include <iostream>
#include <fstream>
file.close();
char ch;
while(!file.eof())
\""<<ch<<"\""<<endl;
file.close();
return 0;
}
OUTPUT:
RESULT:
DATE: NAME:
AIM:
To write a C++ program for perform file operations using random access file functions.
ALGORITHM:
Step 2: Create a filestream object myfile using ofstream to perform write operations.
Step 4: Pass a string as input and write it into the file using the myfile object.
Step 6: Open the file in read mode and check not eof() condition
Step7: move the file pointer 7 bytes from beginning of the file using the seekg function
Step 7:Again move the file pointer 2bytes backwards from current location using the seekg
function.
Step 8: Read the all the characters from the file and print it until it reaches eof().
main()
myFile.open("RANDOM.txt", ios::out);
myFile<<ch;
file.open("RANDOM.txt", ios::in);
cout<<" The output (after skipping first 7 bytes and then move 2 bytes back) is: ";
return 0;
RESULT:
PROGRAM:
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 <<
template.\n"; cout << "i1 = " << i1 << "\ni2 = " << i2;
cout << "\nf1 = " << f1 << "\nf2 = " << f2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;
return 0;
}
OUTPUT:
i1 = 1
i2 = 2
f1 = 1.1
f2 = 2.2
c1 = a
c2 = b
i1 = 2
i2 = 1
f1 = 2.2
f2 = 1.1
c1 = b
c2 = a
RESULT:
DATE: NAME:
CLASS TEMPLATE
AIM:
To write a C++ program to display the calculator operations using class templates.
ALGORITHM:
Step2: Create the template using the class as generic type name T.
Step 3: Create the class name as calculator and declare the variables num1,num2as generic type
T.
Step 5: Define the function add(),subtract(),multiply() and divide() with return type as T.
Step 7: In the main() function pass the data type int, float during object creation and call
the displayResult().
PROGRAM:
#include <iostream>
class Calculator
{
private:
T num1, num2;
public:
num1 = n1;
num2 = n2;
void displayResult()
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
};
int main()
{
Calculator<int> intCalc(2, 1);
intCalc.displayResult();
floatCalc.displayResult();
return 0;
OUTPUT:
Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2
Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2
RESULT:
DATE: NAME:
EXCEPTION HANDLING
AIM :
To write a C++ program to demonstrate exception handling using try and catch block.
ALGORITHM:
Step3: Inside the try block check whether the value of b not equal to 0.
Step 4: Divide the value of a/b and assign to div and type cast to float and check div less than 0.
If the condition is true throw the exception else print the div value.
Step 5: In the Catch block – catch the exception and display the message “Exception: Division
by zero, Exception: Division is less than 1, unknown exception”.
PROGRAM:
#include <iostream>
#include <conio.h>
int main()
int a,b;
try
if (b != 0)
if (div < 0)
throw 'e';
else
throw b;
catch (int e)
{
cout << "Exception: Division by zero";
}
{
cout << "Exception: Division is less than 1";
}
catch(...)
{
cout << "Exception: Unknown";
}
getch();
return 0;
}
OUTPUT:
RESULT:
DATE: NAME:
To write a C++ program to display the error message to the user when the car
crossing the speed limit using user defined exception handling .
ALGORITHM:
Step 2: Create a class with class name overspeed which inherits from exception class.
Step3: Declare the variable speed define the user defined function what().
Step 4: The what() function return the error message to the user.
Step 5: In the main() function check the speed of the car. If the carspeed greater than 100, if true
then throw the exception.
Step 6: Catch function handles the exception and print the error message.
PROGRAM:
#include <iostream>
#include <exception>
using namespace std;
int main()
{
int carspeed=0;
try
{
while(1)
{
carspeed+=10;
if(carspeed>100)
{
OverSpeed s;
throw s;
}
cout<<"Carspeed: "<<carspeed<<endl;
}
}
catch(OverSpeed ex)
{
cout<<ex.what();
}
return 0;
}
OUTPUT:
RESULT: The above program is executed successfully and the output is verified.