类内的运算重载乘法

Operation Overload Multiplication inside a class

本文关键字:重载 运算      更新时间:2023-10-16

我是操作重载的新手。我正在做一个实验室,在那里我需要能够显示圆的面积和周长。用户输入半径和中心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

感谢

根据您的澄清,以下是我可以为您提供的帮助。

我已经粘贴并编辑了您的代码,并在进行更改时添加了注释,以详细说明为什么要进行这些更改。

一些亮点:

  1. 当调用任何函数时,即使是那些没有参数的函数,也仍然需要()

  2. 返回值的函数不应返回void,而应返回返回值的类型。(即以下情况下的float(。

  3. 当您想设置成员变量时,通常使用setVariableName或类似的方法,当您返回成员变量的值(或对其进行简单操作(时,可以使用getVariableName以避免混淆功能应该做什么

  4. 您应该避免在头文件(.h/.hpp文件(中使用using namespace std,以免强制使用代码的每个人也使用相同的名称空间。

  5. radius不应该声明为const,因为您希望能够在创建对象后设置它的值。

  6. 当使用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;
}