我怎么能写一个长度函数来返回间隔的长度

How could I write a length function in which gives back the length of the interval?

本文关键字:函数 返回 一个 怎么能      更新时间:2023-10-16

#include <iostream>
#include <math.h>
using namespace std;
class Interval {
	double x,y;
  public:
    Interval(double,double);
    Interval();
	void print();
	double absz();
	void save(char*);
};
Interval::Interval(double x, double y) {
	this->x = x;
	this->y = y;
}
Interval::Interval() {
	cin >> x >> y;
}
void Interval::print() {
	cout << "(" << x << "," << y << ")the length of the interval:";
}
double Interval::absz() {
	return abs(y-x);
}
int main(int argc, char** argv) {
	Interval *i = new Interval();
	i->print();
	cout << i->absz();
	delete i;
	system("pause");
	return 0;
}

我正在用C++编写一个程序,它可以计算闭合间隔的长度。我的问题是:我怎样才能写一个函数来返回间隔的长度?我感谢每一个帮助。

我想你已经写过了。即,Interval::absz计算间隔的长度。