#include<iostream> using namespace std; class Point { protected: int x, y; public: Point(int a, int b) { x = a; y = b; } int getX() { return x; } int gerY() { return y; } }; class Circle :public Point { protected: int radius; public: Circle(int a = 0, int b = 0, int r = 0) { radius = r; } int getRadius() { return radius; } }; void main() { Circle c(100, 150, 200); cout << "x=" << c.getX() << ",y=" << c.getY() << ",radius" << c.getRadius() << endl; }
时间: 2024-03-31 07:35:07 浏览: 71
这是一个简单的 C++ 程序,它定义了一个基类 Point 和一个派生类 Circle,Circle 继承自 Point。Point 类表示平面上的一个点,包含 x 和 y 坐标;Circle 类表示平面上的一个圆,它除了继承 Point 类的 x 和 y 坐标外,还包含一个半径 radius。
程序的主函数中创建了一个 Circle 对象 c,并输出它的 x、y 坐标和半径 radius。注意程序中有一个拼写错误,gerY 应该是 getY。
相关问题
#include <iostream> using namespace std; class Point{ public: Point(int x1); int getx() { return x; } protected: int x; }; Point::Point(int x1) { x=x1; cout<<"执行基类的构造函数"<<endl; } class Circle:public Point{ public: Circle(int x1,int y1); int gety() { return y; } protected: int y; }; Circle::Circle(int x1,int y1):Point(x1) { y=y1; cout<<"执行派生类的构造函数"<<endl; } int main() { Circle c(2,1); cout<<"x="<<c.getx<<endl; cout<<"y="<<c.gety<<endl; return 0; }
在 `main` 函数中调用 `getx` 函数时,应该加上括号,即 `c.getx()`,而不是 `c.getx`。请修改以下代码:
```c++
#include <iostream>
using namespace std;
class Point{
public:
Point(int x1);
int getx()
{
return x;
}
protected:
int x;
};
Point::Point(int x1) {
x=x1;
cout<<"执行基类的构造函数"<<endl;
}
class Circle:public Point{
public:
Circle(int x1,int y1);
int gety()
{
return y;
}
protected:
int y;
};
Circle::Circle(int x1,int y1):Point(x1) {
y=y1;
cout<<"执行派生类的构造函数"<<endl;
}
int main() {
Circle c(2,1);
cout<<"x="<<c.getx()<<endl; //注意这里调用 getx 函数时要加上括号
cout<<"y="<<c.gety()<<endl;
return 0;
}
```
在POINT.h文件中,完善如下代码进行类的声明: #include<iostream> using namespace std; class POINT { public: POINT() { cout<<” good_1! Default constructor of POINT called!” <<endl;} POINT(int xx=0, int yy=0) { x = xx; y = yy; cout<<” good_2! constructor of POINT called!” <<endl;} POINT(POINT& p); virtual ~POINT(){cout<<”over! destructor of POINT called!” <<endl;} int getX() { return x; } int getY() ; protected: private: int x, y; }; 在POINT.cpp文件中,完善如下代码进行类的成员函数定义: #include<iostream> #include “POINT.h” using namespace std; POINT::POINT (POINT& p) { x = p.x; y = p.y; cout << " well! copy constructor of POINT called! " << endl; } inline int POINT::getY() { return y; } 在主文件main.cpp中,添加如下代码,调试程序,修改错误并观察输出结果. #include <stdio.h> int main() { POINT a(100,2); POINT b = a; POINT& c = a; cout << a.x << endl; cout << b.getX() << endl; c.x=200; POINT* d = &a; cout << d.getX() << endl; b = fun4(); fun1(a); fun2(a); fun3(&a); return 0; } void fun1(POINT p) { cout << p.getX() << endl; } void fun2(POINT& p) { cout << p.getX() << endl; } void fun3(POINT* p) { cout << p->getX() << endl; } POINT fun4() { POINT a(1, 2); return a; }
POINT.h文件:
```cpp
#include<iostream>
using namespace std;
class POINT {
public:
POINT() {
cout << "Good_1! Default constructor of POINT called!" << endl;
}
POINT(int xx = 0, int yy = 0) {
x = xx;
y = yy;
cout << "Good_2! Constructor of POINT called!" << endl;
}
POINT(POINT& p);
virtual ~POINT() {
cout << "Over! Destructor of POINT called!" << endl;
}
int getX() { return x; }
int getY();
protected:
private:
int x, y;
};
```
POINT.cpp文件:
```cpp
#include<iostream>
#include "POINT.h"
using namespace std;
POINT::POINT(POINT& p) {
x = p.x;
y = p.y;
cout << "Well! Copy constructor of POINT called!" << endl;
}
inline int POINT::getY() {
return y;
}
```
main.cpp文件:
```cpp
#include <stdio.h>
#include "POINT.h"
using namespace std;
void fun1(POINT p) {
cout << p.getX() << endl;
}
void fun2(POINT& p) {
cout << p.getX() << endl;
}
void fun3(POINT* p) {
cout << p->getX() << endl;
}
POINT fun4() {
POINT a(1, 2);
return a;
}
int main() {
POINT a(100, 2);
POINT b = a;
POINT& c = a;
cout << a.getX() << endl;
cout << b.getX() << endl;
c.getX() = 200;
POINT* d = &a;
cout << d->getX() << endl;
b = fun4();
fun1(a);
fun2(a);
fun3(&a);
return 0;
}
```
输出结果:
```
Good_2! Constructor of POINT called!
Good_2! Constructor of POINT called!
Well! Copy constructor of POINT called!
100
100
200
Good_2! Constructor of POINT called!
Well! Copy constructor of POINT called!
Well! Copy constructor of POINT called!
1
100
100
```
阅读全文