I'm writing a program that calculates the area and diameter using classes and functions. My issue is that I'm getting an undefined symbol error with my functions. I'm sure it's probably an easy fix ... I just can't figure it out.
(Writing the code on a mac)
Here's the code:
#include<iostream>
using namespace std;
class Circle
{
public:
int radius;
void printArea();
void printDiameter();
};
void Circle::printArea()
{
double area;
area = radius * radius * 3.14159;
cout<<"A circle with radius "<<radius<<" has an area of "<<area<<endl;
}
void Circle::printDiameter()
{
int diam;
diam = radius * 2;
cout<<"A circle with radius "<<radius<<" has a diameter of "<<diam<<endl;
}
int main()
{
void printArea(Circle);
void printDiameter(Circle);
Circle aBigCircle, aLittleCircle;
aBigCircle.radius = 50;
aLittleCircle.radius = 4;
printArea(aBigCircle);
printDiameter(aBigCircle);
printArea(aLittleCircle);
printDiameter(aLittleCircle);
}
The printArea() and printDiameter() methods are encapsulated within the Circle class, so you must call them from an object.
#include<iostream>
using namespace std;
class Circle
{
public:
int radius;
void printArea();
void printDiameter();
};
void Circle::printArea()
{
double area;
area = radius * radius * 3.14159;
cout<<"A circle with radius "<< radius <<" has an area of "<< area << endl;
}
void Circle::printDiameter()
{
int diam;
diam = radius * 2;
cout<<"A circle with radius "<< radius <<" has a diameter of " << diam << endl;
}
int main()
{
Circle aBigCircle, aLittleCircle;
aBigCircle.radius = 50;
aLittleCircle.radius = 4;
aBigCircle.printArea();
aBigCircle.printDiameter();
aLittleCircle.printArea();
aLittleCircle.printDiameter();
return 0;
}
Result:
A circle with radius 50 has an area of 7853.97
A circle with radius 50 has a diameter of 100
A circle with radius 4 has an area of 50.2654
A circle with radius 4 has a diameter of 8
int main()
{
// delete printArea() and printDiameter() lines
// void printArea(Circle);
// void printDiameter(Circle);
Circle aBigCircle, aLittleCircle;
aBigCircle.radius = 50;
aLittleCircle.radius = 4;
aBigCircle.printArea();
aBigCircle.printDiameter();
aLittleCircle.printArea();
aLittleCircle.printDiameter();
}
Delete printArea() and printDiameter() declare in main(). It should fix your problem.
Related
I have an assignment, here is the description:
Create a class named Point. It must have three private float field named as x, y, z to keep coordinates in space and public get and set functions to access these data members ( getX(), getY(), getZ(), setX(), setY(), setZ() ). Create another function that is defined outside of the scope of Point class and named Space. In Space function, you will find the middle point between two points and create a new Point object that will keep calculated coordinates. The written code below must be able to run without errors.
The teacher gave us structure of the code. The code should be like below. only the definition of get, set and space functions can change. I wrote the set and get functions, but I have no idea what to do with the space function. That's why I need help with the space function part.
#include <iostream>
using namespace std;
class Point {
private:
int x, y, z;
public:
float getX();
float getY();
float getZ();
void setX(float x1);
void setY(float y1);
void setZ(float z1);
};
float Point::getX() {
return x;
}
float Point::getY() {
return y;
}
float Point::getZ() {
return z;
}
void Point::setX(float x1) {
x = x1;
}
void Point::setY(float y1) {
y = y1;
}
void Point::setZ(float z1) {
z = z1;
}
Point Space(Point a, Point b)
{
// complete space function
}
int main()
{
float x_, y_, z_;
Point p[3];
for (int i = 0; i < 2; ++i)
{
cout << "Please enter x coordinate for p" << i + 1 << endl;
cin >> x_;
p[i].setX(x_);
cout << "Please enter y coordinate for p" << i + 1 << endl;
cin >> y_;
p[i].setY(y_);
cout << "Please enter z coordinate for p" << i + 1 << endl;
cin >> z_;
p[i].setZ(z_);
}
p[2] = Space(p[0], p[1]);
cout << "Coordinations of middle point (p3) between p1 and p2 is x=" << p[2].getX() << ",y=" << p[2].getY() << ", z=" << p[2].getZ();
return 0;
}
As the instructions says:
In Space function, you will find the middle point between two points and create a new Point object that will keep calculated coordinates.
Per Mid Point Formula in 3D:
A midpoint is the exact center point between two defined points. To find this center point, midpoint formula is applied. In 3-dimensional space, the midpoint between (x1, y1, z1) and (x2, y2, z1) is (x1+x2)/2, (y1+y2)/2, (z1+z2)/2.
So, try this:
Point Space(Point a, Point b)
{
Point mid;
mid.setX((a.getX() + b.getX()) / 2);
mid.setY((a.getY() + b.getY()) / 2);
mid.setZ((a.getZ() + b.getZ()) / 2);
return mid;
}
I've written this C++ code which is supposed to compute circles' areas using the length of the side of the square inscribed in the circle. I get no errors but the result isn't right. Any advice?
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, const char * argv[]) {
float l, ac, r, area;
cout << "Square's side length: ";
cin >> l;
ac = l * sqrt(2);
r = ac/2;
area = M_PI * pow(2,r);
cout << "Circle's area: "<<area<< endl;
return 0;
}
In computing the area you have to write area = M_PI * pow(r,2);: you just inverted the arguments of the pow function.
The diameter of the circle would be equal to the hypotenuse of the diagonal of the square, so assuming l is side length of the square
float diam = std::sqrt(2.0f * std::pow(l, 2));
float radius = diam / 2.0f;
float area = M_PI * std::pow(radius, 2.0f);
This question already has an answer here:
Overloaded Addition assignment operator in C++ for two /more than two objects?
(1 answer)
Closed 5 years ago.
Trying to develop operator == to compare two balls where two balls are considered equal if they have the same radius and operator > to compare two balls. To see if one ball has a bigger radius than another one, for let's say ball x is > than another ball y. += to add the volume of the right-side-operand to the volume of the left-side-operand. It is like to melt two metal balls to make one metal ball. The new ball's radius is cube root of (r1^3 + r2^3). Wish to use pow() function to calculate the cube value and cube root value. operator + to add the two balls together and return a new ball. The size of the new ball is the sum of the size of the two operands connected by the +.
In the main() function, couldn't add ball m(10) with ball n(20) to create another ball d, like d = m+n.
int main()
{
//use ball
ball x; float re;
//radius of ball y is set to 10
ball y(10);
//asks for radius of x?
cout << "Enter radius for ball x: ";
cin >> re;
//sets the radius of x
x.set_radius(re);
ball m(10);
ball n(20);
ball d;
d = m + n;
//cout << "The radius of ball d is " << m.;
system("pause");
return 0;
}
//ball.h
{
class ball
{
public:
//sets the intial raduis to 0
ball() {
radius = 0;
}
ball(float radii) {
radius = radii;
}
float get_radius() {
return radius;
}
void set_radius(float redly) {
radius = redly;
}
bool operator == (ball x) {
if (radius == x.radius)
return true;
else
return false;
}
bool operator > (ball x) {
if (radius > x.radius)
return true;
else
return false;
}
bool operator += (ball x) {
radius += x.radius;
}
ball operator + (ball a, ball b) {
ball d;
d += a;
d += b;
return d;
}
private:
float radius;
};
}
#endif
If you are only looking for (x_volume/ y_volume)% and (x_surfacearea/y_surfacearea)%
I suggest doing :
float vol_over_y() {
float v;
v = ((radius * radius * radius)/(10*10*10));
return v;
}
float sa_over_y() {
float a;
a = (radius * radius /(10*10));
return a;
}
because other constants like (4.0/3.0)* 3.14 in volume and 3.14 in surface area cancel out.
If in case y changes,
float vol_over_y(float y_rad) {
float v;
v = ((radius * radius * radius)/(y_rad*y_rad*y_rad));
return v;
}
float sa_over_y(float y_rad) {
float a;
a = (radius * radius /(y_rad*y_rad));
return a;
}
You thought too much. It is simply a little tweak in your main logic:
ball x;
ball y(10);
// your code to construct x
cout << "The volume of x is " << ( x.volume() / y.volume() )<< "% of the volume of y."<< endl;
// similar for surface area, try it out yourself
If in case you really need to do it in a method(which is quite ridiculous imho), you should just pass the other ball in and calculate:
float volume_ratio_to(const ball& base) {
return volume() / base.volume();
}
And your main logic become
cout << "The volume of x is " << x.volume_ratio_to(y) << "% of the volume of y."<< endl;
Edited for your updated question:
The reason why + operator does not work for you is because you didn't overloaded the operator right.
If you want it to be member of ball, the + operator should only takes one argument.
i.e. looks like:
class ball {
//....
ball operator + (ball another);
}
Or, don't make it member of ball:
// outside ball class
ball operator + (ball a, ball b) {....}
Refer to Overloaded Addition assignment operator in C++ for two /more than two objects? for more detailed description on how to overload addition operator.
There are quite a lot of other problems in your code too
You should have passed balls to methods by reference instead of by value
You should have considered adding const in various places
You += logic is totally wrong. Pay attention to what you quoted:
+= to add the volume of the right-side-operand to the volume of the
left-side-operand. It is like to melt two metal balls to make one
metal ball. The new ball's radius is cube root of (r1^3 + r2^3)
//This helps
int main()
{
//use ball
ball x; float re;
ball y(10);
cout << "Enter radius for ball x: " << endl;
cin >> re;
x.setl(re);
cout << "The volume of x is " << (x.volume()/y.volume())*100 << "%
of the volume of y."<< endl;
cout << "The surfacearea of x box is " <<
(x.surface_area()/y.surface_area())*100 << "% of
the surfacearea of y." << endl;
system("pause");
return 0;
}
//ball.h
#pragma once
#ifndef Ball
#define Ball
namespace bsize
{
class ball
{
public:
ball(){
radius = 0;
}
ball(float radii) {
radius = radii;
}
float volume() {
float v;
v = ((4.0/3.0)* 3.14 * (radius * radius * radius));
return v;
}
float surface_area() {
float a;
a = (4 * 3.14 * radius * radius);
return a;
}
float get_radius(){
return radius;
}
void set_radius(float redly) {
radius = redly;
}
private:
float radius;
};
}
#endif
I'm having trouble with with this project of mine. i want it to draw out an octagon, the code i used worked perfectly fine for other shapes like rhombus and triangle.
the octagon.cpp file http://pastebin.com/iVfdkKEB
the header file http://pastebin.com/a50UQi5F
the main part which runs it all http://pastebin.com/quepi6az
#include "shape.h"
class Octagon : public Shape
{
int radius;
void plotVertices();
public:
Octagon(Vertex point, int radius = 10);
int area();
int perimeter();
};
#include "octagon.h"
Octagon::Octagon(Vertex point, int radius) : Shape(point)
{
// constructs a Octagon of radius around a point in 2D space
if ((radius>centroid.getX() / 2) || (radius>centroid.getX() / 2))
{
cout << "Object must fit on screen." << endl;
system("pause");
exit(0);
this->radius = radius;
plotVertices();
}
// place your code here and add comments that describe your understanding of what is happening
}
void Octagon::plotVertices()
{
int x, y, _x, _y; // declare and intiliase variables for x and y co-ordinates
double radians;
x = centroid.getX(); // places first point A at the centroid
y = centroid.getY() + radius;
vertices.push_back(Vertex(x, y));
x = vertices.back().getX() - centroid.getX();
y = vertices.back().getY() - centroid.getY();
for (int i = 45; i < 360; i += 45) // for loop to draw the shape itself by creating the points.
// i = size of interior angle.
{
radians = i * PI / 180;
_x = round(x * cos(radians) - y * sin(radians));
_y = round(y* cos(radians) + x * sin(radians));
_x = _x + centroid.getX();
_y = _y + centroid.getY();
vertices.push_back(Vertex(_x, _y));
}
}
#pragma once
#include "vertex.h"
#include "shape.h"
#include "octagon.h"
#include "console.h"
#include <iostream>
#include <list>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
list<Shape*> shapes;
int x = 20, y = 60;
shapes.push_back(new Octagon(Vertex(20, 60), 8));
list<Shape*>::iterator itr = shapes.begin();
while(itr!=shapes.end())
{
(*itr)->drawShape();
system("pause");
(*itr)->outputStatistics();
// output shape statistics
(*itr)->scale(2.0);
(*itr)->drawShape();
// scale shape (double it)
// draw shape
(*itr)->rotate(20);
(*itr)->drawShape();
// rotate shape by 20 degrees
// draw shape
itr++;
}
Console::gotoXY(0,0);
system("pause");
return 0;
}
and the draw shape function
void Shape::drawShape()
{
// plots each vertex and draws a line between them using Bresenham's algorithm
// you can adjust your console font and dimensions if you want to increase the resolution of your shapes
list<Vertex>::iterator current = vertices.begin();
list<Vertex>::iterator previous = vertices.begin();
while (current != vertices.end())
{
Console::gotoXY((*current).getX(), (*current).getY());
cout << "*";
if (current != vertices.begin())
drawLine((*current).getX(), (*current).getY(), (*previous).getX(), (*previous).getY());
previous = current;
current++;
}
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
}
I'm far from sure, but I think that the problem can be in the last line of void Shape::drawShape()
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
where isn't a check to see if vertices is empty. In that case, vertices.back() and vertices.front() are undefined (both equal to vertices.end()?) and dereferencing they, to call getX() and getY(), can cause your problem.
It's possible an empty vertices? I don't know because I don't see all code but I see that there is an exit(0) in the if body of the Octagon constructor.
It's plotVertices() that populate vertices? In this case, the exit(0) say us that vertices is empty and that the last drawline() in Shape::drawshape can cause the trouble.
The solution (a solution) is obvious and simple: check if vertices is empty
if ( false == vertices.empty() )
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
How do I find the area of square or rectangle assuming user has entered some accurate points to form a square or rectangle.
I need to calculate the area inside the square class and rectangle class respectively.
I have 2 vector of coordinates, coordX and coordY.
My idea is when either or x or y has same value it will be a line and I can find the distance x2 but I'm not sure how to implement it in code.
double Square::computeArea() {
double area;
for (int x = 0; x < coordX.size; x++) {
for (int y = 0; y < coordY.size; y++) {
if (coordX[x] == coordY[y])
{
//....
}
}
}
return area;
}
This is how i populate my vector with user input
Square Square;
for ( int i = 1; i <= 4; i++) {
cout << "Please enter x-coordinate of pt " << i << ": ";
cin >> x;
Square.setXCoordinate(x);
cout << "Please enter y-coordinate of pt " << i << ": ";
cin >> y;
Square.setYCoordinate(y);
}
this is my mutator function in my class. Square inherit from ShapeTwoD
void ShapeTwoD::setXCoordinate(int x) {
coordX.push_back(x);
}
void ShapeTwoD::setYCoordinate(int y) {
coordY.push_back(y);
}
No need for square root.
Take two edges from one vertex, rotate one by 90°, take dot product.
double dx1 = coordX[3] - coordX[0];
double dy1 = coordY[3] - coordY[0];
double dx2 = coordX[1] - coordX[0];
double dy2 = coordY[1] - coordY[0];
double area = abs(dx1*dy2 - dy1*dx2)
As a bonus, this will calculate the correct area for all parallelograms, not just rectangles.
This assumes, the points are entered in clockwise or couter-clockwise order. If that's not the case, find out which point has the greatest distance to point[0] then discard it and use the other two instead of 1 and 3 above.
Assuming your coordinates are something like
// 3-----------2
// | |
// | |
// 0-----------1
Then you could do
#include <cmath>
double distance(double x1, double x2, double y1, double y2)
{
return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2));
}
double Square::computeArea() const
{
double length = distance(coordX[0], coordX[1], coordY[0], coordY[1]);
double width = distance(coordX[0], coordX[3], coordY[0], coordY[3]);
return length * width;
}
This allows your rectangle to be at any arbitrary orientation, instead of x-y axis aligned. You just need to maintain a convention of the indexes of the corners, like in my example diagram.