![](/img/trans.png)
[英]How to initialize a reference member variable inside a member function & access it inside other member functions - C++
[英]C++ Member Function That Returns A Value of Two other member functions
大约一个月前,我在测试中遇到了这个问题,但我似乎仍然无法完全理解它,坦率地说,这让我发疯了。 我将在底部包含这个问题。 但是,它要求创建一个单参数构造函数,该构造函数创建一个新的“Vector”(类的名称),它是另外两个的总和。 我制作的向量类有一个函数 set/get x 和 set/get y。 我的挂断是我似乎无法弄清楚如何制作一个函数,将vector
和vector1
的两个 x 和 y 相加,以创建一个新的 Vector...称之为vector2
。 我将包括到目前为止我得到的一切。 感谢任何愿意让它通过文本墙的人,因为它必须是令人困惑的哈哈。
编写一个具有以下属性的类
Vertor
并将该类放在单独的头文件中:
Add
具有另一个向量的单个参数的成员函数,并返回一个新向量,该向量是两者之和(要添加向量,您可以对分量求和,例如,Cx = Ax + Bx 和 Cy = Ay + By)。
编写一个包含 Vector 头文件的程序,构造两个不同的向量并演示大小、角度和相加函数。
数据成员
向量
x分量
y 分量
成员函数
设置和获取所有数据成员的函数
幅度成员函数
角度成员函数(角度 = 反正切(y / x))
ps我希望我上传这个并没有做错任何事情,并问我一直在等待,因为我不想打破社区中的某种规则......老实说,我非常渴望成为其中的一员。 我一生都梦想着这样做,最后......啊,我离题了,抱歉,谢谢大家
哦...我的代码
#include "Vertor.h"
#include <iostream>
int main()
{
// creates a vector class
Vector vector;
vector.setXcom(4); // sets X
vector.setYcom(12); // sets Y
Vector vector1; // Creates another vector
vector1.setXcom(3);
vector1.setYcom(52);
Vector vector2; // constructs another vector that returns the sum of two other vectors
cout << vector.getXcom() << endl;
cout << vector.getYcom() << endl;
cout << vector.getMag() << endl;
cout << vector.getAng() << endl;
cout << vector1.getXcom() << endl;
cout << vector1.getYcom() << endl;
cout << vector1.getMag() << endl;
cout << vector1.getAng() << endl;
}
#include<iostream>
using namespace std;
// initalize variables
double xcomponent, ycomponent;
double ans, anns, annns;
class Vector // creates Vector class
{
public:
void setXcom(double x) // setX function
{
xcomponent = x;
}
void setYcom(double y) // setY function
{
ycomponent = y;
}
double getXcom() // getX function
{
return xcomponent;
}
double getYcom() // getY function
{
return ycomponent;
}
double getMag() // get magnitude function
{
double ans = sqrt((xcomponent * xcomponent) + (ycomponent * ycomponent));
return ans;
}
double getAng() // get angle function
{
double annns = atan(xcomponent / ycomponent);
return annns;
}
// setnewvec function to make a new vector from two others
void setNewVec(int a, int b)
{
xcomponent = a;
ycomponent = b;
}
// NOT SURE
Vector getNewVec(int a, int b)
{
return a + a;
return b + b;
}
};
因此,您对对象如何工作的知识存在绝对根本的误解或差距,除非您解决这个问题,否则这项任务将是不可能的。
为了说明这里的一个更简单的例子,用上面的代码风格编写。 我将按照应有的方式编写相同的示例。 这个例子是一个简单的Person
类,它有一个年龄“组件”。
int age;
class Person
{
public:
void setAge(int a) { age = a; }
int getAge() { return age; }
};
int main()
{
Person fred;
fred.setAge(22);
Person mary;
mary.setAge(33);
cout << "Fred is " << fred.getAge() << " and Mary is " << mary.getAge() << endl;
}
如果你运行这个程序,输出将是Fred is 33 and Mary is 33
。 即使您在程序中将他们设置为不同,这两个人的年龄也相同。
问题是,虽然这个程序有两个人,但它只有一个年龄。 所以,两个人的年龄不同,几乎是不可能的。
这是正确编写的程序。 关键的区别在于age
变量在 class 内部。 这意味着每个Person
对象都有自己的年龄。
class Person
{
public:
void setAge(int a) { age = a; }
int getAge() { return age; }
private:
int age;
};
int main()
{
Person fred;
fred.setAge(22);
Person mary;
mary.setAge(33);
cout << "Fred is " << fred.getAge() << " and Mary is " << mary.getAge() << endl;
}
现在输出是Fred is 22 and Mary is 33
应该Fred is 22 and Mary is 33
。
您需要做的第一件事是将xcomponent
和ycomponent
移动到对象内部。 现在它们是全局变量,这意味着它们在您创建的所有对象(以及外部对象)中共享值。
我假设您在移至对象之前已经了解了结构。 如果不先了解结构,就很难理解对象。
结构和类非常相似。 它们都是变量的容器。 类是更高级的版本,通常隐藏原始数据,而是提供成员函数(有时称为方法),允许以更方便的方式操作内部数据。
无论如何,当你创建一个类的新对象时,你创建了一个新的副本,里面的所有成员变量(字段)。 这样,它们可以为每个对象设置不同的值。
在这方面,您的代码很容易修复。 只需在类中移动这些变量的定义即可。
旧代码:
double xcomponent, ycomponent;
double ans, anns, annns;
class Vector // creates Vector class
{
public:
//...
};
新代码:
class Vector // creates Vector class
{
double xcomponent, ycomponent;
double ans, anns, annns;
public:
//...
};
现在我们可以处理返回值。
你的getNewVec
返回值getNewVec
。 您已经声明要返回Vector
类型的对象,而这正是您想要的。 但是,该函数还应将单个向量作为参数。 现在你有参数int a
和int b
,它们都不是Vector
。 我们需要将其更改为Vector otherVector
以执行您的任务所说的。
该函数的调用如下所示: someVector.getNewVec(someOtherVector)
。 当它运行时,您可以在其中访问两个向量。 其中第一个是调用函数的那个。 您可以直接访问其字段。 第二个当然是参数otherVector
。 您可以通过其成员函数访问其字段。 (或者您可以直接访问其私有字段,因为您在其类的成员函数中。)
现在您需要构建新的向量。 最简单的方法是创建它并一一赋值:
Vector getNewVec(Vector otherVector)
{
Vector newVector;
newVector.setXcom(xcomponent + otherVector.getXcom());
newVector.setYcom(ycomponent + otherVector.getYcom());
return newVector;
}
或者:
Vector getNewVec(Vector otherVector)
{
Vector newVector;
newVector.setXcom(xcomponent + otherVector.xcomponent);
newVector.setYcom(ycomponent + otherVector.ycomponent);
return newVector;
}
或者如果你真的想要:
Vector getNewVec(Vector otherVector)
{
Vector newVector;
newVector.setXcom(this->getXcom() + otherVector.getXcom());
newVector.setYcom(this->getYcom() + otherVector.getYcom());
return newVector;
}
( this
是您内部对象的指针。您可以从每个成员函数访问它。)
我推荐第二种选择。
如果您有兴趣,您可以阅读一些其他内容......(我不会在这里详细介绍。)
Vector(int x, int y)
{
xcomponent = x;
ycomponent = y;
}
这允许创建一个对象并在一行中分配值,而不是:
Vector newVector;
newVector.setXcom(12);
newVector.setYcom(42);
你可以有:
Vector newVector(12, 42);
您可以拥有多个具有不同参数列表的构造函数。
Vector operator+(Vector otherVector)
//the body is the same as getNewVec
您可以像普通成员函数一样调用它:
someVector.operator+(someOtherVector)
但更好的写法是:
someVector + someOtherVector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.