C++ 如何在其他类中使用参数化构造函数生成类的对象?

C++ 如何在其他类中使用参数化构造函数生成类的对象?,c++,class,oop,data-structures,constructor,C++,Class,Oop,Data Structures,Constructor,我想在Area类中创建DataArea类的对象,并在main函数中初始化数据。但是我的代码工作的唯一方法是初始化区域类中的数据 此外,我不知道我是否正确地制作了对象。请引导我。我的代码如下: #include<iostream> using namespace std; class DataArea { public: int radius, length, width, base, heigth; DataArea(int l, int w, int b, int

我想在
Area
类中创建
DataArea
类的对象,并在
main
函数中初始化数据。但是我的代码工作的唯一方法是初始化
区域
类中的数据

此外,我不知道我是否正确地制作了对象。请引导我。我的代码如下:

#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();
}
#包括
使用名称空间std;
类数据区
{
公众:
整数半径、长度、宽度、底面、高度;
数据区(整数l、整数w、整数b、整数h、整数r)
{
长度=l;
宽度=w;
半径=r;
高度=h;
基数=b;
}
};
班级面积
{
公众:
数据区*s=新数据区(3,4,5,6,7);
浮动区域卷轴()
{
返回3.142*s->radius*s->radius;
}
浮动区域矩形()
{
返回s->length*s->width;
}
浮动区域
{
返回(s->base*s->heigth)/2;
}
};
类打印单元数据:公共区域
{
公众:
作废打印()
{

cout在我看来,
区域
类对于您试图实现的目标来说是多余的。您可能应该将方法直接放入
数据区域
类中。然后您可以创建任意数量的
数据区域
对象

像这样:

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;
}
类数据区
{
公众:
整数半径、长度、宽度、底面、高度;
数据区(整数l、整数w、整数b、整数h、整数r)
{
长度=l;
宽度=w;
半径=r;
高度=h;
基数=b;
}
浮动区域卷轴()
{
返回3.142*半径*半径;
}
浮动区域矩形()
{
返回长度*宽度;
}
浮动区域
{
返回(基*高)/2;
}
};
int main(int argc,字符**argv)
{
数据区1(1,2,3,4,5);
数据区2(8,2,3,4,5);

std::cout如果您不在
区域
类之外使用,则您的
数据区域
基本上是绝对的。类似地,
打印数据
类也可以替换为

以下是更新的代码,注释将引导您完成

#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;
}

例如,
DataArea da(3,4,5,6,7)
有什么问题吗?它会给您带来什么问题?不太清楚您有什么问题。如何在不初始化值的情况下在Area类中创建对象?如果我不初始化Area类中的值,它会说,“需要5个参数,提供0”你需要一个构造函数来初始化
区域
类中的
数据区域
成员。有人能给我一个有两个类的程序吗?一个类有一个可变半径,一个构造函数初始化值。第二个类应该有第一个类的对象并计算圆的面积。这将帮助我理解这个概念。
Area of Circle is: 153.958
Area of Rectangle is: 12
Area of Traingle is: 15