Answer to Question #216890 in C++ for Srikanth Kodamasim

Question #216890
Define a class called Point that contains two data members: x and y of float type. Define constructors to initialize the data members. Define a friend function to calculate distance between two point objects. Declare a class DArray which has data member size to store the size of the array, and element - a pointer to point class type. Define 1-arg. constructor that takes size as parameter and allocates memory dynamically to element of given size. Also define destructor to release the memory. Overload [] operator that takes the index position as parameter to manipulate (store/retrieve) the required element in the array. In main create an object of DArray to hold 3 objects of Point type and find the distance between every pair of points.
1
Expert's answer
2021-07-14T00:49:21-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


class Point{
	public:
		float x,y;
		
		Point(float a, float b)
		{
			x=a;
			y=b;
		}
		
		friend float distance(Point* a, Point* b);
		
		void display()
		{
			cout<<"x : "<<x<<endl;
			cout<<"y : "<<y<<endl;
		}
};


float distance(Point* a, Point* b)
{
	return sqrt(((b->x - a->x)(b->x - a->x)) + (((b->y - a->y)(b->y - a->y))));
}


class DArray{
	public:
		int *size;
		Point* point;
		
		DArray()
		{
			
		}
		
		Darray(int n)
		{
			size = new int(n);
		}
		
		~DArray()
		{
			
		}
};


int main()
{


	cout<<"Enter number of points : ";
	int n;
	cin>>n;
	DArray arr[n];


	for(int i=0; i<n; i++)
	{
		float a,b;
		cout<<"Enter value of x : ";
		cin>>a;
		cout<<"Enter value of y : ";
		cin>>b;
		cout<<endl;
		Point *p = new Point(a,b);
		arr[i].point = p;
	}
	
	
	cout<<endl<<endl;
	for(int i=0; i<n; i++)
	{	
		for(int j=i+1; j<n; j++)
		{
			float dist = distance(arr[i].point,arr[j].point);
			cout<<"Distance b/w ("<<arr[i].point->x<<","<<arr[i].point->y<<") and ("<<arr[j].point->x<<","<<arr[j].point->y<<") is : "<<dist<<endl;
		}
	}
	
	
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS