尝试使用C ++进行Ray-Sphere交叉程序。
我无法理解C ++的父函数。我试图将它与3个变量一起使用,但它并没有让我这么做。我怎么能这样做?
#include "Point3D.h"
Point3D::Point3D() :X{ 0 }, Y{ 0 }, Z{ 0 }{ }
Point3D::Point3D(double xP, double yP, double zP) :X{ xP }, Y{ yP }, Z{ zP } {}
Point3D::~Point3D(){
this->X = 0;
this->Y = 0;
this->Z = 0;
}
double Point3D::getX(){
return this->X;
}
double Point3D::getY(){
return this->Y;
}
double Point3D::getZ(){
return this->Z;
}
void Point3D::setX(double xP)
{
this->X = xP;
}
void Point3D::setY(double yP)
{
this->Y = yP;
}
void Point3D::setZ(double zP)
{
this->Z = zP;
}
Point3D& Point3D::addVectors(const Point3D& vec){
this->X += vec.X;
this->Y += vec.Y;
this->Z += vec.Z;
}
Point3D& Point3D::subtractVectors(const Point3D& vec){
this->X -= vec.X;
this->Y -= vec.Y;
this->Z -= vec.Z;
}
double Point3D::multiplyVectors(const Point3D& vec){
double val = (this->X *vec.X) + (this->Y *vec.Y) + (this->Z *vec.Z);
return val;
}
double Point3D::squareVector(){
double val = (this->X) *(this->X) + (this->Y) * (this->Y) + (this->Z) * (this->Z);
return val;
}
Point3D operator+(const Point3D &val1, const Point3D &val2, const Point3D &val3){
return Point3D(val1.X + val2.X + val3.X, val1.Y + val2.Y + val3.Y, val1.Z + val2.Z + val3.Z);
}
Point3D operator-(const Point3D &val1, const Point3D &val2, const Point3D &val3){
return Point3D(val1.X - val2.X - val3.X, val1.Y - val2.Y - val3.Y, val1.Z - val2.Z - val3.Z);
}
Point3D operator*(const Point3D &val1, const Point3D &val2, const Point3D &val3){
return Point3D(val1.X * val2.X * val3.X, val1.Y * val2.Y * val3.Y, val1.Z * val2.Z * val3.Z);
}
Point3D operator/(const Point3D &val1, const Point3D &val2, const Point3D &val3){
return Point3D(val1.X / val2.X / val3.X, val1.Y / val2.Y / val3.Y, val1.Z / val2.Z / val3.Z);
}
Point3D operator- (const Point3D &val){
return Point3D( -val.X, -val.Y, -val.Z);
}
bool operator!(const Point3D &val){
cout << "Friend function is called!!!" << endl;
return ( val.X != 0.0 || val.Y != 0.0 || val.Z != 0.0);
}
bool Point3D::operator!(){
return ( ((*this).X != 0) || ((*this).Y != 0) || ((*this).Z != 0));
}
Point3D& Point3D::operator++(){
cout << "Calling prefix ......." << endl;
X += 1;
Y += 1;
Z += 1;
return (*this);
}
Point3D& Point3D::operator++(int){
cout << "Calling postfix ......." << endl;
Point3D temp = *this;
X += 1;
Y += 1;
Z += 1;
return temp;
}
istream& operator>>(istream& istr, Point3D& P){
istr >> P.X >> P.Y >> P.Z;
return istr;
}
ostream& operator<<(ostream& ostr, Point3D& P){
ostr << P.X << " " << P.Y << " " << P.Z;
return ostr;
}
Main.cpp的
#include <iostream>
#include "Point3D.h"
using std::istream; using std::ostream;
int main(){
Point3D P1, P2, P3;
cout << "Enter the first point: " << endl;
cin >> P1;
cout << endl;
cout << "Enter the second point: " << endl;
cin >> P2;
cout << endl;
cout << "Enter the third point: " << endl;
cin >> P3;
cout << endl;
cout << "Point P3 is: " << !P3;
cout << "Points are: " << P1 << " and " << P2 << " and " << P3 << endl;
Point3D P4, P5, P6, P7, P8;
cout << "Point P8 is: " << P8 << endl;
P4 = P1 + P2 + P3;
P5 = P1 - P2 - P3;
P6 = P1*P2*P3;
P7 = P1 / P2 / P3;
Point3D P30 = operator-(P1);
operator << (cout, P30) << endl;
cout << (cout, P30) << endl;
cout << "Point P4 is: " << P4 << endl;
cout << "Point P5 is: " << P5 << endl;
cout << "Point P6 is: " << P6 << endl;
Point3D P9 = P8.operator++();
cout << "Point P8 is: " << P8 << endl;
cout << "Point P9 is: " << P9 << endl;
Point3D P10 = P8++;
cout << "Point P8 is: " << P8 << endl;
cout << "Point P10 is: " << P10 << endl;
if (operator!(P1)) cout << "I'm not equal!!\n";
cout << endl;
return 0;
}