關于cout的使用
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class Teacher
{
public:
?void teach();
};
void Teacher::teach()
{
?cout<<"good morning!"<<endl;
}
int main()
{
?Teacher t;
?coutl<<t.teach()<<endl;//注意這里t.teach的使用,如果我把teach的類型改成int型,是可以使用的,為什么?
?system("pause");
?return 0;
}
-----------------------------------
一下是更改后可正常運行的代碼
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class Teacher
{
public:
?int teach();
};
int Teacher::teach()
{
?cout<<"good morning!"<<endl;
?return 0;
}
int main()
{
?Teacher t;
?cout<<t.teach()<<endl;//注意這里t.teach的使用,如果我把teach的類型改成int型,是可以使用的,為什么?
?system("pause");
?return 0;
}
2019-08-10
int表示一個整型,用它來定義的函數必須要返回一個整數,要用return;而void型定義的函數不需要返回任何值,將void換為int型的不同只是要返回一個整數而已,不影響正常使用