Calculate class Cylinder using class Circle - c++

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.

Related

How to make object of a class with parameterized constructor in other class?

I want to make an object of DataArea class in Area class and initialize data in main function. But the only way my code works is by initializing data in Area class.
Also, I do not know if I have made the object correctly or not. Please guide me. My code is below:
#include<iostream>
using namespace std;
class DataArea
{
public:
int radius, length, width, base, heigth;
DataArea(int l, int w, int b, int h, int r)
{
length = l;
width = w;
radius = r;
heigth = h;
base = b;
}
};
class Area
{
public:
DataArea* s = new DataArea(3, 4, 5, 6, 7);
float AreaCirle()
{
return 3.142 * s->radius * s->radius;
}
float AreaRectangle()
{
return s->length * s->width;
}
float AreaTraingle()
{
return (s->base * s->heigth) / 2;
}
};
class print_data : public Area
{
public:
void print()
{
cout << "Area of Circle is: " << AreaCirle() << endl;
cout << "Area of Rectangle is: " << AreaRectangle() << endl;
cout << "Area of Traingle is: " << AreaTraingle() << endl;
}
};
int main()
{
//DataArea da(3, 4, 5, 6, 7);
print_data m;
m.print();
}
Your DataArea is basically absolute if you do not use it outside of Area class. Similarly, print_data class can be replaced by an operator<< overload.
Following is the updated code, in which the comments will guide you through.
#include <iostream>
// DataArea (optionally) can be the part of Area class
struct DataArea /* final */
{
float length, width, base, height, radius;
DataArea(float l, float w, float b, float h, float r)
: length{ l } // use member initializer lists to initlize the members
, width{ w }
, base{ b }
, height{ h }
, radius{ r }
{}
};
class Area /* final */
{
DataArea mDataArea; // DataArea as member
public:
// provide a constructor which initialize the `DataArea` member
Area(float l, float w, float b, float h, float r)
: mDataArea{ l, w, b, h, r } // member initializer
{}
// camelCase naming for the functions and variables
// mark it as const as the function does not change the member
float areaCirle() const /* noexcept */
{
return 3.142f * mDataArea.radius * mDataArea.radius;
}
float areaRectangle() const /* noexcept */
{
return mDataArea.length * mDataArea.width;
}
float areaTraingle() const /* noexcept */
{
return (mDataArea.base * mDataArea.height) / 2.f;
}
// provide a operator<< for printing the results
friend std::ostream& operator<<(std::ostream& out, const Area& areaObject) /* noexcept */;
};
std::ostream& operator<<(std::ostream& out, const Area& areaObject) /* noexcept */
{
out << "Area of Circle is: " << areaObject.areaCirle() << "\n";
out << "Area of Rectangle is: " << areaObject.areaRectangle() << "\n";
out << "Area of Traingle is: " << areaObject.areaTraingle() << "\n";
return out;
}
int main()
{
// now construct the Area object like this
Area obj{ 3, 4, 5, 6, 7 };
// simply print the result which uses the operator<< overload of the Area class
std::cout << obj;
}
Output:
Area of Circle is: 153.958
Area of Rectangle is: 12
Area of Traingle is: 15
It seems to me that Area class is surplus for what you are trying to achieve. You should probably put methods directly in DataArea class. Then you can create as many of DataArea objects as you like...
Like this:
class DataArea
{
public:
int radius, length, width, base, heigth;
DataArea(int l , int w , int b , int h , int r )
{
length = l;
width = w;
radius = r;
heigth = h;
base = b;
}
float AreaCirle()
{
return 3.142 * radius * radius;
}
float AreaRectangle()
{
return length * width ;
}
float AreaTraingle()
{
return (base * heigth)/2;
}
};
int main(int argc, char **argv)
{
DataArea area1 (1,2,3,4,5);
DataArea area2 (8,2,3,4,5);
std::cout << area1.AreaCirle() << std::endl;
std::cout << area2.AreaCirle() << std::endl;
}
The reason why you are probably having trouble to understand the concept:
You're defining a class and instantiating an object. Sometimes these terms are used interchangeably, but in this case, this is an important distinction.
If you would like for your methods to operate on some other class, that you should make methods that accept that class as an argument. Otherwise, it is unnecessary complex.

Why am I getting this error on encapsulation in C++?

This is my Line2D.h
class Line2D
{
private:
Point2D pt1;
Point2D pt2;
protected:
double length;
// Set function
void setLength();
public:
// Constructor
Line2D();
Line2D(Point2D pt1, Point2D pt2);
// Get functions
Point2D getPt1();
Point2D getPt2();
double getScalarValue();
// Set functions
void setPt1(Point2D pt1);
void setPt2(Point2D pt2);
// Other functions
void printLine2D();
bool operator==(const Line2D& otherL2D) const;
};
This is my Point2D.h,
class Point2D
{
// friend bool operator==(const Point2D& otherP2D) const;
protected:
// private:
// public:
int x;
int y;
double distFrOrigin;
// This function computes the distance of
// the point to the origin (0,0) and initializes
// disFrOrigin with the distance value.
void setDistFrOrigin();
public:
// Constructor
Point2D();
Point2D(int x, int y);
// Get functions
int getX();
int getY();
// Set functions
void setX(int);
void setY(int);
// Accessor method(returns the value of distFrOrigin
double getScalarValue();
void printPoint2D();
bool operator==(const Point2D& otherP2D) const;
};
This is my Line2D.cpp,
Line2D::Line2D()
{
pt1 = Point2D();
pt2 = Point2D();
length = 0.0;
}
Line2D::Line2D(Point2D pt1, Point2D pt2):Point2D(x,y)
{
/*
// Trial 1
pt1.x = Point2D.getX();
pt1.y = Point2D.getY();
pt2.x = Point2D.getX();
pt2.y = Point2D.getY();
*/
// pt1.Point2D(x,y);
// pt2.Point2D(x,y);
// Setting for both points
// this -> pt1 = pt1;
// this -> pt2 = pt2;
// setLength();
}
Line2D::setLength()
{
// int xL = pow((pt1.x - pt2.x),2);
// int yL = pow((pt1.y - pt2.y),2);
int xL = pow((pt1.getX() - pt2.getX()),2);
int yL = pow((pt1.getY() - pt2.getY()),2);
length = sqrt(xL + yL);
this -> length = length;
}
Line2D::getScalarValue()
{
return length;
}
Line2D::getPt1()
{
return pt1;
}
Line2D::getPt2()
{
return pt2;
}
Line2D::setPt1(Point2D pt1)
{
// Setting for first point only
this -> pt1 = pt1;
}
Line2D::setPt1(Point2D pt2)
{
// Setting for second point only
this -> pt2 = pt2;
}
void Line2D::printLine2D()
{
cout << " " << pt1 << " " << pt2 << " " << length << endl;
}
bool Line2D::operator==(const Line2D& otherL2D) const
{
return(pt1 == otherL2D.pt1 &&
pt1 == otherL2D.pt2);
}
this is my code for Point2D.cpp,
`Point2D::Point2D()
{
x = 0;
y = 0;
}
`
Point2D::Point2D(int x, int y)
{
this -> x = x;
this -> y = y;
}
void Point2D::setX(int x)
{
this -> x = x;
}
void Point2D::setY(int y)
{
this -> y = y;
}
int Point2D::getX()
{
return x;
}
int Point2D::getY()
{
return y;
}
void Point2D::setDistFrOrigin()
{
distFrOrigin = sqrt( pow(x,2) + pow(y,2) );
}
void Point2D::printPoint2D()
{
cout << " " << x << " " << y << " " << distFrOrigin << endl;
}
bool Point2D::operator==(const Point2D& otherP2D) const
{
return(x == otherP2D.x &&
y == otherP2D.y);
}
Here's my problem:
My Point2D works fine. Line2D encapsulates Point2D. How do I write the constructor and the setLength() function?
The error I am getting is within these 2:
In Line2D.cpp: Point2D is not a direct base of Line2D
In Line2D.cpp: ISO C++ forbids declaration of 'setLength' with no type
In Line2D.cpp: setLength() cannot be overloaded (qn, what do i need to overload and how?)
In Line2D.cpp: call of overloaded 'pow(int,int)' is ambiguous (Don't understand this)
All the include header etc. is correct so I didn't put them here.
Those are the errors I am getting. I will be grateful for any help.
Thank you!
1. What do you mean by direct base? If you want them to share the same functions etc. you need to use abstract class, other than that, I am not sure if I understood this part of your question.
2. You need to specify "void" keyword in front of your function definition. Other than that, your setLength() function declared as "protected" in your header file. I don't know if you have a specific reason for a "set" function to be protected, but usually "set" functions are declared as publicly.
3. I couldn't see any other declaration of your "setLength()" function in your header file that is gooing to be overloaded later. You need to declare all of your functions even the ones that oyu need to overload too. It is important for compile to understand that which functions that it has.
4. You need to be more specific. I presume that this is the line that you are talking about:
int xL = pow((pt1.getX() - pt2.getX()),2);
xL = (pt1's X - pt2's X) * (pt1's X - pt2's X)
Hope this helps you.

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;
};

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

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.

Implementing operator<< for a derived class

So I have a base class (Shape) and three derived classes, Circle, Rectangle and Square (Square is derived from Rectangle) I'm attempting to implement operator<< which just calls the correct display function for what called it. However, I don't think I have the syntax correct. Here's a snippet--where have I gone wrong?
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual double area() = 0;
virtual void display() = 0;
protected:
double width;
double height;
double radius;
};
ostream & operator<<(ostream & out, const Shape & s)
{
s.display(out);
return out;
}
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area() { return width * height; }
virtual void display()
{
cout << "Width of rectangle: " << width << endl;
cout << "Height of rectangle: " << height << endl;
cout << "Area of rectangle: " << this->area() << endl;
}
};
You're calling display like this:
s.display( out );
But display is defined as:
vritual void display() = 0;
The function was declared and defined taking no parameters. It should take a reference to std::ostream as a parameter:
virtual void display(std::ostream &) = 0;
It should also be a const method as you're passing in a const object through the operator << overload:
virtual void display(std::ostream &) const = 0;
Don't forget that in the definition of display you should be writing to the ostream object, not specifically std::cout.
Here is a compiling program on Ideone.
You have a number of problems here. Firstly, let's deal with the printing problem:
ostream & operator<<(ostream & out, const Shape & s)
Here, you're passing a const Shape. This means you can only call const methods on the s you pass in. However, you haven't marked any of the methods in the base (or derived) class as const. Neither area nor display should change the state of the object. Secondly, you're trying to call s.display(out), that is, passing an ostream& to display. The function signature you have doesn't reflect this. So putting that all together we get:
virtual double area() const = 0;
virtual void display(ostream& out) const = 0;
You've also got some other problems - a base class that doesn't declare a virtual destructor. If you're planning on using a class polymorphically, it must have a virtual destructor:
virtual ~Shape() { }
You also need to amend your methods in the derived class:
double area() const { return width * height; }
void display(ostream& out) const
{
out << "Width of rectangle: " << width << endl;
out << "Height of rectangle: " << height << endl;
out << "Area of rectangle: " << area() << endl;
}
Note that display in Rectangle was always printing to cout beforehand.
You almost got it right, here is the working solution:
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class Shape
{
public:
Shape(double w = 0, double h = 0, double r = 0)
{
width = w;
height = h;
radius = r;
}
virtual ~Shape() {} // Recommended!
virtual double area() const = 0;
virtual void display(ostream & out) const = 0;
protected:
double width;
double height;
double radius;
};
ostream & operator<<(ostream & out, const Shape & s)
{
// Since `s` is `const`, then `display` method should be `const` too.
s.display(out);
return out;
}
class Rectangle : public Shape
{
public:
Rectangle(double w, double h) : Shape(w, h)
{
}
virtual double area() const { return width * height; }
virtual void display(ostream & out) const
{
// Since `display` method is `const`, then `area` method should be
// `const` too.
out << "Width of rectangle: " << width << endl;
out << "Height of rectangle: " << height << endl;
out << "Area of rectangle: " << this->area() << endl;
}
};
void main() {
Rectangle r(1, 2);
cout << r << endl;
}
Please, pay attention to the const qualifiers which enforce const-correctness of class methods. I've added some useful comments so that you can follow the logic smoothly. Take it as a rule of thumb, if the method does not modify class members, then you should declare it const.