下列函数原型声明正确的是: (A) float Volume(int x,y); (B) float Volume(int x,int&y=0); (C) const float Volume(int, float); (D) const float Volume(int x=10, int y);
时间: 2023-12-11 13:05:38 浏览: 201
选项 (B) float Volume(int x, int& y=0); 是正确的。原因如下:
(A) float Volume(int x,y); 这个声明是错误的,因为参数列表中的参数必须要有类型,而这里的 y 没有类型。
(B) float Volume(int x,int&y=0); 这个声明是正确的,它声明了一个名为 Volume 的函数,该函数有两个参数,第一个参数是一个整型 x,第二个参数是一个整型的引用 y,缺省值为 0。
(C) const float Volume(int, float); 这个声明是正确的,它声明了一个名为 Volume 的函数,该函数有两个参数,第一个参数是一个整型,第二个参数是一个浮点型,同时该函数返回值为 const float。
(D) const float Volume(int x=10, int y); 这个声明是错误的,因为有缺省参数的参数必须出现在参数列表的最右侧,但是这里第二个参数 y 没有缺省值,因此不能放在第一个参数 x 的右边。
相关问题
#include<iostream> #include<cmath> using namespace std; class Point{ protected: float x,y; public: Point(float a,float b); void setPoint(float,float); float getX(){return x;} float getY(){return y;} friend ostream& operator<<(ostream & ,const Point &); }; Point::Point(float a,float b){ x=a; y=b; } void Point::setPoint(float a,float b){ x=a; y=b; } ostream & operator<<(ostream &output ,const Point &p){ output<<"["<<p.x<<","<<p.y<<"]"<<endl; return output; } //======================================== class Circle:public Point{ protected: float radius; public: Circle(float x=0,float y=0,float r=0);//构造函数 void setRadius(float); float getRadius() const; float Area() const; friend ostream &operator<<(ostream &,const Circle &); }; Circle::Circle(float x,float y,float r):Point(x,y),radius(r){} void Circle::setRadius(float r){radius=r;} float Circle::getRadius() const {return radius;} float Circle::Area() const {return 3.14*radius*radius;} ostream &operator<<(ostream &output,const Circle &c){ output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.Area()<<endl; return output; } class Cylinder:public Circle{ protected: float height; public: Cylinder(float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; float Area() const; float volume() const; friend ostream& operator<<(ostream & ,const Cylinder &); }; Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){} void Cylinder::setHeight(float h){height=h;} float Cylinder::getHeight() const {return height;} float Cylinder::Area() const {return 2*Circle::Area()+2*3.14*radius*height;} float Cylinder::volume() const {return Circle::Area()*height;} ostream& operator<<(ostream &output ,const Cylinder &cy) { output<<"Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<"area"<<cy.Area()<<",volume"<<cy.volume()<<endl; return output; } int main(){ Cylinder cy1(3.5,6.4,5.2,10); cout<<"x="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight<<",area="<<cy1.Area()<<",volume="<<cy1.volume()<<endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"new"<<cy1;
你提供的代码存在一些问题。在调用 `cy1.getHeight` 和 `cy1.getHeight` 函数时,缺少了函数调用的括号。此外,在使用 `operator<<` 输出 `cy1` 时,缺少了分号。以下是修改后的代码:
```cpp
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x, y;
public:
Point(float a, float b);
void setPoint(float, float);
float getX() { return x; }
float getY() { return y; }
friend ostream& operator<<(ostream&, const Point&);
};
Point::Point(float a, float b){
x = a;
y = b;
}
void Point::setPoint(float a, float b){
x = a;
y = b;
}
ostream& operator<<(ostream& output, const Point& p){
output << "[" << p.x << "," << p.y << "]" << endl;
return output;
}
class Circle: public Point{
protected:
float radius;
public:
Circle(float x = 0, float y = 0, float r = 0);
void setRadius(float);
float getRadius() const;
float Area() const;
friend ostream& operator<<(ostream&, const Circle&);
};
Circle::Circle(float x, float y, float r): Point(x, y){
radius = r;
}
void Circle::setRadius(float r){
radius = r;
}
float Circle::getRadius() const {
return radius;
}
float Circle::Area() const {
return 3.14 * radius * radius;
}
ostream& operator<<(ostream& output, const Circle& c){
output << "Center=[" << c.x << "," << c.y << "], r=" << c.radius << ", area=" << c.Area() << endl;
return output;
}
class Cylinder: public Circle{
protected:
float height;
public:
Cylinder(float x = 0, float y = 0, float r = 0, float h = 0);
void setHeight(float);
float getHeight() const;
float Area() const;
float volume() const;
friend ostream& operator<<(ostream&, const Cylinder&);
};
Cylinder::Cylinder(float a, float b, float r, float h): Circle(a, b, r){
height = h;
}
void Cylinder::setHeight(float h){
height = h;
}
float Cylinder::getHeight() const {
return height;
}
float Cylinder::Area() const {
return 2 * Circle::Area() + 2 * 3.14 * radius * height;
}
float Cylinder::volume() const {
return Circle::Area() * height;
}
ostream& operator<<(ostream& output, const Cylinder& cy){
output << "Center=[" << cy.x << "," << cy.y << "], r=" << cy.radius << ", h=" << cy.height << ", area=" << cy.Area() << ", volume=" << cy.volume() << endl;
return output;
}
int main(){
Cylinder cy1(3.5, 6.4, 5.2, 10);
cout << "x=" << cy1.getX() << ", y=" << cy1.getY() << ", r=" << cy1.getRadius() << ", h=" << cy1.getHeight() << ", area=" << cy1.Area() << ", volume=" << cy1.volume() << endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5, 5);
cout << "new " << cy1;
return 0;
}
```
在这个代码中,我们定义了三个类:Point、Circle 和 Cylinder。Point类表示一个点,Circle类表示一个圆,Cylinder类表示一个圆柱体。每个类都有相应的成员函数和友元函数用于操作和输出对象。在main函数中,创建了一个 Cylinder 对象 cy1,并输出其属性。然后修改 cy1 的属性,并再次输出。
#include"iostream" using namespace std; class Shape { public: virtual float area()const { return 0.0; } virtual float volume()const { return 0.0; } virtual void shapeName()const = 0; }; class Point :public Shape { protected: float x, y; public: Point(float = 0, float = 0); void setPoint(float, float); float getX()const { return x;} float getY()const { return y; } virtual void shapeName()const { cout << "Point: ";} friend ostream & operator<<(ostream&, const Point &); }; Point::Point(float a, float b) { x = a; y = b; } void Point::setPoint(float a, float b) { x = a; y = b; } ostream & operator<<(ostream &output, const Point &p) { return output; } class Circle:public Point { public: Circle(float x = 0,float y = 0,float r = 0); void setRadius(float); float getRadius() const; virtual float area() const; virtual void shapeName() const { cout << "Circle:"; } friend ostream &operator<<(ostream &, const Circle &); protected: float radius; }; Circle::Circle(float a, float b, float r) : Point(a, b),radius(r) {} void Circle::setRadius(float r) {radius = r;} float Circle::getRadius() const { return radius; } float Circle::area() const { return 3.14159 * radius * radius; } ostream &operator<<(ostream &output,const Circle &c) { output << "[" << c.x << "," << c.y << "],r=" << c.radius; return output; } class Cylinder : public Circle { public: Cylinder(float x = 0, float y = 0, float r = 0, float h = 0); void setHeight(float); virtual float area() const; virtual float volume() const; virtual void shapeName() const { cout<<"Cylinder:"; } friend ostream &operator<<(ostream &, const Cylinder&); protected: float height; }; Cylinder::Cylinder(float a, float b, float r, float h): Circle(a, b, r), height(h) {} void Cylinder::setHeight(float h) { height = h; } float Cylinder :: area() const{return 2 * Circle::area() + 2 * 3.14159 * radius * height;} float Cylinder :: volume() const { return Circle::area() * height; } ostream &operator << (ostream &output, const Cylinder& cy) { output << "[" << cy.x << "," << cy.y << "],r=" << cy.radius << ",h=" << cy.height; return output; } int main() { Point point(3.2, 4.5); Circle circle(2.4, 1.2, 5.6); Cylinder cylinder(3.5, 6.4, 5.2, 10.5); point.shapeName(); cout << point<<endl; circle.shapeName(); cout<< circle << endl; cylinder.shapeName(); cout << cylinder << endl << endl; Shape * pt; pt = &point; pt->shapeName(); cout << "x=" << point.getX() << ",y=" << point.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n"; pt = &circle; pt->shapeName(); cout << "x=" << circle.getX() << ",y=" << circle.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n"; pt=&cylinder; pt->shapeName(); cout << "x=" << cylinder.getX() << ",y=" << cylinder.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n"; return 0; }为什么第一个Point不能输出数据
在你的代码中,第一个Point对象的输出运算符重载函数operator<<没有实际的输出语句,所以调用该函数时没有任何输出。你需要在该函数中添加输出语句,例如:
```c++
ostream & operator<<(ostream &output, const Point &p) {
output << "[" << p.x << "," << p.y << "]";
return output;
}
```
这样就能正确输出第一个Point对象的数据了。
阅读全文