OOPS Lab File
OOPS Lab File
SAMALKHA
Submitted To:
B.Tech CSE
Affiliated to:
Aim: Write a function called reversit ( ) that reverses a string (an array of
char). Use a for loopthat swaps the first and last characters, then the second
and next to last characters and so on. The string should be passed to reversit
( ) as an argument. Write a program to exercise reversit ( ). The program
should get a string from the user, call reversit ( ), and print out the result.
SOURCE CODE:-
#include <iostream>
#include <cstring>
void reversit(char[]);
int main()
char str[MAX];
std::cin.get(str, MAX);
reversit(str);
std::cout << "Reversed string is: " << str << std::endl;
return 0;
}
void reversit(char s[])
s[len - j - 1] = temp;
OUTPUT:
PRACTICALNO: 2
AIM:- Write a program that display the menu of Dominos, receives order &
display their bill with tax using Class and objects.
SOURCE CODE:-
};
do
{ cout<<"\nEnter the serial no of an item from the menu";
cin>>srno;
switch(srno)
{
case 1: cout<<"\nYou have ordered Cheese & Tomato";
price=200;
cout<<"\nPrice is\tRs."<<price;
sum=sum+price;
break;
}
void dominos::display( ) //Member Function
{
tax=(float)(sum*12)/100;
cout<<"\n Sub Total:-\tRs."<<sum;
cout<<"\n Tax 12% \tRs."<<tax;
cout<<"\n Total Amount to be paid:-\tRs."<<sum+tax;
cout<<"\n\t\t\t ******Thanks******";
int main( )
{
dominos d; //Declaration of a class member
d.getmenu( ); // Calling of Member Function of a class using
object
d.display( ); // Calling of Member Function of a class using
object
return 0;
cin.get( );
cin.ignore( );
}
Output:
PRACTICAL NO: 3
SOURCE CODE:-
int main( )
{
measure x,y;
x.get();
y.get();
x.increament(y);
x.display();
return 0;
cin.get( );
cin.ignore( );
Output:
PRACTICAL NO. 4
AIM:- Imagine a tollbooth with a class called toll Booth. The two data items
are a type unsigned int to hold the total number of cars, and a type double to
hold the total amount of money collected. A constructor initializes both these
to 0. A member function calledpayingCar ( ) increments the car total and adds
0.50 to the cash total. Another function, called nopayCar ( ), increments the
car total but adds nothing to the cash total. Finally, a member function called
displays the two totals.
SOURCE CODE:-
#include <iostream>
class tollbooth {
private:
double totalcash;
public:
tollbooth() {
totalcars = 0;
totalcash = 0;
void payingcar() {
totalcars += 1;
totalcash += TOLL;
void nopaycar() {
totalcars += 1;
void display() {
};
int main() {
tollbooth booth1;
char ch;
do {
ch = getchar();
if (ch == '0')
booth1.nopaycar();
if (ch == '1')
booth1.payingcar();
booth1.display();
return 0;
OUTPUT:
PRACTICAL NO.5
AIM:-Make a class Employee with a name and salary. Make a class Manager
inherit from Employee. Add an instance variable, named department, of type
string. Supply a method to string that prints the manager s name, department
and salary. Make a class Executive inherit from Manager. Supply a method to
String that prints the string Executive followed by the information stored in
the Manager superclass object. Supply a test program that tests these classes
and methods.
OBJECTIVE:- Inheritance means using the Pre-defined Code. This is very Main
Feature of OOPS. With the advantage of Inheritance we can use any code that is
previously created. With the help of inheritance we uses the code that is previously
defined but always Remember, We are only using that code but not changing that
code. There are various types of inheritance & Multilevel Inheritance is one of
them. Multilevel Inheritance is a method where a derived class is derived from
another derived class.
SOURCE CODE:-
#include <iostream>
#include <string>
class Employee {
protected:
string name;
public:
};
class Manager : public Employee {
protected:
string department;
public:
void toString() {
cout << "Name: " << name << endl << "Deptt: " << department << endl <<
"Salary: Rs." << salary << endl;
};
public:
void toString() {
Manager::toString();
};
int main() {
e.toString();
return 0;
OUTPUT:-
PRACTICAL NO.6
AIM:- Create two classes DM and DB which store the value of distances. DM
stores distances in metres and centimeters and DB in feet and inches. Write a
program that can read values for the class objects and add one object of DM
with another object of DB. Use a friend function to carry out the addition
operation. The object that stores the results maybe a DM object or DB object,
depending on the units in which the results are required. The display should
be in the format of feet and inches or metres and cenitmetres depending on
the object on display.
SOURCE CODE:-
#include<iostream>
class DM {
int metres;
float centimetres;
public:
DM() {
metres = 0;
centimetres = 0.00;
}
metres = m;
centimetres = cm;
int get_m() {
return metres;
float get_cm() {
return centimetres;
void getdata() {
void display() {
};
class DB {
int feet;
float inches;
public:
DB() {
feet = 0;
inches = 0.00;
DB(int f, float i) {
feet = f;
inches = i;
DB(DM dm) {
int m;
m = dm.get_m();
cm = dm.get_cm();
operator DM() {
void getdata() {
void display() {
};
DM a(db);
cm -= 100.00;
m++;
}
int main() {
DB db, db1;
DM dm, dm1;
cout << "enter distance d1 (in metres & centimetres):" << endl;
dm.getdata();
cout << "enter distance d2 (in feet & inches):" << endl;
db.getdata();
dm.display();
db.display();
dm1.display();
db1.display();
return 0;}
Output:
PRACTICAL NO.7
SOURCE CODE:-
#include<iostream>
class Rational{
float num,denom;
public:
Rational()
{
if(denom==0)
exit(1);
this->num = num;
this->denom = denom;
Rational reduce()
Rational temp;
temp.num = num/h;
temp.denom = denom/h;
return temp;
Rational ans;
ans.denom = denom*r.denom;
ans.num = num*r.denom+r.num*denom;
return ans;
}
int hcf(int a, int b);
};
int r;
r = a%b;
while(r)
a=b;
b=r;
r=a%b;
return b;
int a,b;
char c;
s>>a>>c>>b;
if(c!='/')
{
cout<<"use of invalid notation";
exit(0);
if(b==0)
exit(1);
r.num=a;
r.denom=b;
return s;
if(r.denom==1)
s<<r.num;
else
if(r.denom==-1)
s<<-r.num;
else
s<<r.num<<'/'<<r.denom;
}
return s;
int main()
Rational r1,r2,r3;
cout<<"enter r1:";
cin>>r1;
cout<<"enter r2:";
cin>>r2;
r3=r1+r2;
r3 = r3.reduce();
cout<<r3<<endl;
return 0;
Output:
PRACTICAL NO.8
OBJECTIVE:- In C++ the overloading principle applies not only to functions, but
to operators too. A programmer can provide his or her own operator to a class by
overloading the built-in operator to perform some specific computation when the
operator is used on objects of that class. On the other hand, operator overloading,
like any advanced C++ feature, makes the language more complicated. There are
many operators which can be overloaded such as unary operator (‘+’, ‘-‘ ,’++’),
Binary operator(‘+’, ‘-‘ ,’/’) ,
Left shift & right shift (<< & >>), etc. A unary operator is one which is defined
over a single operand whereas binary operator is defined over two operand.
SOURCE CODE:-
(a) Program of Overloading Unary operator….
OUTPUT:
(b)Program of Overloading Binary operator….
#include<iostream> //Header file….
using namespace std;
class integer
{
private:
int x;
float y;
public:
integer(int s=0,float t=0.0)
{
x=s;
y=t;
}
integer operator+ (integer &m)
{
integer t;
t.x=x+m.x;
t.y=y+m.y;
return t;
}
void display( )
{
cout<<"\nx="<<x;
cout<<"\ny="<<y<<"\n";
}
};
int main( )
{
integer a(5,7.8),b(3,8.9);
integer c;
cout<<"\nFor a:";
a.display( );
cout<<"\nFor b:";
b.display( );
c=a+b;
cout<<"\nThe sum is:";
c.display( );
return 0;
}
OUTPUT:
int main( )
{
date dt;
cout<<"\nEnter your date:";
cin>>dt;
cout<<"\nThe date entered is:";
cout<<dt;
return 0;
cin.get( );
cin.ignore( );
}
OUTPUT:
PRACTICAL NO.9
AIM:- Consider the following class definition
class father {
protected : int age;
public:
father (int x) {age = x;}
virtual void iam ( )
{ cout < < “I AM THE FATHER, my age is : ”<< age<< end1:}
};
Derive the two classes son and daughter from the above class and for each,
define iam ( ) to write
our similar but appropriate messages. You should also define suitable
constructors for these
classes. Now, write a main ( ) that creates objects of the three classes and then
calls iam ( ) for
them. Declare pointer to father. Successively, assign addresses of objects of
the two derived
classes to this pointer and in each case, call iam ( ) through the pointer to
demonstrate
polymorphism in action.
SOURCE CODE:-
#include <iostream>
class father {
protected:
unsigned int age;
public:
father() {
age = 60;
father(int x) {
age = x;
cout << "I AM THE FATHER, my age is: " << age << endl;
};
public:
son() {
age = 30;
son(int x) {
age = x;
cout << "I AM THE SON, my age is: " << age << endl;
}
};
public:
daughter() {
age = 24;
daughter(int x) {
age = x;
cout << "I AM THE DAUGHTER, my age is: " << age << endl;
};
int main() {
son s(23);
daughter d(16);
****************\n\n";
f.iam();
s.iam();
****************\n\n";
d.iam();
ptrf = &s;
SON OBJECT*************\n\n";
ptrf->iam();
ptrf = &d;
ptrf->iam();
return 0;
Output:
PRACTICAL NO.10
Aim:- Raising a number n to a power p is the same as multiplying n by itself p
times. Write a function called power ( ) that takes a double value for n and an
int value for p, and returns the result as double value. Use a default argument
of 2 for p, so that if this argument is omitted, the number will be squared.
Write a main ( ) function that gets values from the user to test this function.
(2*2*2……………..n times)
&
If the argument p=2 is omitted then Square of a given number n can be find
out as:
SOURCE CODE:-
#include <iostream>
#include <cmath>
int main() {
double n, r;
int p;
char c;
std::cin >> n;
do {
std::cin >> c;
if (c == 'y') {
std::cin >> p;
r = power(n, p);
} else if (c == 'n') {
p = 2;
r = power(n);
} else {
std::cout << "Invalid choice. Please enter 'y' or 'n'." << std::endl;
std::cout << n << "^" << p << " (" << n << " raised to the power " << p << ") = "
<< r << std::endl;
return 0;
double r = 1;
if (p < 0) {
r = 1 / pow(n, -p);
} else {
r *= n;
}
}
return r;
Output: