課程
/后端開發(fā)
/C++
/C++遠征之封裝篇(上)
為什么答案是不對的,完全讀不出來
2016-02-23
源自:C++遠征之封裝篇(上) 7-2
正在回答
#include <iostream>
#include <string>
using namespace std;
/**
?* 定義類:Student
?* 數(shù)據(jù)成員:m_strName
?* 無參構(gòu)造函數(shù):Student()
?* 有參構(gòu)造函數(shù):Student(string _name)
?* 拷貝構(gòu)造函數(shù):Student(const Student& stu)
?* 析構(gòu)函數(shù):~Student()
?* 數(shù)據(jù)成員函數(shù):setName(string _name)、getName()
?*/
class Student
{
public:
Student(){cout<<"Student()"<<endl;}
Student(string _name){m_strName=_name;}
Student(const Student& stu);
~Student(){cout<<"~Student()"<<endl;}
void setName(string _name){m_strName=_name;}
string getName(){return m_strName;}
private:
string m_strName;
};
int main()
? ? // 通過new方式實例化對象*stu
? ? Student *stu = new Student;
? ? // 更改對象的數(shù)據(jù)成員為“慕課網(wǎng)”
stu->setName("慕課網(wǎng)");
? ? // 打印對象的數(shù)據(jù)成員
cout<<stu->getName()<<endl;
delete stu;
stu=NULL;
return 0;
}
試試這個
舉報
封裝--面向?qū)ο蟮幕窘坛塘η髱椭』锇閭兗磳W即會
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-02-23
#include <iostream>
#include <string>
using namespace std;
/**
?* 定義類:Student
?* 數(shù)據(jù)成員:m_strName
?* 無參構(gòu)造函數(shù):Student()
?* 有參構(gòu)造函數(shù):Student(string _name)
?* 拷貝構(gòu)造函數(shù):Student(const Student& stu)
?* 析構(gòu)函數(shù):~Student()
?* 數(shù)據(jù)成員函數(shù):setName(string _name)、getName()
?*/
class Student
{
public:
Student(){cout<<"Student()"<<endl;}
Student(string _name){m_strName=_name;}
Student(const Student& stu);
~Student(){cout<<"~Student()"<<endl;}
void setName(string _name){m_strName=_name;}
string getName(){return m_strName;}
private:
string m_strName;
};
int main()
{
? ? // 通過new方式實例化對象*stu
? ? Student *stu = new Student;
? ? // 更改對象的數(shù)據(jù)成員為“慕課網(wǎng)”
stu->setName("慕課網(wǎng)");
? ? // 打印對象的數(shù)據(jù)成員
cout<<stu->getName()<<endl;
delete stu;
stu=NULL;
return 0;
}
試試這個