【程序填空】 功能:在下面的横线处填上适应的语句,完成类CTest的定义。 注意:若语句结束已有分号处,填空中内容后面就不需要添加分号。 ----------------------------
时间: 2024-05-26 18:08:37 浏览: 15
class CTest {
public:
CTest(); //构造函数声明
~CTest(); //析构函数声明
void Print() const; //成员函数Print()声明
private:
int m_nData; //私有数据成员m_nData声明
};
CTest::CTest() { //构造函数定义
m_nData = 0;
}
CTest::~CTest() { //析构函数定义
}
void CTest::Print() const { //成员函数Print()定义
cout << m_nData << endl;
}
相关问题
假设已经定义好了一个类CTest,则语句“CTest *cb;”表示创建了一个CTest对象是否正确
答案是不正确的。
语句“CTest *cb;”仅仅是定义了一个指向CTest对象的指针变量cb,并没有创建任何实际的对象。要创建一个CTest对象,需要使用new关键字进行动态内存分配,例如:
```
CTest *cb = new CTest();
```
这样才能真正创建一个CTest对象,并将其地址赋给cb指针变量。
java找出D:\\CTest目录中以x开头,以cpp结尾的文件,保存在Student文件数组中
可以使用Java的文件操作类File来实现:
```java
import java.io.File;
public class Main {
public static void main(String[] args) {
String path = "D:\\CTest";
File dir = new File(path);
File[] files = dir.listFiles((dir1, name) -> name.startsWith("x") && name.endsWith(".cpp"));
Student[] students = new Student[files.length];
for (int i = 0; i < files.length; i++) {
Student student = new Student();
student.setName(files[i].getName());
// 其他属性赋值
students[i] = student;
}
}
}
class Student {
private String name;
// 其他属性
// getter和setter方法
}
```
其中,`dir.listFiles()` 方法可以获取目录下的所有文件,它接收一个 `FileFilter` 参数用于过滤文件。在本例中,使用了一个 lambda 表达式来实现文件过滤,只保留以 `x` 开头、以 `.cpp` 结尾的文件。最后将符合条件的文件属性赋值给一个 `Student` 对象,并存入数组中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)