Object Oriented Programming Lab File
Object Oriented Programming Lab File
Object Oriented Programming Lab File
LAB FILE
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) {}
};
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
OUTPUT:
OUTPUT:
class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
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";
}
};
};
// 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;
};
// 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";
}
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
OUTPUT:
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;
}
};
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
int main( ) {
int x, y;
o3 = o1 + o2; //add to objects, (o1.operator+(02))(03=temp)
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:
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;
}
#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Driver function
int main()
{
Integer i1(3);
#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Driver function
int main()
{
Integer i1(3);
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Driver function
int main()
{
Integer i1(3);
#include <iostream>
using namespace std;
// 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;
}
// + 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:
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";
}
#include <iostream>
using namespace std;
} // end Division
int main()
{
// storing 12.5 in numerator
// and 0 in denominator
float numerator = 12.5;
float denominator = 0;
float result;
} // end main
OUTPUT:
Add given timestamps by overloading +
operator in C++ Time Class
Add given timestamps by overloading +
operator in C++ Time Class