Object Oriented Programming Lab File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

OBJECT ORIENTED PROGRAMMING

LAB FILE

Submitted by: Ritika Punia


Enrollment Number: 02201032020
Subject code: BIT-204
B.Tech (IT), 2 nd year
LAB-1

1.Write a C++ program to illustrate the use of friend function.

CODE:
// C++ program to demonstrate the working of friend function
#include <iostream>
using namespace std;

class Distance {
private:
int meter;

// friend function
friend int addFive(Distance);

public:
Distance() : meter(0) {}

};

// friend function definition


int addFive(Distance d) {
//accessing private members from the friend function
d.meter += 5;
return d.meter;
}

int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}

OUTPUT:

2. Write a C++ program to find the speed difference of car and


truck using friend function. Declare the function as friend of
both classes.
CODE:
#include<iostream>
using namespace std;
class truck;
class car
{
int car_speed;
public:
void set(int x)
{
car_speed = x;
}
void get()
{
cout<<" value of a is: "<<car_speed<<endl;
}
friend void difference(class car, class truck);
};
class truck
{
int truck_speed;
public:
void set(int x)
{
truck_speed = x;
}
void get()
{
cout<<" value of a is: "<<truck_speed<<endl;
}
friend void difference(class car, class truck);
};
void difference(car OB1, truck OB2)
{
int d = OB1.car_speed - OB2.truck_speed;
if (d>0)
{
cout<<"speed of car is greater than truck by "<<d<<endl;
}
else if (d<0)
{
cout<<"speed of truck is greater than car by "<< -d<<endl;
}
else
cout<<" speed of car is equal to speed of truck"<<endl;
}
int main()
{
car obj1;
truck obj2;
obj1.set(5);
obj2.set(10);
difference(obj1, obj2);
}
3. Write a C++ program to find the speed difference of car and
truck using friend function. Declare the function as friend of
one class and public member of another class.
CODE:
#include <iostream>

using namespace std;


class car{
int speed car;
public:
void input(){
cout<<"Enter the speed of car";
cin>>speed car;
}
friend class truck;
};
class truck{
int speed truck;
public:
void input(){
cout<<"Enter the speed of truck";
cin>>speed truck;
}
void difference(car obj1){
if(obj1.speed_car>speed truck){
cout<<"difference in speed is"<<obj1.speed_car-speed truck;
}
else{
cout<<"difference in speed is"<<speed truck-obj1.speed_car;
}
}
};
int main(){
car obj 1;
truck obj 2;
obj1.input();
obj2.input();
obj2.difference(obj1);
}

OUTPUT:

4.Write a C++ program that illustrates the concept of virtual


base class
CODE:
#include <iostream>
using namespace std;

class A {
public:
int a;
A() // constructor
{
a = 10;
}
};

class B : public virtual A {


};

class C : public virtual A {


};

class D : public B, public C {


};

int main()
{
D object; // object creation of class d
cout << "a = " << object.a << endl;

return 0;
}
5. Design a single C++ program illustrating the following
concept of inheritance: -
i. Single inheritance
ii. Multiple Inheritance
ii. Multilevel Inheritance
CODE:
// C++ program to explain
// Single inheritance
#include<iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

// sub class derived from a single base classes


class Car : public Vehicle {

};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
// C++ program to explain
// multiple inheritance
#include<iostream>
using namespace std;

// first base class


class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

// second base class


class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};

// sub class derived from two base classes


class Car : public Vehicle, public FourWheeler {

};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
// C++ program to implement
// Multilevel Inheritance
#include<iostream>
using namespace std;

// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

// first sub_class derived from class vehicle


class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
// sub class derived from the derived base class fourWheeler
class Car: public fourWheeler {
public:
Car()
{
cout << "Car has 4 Wheels\n";
}
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
OUTPUT:

6. Design a single C++ program illustrating the following


concept of inheritance: -
i. Public derivation
ii. Private derivation
iii. Protected derivation #include<bits/stdc++.h>
CODE:
using namespace std;
class A
{
private:
void pri_A()
{
cout<<"private of A"<<endl;
}
protected:
void pro_A()
{
cout<<"protected of A"<<endl;
}
public:
void pub_A()
{
cout<<"public of A"<<endl;
}
};
class B :public A
{
};
class C :protected A
{
};
class D :private A
{
};
int main()
{
class B b;
// b.pri_A(); it will throw an error as private member cannot be
inherited
// b.pro_A(); it will throw an error as protected member cannot be
accessed by an object of class
b.pub_A();
class C c;
// c.pri_A(); it will throw an error as private member cannot be
inherited
// c.pro_A(); it will throw an error as protected member cannot be
accessed by an object of class
// c.pub_A(); it will throw an error as protected member cannot be
accessed by an object of class
class D d;
// d.pri_A(); it will throw an error as private member cannot be
inherited
// d.pro_A(); it will throw an error as private member cannot be
accessed by an object of class
// d.pub_A(); it will throw an error as private member cannot be
accessed by an object of class
}

7. Write a C++ program to illustrate the operator overloading


for addition, subtraction and multiplication of Complex
Numbers (real & imaginary part) using Member Function and
Friend Function.

CODE:
// overload the + relative to coord class
#include <iostream>
using namespace std;
class coord {

int x, y;
// coordinate values public:
coord( ) {
x = 0;
y = 0;
}
coord(int i, int j) {
x = i;
y = j;
}

void get_xy(int &i, int &j) {


i = x;
j = y;
}

coord operator+(coord ob2);

};

// Overload + relative to coord class. coord coord::operator+(coord ob2)


{

coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;

int main( ) {

coord o1(10, 10), o2(5, 3), o3;

int x, y;
o3 = o1 + o2; //add to objects, (o1.operator+(02))(03=temp)

// this calls operator+()

o3.get_xy(x, y);

cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";

return 0;

#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag; }
// Operator overloading
Complex operator - (Complex c2) {
Complex temp;
temp.real = real - c2.real;
temp.imag = imag - c2.imag; return temp;
}
void output() {
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag <<
"i";
}
};
int main() {
Complex c1, c2, result;
cout<<"Enter first complex number:\n";
c1.input();
cout<<"Enter second complex number:\n";
c2.input();
// In case of operator overloading of binary operators in C++ programming,
// the object on right hand side of operator is always assumed as argument by
compiler.
result = c1 - c2;
result.output();
return 0;
}

OUTPUT:

8. Write a C++ program to illustrate the operator overloading for


++ and – for both post and pre functionalities.

CODE:
// C++ program to demonstrate
// prefix increment operator overloading

#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;

public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}

// Overloading the prefix operator


Integer operator++()
{
Integer temp;
temp.i = ++i;
return temp;
}

// Function to display the value of i


void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);

cout << "Before increment: ";


i1.display();

// Using the pre-increment operator


Integer i2 = ++i1;

cout << "After pre increment: ";


i2.display();
}
// C++ program to demonstrate
// postfix increment operator overloading

#include <bits/stdc++.h>
using namespace std;

class Integer {
private:
int i;

public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}

// Overloading the postfix operator


Integer operator++(int)
{
Integer temp;
temp.i = i++;
return temp;
}

// Function to display the value of i


void display()
{
cout << "i = " << i << endl;
}
};

// Driver function
int main()
{
Integer i1(3);

cout << "Before increment: ";


i1.display();
// Using the post-increment operator
Integer i2 = i1++;

cout << "After post increment: ";


i2.display();
}
// C++ program to demonstrate
// prefix decrement operator overloading

#include <bits/stdc++.h>
using namespace std;

class Integer {
private:
int i;

public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}

// Overloading the prefix operator


Integer operator--()
{
Integer temp;
temp.i = --i;
return temp;
}

// Function to display the value of i


void display()
{
cout << "i = " << i << endl;
}
};

// Driver function
int main()
{
Integer i1(3);

cout << "Before decrement: ";


i1.display();

// Using the pre-decrement operator


Integer i2 = --i1;

cout << "After pre decrement: ";


i2.display();
}
// C++ program to demonstrate
// postfix decrement operator overloading
#include <bits/stdc++.h>
using namespace std;

class Integer {
private:
int i;

public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}

// Overloading the postfix operator


Integer operator--(int)
{
Integer temp;
temp.i = i--;
return temp;
}

// Function to display the value of i


void display()
{
cout << "i = " << i << endl;
}
};

// Driver function
int main()
{
Integer i1(3);

cout << "Before decrement: ";


i1.display();

// Using the post-decrement operator


Integer i2 = i1--;

cout << "After post decrement: ";


i2.display();
}
OUTPUT:
9. Write a C++ program to implement + operator overloading
to add two timestamps
CODE:
// C++ program to implement + operator
// overloading to add two timestamps

#include <iostream>
using namespace std;

// Time class template


class Time {
private:
int HR, MIN, SEC;

// Defining functions
public:
// Functions to set the time
// in the Time class template
void setTime(int x, int y, int z)
{
HR = x;
MIN = y;
SEC = z;
}

// Function to print the time


// in HH:MM:SS format
void showTime()
{
cout << endl
<< HR << ":" << MIN << ":" << SEC;
}

// Function to normalize the resultant


// time in standard form
void normalize()
{
MIN = MIN + SEC / 60;
SEC = SEC % 60;
HR = HR + MIN / 60;
MIN = MIN % 60;
}

// + Operator overloading
// to add the time t1 and t2
Time operator+(Time t)
{
Time temp;
temp.SEC = SEC + t.SEC;
temp.MIN = MIN + t.MIN;
temp.HR = HR + t.HR;
temp.normalize();
return (temp);
}
};

// Driver code
int main()
{
Time t1, t2, t3;
t1.setTime(5, 50, 30);
t2.setTime(7, 20, 34);

// Operator overloading
t3 = t1 + t2;

// Printing results
t1.showTime();
t2.showTime();
t3.showTime();

return 0;
}
OUTPUT:

Write a program to handle exception different try of exceptions


in C++.
CODE:
#include <iostream>
using namespace std;

int main()
{
int x = -1;

// Some code
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}

cout << "After catch (Will be executed) \n";


return 0;
}
OUTPUT:

28. Division by zero is an exception. Write a C++ program to


detect such an exception and handle it by displaying an
appropriate message and exit from the program.
CODE:
// Program to show division without using
// Exception Handling

#include <iostream>
using namespace std;

// Defining function Division


float Division(float num, float den)
{
// return the result of division
return (num / den);

} // end Division

int main()
{
// storing 12.5 in numerator
// and 0 in denominator
float numerator = 12.5;
float denominator = 0;
float result;

// calls Division function


result = Division(numerator, denominator);

// display the value stored in result


cout << "The quotient of 12.5/0 is "
<< result << endl;

} // end main

OUTPUT:
Add given timestamps by overloading +
operator in C++ Time Class
Add given timestamps by overloading +
operator in C++ Time Class

You might also like