为了账号安全,请及时绑定邮箱和手机立即绑定
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下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!

本次提问将花费2个积分

你的积分不足,无法发表

为什么扣积分?

本次提问将花费2个积分

继续发表请点击 "确定"

为什么扣积分?

举报

0/150
提交
取消