简体   繁体   中英

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. But the only way my code works is by initializing data in Area class.

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. You should probably put methods directly in DataArea class. Then you can create as many of DataArea objects as you like...

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. Similarly, print_data class can be replaced by an operator<< overload .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM