Error!xcor undeclared at setAll() function
I had created object of PointType in Circle class to do sort of composition, but at the initialization of the constructor of Circle it is showing that
" [Error] 'ycor' was not declared in this scope "
" [Error] 'xcor' was not declared in this scope "
I want xcor and ycor in my Circle class in order to get the Radius using setAll() function
Please help!at what i am messing up.
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
class PointType{
private:
int xcor;//x coordinate
int ycor;//y coordinate
public:
PointType();//constructor
PointType(int x,int y);
void setPoint(int x,int y);
int getx() const;
int gety() const;
};
PointType::PointType():xcor(0),ycor(0)
{
}
PointType::PointType(int x,int y):xcor(x),ycor(y){
}
void PointType::setPoint(int x,int y){
xcor=x;
ycor=y;
}
int PointType::getx() const{
return xcor;
}
int PointType::gety() const{
return ycor;
}
class Circle{
protected:
float Radius;
float Area;
int Circumference;
float pi;
PointType obj1;
public:
Circle();
void setAll();
float getRadius();
float getArea();
float getCircumference();
void callFunction();
void printAll();
void pt(int x,int y);
};
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(xcor,ycor){
}
void Circle::setAll(){
Radius=sqrt( (xcor*xcor) + (ycor*ycor) );
Area=pi*Radius*Radius;
Circumference=2*pi*Radius;
}
float Circle::getRadius(){
return Radius;
}
float Circle::getArea(){
return Area;
}
float Circle::getCircumference(){
return Circumference;
}
void Circle::printAll(){
cout<<"The Area is :"<<Area<<endl;
cout<<"The Circumference is :"<<Circumference<<endl;
}
void Circle::pt(int x,int y){
obj1.setPoint(x,y);
}
Your class Circle doesn't have any member variable called xcor or ycor. If you want to get the values of your PointType object, your setAll function should look like:
Radius=sqrt( (obj1.getx()*obj1.getx()) + (obj1.gety()*obj1.gety()) );
Also you have to change your constructor:
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(0,0)
as it doesn't have access to any xcor or ycor either.
You have to change your Circle constructor, because it knows nothing about xcor and ycor:
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(42,56)
The Circle class still doesn't know about xcor and ycor, so you have to change setAll method:
Radius=sqrt( static_cast<double>((obj1.getx()*obj1.getx()) + (obj1.gety()*obj1.gety())) );
declare xcor,ycor to Circle' constructor argument, and
add this line before computing Radius:
int xcor = obj1.getx(), ycor = obj1.gety();
Radius=sqrt( (xcor*xcor) + (ycor*ycor) );
You could also consider to inherit PointType for Circle, instead of embedded obj1:
class Circle : public PointType
{
public: Circle(int x, int y):PointType(x, y) {}
...
/* remove obj1 */
}
Related
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.
I am new to coding and I am trying to write a program that has polymorphism but my rectangle part is not working. I have tried to add variables, in all three files to accommodate it but I keep getting errors. The code below is my latest attempt to fix this. The error that I am getting now is [error] request for member 'area' in 'r' which is non-class type 'float' and for parameter in 'r' which is non-class type 'float'. I am at a lost as to how to fix this at this point. Please help if you can!
Main.cpp
#include <iostream>
#include "shape.h"
#include "shape.cpp"
using namespace std;
int main() {
float r, a, b, a1, b1;
cout<<"This program will ask you to input some data in order to find the area and the parameter of 3 shapes."<<endl;
cout<<"\nInput the circles radius --everything should be in inches (i.e 5):";
cin>>r;
Circle c(r);
cout<<"\nPlease input two side of the Right Triangle excluding the hypotenuse-- everything should be in inches( i.e 5 5): ";
cin>>a>>b;
RTriangle rt(a,b);
cout<<"\nPlease input two side of the Rectangle -- everything should be in inches( i.e 5 5): ";
cin>>a>>b;
Rectangle r(a1,b1);
cout<<"\n\nThe Circles Area is:"<<c.area()<<" inches, The Parameter is:"<<c.parameter()<<" inches"<<endl;
cout<<"The Rectangle Area is:"<<r.area()<<" inches, The Parameter is:"<<r.parameter()<<endl;
cout<<"The Right Triangle Area is:"<<rt.area()<<" inches, The Parameter is:"<<rt.parameter()<<" inches"<<endl;
cout<<"Thanks once agin for using this program for your AREA and PARAMETER needs!"<<endl;
system ("PAUSE");
return 0;
}
Shape.cpp
#include"shape.h"
Shape::Shape(){
sideA = sideB = 0;
}
Shape::Shape(int a, int b){
sideA = a;
sideB = b;
}
//these will get overrided
float Shape::area(){return 0;}
float Shape::parameter(){return 0;}
//rectangle definations
Rectangle::Rectangle(float a, float b):Shape(a,b){
//calling parent class constructor
}
float Rectangle::area(){
return sideA*sideB;
}
float Rectangle::parameter(){
return 2*(sideA+sideB);
}
//right triangle definations
RTriangle::RTriangle(float h, float w):Shape(h, w){
}
float RTriangle::area(){
return 0.5*sideA*sideB;
}
float RTriangle::parameter(){
float hyp = sqrt(sideA*sideA + sideB*sideB);
return sideA + sideB + hyp;
}
//circle definations
Circle::Circle(float r){
sideA = r;
}
float Circle::area(){
return 3.14 * sideA * sideA;
}
float Circle::parameter(){
return 2 * 3.14 * sideA;
}
Shape.h
#ifndef SHAPES
#define SHAPES
#include<cmath>
class Shape {
protected:
float sideA, sideB;
float radius;
public:
Shape();
Shape(int,int);
virtual float area();
virtual float parameter();
};
class Rectangle : public Shape{
public:
Rectangle(float a, float b);
float area();
float parameter();
};
class RTriangle : public Shape{
public:
RTriangle(float h, float w);
float area();
float parameter();
};
class Circle : public Shape{
public:
Circle(float r);
float area();
float parameter();
};
#endif
Take a look at Main.cpp, you will find two lines like
float r, a, b, a1, b1;
and
Rectangle r(a1,b1);
As you can see r is defined twice. I suggest to replace Rectangle r(a1,b1); with Rectange rect(a1, b1);
This should help.
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;
};
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;
}
I have made two files that are MathUtils.h
#include "iostream"
and MathUtils.cpp
#include "MathUtils.h"
using namespace std;
//Box class .....................
class Box
{
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
void setParameters(int l,int b,int h);
int volume();
int area();
};
void Box::setParameters(int l, int b, int h)
{
length=l;
breadth=b;
height=h;
}
int Box::volume()
{
return length*breadth*height;
}
int Box::area()
{
return (2*(length*breadth) + 2*(breadth*height) + 2*(height*length));
}
//sphere class................
class Sphere
{
private:
double pi=3.14;
double r;
public:
void setParameters(int radius);
int volume();
int area();
};
void Sphere::setParameters(int radius)
{
r=radius;
}
int Sphere::volume()
{
return (4/3)*pi*r*r;
}
int Sphere::area()
{
return 4*pi*r*r;
}
How can we use this file in my project could any one help me.I have never use c++ files in my project so I want to know how can we use Box and Sphere class object in other viewController file.
Thanks!.
You define your classes in the .h file.
For your example, move:
class Box
{
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
void setParameters(int l,int b,int h);
int volume();
int area();
};
class Sphere
{
private:
double pi=3.14;
double r;
public:
void setParameters(int radius);
int volume();
int area();
};
to mathutils.h and add #include "mathutils.h" to your viewController file. Your member functions for Box should still be in mathutils.c
A function in your view controller can then make use of it:
{
Box b;
b.setParameters(1,2,3);
int v = b.volume();
int a = b.area();
}