亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
B:
private:
string m_strName="世間第一帥氣人";
};
Student::Student(string name) :m_strName(name) {}
void Student::setName(string name) {
m_strName = name;
}
string Student::getName() {
return m_strName;
}
int main(void) {
Student *stu = new Student;
stu->setName("慕課網");
A:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(string name) ;
Student(const Student & stuc) {};
Student() {};
~Student() {};
void setName(string name);
string getName();
頭文件B:
int getMax();
private:
string m_strName;
int m_iAge;
const int m_iMax = 1;
};
頭文件A:
#include"string"
#include"iostream"
using namespace std;
class Student {
public:
Student(string name = "小白", int Age = 1, int Max = 100);//初始化構造函數 構造函數——用以初始化
Student(const Student &stu);//拷貝構造函數
void setName(string name);
string getName();
void setAge(int Age);
int getAge();
源2B:
void Student::setName(string name) {
m_strName = name;
}
string Student::getName() {
return m_strName;
}
void Student::setAge(int Age) {
m_iAge = Age;
}
int Student::getAge() {
return m_iAge;
}
int Student::getMax() {
return m_iMax;
}
源2A:
#include"標頭.h"
Student::Student(string name, int Age, int Max) :m_strName(name), m_iAge(Age), m_iMax(Max) { cout << "you used initialization_code"<<endl; } //初始化列表
Student::Student(const Student & stu) { cout << "you used copy_code"<<endl; }//拷貝構造函數
源1:
#include"標頭.h"
int main() {
Student *str = new Student;//初始化——指針方式
Student stu1("小白", 12, 150);//初始化
str->setName("慕課網");
Student stu2 = stu1;//測試拷貝函數部分
cout << stu1.getName() << " \t " << str->getAge() << "\t" << str->getMax();
delete str;
str = NULL;
return 0;
}
#include"string"
#include"iostream"
using namespace std;
class Student {
public:
Student(string name = "小白",int Age=1,int Max=100);//構造函數初始化
void setName(string name);
string getName();
void setAge(int Age);
int getAge();
int getMax();
private:
string m_strName;
int m_iAge;
const int m_iMax
Student::Student(string name,int Age,int Max) :m_strName(name), m_iAge(Age),m_iMax(Max){} //初始化列表
void Student::setName(string name) {
m_strName = name;
}
string Student::getName() {
return m_strName;
}
void Student::setAge(int Age) {
m_iAge = Age;
}
int Student::getAge() {
return m_iAge;
}
DEMO1:
#include"標頭.h"
int main() {
Student *str = new Student;
Student stu1("小白", 12, 150);
str->setName("慕課網");
cout << stu1.getName() << " \t " << str->getAge() << "\t" << str->getMax();
delete str;
str = NULL;
return 0;
}
class Student {
public:
void setName(string name) {
m_strName = name;
}
string getName() {
return m_strName;
}
private:
string m_strName;
};
int main() {
Student *str = new Student;
str->setName("慕課網");
cout << str->getName();
delete str;
str = NULL;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
class student {
public:
string m_strName = "王發財";
int m_iAge = 199;
}stu;
stu.m_iAge = 2;
stu.m_strName = "慕課網";
cout << stu.m_iAge <<endl<< stu.m_strName << endl;
}

最贊回答 / __盛夏光年__
無參構造函數和有參構造函數放在類外定義沒有返回值,所以去掉void即可。ps:在定義類的時候Student()后面沒有;
課程須知
本課程是C++初級課程 需要熟練掌握C++語言基礎語法
老師告訴你能學到什么?
1、類的定義與對象實例化 2、string類型及屬性封裝 3、類外定義 4、構造函數與初始化列表 5、析構函數

微信掃碼,參與3人拼團

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!

本次提問將花費2個積分

你的積分不足,無法發表

為什么扣積分?

本次提問將花費2個積分

繼續發表請點擊 "確定"

為什么扣積分?

舉報

0/150
提交
取消