Home > Enterprise >  Calculate class Cylinder using class Circle
Calculate class Cylinder using class Circle

Time:05-24

The constructor of class "Circle" allows the radius to be specified via a parameter, while it is not possible to create objects of the Circle type without specifying the parameter. Also, automatic conversion of real numbers into Circle objects must not be allowed. The Set method, which does the same thing as a constructor, should also be supported, except that it allows the radius of an already created object to be changed later.

The Cylinder class constructor requires two parameters that represent the base radius and the height of the roller, respectively. Instances of this class also cannot be created without specifying the mentioned information. It should also support the "Set" function, which does the same thing as a constructor, except that it allows you to modify an already created object.

Both classes must have other methods (listed in code).

I need to use class Circle inside class Cylinder to enable calculating volume, area, and other functions.

#include <cmath>
#include <iostream>

class Circle {
  double radius;

  public:
  Circle(double r);
  void Set(double r);
  double GetRadius() const;
  double GetPerimeter() const;
  double GetArea() const;
  void Scale(double s);
  void Print() const;
};

class Cylinder {
  Circle baze;
  double height;

  public:
  Cylinder(double r_baze, double h);
  void Set(double r_baze, double h);
  Circle GetBaze() const;
  double GetRadius() const;
  double GetHeight() const;
  double GetArea() const;
  double GetVolume() const;
  void Scale(double s);
  void Print() const;
};

int main() { 
  return 0; 
}

Circle::Circle(double r) {
  radius = r;
}

void Circle::Set(double r) {
  radius = r;
}

double Circle::GetRadius() const { return radius; }

double Circle::GetPerimeter() const { return 2 * 4 * atan(1) * radius; }

double Circle::GetArea() const { return radius * radius * 4 * atan(1); }

void Circle::Scale(double s) {
  radius *= s;
}

void Circle::Print() const {
  std::cout << "R= " << GetRadius() << " O= " << GetPerimeter()
            << " P= " << GetRadius();
}

Cylinder::Cylinder(double r_baze, double h) {
  baze.GetRadius() = r_baze;
  height = h;
}

void Cylinder::Set(double r_baze, double h) {
  baze.GetRadius() = r_baze;
  height = h;
}

Circle Cylinder::GetBaze() const { return baze; }

double Cylinder::GetRadius() const { return baze.GetRadius(); }

double Cylinder::GetHeight() const { return height; }

double Cylinder::GetArea() const {
  return baze.GetArea() * 2   baze.GetPerimeter() * height;
}

double Cylinder::GetVolume() const { return baze.GetArea() * height; }

void Cylinder::Scale(double s) {
  baze.GetRadius() *= s;
  height *= s;
}

void Cylinder::Print() const {
  std::cout << "R= " << baze.GetRadius() << " H= " << height
            << " P= " << GetArea() << " V= " << GetVolume();
}

I'm new to objected-oriented programming concept. Could you help me to understand where I'm making mistakes?

I cannot compile this, because I get errors:

57 : no matching function for call to ‘Circle::Circle()’

14: note: candidate: ‘Circle::Circle(double)’

14: note: candidate expects 1 argument, 0 provided

3: note: candidate: ‘constexpr Circle::Circle(const Circle&)’

3: note: candidate expects 1 argument, 0 provided

62, 70, 91 : lvalue required as left operand of assignment

CodePudding user response:

Cylinder::Cylinder(double r_baze, double h) {
 baze.GetRadius() = r_baze;
 height = h;
}

In your Cylinder class, when your constructor is called, baze is implicitly initialized with a default constructor that does not exist.

You want to use an initializer list to handle that initialization, at which point the code inside your Cylinder constructor becomes unnecessary.

Cylinder::Cylinder(double r_baze, double h) 
: baze(r_baze), height(h) {
}

Alternatively, you could provide functionally a default constructor for your Circle class, and then Set the radius in Cylinder's constructor, but that's more work.

Circle::Circle(double r=0.0) {
  radius = r;
}

Cylinder::Cylinder(double r_baze, double h) {
  baze.Set(r_baze);
  height = h;
}

Also...

Please note that GetRadius returns a double and cannot be assigned to, so you will get an error on that line of code.

  • Related