Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have  a simple querry here.

First the exercise:-
Create a class called Triangle that stores the length of the base and height of a right triangle in two private instance variables. Include a constructor that sets these values. Define a function area( ) which returns the area of the triangle.


What I have tried:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
class triangle
{
private:
double length,height;
double ar;
public:
triangle(double l, double h);
double area();
};
triangle::triangle(double l, double h)
{
length=l;
height=h;
}
double triangle::area()
{
ar=(length*height)/2;
return ar;

}
main()
{
double l,h;
clrscr();
cout<<"enter length of the base of the right angled triangle: ";
cin>>l;
cout<<"\nenter height the right angled triangle: ";
cin>>h;
triangle o(l,h);
o.area();
getch();
}



/*When I replace "return ar" with  "cout<<ar"; it shows correct result. 

But in the exercise, it tells me to use the return statement. How can I get the result with the return statement ? i.e. I want to use the return statement. */
Posted
Updated 1-Feb-17 9:22am
Comments
[no name] 1-Feb-17 13:49pm    
Okay.....
double area = o.area();

The method is returning something, if you want to use it, capture it with a local variable of the prototype specified type.

main()
{
double l,h;
clrscr();
cout<<"enter length of the base of the right angled triangle: ";
cin>>l;
cout<<"\nenter height the right angled triangle: ";
cin>>h;
triangle o(l,h);
double ar = o.area();
//do something with ar variable
getch();
}
 
Share this answer
 
Comments
Member 12959299 2-Feb-17 9:18am    
Thanks Mr. Holguin
Your triangle::area() method is working correctly; it's your main() function that has the problem,

In the line
C++
o.area();
you are calculating the value, and then doing nothing with it. All you need to do is modify it to
C++
double a = o.area();
This will call triangle::area(), and assign the result to 'a'.

What you do with 'a' afterwards is up to you.
 
Share this answer
 
Comments
Member 12959299 2-Feb-17 9:19am    
Thanks Mr. Pfeffer
As the other ones suggested, the value your functions returns it is available in the main function.
Please note you don't need the member variable ar, it is a dependent quantity (usually is not wise using redundant info). try

C++
#include <iostream>
using namespace std;

class triangle
{
private:
  double length, height;
public:
 triangle(double l, double h):length(l), height(h){}
  double area();
};

double triangle::area()
{
  return (length*height)/2;
}

int main()
{
  double l, h;
  cout<<"enter length of the base of the right angled triangle: ";
  cin >> l;
  cout << endl <<"enter height the right angled triangle: ";
  cin >> h;
  triangle o(l,h);
  cout << endl << "the area of the triangle is " << o.area() << endl;
}
 
Share this answer
 
Comments
nv3 1-Feb-17 17:33pm    
Very useful hint!
Stefan_Lang 2-Feb-17 4:31am    
Very good.
However, I disagree with the notion not to use intermediate variables, even if they are redundant: For one, the compiler will normally eliminate trivial redundancy of that kind in 'Release' mode; second, these redundand variables can be a great help to analyze the state of your program when debugging.

I used to write down formulas on paper (and, later, in programs) using only a minimum of intermediate values. But over time I've learned that even though intermediate values can be redundand, it greatly helps for readability and understanding. The same goes for chaining function calls and dereferencing: it can help to check whether the intermediate results are actually valid before being piped to the next operation.
CPallini 2-Feb-17 5:57am    
Thank you.
Well, usage of redundant values is also a matter of personal taste. Anytime there is a stored dependent value, there is also the potential danger of going 'out-of-sync'. However I acknowledge its debugging usefulness. Another possible use of redundant values, is, of course, for improving execution speed. I consider such a scenarios a bit 'advanced', though. Hence my simple rule for the beginner is 'avoid redundant variables'.
Member 12959299 2-Feb-17 9:26am    
Yes, confusing for a beginner
Member 12959299 2-Feb-17 9:20am    
Thanks Mr. Pallini for replying me for the second time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900