c++关于构造函数带默认参数的问题。求帮忙
//[11] 建立一个CPoint类,该类有两个私有成员变量x,y,表示点的坐标。
//有一个构造函数用于设置坐标,还有两个公有的成员函数:分别用于获取x和y 的坐标。
//由CPoint公共派生出CCircle类,派生类CCircle增加两个私有成员变量分别用于表示半径和圆心坐标,
//并用派生类的构造函数设置半径,同时增加三个成员函数:一个用于获取半径,另两个分别用于获取圆内接正方形左上角的x坐标和y坐标。
//构造函数初始值都为0。
#include<iostream.h>
class CPoint
{
private:
int px;
int py;
public:
CPoint(){};
CPoint(int x,int y);
int px1();
int py1();
};
int CPoint::px1()
{
return px;
}
int CPoint::py1()
{
return py;
}
CPoint::CPoint(int x,int y)
{
px=x;
py=y;
}
class CCircle:public CPoint
{
private:
double r;
CPoint p;
public:
double r1();
double r2();
double r3();
CCircle(){};
};
double CCircle::r3()
{
return (p.py1()+r);
}
double CCircle::r2()
{
return (p.px1()-r);
}
double CCircle::r1()
{
return r;
}
CCircle::CCircle(CPoint p1,double r12=0)
{
p=p1;
r=r12;
}
void main()
{
int a,b,c;
CPoint *pt1;
pt1=new CPoint[1];
cout<<"输入点的坐标,半径"<<endl;
cin>>a>>b>>c;
pt1[0]=CPoint(a,b);
CCircle *t;
t=new CCircle[1];
t[0]=CCircle(pt1[0],c);
cout <<"圆内接正方形左上角的坐标为<"<<t[0].r2()<<","<<t[0].r3()<<">"<<endl;
}
-------------------------------------------
题目要求了基类和派生类都需要构造函数为0;可是基类构造函数为0后没办法解决CCircle(CPoint p1,double r12);这里对p1的声明额,报ambiguous call to overloaded function;
有修改办法啊?(去掉构造函数的默认参数这个就算了吧) C++ 类
[解决办法]
不是很明白你的意思。。改了一下。。你看看符不符合。。
//构造函数初始值都为0。
#include<iostream.h>
class CPoint
{
private:
int px;
int py;
public:
CPoint(int x = 0,int y = 0);//这里改成用默认参数。。
int px1();
int py1();
};
int CPoint::px1()
{
return px;
}
int CPoint::py1()
{
return py;
}
CPoint::CPoint(int x,int y)
{
px=x;
py=y;
}
class CCircle:public CPoint
{
private:
double r;
CPoint p;
public:
double r1();
double r2();
double r3();
CCircle(CPoint p1 = CPoint(),double r12=0);//这里这样改。。
};
double CCircle::r3()
{
return (p.py1()+r);
}
double CCircle::r2()
{
return (p.px1()-r);
}
double CCircle::r1()
{
return r;
}
CCircle::CCircle(CPoint p1, double r12)
{
p=p1;
r=r12;
}
void main()
{
int a,b,c;
CPoint *pt1;
pt1=new CPoint[1];
cout<<"输入点的坐标,半径"<<endl;
cin>>a>>b>>c;
pt1[0]=CPoint(a,b);
CCircle *t;
t=new CCircle[1];
t[0]=CCircle(pt1[0],c);
cout <<"圆内接正方形左上角的坐标为<"<<t[0].r2()<<","<<t[0].r3()<<">"<<endl;
}