data member init.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int enterSide() {  int enter; std::cin >> enter; return enter; } // this is defined in the class


void area::getSide( int x , int y , int z ) {
	side1 =	x;
	side2 = y;
	side3 = z;
} // class mem. func




int _tmain(int argc, _TCHAR* argv[])
{

	area myArea;

	myArea.getSide(myArea.enterSide(),myArea.enterSide(),myArea.enterSide());

	std::cout << "You enter: "<< myArea.side1 << std::endl;
	std::cout << "You enter: "<< myArea.side2 << std::endl;
	std::cout << "You enter: "<< myArea.side3 << std::endl;



	keep_window_open();

}

why i keep getting output backward?

when the users enter 1 2 3
it outputs
You enter: 3 
You enter: 2
You enter: 1

it should be


You enter: 1
You enter: 2
You enter: 3

EDIT: forgot to put code format
Last edited on
Hi,

A few things ......

Why is your class named area and not Triangle? Area should be a class function, not a class of it's own. Other functions might include Perimeter, Centroid etc.

Write a constructor for you class, that takes 3 arguments. Use an initialiser list to set the values of the private member variables. This will avoid the rather roundabout methodology you have at the moment :+)

The type of the side lengths might be more useful if they were double .

Make member functions that don't alter the value of member variables const.

You can read about how to do all these things in the tutorials and articles and reference section at the top left of this page.

Hope all goes well and good luck :+D

EDIT:

Write a member function that outputs all the info that you want, that way you can keep all your data private:

You can have accessor functions that retrieve the values of your data members, these are often called "get" functions. You have an input function getSide which is confusing.

Try to avoid having any "set" functions.

Instead, think about what your object can actually DO. For example, if you had coordinates for the vertices, you could have move and rotate and scale functions.

If you have any problems, just post your code - there are plenty of people to help :+D
Last edited on
Write a constructor for you class, that takes 3 arguments. 


well , im trying to write it first without a constructor


@TheIdeasMan
how about my problem?
Last edited on
Standard does not specify in which order function arguments will be evaluated.
In your case it was
1
2
//                    _3_                _2_                _1_
myArea.getSide(myArea.enterSide(),myArea.enterSide(),myArea.enterSide());
and I suppose it was GCC compiler which likes to do that. Other variants are
1
2
3
4
5
//                    _1_                _2_                _3_
myArea.getSide(myArea.enterSide(),myArea.enterSide(),myArea.enterSide());
//or even
//                    _1_                _3_                _2_
myArea.getSide(myArea.enterSide(),myArea.enterSide(),myArea.enterSide());
All those and any variant not shown here are allowed by standard. And furthermore, compiler does not have to be consistent: order of argument evaluation could change between compilations or even between two consequitive calls.

In short: never write code which depends on order of function argumnet evaluation.
Last edited on
Hi,

I just edited my other post :+)

I don't know why your output is messed up, however it wouldn't be if you wrote a PrintInfo function.

well , im trying to write it first without a constructor


I urge to learn how to do that sooner rather than later :+). It's a bit like going to a shop and not saying what you want to buy : obviously much easier to specify want you want.

I just typed this in, hopefully not too many syntax disasters :+)

CTriangle.hpp header file with cpp code inside
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CTriangle {
public:

CTriangle (const double Side1, const double Side2, const double Side3);
double Area() ;
void PrintInfo() const;

private:

double m_Side1;
double m_Side2;
double m_Side3;

double m_Area;
double m_Perimeter;

};


CTriangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include "CTriangle.hpp"

CTriangle::CTriangle(double Side1, double Side2, double Side3) : // colon introduces initialiser list
    m_Side1(Side1),
    m_Side2(Side2),
    m_Side3(Side3),
    m_Area(0.0),
    m_Perimeter(0.0) {}

double CTriangle::Area()  {

    // calculate the area here and assign the value to m_Area

   return m_Area;
}

void CTriangle::PrintInfo() const  {
 //std::cout all your info here
    std::cout << m_Side1 << "\n";

    std::cout << m_Area << "\n";
}


GeometryApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

#include "CTriangle.hpp"

int main() {

// optionally get some user input
// create a Ctriangle object, calling it's constructor

CTriangle MyTriangle(3.0, 4.0, 5.0);

// Call the Area function
   double MyTriangleArea =  MyTriangle.Area();
// Print the Data
   MyTriangle.PrintInfo();

return 0;
}



EDIT
Now you could have some get functions, just don't do any trivial set functions.

Put in some missing parentheses
Last edited on
Hello again,

Just some other ideas:

If you need to work out the area given 3 sides, you might use the cosine rule. In that case, it might be better to name your member variables to SideA, SideB, SideC to facilitate this. Could have similar names for the angles.

There are other methods of working it out, use whatever names that make sense for the Formula that you are using.

Meaningful variable names help a lot with understanding, and are a form of self documentation, which means you may not have to write as many comments. I use comments for things like pre and post conditions for functions, web addresses of relevant documentation, and short descriptions of what classes and functions are for - if necessary.

Any way it's late at this end, though you are in the expert care of MiiNiPaa .

I look forward to seeing how you got on, tmrw :+)
Last edited on
Topic archived. No new replies allowed.