I have to define a class for equilateral triangles. The triangle should be defined by a center point (x, y) and the length of the sides. The class should have a constructor which allows defining the triangle, a constructor with no arguments, and member functions for the x coordinate, y coordinate, length, height, perimeter, area, and distance from the center to the vertex.
I have to define triangle1, and triangle2. triangle1 has no values defined. triangle2 will have x coordinate of 5.0, y coordinate of 8.3, and length of 10.0. All the member functions should work for both triangles. Then triangle2 should be reset to x=7.0, y=3.0, and length = 6.5 and show the functions work.

I can't get the values set for triangle 2 and i have no idea what i should do. i have been trying to figure it out for over an hour. any help would be appreciated. this is what i have done so far...

#include<iostream.h>
#include<math.h>

class Etriangle
{
public:
	Etriangle (float x,float y,float length);
	Etriangle ();

	float height(); 
	float perimeter();
	float area();
	float dist();
	void output(ostream &output);

private:
	float length, x, y;
};

void main()
{
	Etriangle triangle1, triangle2(5.0, 8.3, 10.0);
	cout<<"Triangle 1 is initialized as:\n";
	triangle1.output(cout);
	
	cout<<"\nTriangle 2 is initialized as:\n";
	triangle2.output(cout);

	triangle2 = Etriangle(7.0, 3.0, 6.5);
	cout<<"\nTriangle 2 is now set as:\n";
	triangle2.output(cout);

}

Etriangle::Etriangle()
{
	x=0;
	y=0;
	length=0;
}
Etriangle::Etriangle(float x, float y, float length)
{
	x = 5.0;
	y = 8.3;
	length = 10.0;
	
}
float Etriangle::area()
{
	float s;
	s = (length + length + length) / 2;
	return (sqrt(s*(s-length)*(s-length)*(s-length)));
	 
}
float Etriangle::dist()
{
	return(length * length * length) / (4 * area());
}
float Etriangle::height()
{
	return (sqrt((length*length)-((length/2)*(length/2))));
}
float Etriangle::perimeter()
{
	return (length + length + length);
}
void Etriangle::output(ostream &output)
{
	output << "X = " << x << endl;
	output << "Y = " << y << endl;
	output << "Length = " << length << endl;
	output << "Height = " << height() << endl;
	output << "Perimeter = " << perimeter() << endl;
	output << "Area = " << area() << endl;
	output << "Distance from center to vertex = " << area() << endl;
}

Recommended Answers

All 3 Replies

First off void main is wrong!
Second off you do not call the constuctor to re-set your values; It should only be called once when you declare an instance of your class.
You make member functions to set your values, for example

void setLength(float len)
{

  length = len;

}

so on and so forth for your other values.

Oh and just looking at your constructor you are going to want to change it because it is wrong.
It should look like this

Etriangle::Etriangle(float xCoord, float yCoord, float len)
{
	x = xCoord;
	y = yCoord;
	length = len;
	
}

thx for your help...i have it working now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.