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

OOPS Lab File (4) Edited Saurabh

A C++ program defines a rational class to represent numerical values as fractions. The class includes constructors, a reduce method to simplify fractions, and overloaded operators for input, output, and addition. A main function tests the class by inputting rational numbers, adding them, and outputting the result.

Uploaded by

Aadarsh verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

OOPS Lab File (4) Edited Saurabh

A C++ program defines a rational class to represent numerical values as fractions. The class includes constructors, a reduce method to simplify fractions, and overloaded operators for input, output, and addition. A main function tests the class by inputting rational numbers, adding them, and outputting the result.

Uploaded by

Aadarsh verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Gurugram University

Object Oriented Programming


Lab File
Submitted To:
Prof. Mahima Goyal Submitted
By:
Saurabh
201030050058
B.Tech CSE (AI)
INDEX

PROGRAM PROGRAM NAME DATE REMARKS


NO.

1 Find the power of the number


2 Write a program that uses co-ordinate to model a point
3 Write a program to make a calculator
4 Write a program to create two classes DM and DB which
store the value of distances
5 Create a class rational which represents a numerical value
by two double numerator and denominator
6 Create a base class called shape which store double type
values used to compute areas of shapes
7 Sample program of class
8 Sample program of friend function
9 Sample program of static data members

10 Program to illustrate the implementation of static


member function
11 Program to illustrate the implementation of system
defined default constructor
12 Program to illustrate the implementation of
parameterized constructor
13 Program to illustrate the implementation of copy
constructor
14 Program to illustrate the implementation of destructors

15(a) A program to illustrate single inheritance in public mode

15(b) A program to illustrate single inheritance in private mode

16 A program to illustrate a multilevel inheritance

17 A program to illustrate a multiple inheritance

18(a) Unary operator overloading using member function

18(b) Unary operator overloading using friend function

19(a) Binary operator overloading using member function

19(b) Binary operator overloading using friend function

20 A program to illustrate virtual function


PROGRAM NO-1
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 double value for n and an int value for p and returns the
result as double value. Use 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. Find the power of the number.
#include <iostream> using
namespace std; double
power(double n, int p)
{ if (p ==
0) return
1; if (p ==
1) return
n; if (p &
1)
return n * power(n, p - 1); return
power(n * n, p / 2);
} int
main(void)
{ double n,
r; int p;
cout << "Enter N " << endl; cin
>> n;
cout << "Enter Power To Be Raised" << endl; cin
>> p;
r = p < 0 ? 1 / power(n, -p) : power(n, p); cout
<< n << " ^ " << p << " : " << r << endl; return
0;
}
OUTPUT
PROGRAM NO-2
AIM – A point on the two-dimensional plane can be represented by two numbers: an X
coordinate and a Y coordinate. For example, (4,5) represents a point 4 units to the right of
the origin along the X axis and 5 units up the Y axis. The sum of two points can be defined as
a new point whose X coordinate is the sum of the X coordinated of the points and whose Y
coordinate is the sum of their Y coordinates. Write a program that uses a structure called
point to model a point. Define three points, and have the user input valued to two of them.
Then, set the third point equal to the sum of the other two, and display the value of the new
point.
Write a program that uses co-ordinate to model a point.
#include <iostream>
#include <conio.h>
using std::cin; using
std::cout; using
std::endl; using
std::size_t; Typedef
struct point{ int x;
int y; } pt;
void display(pt pointer){
cout << "Sum of points is " << "("
<< pointer.x << ","
<< pointer.y << ")" << endl;
} int
main(void){ pt
pt1;
cout << "Enter 1st coordinates :"
<< " x = ";
cin >> pt1.x; cout <<
"\t\t\ty = "; cin >>
pt1.y; pt pt2;
cout << "Enter 2nd coordinates :"
<< " x = "; cin
>> pt2.x; cout <<
"\t\t\ty = "; cin >>
pt2.y; pt ans;
ans.x = pt1.x + pt2.x;
ans.y = pt1.y + pt2.y;
display(ans);
getch(); return 0;
}
OUTPUT
PROGRAM NO-3
AIM - Create the equivalent of a four-function calculator. The program should request the
user to enter a number, an operator, and another number. It should then carry out the
specified arithmetical operation: adding, subtracting, multiplying or dividing the two
numbers. (It should use a switch statement to select the operation). Finally, it should display
the result. When it finishes the calculation, the program should ask if the user wants to do
another calculation. The response can be Y or N.
Write a program to make a Calculator.
#include <iostream>
using std::cin;
using std::cout;
int main(void){
char oprtr, c; int
a, b; do{
cout << "Enter numbers ";
cin >> a >> b;
cout << "Enter operator from : add(+), subtract(-),product(*),
quotient(/), modulo(%)\n"; cin >> oprtr; switch (oprtr){
case '+': cout << "Answer=" << a + b;
break;
case '-': cout << "Answer=" << a - b;
break;
case '*': cout << "Answer=" << a * b;
break;
case '/': cout << "Answer=" << a / b;
break;
case '%': cout << "Answer=" << a % b;
break;
default:cout << "This operator is not present";
break;
} do{
cout << "\n Do another(y/n)?:";
cin >> c; if (c != 'y' && c !=
'n') cout << "Invalid choice";
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
} while (c == 'y' || c == 'Y');
return 0;
}
OUTPUT
PROGRAM NO-4
AIM - To Create two classes DM and DB which store the value of distances. DM stores
distances in metres and centi-meters and DB in feet and inches. Write a program that can
read values for the class objects and add one object of DM with other 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 meters and centi-meters depending on
the object.
#include <iostream>
using namespace std;
using std::size_t;
class DB; class DM{
public:
float meter, centi;
void getdata(){
cout << "\nEnter the distance in (meter-centimeter):";
cout << "\nInput meter part "; cin >> meter;
cout << "\nInput centimeter part ";
cin >> centi;
} void display(){
cout << "\nThe distance is:";
cout << meter << "meters and " << centi << "centimeters";
}
friend void add(DM &, DB &);
};
class DB{
public:
float inch, feet;
void getdata(){ cout
<< "\nEnter the distance in
(feet-inch):"; cout <<
"\nInput feet part ";
cin >> feet;
cout << "\nInput inch part ";
cin >> inch;
}
void display(){
cout << "\nThe distance is: ";
cout << feet << "feet and " << inch << "inches";
}
friend void add(DM &, DB &);
};
void add(DM &a, DB &b){
int ch;
cout << "\nPress 1 for meter-centi and 2 for feet-inch" <<
endl;
cout << "Enter your choice:";
cin >> ch; if (ch == 1){
DM d;
int c = (a.meter * 100 + a.centi + b.feet * 30.48 + b.inch *
2.54);
if (c >= 100){
d.meter = c / 100;
d.centi = c % 100;
} else{
d.meter = 0;
d.centi = c;
}
d.display();
}
else{
DB d;
int i = (a.meter * 39.37 + a.centi * .3937008 + b.feet * 12 +
b.inch); if (i >= 12){
d.feet = i / 12;
d.inch = i % 12;
} else{
d.feet = 0;
d.inch = i;
}
d.display();
}
}
int main(void){
DM a; DB b;
a.getdata();
b.getdata();
add(a, b); return
0;
}
OUTPUT
PROGRAM NO-5
AIM - Create a class rational which represents a numerical value by two double
NUMERATOR & DENOMINATOR. Include the following public member Functions
1. Constructor with no arguments (default).
2. Constructor with two arguments.
3. void reduce that reduces the rational number by eliminating the highest common factor
between the numerator and denominator
4. Overload + operator to add two rational number.
5. Overload >> operator to enable input through cin.
6. Overload << operator to enable output through cout.
7. Write a main() to test all the functions in the class.
#include <iostream>
#include <conio.h>
using namespace std;
class rational{
double num, denum;
public: rational(){
num = 1; denum = 1;
}
rational(double a, double b){
num = a; denum = b; }
void reduce(){ long x, y;
x = num; y = denum; int
z; if (num > denum){
do{ z
= x % y;
x = y; y
= z;
} while (z != 0);
num /= x; denum /=
x;
}
else{ do{
z = y % x;
y = x; x
= z;
} while (z != 0);
num /= y; denum /=
y;
}
}
rational operator+(rational r){
rational r1;
r1.denum = denum * r.denum;
r1.num = (num * r.denum) + (r.num * denum);
return r1; }
friend void operator>>(istream &, rational &);
friend void operator<<(ostream &, rational &);
};
void operator>>(istream &in, rational &r){
cout << "\nEnter numerator and denominator for the rational
number:";
in >> r.num >> r.denum;
}
void operator<<(ostream &out, rational &r){
cout << "\nThe rational number is:" << r.num << "/" << r.denum;
} int main(void){
rational r1, r2(10, 20), r3;
cout << r2; r2.reduce();
cout << r2; cin >> r1;
r3 = r1 + r2; cout << r3;
r3.reduce(); cout << r3;
getch(); return 0;
}

OUTPUT
PROGRAM NO-6
AIM - Create a base class called shape. Use this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called triangle and
rectangle from the base shape. And the base class, a member function get_data() to
initialize base class data members and another member function display_area() to compute
and display the area of figures. Make display area() as a virtual function and define this
function in the derived classes to suit their requirements. Using these three classes, design a
program that will accept dimensions of a triangle or a rectangle interactively, and display
the area. Remember the two values given as input will be treated as lengths of two sides in
the case of rectangles, and as base and height in the case of triangles, and used as follows:
area of rectangle = x*y, area of triangle = ½x*y.
#include <iostream>
using namespace std;
class Shape {
protected: double l,
b; public:
void getdata(double l, double b) { this-
>l = l; this->b = b;
}
void virtual display_area() = 0;
};
class triangle : public Shape { public:
void display_area() {
cout << "\nArea of Triangle:" << (0.5 * l * b);
} };
class Rectangle : public Shape { public:
void display_area() {
cout << "\nArea of Rectangle:" << (l * b);
}
}; int
main(void) {
Shape *s1; triangle t1; Rectangle r1; s1 = &t1;
s1->getdata(30, 20); s1->display_area(); s1 = &r1;
s1->getdata(6, 5); s1->display_area(); return 0;
}
OUTPUT
PROGRAM NO-7
AIM - Sample program of class.
#include <iostream>
using namespace std;
class item {
private:
int number; float cost; public:
void getdata(int a, float b) { number
= a; cost = b;
} void putdata() { cout <<
"Number:" << number<<endl; cout
<< "Cost:" << cost;
} }; int
main(void) {
item x;
x.getdata(14, 2.5);
x.putdata(); return
0;
}
OUTPUT
PROGRAM NO-8
AIM - Sample program of friend function.
#include <iostream> using
namespace std; class
sample { int a, b; friend
void print(sample);
}; void print(sample
s) {
s.a = 10;
s.b = 20; cout << "a="
<< s.a; cout << "\nb="
<< s.b;
} int main(void)
{ sample s;
print(s); return
0;
}
OUTPUT

PROGRAM NO-9
AIM - Sample program of static data members.
#include <iostream>
using namespace std;
class test { public:
static int a; void
geta() { cout <<
"\na" << a;
} }; int test::a =
10; int main(void) {
test::a = 100; test
t; cout << "\na" <<
t.a;
t.geta(); return
0;
}

OUTPUT
PROGRAM NO-10
AIM - Program to illustrate the implementation of static member functions.
#include <iostream>
using namespace std;
class test { int
code; static int
count; public:
void setcode(){ code
= ++count;} void
showcode(){
cout << "Object number:" << code << "\n";}
static void showcount(){ cout << "count:"
<< count << "\n";
} }; int
test::count; int
main(void){ test
t1, t2;
t1.setcode();
t2.setcode();
test::showcount()
; test t3;
t3.setcode();
test::showcount()
; t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
OUTPUT
PROGRAM NO-11
AIM - Program to illustrate the implementation of system defined default constructor.
#include <iostream> using
namespace std; class test {
int a, b; public: void get()
{ a = 10; b = 20; } void
show() { cout << "a = " << a
<< "\n"; cout << "b = " << b
<< "\n";
} }; int
main(void) {
test t;
t.get();
t.show(); return
0;
}

OUTPUT
PROGRAM NO-12
AIM - Program to illustrate the implementation of parameterized constructor.
#include <iostream> using
namespace std; class integer
{ int m, n; public:
integer(int x, int y) { m =
x; n = y; } void display() {
cout << "m = " << m << "\n";
cout << "n = " << n << "\n";
} }; int main(void) { integer
int1(0, 100); integer int2 =
integer(10, 20); cout <<
"Object 1 \n"; int1.display();
cout << "Object 2 \n";
int2.display(); return 0;
}

OUTPUT
PROGRAM NO-13
AIM - Program to illustrate the implementation of copy constructor.
#include <iostream>
using namespace std;
class code { int id;
public: code(int a)
{ id = a; }
code(code &x) { id =
x.id; } void
display(void) { cout
<< id << "\n";
} }; int main(void)
{ code A(100); code
B(A); code C = A;
cout << "id of A :";
A.display(); cout <<
"id of B :";
B.display(); return
0;
}
OUTPUT
PROGRAM NO-14
AIM - Program to illustrate the implementation of destructors.
#include <iostream>
using namespace std;
int count; class
alpha { public:
alpha() { count++;
cout << "Object created " << count << endl;
} ~alpha()
{
cout << "Object destroyed " << count << endl;
count--; }}; int main(void) { cout << "Main
Block" << endl; alpha A1, A2, A3, A4;
{
cout << "Inner block" << endl;
alpha A5; cout << "Exit" <<
endl;}
{
cout << "Inner block 2" << endl;
alpha A6; cout << "Exit" << endl;}
cout << "Main block exit" << endl;
return 0;}
OUTPUT
PROGRAM NO-15(a)
AIM - Program to illustrate single inheritance in public mode.
#include <iostream>
using namespace std;
class B { int a;
public: int b; void
get_ab(); int
get_a(void); void
show_a(); }; class D
: public B { int c;
public:
void mul(void); void
display(void);
}; void B::get_ab(void) {
a = 5; b = 10; } int
B::get_a() { return a; }
void B::show_a() { cout <<
"a:" << a << endl;
} void D::mul()
{ c = b *
get_a();
} void D::display()
{ cout << "a=" <<
get_a() << endl;
cout << "b=" << b
<< endl; cout <<
"c=" << c << endl;
} int main(void)
{
D d;
d.get_ab(); d.mul();
d.show_a(); d.display();
d.b = 20;
d.mul();
d.display();
return 0; }

OUTPUT
PROGRAM NO-15(b)
AIM - Program to illustrate single inheritance in private mode.
#include <iostream>
using namespace std;
class student { int
id; char name[10];
public:
void getstu() { cout <<
"Enter ID" << endl; cin >>
id;
cout << "Enter the name" << endl;
cin >> name; } void putstu() {
cout << "ID " << id << endl; cout
<< "Name " << name << endl;
} };
class phy : private student {
float height, weight; public:
void getphy() {
cout << "Enter the height in cm" << endl;
cin >> height;
cout << "Enter the weight in Kg" << endl;
cin >> weight; getstu(); } void putphy()
{
cout << "Height " << height << endl;
cout << "Weight " << weight << endl;
putstu(); } }; int main(void) { phy
p;
p.getphy();
p.putphy();
return 0; }
OUTPUT
PROGRAM NO-16
AIM - Program to illustrate multi-level inheritance.
#include <iostream>
using namespace std;
class student {
protected: int
rollNo; public:
void get_number(int); void
put_number(void);
};
void student::get_number(int a) {
rollNo = a; }
void student::put_number(void) { cout <<
"Roll Number:" << rollNo << endl;
}
class test : public student {
protected: float subject1;
float subject2; public:
void get_marks(float, float); void
put_marks(void);
};
void test::get_marks(float a, float b) {
subject1 = a; subject2 = b; }
void test::put_marks(void) {
cout << "Marks in subject 1:" << subject1 << endl; cout << "Marks in subject
2:" << subject2 << endl;
}
class result : public test {
float total; public:
void display() { total =
subject1 + subject2;
put_number(); put_marks();
cout << "Total=" << total << endl;
} }; int main(void) {
result r1;
r1.get_number(54);
r1.get_marks(50, 30);
r1.display(); return
0;
}
OUTPUT
PROGRAM NO-17
AIM - Program to illustrate multiple inheritance.
#include <iostream>
using namespace std;
class student { int
id; char name[30];
public:
void getstu() { cout <<
"Enter ID" << endl; cin >>
id;
cout << "Enter the name" << endl;
cin >> name; } void putstu() {
cout << "ID " << id << endl; cout
<< "Name " << name << endl;
} }; class
marks {
protected: int
m1, m2, m3;
public:
void getmarks() { cout << "Enter
marks: " << endl; cout << "M1:"
<< endl; cin >> m1; cout << "M2:"
<< endl; cin >> m2; cout << "M3:"
<< endl; cin >> m3;
} void putmarks() { cout <<
"M1:" << m1 << endl; cout <<
"M2:" << m2 << endl; cout <<
"M3:" << m3 << endl;

} };
class result : public student, public marks {
int total; float average; public: void show()
{ total = m1 + m2 + m3; average = (total) /
3; cout << "Total=" << total << endl; cout <<
"average=" << average << endl;
} }; int
main(void) {
result r1;
r1.getstu();
r1.getmarks();
r1.putstu();
r1.putmarks();
r1.show();
return 0;
}
OUTPUT
PROGRAM NO

-18(a)
AIM - Unary operator overloading using member function.
#include <iostream> using
namespace std; class
complex{ private: int a;
int b; public: void
setData(int x,int y){ a=x;
b=y; } void showData(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
} complex operator-
(){ complex temp;
temp.a=-a; temp.b=-
b; return temp;}};
int main(){ complex
c1,c2;
c1.setData(99,23);
c2=-c1;
c2.showData();
return 0;}

OUTPUT
PROGRAM NO

-18(b)
AIM - Unary operator overloading using friend function.
#include <iostream> using
namespace std; class complex
{ private: int a; int b;
public: void setData(int x,
int y) { a = x; b = y; }
void showData() { cout <<
"a=" << a << endl; cout <<
"b=" << b << endl;
}
friend complex operator-(complex);};
complex operator-(complex X) {
complex temp; temp.a = -X.a; temp.b
= -X.b; return temp;} int main(void)
{ complex c1, c2; c1.setData(88,
27); c2 = -c1; c2.showData(); return
0;}

OUTPUT
PROGRAM NO

-19(a)
AIM - Binary operator overloading using member function.
#include <iostream> using
namespace std; class complex {
private: int a; int b; public:
void setData(int x, int y) { a
= x; b = y; } void showData()
{ cout << "a=" << a << endl;
cout << "b=" << b << endl;}
complex operator+(complex c) {
complex temp; temp.a = a +
c.a; temp.b = b + c.b; return
temp;}}; int main(void) {
complex c1, c2, c3;
c1.setData(23, 45);
c2.setData(27, 33); c3 = c1 +
c2; c3.showData(); return 0;}
OUTPUT
PROGRAM NO-19(b)
AIM - Binary operator overloading using friend function.
#include <iostream> using
namespace std; class complex
{ private: int a; int b;
public: void setData(int x,
int y) { a = x; b = y;} void
showData() { cout << "a=" <<
a << endl; cout << "b=" << b
<< endl;}
friend complex operator+(complex X, complex Y);};
complex operator+(complex X, complex Y) {
complex temp; temp.a = X.a + Y.a; temp.b = X.b +
Y.b; return temp;} int main(void){ complex c1,
c2, c3; c1.setData(33, 433); c2.setData(28, 52);
c3 = c1 + c2; c3.showData(); return 0;
}

OUTPUT
PROGRAM NO-20
AIM - Program to illustrate virtual function.
#include <iostream>
using namespace std;
class Base { public:
void display() { cout <<
"Display base" << endl;} virtual
void show() { cout << "Show
Base" << endl;}}; class Derived
: public Base { public:
void display() { cout << "Display
Derived" << endl;} void show() {
cout << "Show Derived" << endl;}};
int main(void) { Base B;
Derived D; Base
*bptr;
cout << "bptr points to base" << endl;
bptr = &B; bptr->display(); bptr-
>show();
cout << "bptr points to Derived" << endl;
bptr = &D; bptr->display(); bptr->show();
return 0;}

OUTPUT

You might also like