任务是在上面的代码中编写计算新点的成员函数,这是另外两个点的数量。而且我不知道如何返回物体或我应该怎么做。这是代码,功能标有三个!!!该函数必须返回一些东西,我不能使它无效,因为不允许引用void。
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
我做到了!这是必须添加的功能
class point {
private:
float x;
float y;
public:
point();
point(float xcoord, float ycoord);
void print();
float dist(point p1, point p2);
!!! float &add(point p1, point p2);
float &X();
float &Y();
~point();
};
float & point::X() { return x; }
float & point::Y() { return y; }
point::point() {
cout << "Creating POINT (0,0)" << endl;
x = y = 0.0;
}
point::point(float xcoord, float ycoord) {
cout << "Creating POINt (" << xcoord << "," << ycoord << ")" << endl;
x = xcoord;
y = ycoord;
}
void point::print() {
cout << "POINT (" << x << "," << y << ")";
}
float point::dist(point p1, point p2) {
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
!!!// float & point::add(point p1, point p2) {
point z;
z.X() = p1.X() + p2.X();
z.Y() = p1.Y() + p2.Y();
z.print();
}
point::~point() {
cout << "Deleting ";
print();
cout << endl;
}
int main() {
point a(3, 4), b(10, 4);
cout << "Distance between"; a.print();
cout << " and "; b.print();
cout << " is " << a.dist(a, b) << endl;
}
非常感谢ForceBru !!和你们所有人
答案 0 :(得分:3)
您也可以返回point
:
point point::add(const point& p1, const point& p2) {
point result;
result.x = p1.x + p2.x;
result.y = p1.y + p2.y;
return result;
}
请注意,这里不需要使用X()
和Y()
函数,因为此方法已经可以访问私有成员。
也可以进行操作员重载
/*friend*/ point operator+ (const point& one, const point& two) {
// the code is the same
}
int main() {
point one(2,5), two(3,6), three;
three.add(one, two);
std::cout << "Added " << one.print() << " and " << two.print();
std::cout << " and got " << three.print() << std::endl;
return 0;
}
修改:正如评论中所述,您不应该返回对add
函数中创建的对象的引用,因为在这样的情况下您只允许返回对类成员和static
变量的引用的情况。
答案 1 :(得分:0)
您可以在此处使用运算符重载:
point point::operator+(const point & obj) {
point obj3;
obj3.x = this->x + obj.x;
return obj3;
}
添加两点后返回对象。
简单示例:
class Addition {
int a;
public:
void SetValue(int x);
int GetValue();
Addition operator+(const Addition & obj1);
};
void Addition::SetValue(int x)
{
a = x;
}
int Addition::GetValue()
{
return a;
}
Addition Addition::operator+(const Addition &obj1)
{
Addition obj3;
obj3.a = this->a + obj1.a;
return obj3;
}
int _tmain(int argc, _TCHAR* argv[])
{
Addition obj1;
int Temp;
std::cout<<"Enter Value for First Object : "<<std::endl;
std::cin>>Temp;
obj1.SetValue(Temp);
Addition obj2;
std::cout<<"Enter Value for Second Object : "<<std::endl;
std::cin>>Temp;
obj2.SetValue(Temp);
Addition obj3;
obj3 = obj1 + obj2;
std::cout<<"Addition of point is "<<obj3.GetValue()<<std::endl;
return 0;
}