C++. Classes and arrays. Initializing arrays with data values from class objects. Examples




Classes and arrays. Initializing arrays with data values from class objects. Examples


Contents


Search other websites:

1. Initializing an array by values that are data members of the object. Example

There are cases when you need to combine variables that are members of a class object in an array. In the following example, an array is initialized, which uses the data members of the object as initializers. At the time of declaration, the data members of the object must have some meaning.

The example defines the Radius class, in which are declared:

  • an internal variable named radius;
  • internal private method CalcValues(), which fills with values public-variables length, area, volume;
  • public internal variables of class length, area, volume, containing the corresponding values of the circumference, the area of the circle and the volume of a sphere for radius r;
  • class constructors;
  • access methods GetR(), SetR().

The class listing is as follows:

// class realizing the radius of a geometric figure
class Radius
{
    double radius; // variable defining the radius

    // An internal method that forms the values of variables length, area, volume
    void CalcValues(void)
    {
        const double pi = 3.141592653589;
length = 2 * pi * radius;
        area = pi * radius * radius;
        volume = 4.0/3.0 * pi * radius * radius * radius;
    }

    public:
    double length; // circumference
    double area; // area of a circle
    double volume; // the volume of a sphere

    // constructors
    Radius()
    {
        radius = 1;
        CalcValues(); // fill the variables length, area, volume
    }
    Radius(double radius)
    {
        this->radius = radius;
        CalcValues(); // fill the variables length, area, volume
    }

    // access methods
    int GetR(void) { return radius; }
    void SetR(double radius)
    {
        this->radius = radius;
        CalcValues(); // fill the variables length, area, volume
    }
};

Using a class in main() or another function:

Radius r(3); // the object r of class Radius

// initializing an array V with values that are members of the data of the object r
double V[] = {
    r.length,
    r.area,
    r.volume
};

double x;
x = V[0]; // x = 18.8496 - circumference
x = V[1]; // x = 28.2743 - area of a circle
x = V[2]; // x = 113.097 - volume of a sphere

2. Initializing the array with values that return the objects of the class. Example

Elements of an array can be initialized with values that return the methods of a certain class. The example demonstrates the initialization of the V array with values that are returned by the methods of the Radius class: Length(), Area(), Volume().






The declaration of the class has the form;

// class that realizes the radius value of some geometric figure
class Radius
{
    double radius; // variable defining the radius

    public:
    double Length()
    {
        return (2 * 3.1415 * radius);
    }

    double Area()
    {
        return (3.1415 * radius * radius);
    }

    double Volume()
    {
        return (4.0/3.0 * 3.1415 * radius * radius * radius);
    }

    // constructors
    Radius()
    {
        radius = 1;
    }
    Radius(double radius)
    {
        this->radius = radius;
    }

    // access methods
    int GetR(void) { return radius; }
    void SetR(double radius)
    {
        this->radius = radius;
    }
};

Использование класса в некотором методе

Radius r(3); // the object r of Radius class

// Initializing an array V with values that are returned by the object's methods of class r
double V[] = {
    r.Length(),
    r.Area(),
    r.Volume()
};

double x;
x = V[0]; // x = 18.8496 - circumference
x = V[1]; // x = 28.2743 - area of a circle
x = V[2]; // x = 113.097 - volume of a sphere


Related topics