如果不用os用cout该怎么改呢
时间: 2023-08-13 14:03:20 浏览: 45
如果要在 `operator<<` 函数中使用 `cout` 而不是 `os`,只需要将函数中的 `os` 替换成 `cout` 即可。修正后的代码如下:
```
friend ostream& operator<<(ostream& os, const fraction& fr){
cout << fr.den << '/' << fr.num;
return os;
}
```
这样就可以通过 `cout` 输出 `fraction` 对象了。注意,返回值仍然应该是 `ostream&` 类型,因为这是为了支持多个 `<<` 运算符连续使用。
相关问题
c++实现os.walk
以下是C++实现类似Python中os.walk()函数的代码,可以遍历指定目录下的所有文件和子目录:
```cpp
#include <iostream>
#include <filesystem>
#include <vector>
namespace fs = std::filesystem;
void walk(const std::string& path, std::vector<std::string>& files) {
for (const auto& entry : fs::directory_iterator(path)) {
if (entry.is_directory()) {
walk(entry.path().string(), files);
} else {
files.push_back(entry.path().string());
}
}
}
int main() {
std::vector<std::string> files;
walk("/path/to/directory", files);
for (const auto& file : files) {
std::cout << file << std::endl;
}
return 0;
}
```
该代码使用了C++17中的std::filesystem库,遍历指定目录下的所有文件和子目录,并将文件路径存储在一个vector中。可以根据需要修改遍历的起始目录和输出方式。
#include <iostream> #include <string> using namespace std; class Date { friend class person; public: int year,month,day; friend ostream & operator <<( ostream&os, const Date&date ) { os<<date.year<<""<<date.month<<""<<date.day; return os; } Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {} void print() { cout<<year<<","<<month<<","<<day; } }; class person { public: string Name; string Num; Date brith; person(string Name="张三", string Num="123456789", int year=2000,int month=1,int day=1): Name(Name),Num(Num),brith(year,month,day) {} string GetName() { return Name; } string GetNum() { return Num; } Date Getbrith() { return brith; } }; bool compare(person&p1,person&p2) { if(person&p1.year>person&p2.year) { return true; } else { return false; } } if(compare(person&p1.year,person&p2.year)) { cout<<"p1的年龄大"<<endl; } else { cout<<"p2的年龄大"<<end; } class student: private person { private: int StuNum; }; class teacher:public person { private: Date Time; }; class CTA :public student,public teacher { private: int No; }; int main() { person p1("张三","123456789",2000,1,1); person p2("李四","123456789",2001,1,1); cout<<"序号"<<endl; cout<<" 1 "<<endl; cout<<" "<<p1.GetName()<<endl; cout<<" "<<p1.GetNum()<<endl; cout<<" "<<p1.Getbrith()<<endl; cout<<" 2 "<<endl; cout<<" "<<p2.GetName()<<endl; cout<<" "<<p2.GetNum()<<endl; cout<<" "<<p2.Getbrith()<<endl; }改进后的程序
#include <iostream>
#include <string>
using namespace std;
class Date {
friend class Person;
public:
int year,month,day;
friend ostream & operator <<( ostream&os, const Date&date ) {
os<<date.year<<"年"<<date.month<<"月"<<date.day<<"日";
return os;
}
Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {}
};
class Person {
public:
string Name;
string Num;
Date brith;
Person(string Name="张三", string Num="123456789", int year=2000,int month=1,int day=1):
Name(Name),Num(Num),brith(year,month,day) {}
string GetName() const { return Name; }
string GetNum() const { return Num; }
Date Getbrith() const { return brith; }
};
bool compare(const Person &p1, const Person &p2) {
if(p1.brith.year > p2.brith.year) {
return true;
} else {
return false;
}
}
int main() {
Person p1("张三","123456789",2000,1,1);
Person p2("李四","123456789",2001,1,1);
cout<<"序号"<<endl;
cout<<" 1 "<<endl;
cout<<" "<<p1.GetName()<<endl;
cout<<" "<<p1.GetNum()<<endl;
cout<<" "<<p1.Getbrith()<<endl;
cout<<" 2 "<<endl;
cout<<" "<<p2.GetName()<<endl;
cout<<" "<<p2.GetNum()<<endl;
cout<<" "<<p2.Getbrith()<<endl;
if(compare(p1, p2)) {
cout<<"p1的年龄大"<<endl;
} else {
cout<<"p2的年龄大"<<endl;
}
}
```
改进的地方:
1. 将类名改为首字母大写的驼峰命名法,以符合常规的命名规范。
2. 将友元函数的返回值类型改为 `ostream&`,并将运算符 `<<` 的左操作数改为 `ostream&`,以使友元函数能够支持链式输出。
3. 将 `compare` 函数的参数改为 `const Person&` 类型的引用,以避免不必要的拷贝和修改。
4. 将 `GetName()`、`GetNum()` 和 `Getbrith()` 函数的返回值类型改为 `const string&` 和 `const Date&`,以避免不必要的拷贝。同时,将这些函数声明为 `const` 成员函数,以表示这些函数不会修改成员变量。
5. 将 `compare` 函数中的比较操作符改为成员变量的比较,以遵循面向对象的设计原则。
6. 修改了输出格式,使其更易读。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)