Main
#include <iostream>
#include "LandOwnerv4.h"
#include "Student.h"
using namespace std;
int main() {
Student stu1;
Student stu2(" dennis ", "Got a name ");
Student stu3(6);
stu2.ShowInfo();
auto* stu5 = new Student(" Charling ", " Is a girl "); //这里本来是,程序自动更新 Student* stu5 = new Student(" Charling ", " Is a girl "); 定义一个指针
stu5->ShowInfo(); //指针调用
return 0;
}
头文件
//
// Created by llb on 2020/4/22.
//
#ifndef CLASS_EXERCISE_STUDENT_H
#define CLASS_EXERCISE_STUDENT_H
#include <string>
#include <iostream>
using namespace std;
class Student {
private:
string m_name;
string m_describe;
int m_age{};
public:
const string &getMName() const;
void setMName(const string &mName);
const string &getMDescribe() const;
void setMDescribe(const string &mDescribe);
int getMAge() const;
void setMAge(int mAge);
Student();
Student(string name, string describe);
explicit Student(int age);
void ShowInfo();
};
#endif //CLASS_EXERCISE_STUDENT_H
构造函数
//
// Created by llb on 2020/4/22.
//
#include "Student.h"
const string &Student::getMName() const {
return m_name;
}
void Student::setMName(const string &mName) {
m_name = mName;
}
const string &Student::getMDescribe() const {
return m_describe;
}
void Student::setMDescribe(const string &mDescribe) {
m_describe = mDescribe;
}
int Student::getMAge() const {
return m_age;
}
void Student::setMAge(int mAge) {
if (mAge < 0) {
m_age = 18;
} else {
m_age = mAge;
}
}
Student::Student() {
cout << "default info" << endl;
}
Student::Student(string name, string describe) {
setMName(name); //equal to: m_name = name;
setMDescribe(describe);
cout << "has name and describe parameters" << endl;
}
Student::Student(int age) {
setMAge(age);
cout << "has age parameters" << endl;
}
void Student::ShowInfo() {
cout << m_describe << m_name << " pppppp" << endl;
}
本文详细介绍了使用C++实现的学生类构造过程,包括默认构造函数、带参数构造函数及显示类型构造函数的定义与使用。同时,深入探讨了成员变量的初始化、成员函数的实现以及通过指针调用成员函数的方法。
2432

被折叠的 条评论
为什么被折叠?



