Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Displaying results of methods on the screen

My task is to practice inheritance, putting all the classes in separate files. I have a base class Circle and a derived class Cylinder.

What I’m stuck on is trying to display on the screen the result of my calculated area and volume for an object B of a Cylinder type. I found a way to do that for a Circle, though it doesn’t work for my Cylinder.

circle.h

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#pragma once
#include <iostream>

class Circle {
 public:
    float r; 
    Circle();
    Circle(float r);
    Circle circumference();
    Circle area();
    void view();
    void getArea();
    void getCircum();
};

circle.cpp

#include "circle.h"
#include <cmath>

using namespace std;

Circle::Circle() : r(5) {
    cout << "Default constructor has been called for a circle\n";
}

Circle::Circle(float r) {
    this->r = r;
    cout << "Constructor with parameters has been called for a circle\n";
}

void Circle::view() {
    cout << "Radius = " << r << endl;
}

Circle Circle::circumference() {
    return 2 * M_PI * r;
}

Circle Circle::area() {
    return M_PI * pow(r, 2);
}

void Circle::getArea() {
    cout << "Area = " << r << " m^2";
}

void Circle::getCircum() {
    cout << "Circumference = " << r << " m";
}

cylinder.h

#pragma once
#include <iostream>
#include "circle.h"

class Cylinder : public Circle {
 public:
    float h; 
    Cylinder();
    Cylinder(float r, float h);
    void view();
    double area();
    double volume(float r, float h);
    void getArea();
    void getVolume();
};

cylinder.cpp

#include "cylinder.h"
#include <cmath>

using namespace std;

Cylinder::Cylinder() : h(7) {
    cout << "Default constructor has been called for a cylinder\n";
}

Cylinder::Cylinder(float r, float h) : Circle(r) {
    this->h = h;
    cout << "Constructor with parameters has been called fo a cylinder\n";
}

void Cylinder::view() {
    Circle::view();
    cout << "Height = " << h << endl;
}

double Cylinder::area() {
    return 2 * M_PI * r * h;
}

double Cylinder::volume(float r, float h) {
    return M_PI * pow(r, 2) * h;
}

void Cylinder::getArea() {
    cout << "Area = " << h;
}

void Cylinder::getVolume() {
    cout << "Volume = " << h;
}

main.cpp

#include <iostream>
#include "circle.h"
#include "cylinder.h"

using namespace std;

int main() {
    Circle A;
    A.view();
    Circle A1(8);
    A1.view();
    
    Cylinder B;
    B.view();
    Cylinder B1(4, 6);
    B1.view();
    
    //A.area().getArea();
    //cout << endl;
        //A.circumference().getCircum();
        //cout << endl;
        //A1.area().getArea();
    //cout << endl;
        //A1.circumference().getCircum();
    
        B.area().getArea();

    return 0;
}

The error that I’m getting:

main.cpp: In function ‘int main()’:
main.cpp:26:14: error: request for member ‘getArea’ in ‘B.Cylinder::area()’, which is of non-class type ‘double’
   26 |     B.area().getArea();
      |              ^~~~~~~

I feel like neither my code in main() for instance B, nor my methods getArea() and getVolume() in class Cylinder, are correct. And there is probably a better approach to do the same for an object A and A1 of a Circle type, though the code I commented out actually works.

I know that this is a dumb question, and such things should be quite straightforward, but I am trying to learn and would be grateful for any advice on how I can fix this.

>Solution :

In your getArea() function, instead of saying:

cout << "Area = " << endl;

Just say:

cout << "Area = " << area() << endl;

Then in your main.cpp, just call B.getArea().

Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading