C++ Distance Calculation Program
Introduction
This program calculates the distance between two points in feet and inches. It takes input from the user for two distances and performs addition and subtraction operations on them using friend functions.
Key Concepts
- Classes and Objects: The program uses a class called "Distance" to represent distances in feet and inches. Objects of this class are created to store the input distances and perform calculations.
- Friend Functions: The program defines two friend functions, "add" and "subtract", which can access the private members of the Distance class. These functions are used to perform addition and subtraction operations on two-distance objects.
- Input and Output: The program prompts the user to enter the distances in feet and inches and displays the calculated results.
Code Structure
The code starts with the necessary header files, followed by the definition of the Distance class. Inside the class, there are private data members for feet and inches, along with public member functions for input and display.
After the class definition, the friend function declarations for "add" and "subtract" are provided. These functions are defined outside the class and have access to the private members of the Distance class.
The main function creates objects of the Distance class and calls the input function to get user input for two distances. Then, it calls the friend functions "add" and "subtract" to perform calculations and stores the results in separate objects. Finally, it displays the original distances, the addition result, and the subtraction result.
Code Examples
Here is an example of how to use the Distance class and the friend functions:
C++
#include<iostream> #include<math.h> using namespace std; class Distance { int feet; int inch; //Data members public: void input() { cout<<"Enter Feet : "; cin>>feet; cout<<"Enter Inch : "; cin>>inch; } void display() //member fun. definition { cout<<"Distance is = "<<abs(feet)<<" Feet "<<abs(inch)<<" Inch"<<endl; } friend Distance add(Distance,Distance); friend Distance subtract(Distance,Distance); //Friend function declaration }; Distance add(Distance d1,Distance d2) //friend function definition { Distance temp; temp.feet = d1.feet + d2.feet; temp.inch = d1.inch + d2.inch; if(temp.inch>12) { temp.feet = temp.feet + temp.inch / 12; temp.inch = temp.inch % 12; } return temp; } Distance subtract(Distance d1,Distance d2) //friend function definition { Distance temp; if(d1.feet>d2.feet) { if(d1.inch<d2.inch) { d1.feet = d1.feet - 1; d1.inch = d1.inch + 12; } temp.feet = d1.feet - d2.feet; temp.inch = d1.inch - d2.inch; } if(d1.feet<d2.feet) { if(d1.inch>d2.inch) { d2.feet = d2.feet - 1; d2.inch = d2.inch + 12; } temp.feet = d2.feet - d1.feet; temp.inch = d2.inch - d1.inch; } return temp; } int main() { Distance dis1,dis2,addition,subtraction; dis1.input(); dis2.input(); addition = add(dis1,dis2); //friend function calling subtraction = subtract(dis1,dis2); //friend function calling cout<<endl<<"First Distance object : "; dis1.display(); cout<<endl<<"Second Distance object : "; dis2.display(); cout<<endl<<"Addition : "; addition.display(); cout<<endl<<"Subtraction : "; subtraction.display(); return 0; }
Output
Enter Feet : 5
Enter Inch : 8
Enter Feet : 8
Enter Inch : 6
First Distance object : Distance is = 5 Feet 8 Inch
Second Distance object : Distance is = 8 Feet 6 Inch
Addition : Distance is = 14 Feet 2 Inch
Subtraction : Distance is = 2 Feet 10 Inch
Conclusion
This program demonstrates the use of classes, objects, and friend functions in C++. It allows the user to input two distances in feet and inches and performs addition and subtraction operations on them. The calculated results are then displayed to the user.
Other C++ Program
- Operator Overloading in a 3D Vector Class in C++
- Counting Characters in a String in C++
- Explaining the getline Function in C++
- Data Hiding in C++
- Efficient Array Sorting in C++: Static Overloaded Functions for Floats and Integers
- Programm to demonstrate constructor with default arguments
- Understanding Function Call by Value in C++
- Exploring Virtual Functions with Default Arguments in C++
- Multiple Inheritance in C++
- Binary + Operator Overloading In CPP
- Exploring Class-to-Basic Type Conversion with a Product Example in C++
- Creating a Diamond Pattern in the CPP Program
- Write a CPP program to perform arithmetic operations using an inline function.
- Write a program in C++ to create a structure named complex having data members real and images. Create member function add_complex which takes structure as an argument and returns structure. Using the function add two complex numbers.
- Write program to generate following pattern