同學們,C++封裝4-3練習題 我這樣寫為什么不對?謝謝大伙~
我不明白為什么有人將_name寫做str
我不明白實例化對象時候為什么有人用str->setName,用p有區別么
以下是我的代碼:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Student
{
public:
void setName(string _name)
{
m_strname = _name;
}
string getName()
{
return m_strname;
}
private:
string m_strname;
};
int main(void)
{
Student*p = new Student();
if (NULL==p)
{
return 0;
}
p->setName("zhangsan");
cout << p->getName()<<endl;
delete p;
p = NULL;
system("pause");
return 0;
}
2017-02-05
沒有區別,運行出來結果都一樣的,只是變量名不同而已,變量名你自己取什么就是什么,除非一些特殊的地方一定要取這個名字。不過變量名最好取能形容用途的。
2017-02-05
#include <string>
2017-02-05
我還少寫了#include<string>