using namespace std; class A public: A(int xx,int yy){X=xx;Y=y) void Set(A &p); void Disp() {cout<<X<<","<<Y<<en private: int X,Y; }; void A:Set(A &p) if(this!=&p) {X=p.X;Y=p.Y;} int main() A 01(2,5),o2(10,20); o1.Disp(); o1.Set(o2); o1.Disp(; return 0; }
时间: 2023-11-27 15:53:01 浏览: 59
这段代码有一些语法错误,我稍作修改:
```
#include <iostream>
using namespace std;
class A {
public:
A(int xx, int yy) {
X = xx;
Y = yy;
}
void Set(A &p) {
if (this != &p) {
X = p.X;
Y = p.Y;
}
}
void Disp() {
cout << X << "," << Y << endl;
}
private:
int X, Y;
};
int main() {
A o1(2, 5), o2(10, 20);
o1.Disp();
o1.Set(o2);
o1.Disp();
return 0;
}
```
这是一个简单的类 `A`,其中包含私有成员变量 `X` 和 `Y`,以及公有成员函数 `Set` 和 `Disp`。在 `main` 函数中,我们创建了两个 `A` 类型的对象 `o1` 和 `o2`,并调用了它们的 `Disp` 函数输出它们的成员变量。然后我们调用了 `o1` 的 `Set` 函数,将它的成员变量设置为和 `o2` 一样,并再次输出 `o1` 的成员变量,可以看到已经发生了改变。
相关问题
include using namespace std; class A { public: A(int xx,int yy){X=xx;Y=yy;} void Set(A &p); void Disp() {cout<<X<<","<<Y<<endl;} private: int X,Y; }; void A::Set(A &p) { if(this!=&p) {X=p.X;Y=p.Y;} } int main() { A o1(2,5),o2(10,20); o1.Disp(); o1.Set(o2)
这代码有一些问题,缺少了一些必要的语句,同时最后一行也没有正确结束。下面是修改后的代码:
```cpp
#include <iostream>
using namespace std;
class A {
public:
A(int xx, int yy) {
X = xx;
Y = yy;
}
void Set(A &p);
void Disp() {
cout << X << "," << Y << endl;
}
private:
int X, Y;
};
void A::Set(A &p) {
if (this != &p) {
X = p.X;
Y = p.Y;
}
}
int main() {
A o1(2, 5), o2(10, 20);
o1.Disp();
o1.Set(o2);
o1.Disp();
return 0;
}
```
这段代码定义了一个类A,它有两个私有成员变量X和Y,以及一个构造函数和两个成员函数Set和Disp。其中Set函数用来将A类型的对象p的值赋给当前对象,Disp函数用来输出当前对象的X和Y值。在main函数中,先创建两个A类型的对象o1和o2,然后分别输出o1的X和Y值,再调用o1的Set函数将o2的值赋给o1,最后再次输出o1的X和Y值。
#include<iostream> using namespace std; class Point { private: int x,y; public: Point( int xx=0,int yy=0 ); void Set(int xx,int yy); void Show(); }; Point::Point( int xx,int yy ) { x=xx; y=yy; } void Point::Set( int xx,int yy ) { x=xx; y=yy; } void Point::Show() { printf("(%d,%d)\n",x,y); } int main() { Point p1,p2(20,100); cout<<"Show object p1:"; p1.Show(); cout<<"Show object p2:"; p2.Show(); int x,y; cin>>x>>y; cout<<"Reset and Show object p1:"; p1.Set(x,y); p1.Show(); cin>>x>>y; cout<<"Reset and Show object p2:"; p2.Set(x,y); p2.Show(); return 0; }
这段代码实现的功能和上面的代码是相同的,只是使用了不同的语法形式。以下是点类 Point 的实现代码:
```cpp
#include<iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
void Set(int xx, int yy) {
x = xx;
y = yy;
}
void Show() {
printf("(%d,%d)\n", x, y);
}
};
int main() {
Point p1, p2(20, 100);
cout << "Show object p1:";
p1.Show();
cout << "Show object p2:";
p2.Show();
int x, y;
cin >> x >> y;
cout << "Reset and Show object p1:";
p1.Set(x, y);
p1.Show();
cin >> x >> y;
cout << "Reset and Show object p2:";
p2.Set(x, y);
p2.Show();
return 0;
}
```
在类定义中,构造函数和成员函数的实现都放在了类的内部,并且在函数名后面加上了类名和作用域解析符号 `::`。在构造函数中使用了默认参数,可以在创建对象时不传递参数,或者只传递一个参数。在 Show 函数中使用了 `printf` 函数输出,而不是 cout 对象。这些都只是语法上的差异,实现的功能是一样的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)