vector2D.hファイル
#ifndef VECTOR2D_H
#define VECTOR2D_H
class Vector2D {
public:
double x;
double y;
Vector2D(); double length();
};
#endif // VECTOR2D_H
vector2D.cppファイル
#include "vector2D.h"
#include <math.h>
#include <iostream>
using namespace std;
// コンストラクタ
Vector2D::Vector2D() :x(0.0), y(0.0) {
}
double Vector2D::length() {
double length;
length = sqrt(x * x + y * y);
return length;
}
main.cppファイル
#include <iostream>
#include "Vector2D.h"
using namespace std;
int main(int argc, char** argv) {
// 2次元ベクトルの宣言
Vector2D v;
// ベクトルのx成分、y成分の値を入力
cout << "v.x=";
cin >> v.x;
cout << "v.y=";
cin >> v.y;
cout << "v=(" << v.x << "," << v.y << ")" << endl;
cout << "vの長さ" << v.length() << endl; return 0;
}
「質問」
上記プログラムを実行して、
v.x:1.0
v.y:2.0
を入力したときに、
v=(1,2)
vの長さ2.23607
となります。
v=(1,2)の中身の1と2はdouble型なのに
なぜ、v(1.0,2.0)
とはならないのでしょうか?

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2025/05/25 05:08
2025/05/25 05:35 編集
2025/05/26 06:19 編集
2025/05/31 05:35
2025/05/31 12:21
2025/05/31 12:26