如何在其他类中使用参数化构造函数制作类的对象?
How to make object of a class with parameterized constructor in other class?
我想在类中创建一个DataArea
类的对象Area
并在main
函数中初始化数据。但是我的代码工作的唯一方法是初始化类中的数据Area
。
另外,我不知道我是否正确制作了对象。请指导我。我的代码如下:
#include<iostream>
using namespace std;
class DataArea
{
public:
int radius, length, width, base, heigth;
DataArea(int l, int w, int b, int h, int r)
{
length = l;
width = w;
radius = r;
heigth = h;
base = b;
}
};
class Area
{
public:
DataArea* s = new DataArea(3, 4, 5, 6, 7);
float AreaCirle()
{
return 3.142 * s->radius * s->radius;
}
float AreaRectangle()
{
return s->length * s->width;
}
float AreaTraingle()
{
return (s->base * s->heigth) / 2;
}
};
class print_data : public Area
{
public:
void print()
{
cout << "Area of Circle is: " << AreaCirle() << endl;
cout << "Area of Rectangle is: " << AreaRectangle() << endl;
cout << "Area of Traingle is: " << AreaTraingle() << endl;
}
};
int main()
{
//DataArea da(3, 4, 5, 6, 7);
print_data m;
m.print();
}
如果你不在类之外使用它,你的DataArea
基本上是绝对的Area
。同样,print_data
类也可以替换为operator<<
重载。
以下是更新的代码,其中的注释将指导您完成。
#include <iostream>
// DataArea (optionally) can be the part of Area class
struct DataArea /* final */
{
float length, width, base, height, radius;
DataArea(float l, float w, float b, float h, float r)
: length{ l } // use member initializer lists to initlize the members
, width{ w }
, base{ b }
, height{ h }
, radius{ r }
{}
};
class Area /* final */
{
DataArea mDataArea; // DataArea as member
public:
// provide a constructor which initialize the `DataArea` member
Area(float l, float w, float b, float h, float r)
: mDataArea{ l, w, b, h, r } // member initializer
{}
// camelCase naming for the functions and variables
// mark it as const as the function does not change the member
float areaCirle() const /* noexcept */
{
return 3.142f * mDataArea.radius * mDataArea.radius;
}
float areaRectangle() const /* noexcept */
{
return mDataArea.length * mDataArea.width;
}
float areaTraingle() const /* noexcept */
{
return (mDataArea.base * mDataArea.height) / 2.f;
}
// provide a operator<< for printing the results
friend std::ostream& operator<<(std::ostream& out, const Area& areaObject) /* noexcept */;
};
std::ostream& operator<<(std::ostream& out, const Area& areaObject) /* noexcept */
{
out << "Area of Circle is: " << areaObject.areaCirle() << "n";
out << "Area of Rectangle is: " << areaObject.areaRectangle() << "n";
out << "Area of Traingle is: " << areaObject.areaTraingle() << "n";
return out;
}
int main()
{
// now construct the Area object like this
Area obj{ 3, 4, 5, 6, 7 };
// simply print the result which uses the operator<< overload of the Area class
std::cout << obj;
}
输出:
Area of Circle is: 153.958
Area of Rectangle is: 12
Area of Traingle is: 15
在我看来,Area
类对于你想要实现的目标来说是多余的。您可能应该将方法直接放在类DataArea
。然后,您可以根据需要创建任意数量的DataArea
对象...
喜欢这个:
class DataArea
{
public:
int radius, length, width, base, heigth;
DataArea(int l , int w , int b , int h , int r )
{
length = l;
width = w;
radius = r;
heigth = h;
base = b;
}
float AreaCirle()
{
return 3.142 * radius * radius;
}
float AreaRectangle()
{
return length * width ;
}
float AreaTraingle()
{
return (base * heigth)/2;
}
};
int main(int argc, char **argv)
{
DataArea area1 (1,2,3,4,5);
DataArea area2 (8,2,3,4,5);
std::cout << area1.AreaCirle() << std::endl;
std::cout << area2.AreaCirle() << std::endl;
}
您可能难以理解该概念的原因: 您正在定义一个类并实例化一个对象。有时这些术语可以互换使用,但在这种情况下,这是一个重要的区别。
如果您希望您的方法对其他类进行操作,则应创建接受该类作为参数的方法。否则,它是不必要的复杂。
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 打印 2D 动态数组 c++ 函数
- C++数组函数
- 返回二叉搜索树特定级别的节点数的函数
- 在数组函数指针中调用void
- 数组函数似乎没有找到最大值
- 是否可以通过全局变量定义具有可变参数数的函数中的参数数量
- 使用数组函数和循环求解伪元编码|C
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 数组函数错误输出
- 读取文件,然后创建数组函数C++
- 快速Dobling找到Fibonacci数,函数在C++中不起作用
- 指针数组函数参数-奇怪的行为
- 为什么我的 RGB 转十六进制函数在传递颜色分量时返回 0
- C++:指向具有可变参数数的函数的函数指针
- 二维数组 - 函数
- "return"没有从数组函数返回正确的值
- 数组函数问题
- 数组函数的大小返回 int 的大小而不是数组
- 从数组函数中删除元素