Assignment 4: 1.write A C++ Program To Get The Area of Square and Rectangle Using Friend Class and Constructors
Assignment 4: 1.write A C++ Program To Get The Area of Square and Rectangle Using Friend Class and Constructors
1.Write a C++ program to get the area of square and rectangle using friend class
and constructors.
Algorithm:-
Step 1: Start.
Step 2: Create separate classes for square and rectangle.
Step 3: Make the square class a friend of the rectangle class.
Step 4: Calculate the area of the shapes using a function from rectangle class.
Step 5: Show the output.
Step 6: Stop.
Source Code:-
#include <iostream>
using namespace std;
class Rectangle
{
private:
float length, breadth;
public:
Rectangle ()
{
cout << "ENTER THE RECTANGLE'S LENGTH: ";
cin >> length;
cout << "ENTER THE RECTANGLE'S BREADTH: ";
cin >> breadth;
area ();
}
friend class Square;
public:
void area ()
{
cout << "THE AREA IS " << (length * breadth) << " SQ. UNITS" << endl;
}
};
class Square
{
private:
float side;
public:
Square (Rectangle R)
{
cout << "ENTER THE SQUARE'S SIDE: ";
cin >> R.length; R.breadth = R.length;
R.area ();
}
};
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
Rectangle R;
Square S (R); return 0;
}
Screenshot:-
2.Write a C++ program to implement swapping of two numbers using friend
function.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to swap the numbers.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <iostream>
using namespace std;
class Numbers
{
private:
int a, b;
public:
Numbers ()
{
cout << "ENTER INTEGER A: ";
cin >> a;
cout << "ENTER INTEGER B: ";
cin >> b;
cout << "BEFORE SWAPPING" << endl;
showNums ();
}
void showNums ()
{
cout << "A = " << a << endl;
cout << "B = " << b << endl;
}
friend void swap (Numbers N);
};
void swap (Numbers N)
{
int r;
r = N.a;
N.a = N.b;
N.b = r;
cout << "AFTER SWAPPING" << endl;
N.showNums ();
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
Numbers N;
swap (N);
return 0;
}
Screenshot:-
Screenshot:-
9.Write a C++
program to
implement Heap Sort
using object as
parameter.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
void showList ()
{
cout << endl << "THE LIST IS" << endl;
int i;
for (i = 0; i < length; i++)
cout << "ITEM " << (i+1) << ": " << list[i] << endl;
}
};
void heapify (NumberList N, int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && N.list[l] > N.list[largest])
largest = l;
if (r < n && N.list[r] > N.list[largest])
largest = r;
if (largest != i)
{
swap(N.list[i], N.list[largest]);
heapify(N, n, largest);
}
}
void heapSort (NumberList N)
{
int i, temp, n = N.length;
for (i = n/2 - 1; i >= 0; i--)
heapify (N, n, i);
for (i = n-1; i >= 0; i--)
{
temp = N.list[0];
N.list[0] = N.list[i];
N.list[i] = temp;
heapify (N, i, 0);
}
cout << endl << "THE LIST IS SORTED";
N.showList ();
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N;
heapSort (N);
return 0;
}
Screenshot:-