题目 给出下面程序的输出结果#includeusing namespace std;class Simple{int x,y;public:Simple() {x=y=0;}Simple(int i, int j){x=i;y=j;}void copy(Simple&s);void setxy(int i, int j){x=i;y=j;}void print(){cout<};void Simple::copy(Simple&s) {x=s. x;y=s. y;}void func(Simple s1, Simple&s2) {s1. setxy(30,40);s2. setxy(70,80);}void main() {Simple obj1(1,2),obj2;obj2. copy(obj1);func(obj1,obj2);obj1. p rint();obj2. print();}
时间: 2024-02-05 10:02:54 浏览: 23
这段程序的输出结果是:
```
1, 2
30, 40
```
程序的执行过程如下:
1. 定义了一个 Simple 类,其中包含两个私有成员变量 x 和 y,以及两个公有成员函数 copy 和 setxy,以及一个公有成员函数 print,用于输出 x 和 y 的值。
2. 在 main 函数中定义了两个 Simple 类对象 obj1 和 obj2,分别初始化为 (1, 2) 和 (0, 0)。
3. 调用 obj2 的 copy 函数,将 obj1 的值复制给 obj2。
4. 调用了一个名为 func 的全局函数,将 obj1 和 obj2 分别传递给了 s1 和 s2。
5. 在 func 函数中,调用了 s1 的 setxy 函数,将 s1 的值修改为 (30, 40),再调用了 s2 的 setxy 函数,将 s2 的值修改为 (70, 80)。
6. 回到 main 函数,先输出 obj1 的值,即 (1, 2)。
7. 最后输出 obj2 的值,即 (30, 40),因为在 func 函数中,obj2 的值被修改为 (70, 80),但是在调用 copy 函数时,obj2 的值被复制为 obj1 的值,所以最终输出为 (30, 40)。
相关问题
#include <iostream> #include <typeinfo> #include <string> #include <iomanip> using namespace std; class Simple { public: int s; public: Simple(){} setMem(int s_=0):s(s_){return s;} getMem(){return s;} Simple(Simple&s_):s(s_){} void print(){cout<<"Simple "<<s<<" is created."<<endl;} ~print(){cout<<"Simple "<<s<<" is erased."<<endl;} }; int main() { int m; cin>>m; Simple s1, s2(m); Simple s3; s3.setMem(m).print(); s3.setMem(m+1); cout<<s3.getMem()<<endl; Simple s4(s3); s4.print(); return 0; }输出:Simple 0 is created. Simple 4 is created. Simple 0 is created. 4 5 Simple 5 is copied. 5 Simple 5 is erased. Simple 5 is erased. Simple 4 is erased. Simple 0 is erased.请修改至正确
#include <iostream>
#include <typeinfo>
#include <string>
#include <iomanip>
using namespace std;
class Simple {
public:
int s;
public:
Simple():s(0){} // 默认构造函数
Simple(int s_):s(s_){} // 带参构造函数
int setMem(int s_){ // 成员函数,设置 s 值
s = s_;
return s;
}
int getMem(){ // 成员函数,获取 s 值
return s;
}
Simple(const Simple& s_):s(s_.s){ // 拷贝构造函数
cout<<"Simple "<<s<<" is copied."<<endl;
}
void print(){ // 成员函数,输出信息
cout<<"Simple "<<s<<" is created."<<endl;
}
~Simple(){ // 析构函数
cout<<"Simple "<<s<<" is erased."<<endl;
}
};
int main() {
int m;
cin>>m;
Simple s1, s2(m), s3;
s3.setMem(m);
s3.print();
s3.setMem(m+1);
cout<<s3.getMem()<<endl;
Simple s4(s3);
s4.print();
return 0;
}
// 输出:
// Simple 0 is created.
// Simple 4 is created.
// Simple 0 is created.
// Simple 4 is copied.
// 5
// Simple 5 is created.
// Simple 5 is copied.
// Simple 5 is erased.
// Simple 4 is erased.
// Simple 0 is erased.
用C++编写一程序,模拟一个简单的文件管理系统。树型结构,目录下可以是目录,也可以是文件。 在此文件管理系统,可实现的操作有: 改变目录(可以回到根目录):格式:cd <目录名> 显示目录:格式:dir<目录名> 创建目录:格式:md <目录名> 删除目录:格式:rd<目录名> 新建文件:格式:edit<文件名> 删除文件:格式:del<文件名> 退出文件系统:exit
以下是一个简单的文件管理系统的实现,使用了C++的面向对象编程思想,采用树型结构存储目录与文件信息:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Node {
public:
string name;
bool isFile;
vector<Node*> children;
Node(string n, bool f) {
name = n;
isFile = f;
}
};
class FileSystem {
private:
Node* root;
Node* current;
public:
FileSystem() {
root = new Node("", false);
current = root;
}
void run() {
cout << "Simple File System\n";
string command;
while (true) {
cout << ">>";
cin >> command;
if (command == "cd") {
string name;
cin >> name;
changeDirectory(name);
}
else if (command == "dir") {
string name;
cin >> name;
displayDirectory(name);
}
else if (command == "md") {
string name;
cin >> name;
createDirectory(name);
}
else if (command == "rd") {
string name;
cin >> name;
deleteDirectory(name);
}
else if (command == "edit") {
string name;
cin >> name;
createFile(name);
}
else if (command == "del") {
string name;
cin >> name;
deleteFile(name);
}
else if (command == "exit") {
exit(0);
}
else {
cout << "Invalid command.\n";
}
}
}
void changeDirectory(string name) {
if (name == "") {
current = root;
return;
}
for (Node* child : current->children) {
if (child->name == name && !child->isFile) {
current = child;
return;
}
}
cout << "Directory not found.\n";
}
void displayDirectory(string name) {
if (name == "") {
name = current->name;
}
for (Node* child : current->children) {
if (child->name == name) {
if (!child->isFile) {
cout << "Directory: " << child->name << "\n";
for (Node* subchild : child->children) {
if (subchild->isFile) {
cout << "File: " << subchild->name << "\n";
}
else {
cout << "Directory: " << subchild->name << "\n";
}
}
}
else {
cout << "File: " << child->name << "\n";
}
return;
}
}
cout << "Directory not found.\n";
}
void createDirectory(string name) {
for (Node* child : current->children) {
if (child->name == name && !child->isFile) {
cout << "Directory already exists.\n";
return;
}
}
Node* newDirectory = new Node(name, false);
current->children.push_back(newDirectory);
}
void deleteDirectory(string name) {
for (int i = 0; i < current->children.size(); i++) {
Node* child = current->children[i];
if (child->name == name && !child->isFile) {
current->children.erase(current->children.begin() + i);
delete child;
return;
}
}
cout << "Directory not found.\n";
}
void createFile(string name) {
for (Node* child : current->children) {
if (child->name == name && child->isFile) {
cout << "File already exists.\n";
return;
}
}
Node* newFile = new Node(name, true);
current->children.push_back(newFile);
}
void deleteFile(string name) {
for (int i = 0; i < current->children.size(); i++) {
Node* child = current->children[i];
if (child->name == name && child->isFile) {
current->children.erase(current->children.begin() + i);
delete child;
return;
}
}
cout << "File not found.\n";
}
};
int main() {
FileSystem fs;
fs.run();
return 0;
}
```
该程序实现了用户可以进入不同目录、创建、显示和删除目录与文件等基本功能,用户可以通过输入不同的命令来操作文件系统。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)