[go: up one dir, main page]

0% found this document useful (0 votes)
4 views41 pages

C++(OOPs)-2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 41

Program-5

Aim:- write a program to perform addition of two complex numbers


using constructor overloading. The first constructor which takes no
argument is used to create objects which are not initialized, second
which takes one argument is used to initialize real and imag parts to
equal values and third which takes two argument is used to initialized
real and imag to two different values.

#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;

// Parameterised constructor used


Complex A(-2);

// Parameterised constructor used


Complex B(7, -3);

cout << "Complex A: ";


A.print();

cout << "Complex B: ";


B.print();

// Add two complex numbers and store in C


C = A.add(B);
// Print sum
cout << "Complex C (sum): ";
C.print();

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:

// Inputs private member 'num' of A


void input()
{
cout << "Enter num value for A: ";
cin >> num;
}
// Friend function declaration
friend int compare(A, B);
};
class B
{
// Private member of B
int num;

public:

// Inputs private member 'num' of B


void input()
{
cout << "Enter num value for B: ";
cin >> num;
}

friend int compare(A, B);


};

// 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();

// Output sum by calling friend function


cout << "Larger of both private members (A and B): " <<
compare(a_obj, b_obj);

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;
}

// Concatenates two strings


String operator + (const String &op)
{
String res;
res = *this;
strcat(res.s, op.s);
return res;
}
// Compares two strings based on length
bool operator <= (String op)
{
if (length() <= op.length())
return true;
else
return false;
}
// Converts the original string to all lowercase
String tolower()
{
for (int i = 0; i < length(); i++)
{
s[i] = std::tolower(s[i]);
}
return *this;
}
// Converts the original string to all lowercase
String toupper()
{
for (int i = 0; i < length(); i++)
{
s[i] = std::toupper(s[i]);
}
return *this;
}
};
int main()
{
String A, B;
std::cout << "Enter the first string: ";
A.get_string();
std::cout << "Enter the second string: ";
B.get_string();
// Assignment and Concatenation
String C = A + B;
std::cout << "\nConcatenated string: " << C.put_string() <<
"\n";
// 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";
// tolower and toupper
String A_lower = A.tolower(), A_upper = A.toupper();
String B_lower = B.tolower(), B_upper = B.toupper();
std::cout << "tolower(A): " << A_lower.put_string() << "\
n";
std::cout << "toupper(A): " << A_upper.put_string() << "\
n";
std::cout << "tolower(B): " << B_lower.put_string() << "\
n";
std::cout <<"toupper(B): " << B_upper.put_string() << "\n";

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>

using namespace std;


class student
{
string name;
int age;
public:
student()
{
cout<< "Constructor is called\n" ;
}
student(string name, int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size <<
endl;
void * p = ::operator new(size);
//void * p = malloc(size); will also work fine

return p;
}

void operator delete(void * p)


{
cout<< "Overloading delete operator " << endl;
free(p);
}
};

int main()
{
student * p = new student("Dibya", 19);

p->display();
delete p;
}
Program-14
Aim:- write a program to overload unary increament (++) operator.

// 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();
}

You might also like