关于C++继承与派生的问题求解,哪里出错了? 15
//1.创建以Point类对象作为数据成员的Circle类//编写Point类://成员函数:#include<iostream.h>classpoint{private...
//1.创建以Point类对象作为数据成员的Circle类
//编写Point 类:
//成员函数:
#include<iostream.h>
class point
{
private:
double x,y;//(1)数据成员:x、y,分别对应横、纵坐标;
public:
point(double i,double j)//(2)有参构造函数:利用参数对数据成员赋值;
{
x=i;
y=j;
}
point(point &p)//(3)提供拷贝构造函数
{
x=p.x;
y=p.y;
}
double getx()//(4) 函数Getx:获取点类的横坐标值;
{
return x;
}
double getu()//(5)函数Gety:获取点类的纵坐标值;
{
return y;
}
};
//编写圆类circle:
//(6)数据成员,包括圆心O(是Point类的对象)和半径r
// 成员函数:
//(7)构造函数:给点类的数据成员以及圆类的半径赋值
//(8)void Display();显示该圆的圆点信息和半径
class circle:public point
{
private:
double r;
public:
circle(double i,double j,double k):point(i,j)
{
r=k;
}
display()
{
cout<<"该圆的圆心为("<<x<<","<<y<<")"<<endl;
cout<<"半径为"<<r<<endl;
}
};
void main()
{
double x,y,r;
cout<<"请输入圆心坐标:\n";
cin>>x>>y;
cout<<"请输入半径:\n";
cin>>r;
circle c(x,y,r);
c.display();
}
--------------------Configuration: 13 - Win32 Debug--------------------
Compiling...
1.cpp
F:\13\1.cpp(54) : warning C4183: 'display': member function definition looks like a ctor, but name does not match enclosing class
F:\13\1.cpp(52) : error C2248: 'x' : cannot access private member declared in class 'point'
F:\13\1.cpp(11) : see declaration of 'x'
F:\13\1.cpp(52) : error C2248: 'y' : cannot access private member declared in class 'point'
F:\13\1.cpp(11) : see declaration of 'y'
执行 cl.exe 时出错.
1.obj - 1 error(s), 0 warning(s) 展开
//编写Point 类:
//成员函数:
#include<iostream.h>
class point
{
private:
double x,y;//(1)数据成员:x、y,分别对应横、纵坐标;
public:
point(double i,double j)//(2)有参构造函数:利用参数对数据成员赋值;
{
x=i;
y=j;
}
point(point &p)//(3)提供拷贝构造函数
{
x=p.x;
y=p.y;
}
double getx()//(4) 函数Getx:获取点类的横坐标值;
{
return x;
}
double getu()//(5)函数Gety:获取点类的纵坐标值;
{
return y;
}
};
//编写圆类circle:
//(6)数据成员,包括圆心O(是Point类的对象)和半径r
// 成员函数:
//(7)构造函数:给点类的数据成员以及圆类的半径赋值
//(8)void Display();显示该圆的圆点信息和半径
class circle:public point
{
private:
double r;
public:
circle(double i,double j,double k):point(i,j)
{
r=k;
}
display()
{
cout<<"该圆的圆心为("<<x<<","<<y<<")"<<endl;
cout<<"半径为"<<r<<endl;
}
};
void main()
{
double x,y,r;
cout<<"请输入圆心坐标:\n";
cin>>x>>y;
cout<<"请输入半径:\n";
cin>>r;
circle c(x,y,r);
c.display();
}
--------------------Configuration: 13 - Win32 Debug--------------------
Compiling...
1.cpp
F:\13\1.cpp(54) : warning C4183: 'display': member function definition looks like a ctor, but name does not match enclosing class
F:\13\1.cpp(52) : error C2248: 'x' : cannot access private member declared in class 'point'
F:\13\1.cpp(11) : see declaration of 'x'
F:\13\1.cpp(52) : error C2248: 'y' : cannot access private member declared in class 'point'
F:\13\1.cpp(11) : see declaration of 'y'
执行 cl.exe 时出错.
1.obj - 1 error(s), 0 warning(s) 展开
2个回答
展开全部
#include <iostream>
using namespace std;
class point
{
private:
double x, y; //(1)数据成员:x、y,分别对应横、纵坐标;
public:
point(double i, double j) //(2)有参构造函数:利用参数对数据成员赋值;
{
x = i;
y = j;
}
point(point &p)//(3)提供拷贝构造函数
{
x = p.x;
y = p.y;
}
double getx()//(4) 函数Getx:获取点类的横坐标值;
{
return x;
}
double gety()//(5)函数Gety:获取点类的纵坐标值;
{
return y;
}
};
//编写圆类circle:
//(6)数据成员,包括圆心O(是Point类的对象)和半径r
// 成员函数:
//(7)构造函数:给点类的数据成员以及圆类的半径赋值
//(8)void Display();显示该圆的圆点信息和半径
class circle: public point
{
private:
double r;
public:
circle(double i, double j, double k): point(i, j)
{
r = k;
}
void display()
{
cout << "该圆的圆心为(" << getx() << "," << gety() << ")" << endl;
cout << "半径为" << r << endl;
}
};
int main()
{
double x, y, r;
cout << "请输入圆心坐标:\n";
cin >> x >> y;
cout << "请输入半径:\n";
cin >> r;
circle c(x, y, r);
c.display();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询