Program for calculating the area and perimeter – problem with functions [closed]

  area, c++, oop

I wrote a program calculating the field and perimeter of the rectangle. In the program I need to use static fields (teacher’s recommendation) and the whole program must be written in object-oriented way.The program gets such values as: owner’s data, length, width and ID correctly – however, the problem is in the field and perimeter calculation, because it results the value of 1 (for both field and perimeter) when I run the program (probably it does not enter the function at all).

Here’s my code:

#include <iostream>

using namespace std;

class Data
{
    public:
    float length;
    float width;
    float field;
    float perimeter;
    int id;
    static string owner;
    void calc_field(float&,float&);
    void calc_perimeter(float&,float&);
    void datad(string& ,float&, float&, float&, float& ,int&);

};

string Data::owner = "XYZ";


void Data::calc_field(float &length,float &width)
{
    field=length*width;
}

void Data::calc_perimeter(float &length,float &width)
{
    perimeter=(2*length)+(2*width);
}

void Data::datad(string &ownerf ,float &lengthf, float &widthf, float &fieldf, float &perimeterf, int &IDf) {

    ownerf = owner;
    lengthf = length;
    widthf = width;
    fieldf = field;
    IDf = id;
    perimeterf = perimeter;
}

int main()
{
    Data data1;

    cout<<"Length: ";
    cin >> data1.length;

    cout<<"Width: ";
    cin >> data1.width;

    cout<<"Id: ";
    cin >> data1.id;


    cout << "Data: " << endl;
    cout << "Owner Name: " << Data::owner <<endl;
    cout << "Length: " << data1.length << endl;
    cout << "Width: " << data1.width << endl;
    cout << "Id: "<< data1.id <<endl;
    cout << "Field: "<<Data::calc_field<<endl;
    cout << "Perimeter: "<<Data::calc_perimeter<<endl;

    return 0;
}

How to solve this problem? Thank you in advance for your help.

Source: Windows Questions C++

LEAVE A COMMENT