如何使用return语句获得结果? [英] How can I get the result with return statement ?

查看:70
本文介绍了如何使用return语句获得结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.





我尝试过: < br $>




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. */

推荐答案

该方法返回一些东西,如果你想使用它,用原型指定类型的局部变量捕获它。



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();
}


你的 triangle :: area()方法正常工作;这是你的 main()函数有问题,



在行
Your triangle::area() method is working correctly; it's your main() function that has the problem,

In the line
o.area();

您正在计算该值,然后对其执行任何操作。您需要做的就是将其修改为

you are calculating the value, and then doing nothing with it. All you need to do is modify it to

double a = o.area();

这将调用 triangle :: area(),并将结果分配给'a'。



之后使用'a'做什么最多你。

This will call triangle::area(), and assign the result to 'a'.

What you do with 'a' afterwards is up to you.


正如其他建议的那样,你的函数返回它的值可以在 main 函数中找到。

请注意,您不需要成员变量 ar ,它是一个依赖数量(使用冗余信息通常不明智)。尝试



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

#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;
}


这篇关于如何使用return语句获得结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆