#include<iostream.h> class point { int x; int y; /***********此行有错***********/ private: point(int a,int b) { x=a;y=b; } int getx(){return x;} int gety(){return y;} }; /***********此行有错*
时间: 2023-07-05 16:36:04 浏览: 53
这段代码有两处错误。第一处错误在于类的声明中没有使用访问修饰符来限定数据成员x和y的访问权限,应该改为:
```c++
class point {
private:
int x;
int y;
public:
point(int a, int b) {
x = a;
y = b;
}
int getx() {
return x;
}
int gety() {
return y;
}
};
```
第二处错误在于类的定义中,构造函数的访问权限应该是public,而不是private,因为构造函数是用来创建对象的,必须是public,否则无法创建对象。因此应该改为:
```c++
class point {
private:
int x;
int y;
public:
point(int a, int b) {
x = a;
y = b;
}
int getx() {
return x;
}
int gety() {
return y;
}
};
```
修改后的代码已经编译通过,可以正常运行。
相关问题
#include <iostream>和#include <stdio.h>区别
`#include <iostream>` 和 `#include <stdio.h>` 都是 C++ 和 C 编程中用于引入标准输入输出库的预处理器指令,但它们在语法、功能和兼容性上存在一些差异:
1. **C++**:`<iostream>` 是 C++ 标准库的一部分,专门用于处理输入输出操作,包括 cin/cout 对象(用于标准输入/输出)和 endl 操作符(用于换行)。`iostream` 包含了 `<fstream>` 和 `<iomanip>` 等其他文件,提供了丰富的输入输出控制。
2. **C**:`<stdio.h>` 是 C 语言的传统输入输出库,包含了基本的 I/O 函数,如 scanf(), printf(), getchar(), stdout, stdin, stderr 等。这个库在 C 语言的标准头文件中是标准的一部分。
- **语法不同**:C++ 使用尖括号 `< >`,而 C 语言使用花括号 `{ }`。
- **命名约定**:C++ 中的流类如 `std::cout` 通常使用 `::` 标识命名空间,而 C 中则直接使用库名。
- **模板和异常处理**:C++ 提供了模板和异常处理机制,`iostream` 中的很多类和函数支持这些特性,而 `<stdio.h>` 更基础,不包含这些现代特性。
**相关问题**:
1. C++ 何时会推荐使用 `<iostream>` 而不是 `<cstdio>`?
2. C 语言中,如果要进行更复杂的文件操作,应该如何引入相应的库?
3. C++ 和 C 在处理输入输出时,哪一种更容易出错或需要额外的关注?
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; class a{ public: int name; int number; void set_name(int ne) { name=ne; } void set_number(int nr) { number=nr; } void show_name() { cout << name; } void show_number() { cout << number; } }; int main() { a a1; a*p; int x; cin>>x; p->set_number(x); p->show_name(); };
在你的代码中,你定义了一个指向类 a 的指针 p,但是你没有为其分配内存空间,因此在使用该指针时会发生未定义行为。你需要使用 new 运算符为该指针分配内存空间,或者直接定义一个类 a 的对象,不使用指针。另外,在 show_name 函数中,你应该输出 name 而不是 number。修改后的代码如下:
```
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class a{
public:
int name;
int number;
void set_name(int ne) {
name = ne;
}
void set_number(int nr) {
number = nr;
}
void show_name() {
cout << name;
}
void show_number() {
cout << number;
}
};
int main() {
a a1;
int x;
cin >> x;
a1.set_number(x);
a1.show_name();
return 0;
}
```
这个程序的功能为:从标准输入中读取一个整数,将其设置为 a1 的 number 值,然后输出 a1 的 name 值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)