Overloaded Circle() is ambiguous in C++ Circle Class - c++

I am currently trying to create a circle class in C++ but when I compile I get an error saying "call of overloaded 'Circle' is ambiguous. I am quite new to C++ and not sure what this means. I have been using a similar example found here Class tutorial
#include <iostream>
using namespace std;
const double pi = 3.14159265;
class Circle
{
private:
double radius, xpos, ypos, area;
public:
Circle(double r, double xposition, double yposition) {
r = radius;
xposition = xpos;
yposition = ypos;
}
Circle(double r = 0) {
radius = r;
xpos = 0;
ypos = 0;
}
Circle() {
radius = 0;
xpos = 0;
ypos = 0;
}
double getRadius() {return radius;}
double getX() {return xpos;}
double getY() {return ypos;}
double getArea() {return pi*radius*radius;}
Circle operator+(const Circle& c) {
Circle circle;
circle.area = this -> getArea() + c.getArea();
return circle;
}
};
int main()
{
Circle circ(3,2,1);
double x = circ.getX();
cout << x << endl;
return 0;
}

Your problem is this:
Circle(double r = 0);
Circle();
Nothing much more to say about it that isn't already obvious from the above.
The default value in Circle(double r = 0) makes the use of Circle() ambiguous.
By the way, you've got your member variable initialization all wrong here:
Circle(double r, double xposition, double yposition)
{
r = radius;
xposition = xpos;
yposition = ypos;
}
Instead of setting the member variables to the values of the input arguments, you are setting the input arguments to the "junk" values of the member variables...

You have two different constructors that can be called with no parameters, because one of them has a default for the parameter. You can delete the default constructor, since it's redundant.

Related

Function only returning 1's for the area and disregards the formula?

I am trying to calculate area of all shapes (rectangle, rhombus, triangle, circle) through using virtual and override methods. But when I execute the code it returns 1 for the area for all shapes even though I have tried with the rectangle to alter it to input the given area multiple times in int main() it still only outputs "My figure type is My area is 1 My figure type is Triangle My area is 1 My figure type is Circle My area is 1 My figure type is Rhombus My area is 1"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Figure
{
protected:
double x;
double y;
string type;
public:
Figure(double m_x, double m_y) : x{ m_x }, y{ m_y } {};
virtual double area(double x, double y) { return 0; };
Figure(double m_x, double m_y, double x = 0, double y = 0) { m_x = x; m_y = y; }
virtual void Print() const final;
Figure(const Figure& obj);
Figure() {};
};
class Rectangle : public Figure
{
public:
Rectangle(double x, double y)
{
this->x = x;
this->y = y;
type = " Rectangle";
double area();
}
double area(double x, double y) override {
return x * y;
}
Rectangle() {};
};
class Triangle : public Figure
{
public:
Triangle(double x, double y)
{
this->x = x;
this->y = y;
type = " Triangle";
double area();
}
double area(double x, double y)override {
return x * y / 2;
}
Triangle() {};
};
class Circle : public Figure
{
public:
Circle(double x, double y)
{
this->x = x;
this->y = y;
type = " Circle";
double area();
}
double area(double x, double y)override {
return pow(x, 2) * 3.14;
}
Circle() {};
};
class Rhombus : public Figure
{
public:
Rhombus(double x, double y)
{
this->x = x; this->y = y; type = " Rhombus"; double area();
}
double area(double x, double y)override {
return x * y / 2;
}
Rhombus() {};
};
void Figure::Print() const
{
cout << " My figure type is" << type
<< " My area is " << &Figure::area;
}
int main()
{
Rectangle rectangle;
rectangle.area(5.4,6.2);
rectangle.Print();
Triangle triangle(4.5,5.3);
triangle.Print();
Circle circle(6.6, 8.8);
circle.Print();
Rhombus rhombus(3.4,5.4);
rhombus.Print();
}
You are getting 1 because a valid function pointer is treated as true.
You should call the function area like Figure::area(x, y) instead of getting the address of the function area like &Figure::area.
it's simple. really.
When you do
rectangle.area(x, y);
you just return the value you obtain from the variables that you passed in. You never assign a value to the x and y of the actual rectangle. So when you do print the area of the rectangle you use its real x and y, which do not have a value assigned to them, hence resulting in a 1. it's the same for the other shapes.

Calculate class Cylinder using class Circle

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 GetRadiusOfBaze() 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::GetRadiusOfBaze() 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.GetRadiusOfBaze() << " 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
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.

Point, square, and cube program help on C++

I've been writing a program for CS class that's supposed to get the X and Y coordinates from the user, as well as the length of a square and the height of the cube, and it should then calculate the area of the square and the surface area and volume of the cube (plus some coordinates stuff but that's not a pressing issue right now)
I've written the test file and it compiled successfully, but I've been getting very long answers for the square and cube properties that are obviously wrong. Can anyone point out whatever logical errors I might have or if I have the access specification and relationship between the classes wrong?
Point.h
class Point
{
protected:
double Xint, Yint;
public:
Point();
void setX(double);
void setY(double);
double getX() const;
double getY() const;
};
Point.ccp
Point::Point()
{
Xint = 0;
Yint = 0;
}
void Point::setX(double x)
{ Xint = x; }
void Point::setY(double y)
{ Yint = y; }
double Point::getX() const
{ return Xint; }
double Point::getY() const
{ return Yint; }
Square.h
#include "Point.h"
class Square : public Point
{
protected:
Point lowerLeft;
double sideLength;
public:
Square(double sideLength, double x, double y) : Point()
{
sideLength = 0.0;
x = 0.0;
y = 0.0;
}
void setLowerLeft(double, double);
void setSideLength(double);
double getSideLength() const;
double getSquareArea() const;
};
Square.ccp
#include "Square.h"
void Square::setLowerLeft(double x, double y)
{
lowerLeft.setX(x);
lowerLeft.setY(y);
}
void Square::setSideLength(double SL)
{ sideLength = SL; }
double Square::getSideLength() const
{ return sideLength; }
// Calculate the area of square
double Square::getSquareArea() const
{ return sideLength * sideLength; }
Cube.h
#include "Square.h"
class Cube : public Square
{
protected:
double height;
double volume;
public:
Cube(double height, double volume) : Square(sideLength, Xint, Yint)
{
height = 0.0;
volume = 0.0;
}
double getSurfaceArea() const;
double getVolume() const;
};
Cube.ccp
#include "Cube.h"
// Redefine GetSquareArea to calculate the cube's surface area
double Cube::getSurfaceArea() const
{ return Square::getSquareArea() * 6; }
// Calculate the volume
double Cube::getVolume() const
{ return getSquareArea() * height; }
"Can anyone point out whatever logical errors I might have or if I have the access specification and relationship between the classes wrong?"
Well, from our well known 3-dimensional geometry a cube is made up from exactly 6 squares.
So how do you think inheriting a Cube class from a Square actually should work well?
You can easily define a Cube class by means of a fixed Point (e.g. the upper, left, front corner) and a fixed size of the edge length.
If you really want and need to, you can add a convenience function for your Cube class, that returns all of the 6 Squares it consist of in 3 dimensional space:
class Cube {
public:
Cube(const Point& upperLeftFrontCorner, double edgeLength);
std::array<Square,6> getSides() const;
};

C++ inheritance (overriding constructors)

I am learning OpenGL w/ C++. I am building the asteroids game as an exercise. I'm not quite sure how to override the constructors:
projectile.h
class projectile
{
protected:
float x;
float y;
public:
projectile();
projectile(float, float);
float get_x() const;
float get_y() const;
void move();
};
projectile.cpp
projectile::projectile()
{
x = 0.0f;
y = 0.0f;
}
projectile::projectile(float X, float Y)
{
x = X;
y = Y;
}
float projectile::get_x() const
{
return x;
}
float projectile::get_y() const
{
return y;
}
void projectile::move()
{
x += 0.5f;
y += 0.5f;
}
asteroid.h
#include "projectile.h"
class asteroid : public projectile
{
float radius;
public:
asteroid();
asteroid(float X, float Y);
float get_radius();
};
main.cpp
#include <iostream>
#include "asteroid.h"
using namespace std;
int main()
{
asteroid a(1.0f, 2.0f);
cout << a.get_x() << endl;
cout << a.get_y() << endl;
}
error I'm getting:
main.cpp:(.text+0x20): undefined reference to `asteroid::asteroid(float, float)'
You can use the : syntax to call the parent's constructor:
asteroid(float X, float Y) : projectile (x ,y);
Ok, just figured it out.
I actually don't have asteroid constructors defined because I thought they would inherit. But I think I have to do the following in asteroid.h:
asteroid(float X, float Y) : projectile(X, Y){];
You need a asteroid.cpp.
Even though inheriting from projectile, for non-default constructors (i.e., asteroid(float,float)), you still need to define the child class constructor.
You'll also need to define get_radius, as it's not defined in your base class.
Here's how that might look (I've taken the liberty of passing values for radius into both ctors):
#include "asteroid.h"
asteroid::asteroid(float r)
: projectile()
{
radius = r;
}
asteroid::asteroid(float x, float y, float r)
: projectile(x, y)
{
radius = r;
}
float asteroid::get_radius()
{
return radius;
}

how to resolve an ambigious base class in c++

Both base classes, Arc and Lines, are derived from class Shape.
The compiler says Ojbect b1 "error: shape is ambiguous". I know that two instances of Shape are being created, but don't know how to resolve it?
Graph_lib::Box b1(Point,100,100), 100,100);
win1.attach(b1);
This class will be able to draw a box with rounded corners. I just wrote the code for the Box Lines part, I didn't get to the Arc yet since this won't even work.
//------------------------------------------------------------------------------
struct Box : Lines , Arc {
Box(Point xy, int ww, int hh);
void Top_segment();
void Bottom_segment();
void Left_side_segment();
void Right_side_segment();
void draw_lines() const;
int height() const { return h; }
int width() const { return w; }
private:
int h; // height
int w; // width
double width_tenth; //10% of the width that will calculate the length to remove from each side to make room for the arcs
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Box::Box(Point xy, int ww, int hh): w(ww), h(hh)
{
width_tenth = (xy.x + w) * 0.10;
if (h<=0 || w<=0) error("Bad box: non-positive side");
}
//------------------------------------------------------------------------------
void Box::Top_segment()
{
double top_seg_begin_w; //where the line segment will begin after deducting 10% of w;
double top_seg_end_w; //where the line segment will end after deducting 10% of w;
top_seg_begin_w = xy.x + width_tenth;
top_seg_end_w = (xy.x + w) - width_tenth;
Lines::add(Point(top_seg_begin_w,xy.y),Point(top_seg_end_w,xy.y));
}
//------------------------------------------------------------------------------
void Box::Bottom_segment()
{
double bottom_seg_begin_w;
double bottom_seg_end_w;
bottom_seg_begin_w = xy.x + width_tenth;
bottom_seg_end_w = (xy.x + w) - width_tenth;
double y_bottom = xy.y + h;
Lines::add(Point(bottom_seg_begin_w,y_bottom),Point(bottom_seg_end_w,y_bottom));
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void Box::Left_side_segment()
{
double left_seg_begin_h;
double left_seg_end_h;
left_seg_begin_h = xy.y + width_tenth;
left_seg_end_h = (xy.y + h) - width_tenth;
double x_left = xy.x;
Lines::add(Point(x_left,left_seg_begin_h),Point(x_left,left_seg_end_h));
}
//------------------------------------------------------------------------------
void Box::Right_side_segment()
{
double right_seg_begin_h;
double right_seg_end_h;
right_seg_begin_h = xy.y + width_tenth;
right_seg_end_h = (xy.y + h) - width_tenth;
double x_right = xy.x + w;
Lines::add(Point(x_right,right_seg_begin_h),Point(x_right,right_seg_end_h));
}
//------------------------------------------------------------------------------
Use virtual inheritance for classes Lines and Arc. For example
class Lines : virtual public Shape
{
//...
};
class Arc : virtual public Shape
{
//...
};