OOPs File University Experiments (2 To 8)
OOPs File University Experiments (2 To 8)
SOURCE CODE:
#include <iostream>
using namespace std;
public:
// Default constructorComplex()
{
real = 0;
imag = 0;
}
};
int main()
{
// Default constructor usedComplex C;
// Parameterised constructor usedComplex A(-2);
// Parameterised constructor usedComplex B(7, -3);
AIM: To write a C++ program to find the greater of two given private numbers intwo
different classes using a Friend function.
SOURCE CODE:
#include <iostream>
using namespace std;
class A
{
// Private member of A.
int num;
public:
class B
{
// Private member of Bint num;
public:
int main()
{
// Create objects of both classesA a_obj;
B b_obj;
return 0;
}
OUTPUT:
Experiment - 4
SOURCE CODE 1:
#include <iostream>
using namespace std;
class Area
{
float area;
public:
// Area of a triangle
void find_area(float B, float H)
{
area = 0.5 * B * H;
}
// Area of a trapezium
void find_area(float A, float B, float H)
{
area = 0.5 * (A+B) * H;
}
};
int main()
{
int A, B, C, ch;
float ans;
Area obj;
switch(ch)
{
case 1: // Area of circle
cout << "\nEnter radius: ";cin >> A;
obj.find_area(A);
break;
cout << "Area: " << obj.print_area() << "\n";cout << "\nHardik Sharma |
10214902719\n";
return 0;
}
SOURCE CODE 2:
#include <iostream>
#include <cstring>
#include <cctype>
class String
{
public:
};
int main()
{
String A, B;
std::cout << "Enter the first string: ";A.get_string();
std::cout << "Enter the second string: ";B.get_string();
// Comparison
std::cout << "String A <= String B: " << (A <= B ? "true\n": "false\n");
// Length
std::cout << "Length of string A: " << A.length() << "\n";std::cout << "Length of string B:
" << B.length() << "\n";
return 0;
}
OUTPUT:
EXPERIMENT - 5
AIM: To create a class called LIST with two pure virtual functions
store () and retrieve (). To store a value call storeand to retrieve, call
retrieve function. Derive two classes stack and queue from it and
override store and retrieve.
SOURCE CODE:
#include <iostream>
using namespace std;
// LIST Class
class LIST
{
public:
// Virtual function virtual
void store()
{
cout << "LIST class store() called.\n";
cout << "Enter the value: ";
cin >> List[pos++];
cout << "Value stored: " << List[pos-1] << "\n";
}
// STACK Class
class STACK : public LIST
{
public:
// Overriden store() functionvoid store()
{
cout << "STACK class store() called.\n";
// QUEUE Class
class QUEUE : public LIST
{
public:
// Overriden store() functionvoid store()
{
cout << "QUEUE class store() called.\n";
cout << "Enter the value: ";
cin >> List[pos++];
cout << "Value stored: " << List[pos-1] << "\n";
}
int main()
{
// Create LIST base pointerLIST
*list_ptr;
s.store();
q.store();
return 0;
}
OUTPUT:
EXPERIMENT – 6
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
// Initialise different data types
int a;
float b;
long c;
double d;
OUTPUT:
EXPERIMENT - 7
AIM: To write a program to demonstrate the use of special functions,
constructor and destructor in the class template. The program is used to
find the bigger of two entered numbers.
SOURCE CODE:
#include <iostream>
using namespace std;
// Number class
class Number
{
int N;
public:
// Parameterised constructorNumber(int X)
{
N = X;
}
int max(Number A)
{
if (N > A.N)
return N;
else;
return A.N;
}
// Destructor
~Number()
{
cout << "Destructor working.\n";
}
};
int main()
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Larger of two numbers is: " << A.max(B) <<"\n";
return 0;
}
OUTPUT:
EXPERIMENT - 8
SOURCE CODE:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Open input file if stream fi;
fi.open("input.txt");
char ch;
{
fo << ch;
}
}
fi.close();
fo.close();
in.close();
out.close();
return 0;
}
OUTPUT: