类内的运算重载乘法
Operation Overload Multiplication inside a class
我是操作重载的新手。我正在做一个实验室,在那里我需要能够显示圆的面积和周长。用户输入半径和中心x,y点。我的问题是,我迷失在如何正确执行乘法运算过载的问题上。有人能帮我吗?
这是主.cpp 的一部分
cout << "====================================================" << endl;
cout << "Radius is: " << circle1.setRadius << endl;
cout << "Area is: " << circle1.setArea << endl;
cout << "Circumference is: " << circle1.setCircumference << endl;
这是我的circleTypeImp.cpp
#include <iostream>
#include "circleType.h"
#include "pointType.h"
class CircleType;
const float PI = 3.14;
void CircleType::setRadius (float r)
{
float radius const=r;
}
void CircleType::printCircle() const
{
}
void CircleType::setArea ()
{
return PI * radius * radius;
}
void CircleType::setCircumference ()
{
return 2 * PI * radius;
}
这是我的CircleType.h
#ifndef CIRCLETYPE_H
#define CIRCLETYPE_H
#include "pointType.h"
#include <iostream>
using namespace std;
class CircleType : public PointType
{
public:
void setRadius(float r);
void printCircle() const;
CircleType& operator* (const CircleType& radius);
void setArea();
void setCircumference();
private:
float radius const;
};
#endif
感谢
根据您的澄清,以下是我可以为您提供的帮助。
我已经粘贴并编辑了您的代码,并在进行更改时添加了注释,以详细说明为什么要进行这些更改。
一些亮点:
-
当调用任何函数时,即使是那些没有参数的函数,也仍然需要
()
。 -
返回值的函数不应返回
void
,而应返回返回值的类型。(即以下情况下的float
(。 -
当您想设置成员变量时,通常使用
setVariableName
或类似的方法,当您返回成员变量的值(或对其进行简单操作(时,可以使用getVariableName
以避免混淆功能应该做什么 -
您应该避免在头文件(.h/.hpp文件(中使用
using namespace std
,以免强制使用代码的每个人也使用相同的名称空间。 -
radius
不应该声明为const
,因为您希望能够在创建对象后设置它的值。 -
当使用
PI
时,您通常需要变量类型的最准确表示。在cmath
中的许多系统上,您经常可以将其作为M_PI
(#include <cmath>
(找到。
我还没有对PointType
类或重载的乘法运算符1做任何实际操作。我不知道PointType
类是什么样子的,乘法运算符似乎也没有必要。
下面是一个使用此类的示例程序,用于说明如何使用成员函数。
CircleType.h
#ifndef CIRCLE_TYPE_H
#define CIRCLE_TYPE_H
#include "PointType.h"
#include <iostream>
// It's common practice to not put "using namespace" in a header file.
// If you do, anyone including your header file has to use it.
class CircleType : PointType {
public:
void setRadius(const float r);
void printCircle() const;
CircleType operator * (const circleType& c) const;
float getRadius() const;
float getArea() const;
float getCircumference() const;
private:
float radius; // Note because you want to set radius
// after creation of a circle object
// radius should not be const
}
#endif
CircleType.cc
#include "CircleType.h" // PointType.h will also be included
#include "PointType.h" // But it's also fine to explicitly include it
#include <iostream>
using namespace std;
// you don't need a dummy class here, In fact that will likely cause a compiler issue
// You should probably use a more accurate value of Pi
const float PI = 3.1415927; // Or use M_PI in "cmath" (if it's defined on your system)
void CircleType::setRadius(const float r){
radius = r;
}
void CircleType::printCircle() const {
// Do whatever you need to print a circle
}
// As far as I can tell, you don't actually need to overload
// multiplication to do any the tasks you mentioned.
CircleType CircleType::operator * (const CircleType& c) const {
CircleType tmp;
// I have no clue what multiplying two circles gives you,
// this is the general form of the multiplication operator
return tmp;
}
// It is customary to name a function that returns a private variable
// or the result of simpler operations on private variables with getXXX
// like the ones below -- Note the return type is not void but
// the actual type you expect.
float CircleType::getRadius() const {
return radius;
}
float CircleType::getArea() const {
return PI * radius * radius;
}
float CircleType::getCircumference() const {
return 2 * PI * radius;
}
main.cc
#include "CircleType.h"
#include "PointType.h"
#include <iostream>
using namespace std;
int main(int argc, char ** argv){
CircleType c1;
float tmp;
cout << "Input radius of circle: ";
cin >> tmp;
c1.setRadius(tmp);
cout << "=============================================" << endl;
// note even for functions that take no parameters, you still need the () to call it
cout << "Radius is: " << c1.getRadius() << endl;
cout << "Area is: " << c1.getArea() << endl;
cout << "Circumference is: " << c1.getCircumference() << endl;
return 0;
}
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- GCC本机矩阵运算库
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 矩阵运算C++运算符重载程序错误
- 为什么++重载运算的加法会导致未过期的值
- 类内的运算重载乘法