简体   繁体   English

如何在其他类中使用参数化构造函数创建类的对象?

[英]How to make object of a class with parameterized constructor in other class?

I want to make an object of DataArea class in Area class and initialize data in main function.我想在Area类中创建一个DataArea类的对象并在main函数中初始化数据。 But the only way my code works is by initializing data in Area class.但是我的代码工作的唯一方法是在Area类中初始化数据。

Also, I do not know if I have made the object correctly or not.另外,我不知道我是否正确地制作了对象。 Please guide me.请指导我。 My code is below:我的代码如下:

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

It seems to me that Area class is surplus for what you are trying to achieve.在我看来, Area课程对于您想要实现的目标来说是多余的。 You should probably put methods directly in DataArea class.您可能应该将方法直接放在DataArea类中。 Then you can create as many of DataArea objects as you like...然后,您可以根据需要创建任意数量的DataArea对象...

Like this:像这样:

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

The reason why you are probably having trouble to understand the concept: You're defining a class and instantiating an object.您可能难以理解这个概念的原因是:您正在定义一个类并实例化一个对象。 Sometimes these terms are used interchangeably, but in this case, this is an important distinction.有时这些术语可以互换使用,但在这种情况下,这是一个重要的区别。

If you would like for your methods to operate on some other class, that you should make methods that accept that class as an argument.如果您希望您的方法对某个其他类进行操作,那么您应该创建接受该类作为参数的方法。 Otherwise, it is unnecessary complex.否则,它是不必要的复杂。

Your DataArea is basically absolute if you do not use it outside of Area class.如果您不在Area类之外使用它,您的DataArea基本上是绝对的。 Similarly, print_data class can be replaced by an operator<< overload .同样, print_data类可以替换为operator<<重载

Following is the updated code, in which the comments will guide you through.以下是更新后的代码,其中的注释将引导您完成。

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

Output :输出

Area of Circle is: 153.958
Area of Rectangle is: 12
Area of Traingle is: 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++ 的类的默认构造函数中调用成员 object 变量的参数化构造函数? - How to call parameterized constructor of member object variable in a class' default constructor in C++? 使用其他对象的类构造函数 - Class constructor using other object 如何在另一个类中创建一个类的参数化构造函数作为数据成员? - How to create parameterized constructor of a class in another class as a data member? 继承的类模板的参数化构造函数 - parameterized constructor for inherited class templates 对于带有参数化构造函数的 class,创建一个 object 而不传递 arguments - For a class with parameterized constructor, create an object without passing arguments 在另一个类构造函数中初始化类对象 - Initializing class object inside the other class constructor 在另一个类构造函数中初始化一个类对象 - Initialize a class object inside the other class constructor 如何在虚拟继承下游使用参数化基类构造函数 - How to use parameterized base class constructor downstream of virtual inheritance 如何使用 std::vector 动态实例化具有参数化构造函数的类? - How to dynamically instantiate a class with parameterized constructor using std::vector? 如何在C++中调用Base class的参数化构造函数? - How to call parameterized Constructor of Base class in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM