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.
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 */
}
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())) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.