Friday, April 22, 2022

In this program we are going to Create two classes DM and DB which store the value of distances. DM stores distances in metres and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results maybe a DM object or DB objects, depending on the units in which the results are required. The display should be in the format of feet and inches or metres and cenitmetres depending on the object on display.

 #include <iostream>

#include <conio.h>
using namespace std;
class DB;
class DM
{

    float meters, centimeters;

public:
    DM(float &meters, float &centimeters)
    {
        this->meters = meters;
        this->centimeters = centimeters;
    }
    void conversion()
    {
        meters = 3.37 * meters;
        centimeters = 0.394 * centimeters;
    }
    friend void add(DM, DB);
};
class DB
{
    float feet, inches;

public:
    DB(float &feet, float &inches)
    {
        this->feet = feet;
        this->inches = inches;
    }
    void conversion()
    {
        feet = 0.305 * feet;
        inches = 2.54 * inches;
    }
    friend void add(DM, DB);
};
void add(DM A, DB D)
{
    A.meters = A.meters + D.feet;
    A.centimeters += D.inches;
    cout << A.meters << "\t" << A.centimeters << endl;
}

int main()
{
    float num1, num2, num3, num4;
    cout << "enter distance in feet and inches\n";
    cout << "enter feet\n";
    cin >> num1;
    cout << "enter inches\n";
    cin >> num2;
    DB obj1(num1, num2);
    system("cls");
    cout << "\nenter distance in meters and centimeters\n";
    cout << "enter meters\n";
    cin >> num3;
    cout << "enter centimeters\n";
    cin >> num4;
    DM obj2(num3, num4);
    int choice;
    system("cls");
    cout << "enter\n1. for adding data and display addition in meters and centimeters\n2. for adding data and display addition in feet and inches\n";
    cin >> choice;
    if (choice == 1)
    {
        system("cls ");
        cout << "meters "
             << "\t"
             << "centimeters \n";
        obj1.conversion();
        add(obj2, obj1);
    }
    else if (choice == 2)
    {
        system("cls");
        cout << "feet "
             << "\t"
             << "inches \n";
        obj2.conversion();
        add(obj2, obj1);
    }
    return 0;
}

No comments:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts