C++(OOPs)-2
C++(OOPs)-2
C++(OOPs)-2
#include <iostream>
using namespace std;
// Complex number class
class Complex
{
int real, imag;
public:
// Default constructor
Complex()
{
real = 0;
imag = 0;
}
// Parameterised constructor
Complex(int val)
{
real = val;
imag = val;
}
// Parameterised constructor
Complex(int val1, int val2)
{
real = val1;
imag = val2;
}
// Adds two complex numbers and returns result
Complex add(Complex C)
{
real = real + C.real;
imag = imag + C.imag;
return *this;
}
// Prints complex number as "A + Bi"
void print()
{
cout << real << " + " << imag << "i\n";
}
};
int main()
{
// Default constructor used
Complex C;
return 0;
}
Program-6
Aim:- write a C++ program to find the greater of two given private
numbers in two different classes using a Friend function.
#include <iostream>
using namespace std;
// Forward declaration of class B
class B;
class A
{
// Private member of A
int num;
public:
public:
// Friend function
// Classes: A and B
// Returns the private members of both classes which is greater
int compare(A a, B b)
{
return (a.num > b.num ? a.num : b.num);
}
int main()
{
// Create objects of both classes
A a_obj;
B b_obj;
// Input objects of both classes
a_obj.input();
b_obj.input();
return 0;
}
Program-7
Aim:- To implement a class string containing the following functions:
a. Overload + operator to carry out the concatenation of strings.
b. Overload = operator to carry out string copy.
c. Overload <= operator to carry out the comparison of strings.
d. Function to display the length of string.
e. Function tolower() to convert upper case to lower case.
f. Function toupper() to convert lower case letters to upper case.
Source code 1
#include <iostream>
using namespace std;
const float PI = 3.14159;
class Area
{
float area;
public:
// Area of a circle
void find_area(float R)
{
area = PI * R * R;
}
// 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;
}
// Print area
float print_area()
{
return area;
}
};
int main()
{
int A, B, C, ch;
float ans;
cout << "| Area of figure |\n";
cout << "1. Circle\n";
cout << "2. Triangle\n";
cout << "3. Trapezium\n";
cout << "Enter choice: ";
cin >> ch;
Area obj;
switch(ch)
{
case 1: // Area of circle
cout << "\nEnter radius: ";
cin >> A;
obj.find_area(A);
break;
case 2: // Area of triangle
cout << "\nEnter the base and height: ";
cin >> A >> B;
obj.find_area(A, B);
break;
case 3: // Area of trapezium
cout<<"\nEnter the two parallel sides and height:";
cin >> A >> B >> C;
obj.find_area(A, B, C);
break;
default:
obj.find_area(0);
break;
}
cout << "Area: " << obj.print_area() << "\n";
return 0;
}
Source code 2
#include <iostream>
#include <cstring>
#include <cctype>
class String
{
public:
// Original string (char array)
char s[100];
// Inputs the original string
String get_string()
{
std::cin >> s;
return *this;
}
// Outputs the original string
char* put_string()
{
return (char*) s;
}
// Computes length of string
int length()
{
return strlen(s);
}
// Assigns original strings of one object to another
String operator = (const String &op)
{
strcpy(s, op.s);
return *this;
}
return 0;
}
Program-8
Aim:- create a class called LIST with two pure virtual functions store ()
and retrieve (). To store a value call store and to retrieve, call retrieve
function. Derive two classes stack and queue from it and override store
and retrieve.
#include <iostream>
using namespace std;
const int MAX=100;
int List[MAX];
int pos = 0;
// 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";
}
// Virtual function
virtual void retrieve()
{
cout << "LIST class retrieve() called.\n";
cout << "Enter the index: ";
int i;
cin >> i;
cout << "Value retrieved: " << List[i] << "\n";
}
};
// STACK Class
class STACK : public LIST
{
public:
// Overriden store() function
void store()
{
cout << "STACK class store() called.\n";
cout << "Enter the value: ";
cin >> List[pos++];
cout << "Value stored: " << List[pos-1] << "\n";
}
// Overriden retrieve() function
void retrieve()
{
cout << "STACK class retrieve() called.\n";
cout << "Enter the index: ";
int i;
cin >> i;
cout << "Value retrieved: " << List[i] << "\n";
}
};
// QUEUE Class
class QUEUE : public LIST
{
public:
// Overriden store() function
void store()
{
cout << "QUEUE class store() called.\n";
cout << "Enter the value: ";
cin >> List[pos++];
cout << "Value stored: " << List[pos-1] << "\n";
}
// Overriden retrieve() function
void retrieve()
{
cout << "QUEUE class retrieve() called.\n";
cout << "Enter the index: ";
int i;
cin >> i;
cout << "Value retrieved: " << List[i] << "\n";
}
};
int main()
{
// Create LIST base pointer
LIST *list_ptr;
// Create STACK and QUEUE objects
STACK s;
QUEUE q;
// Bind base pointer to derived class objects
list_ptr = &s;
s.store();
q.store();
// Base class virtual function will not be called
list_ptr->store();
list_ptr->retrieve();
return 0;
}
Program-9
Aim:- create a class Student, which have data members as name,
branch, roll no., age, sex, five subjects. Display the name of the student
& his percentage who has more than 70%.
#include <iostream>
#include <cmath>
using namespace std;
class Student
{
string name;
string branch;
int roll;
int age;
char sex;
int marks[5];
float perc;
public:
// Read student data
void read()
{
cout << "\nEnter name: ";
cin >> name;
cout << "Enter branch: ";
cin >> branch;
cout << "Enter roll no.: ";
cin >> roll;
cout << "Enter age: ";
cin >> age;
cout << "Enter sex: ";
cin >> sex;
cout << "Enter marks in 5 subjects: ";
for (int i = 0; i < 5; i++)
cin >> marks[i];
}
// Calculate percentage
void calculate()
{
float total = 0;
for (int i = 0; i < 5; i++)
total += marks[i];
perc = total / 5;
}
// Print student info with percentage > 70%
void print()
{
if (perc > 70.0)
{
cout << "\nName: " << name << "\n";
cout << "Percentage: " << perc <<
"\n";
}
}
};
int main()
{
// Input student data size
int N;
cout << "Enter number of students: ";
cin >> N;
// Create Student array
Student S[N];
// Read Student data and calculate percentage
for (int i = 0; i < N; i++)
{
S[i].read();
S[i].calculate();
}
// Output student with percentage > 70%
for (int i = 0; i < N; i++)
{
S[i].print();
}
return 0;
}
Program-10
Aim:- create a class TIME with members hours, minutes and seconds.
Take input, add two-time objects and passing objects to function and
display the result.
#include <iostream>
using namespace std;
// Time class
class Time
{
int hours;
int minutes;
int seconds;
public:
// Parameterised Constructor
Time(int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
// Adds two time objects
void add(Time &T)
{
seconds += T.seconds;
minutes += (seconds / 60);
seconds %= 60;
minutes += T.minutes;
hours += (minutes / 60);
minutes %= 60;
hours += T.hours;
}
void print()
{
cout << "Time: " << hours << "h " <<
minutes << "m " << seconds << "s\n";
}
};
int main()
{
int a, b, c;
// Enter both times
cout << "Enter Time A (hours, minutes,seconds): ";
cin >> a >> b >> c;
// Create Time object
Time A(a, b, c);
cout << "Enter Time B (hours, minutes,seconds): ";
cin >> a >> b >> c;
// Create Time object
Time B(a, b, c);
cout << "\nTime A\n";
A.print();
cout << "\nTime B\n";
B.print();
cout << "\nAdded time\n";
// Add Times with add() function
A.add(B);
A.print();
return 0;
}
Program-11
Aim:- write a program to find the biggest of three numbers using a
Friend Function.
#include <iostream>
using namespace std;
class Numbers
{
int LIST[100];
int N;
int MAX;
public:
// Constructor
Numbers(int size)
{
N = size;
}
// Friend function to find MAX
friend void find_max(Numbers&);
// Read the numbers
void read()
{
cout << "Enter the numbers: ";
for (int i = 0; i < N; i++)
{
cin >> LIST[i];
}
}
// Print max
void print_max()
{
cout << MAX << "\n";
}
};
// Friend function definition
void find_max(Numbers &A)
{
A.MAX = A.LIST[0];
for (int i = 1; i < A.N; i++)
{
A.MAX = max(A.MAX, A.LIST[i]);
}
}
int main()
{
// Input number
cout << "Enter the size: ";
int N;
cin >> N;
// Create Numbers object
Numbers A(N);
// Read the numbers
A.read();
// Find maximum of all numbers using friend
function
find_max(A);
// Print maximum of all numbers
A.print_max();
return 0;
}
Program-12
Aim:- write a program to find the sum of two numbers declared in a
class and display the numbers and sum using friend class
#include<iostream>
using namespace std;
class B;
class A
{
private:
int numA;
public:
A(): numA(23) { }
friend int add(A, B);
};
class B
{
private:
int numB;
public:
B(): numB(14) { }
friend int add(A , B);
};
int add(A objectA, B objectB)
{
return (objectA.numA + objectB.numB);
}
int main()
{
A objectA;
B objectB;
cout<<"Sum: "<<add(objectA,objectB);
return 0;
}
Program-13
Aim:- Write a program to overload new and delete operator.
#include<iostream>
#include<stdlib.h>
return p;
}
int main()
{
student * p = new student("Dibya", 19);
p->display();
delete p;
}
Program-14
Aim:- write a program to overload unary increament (++) operator.
#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);