多文件组织项目Student类

本文介绍了一个简单的C++学生信息类的实现过程,包括类的设计、成员变量及方法的定义,并通过实例演示了如何使用该类进行学生信息的设置与获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Student.h

#include <string>
using namespace std;

class Student
{
private:
 string user_name;
 string pass_word;

public:
 Student(string user_name, string pass_word);

 string get_user_name();
 string get_pass_word();
 void set_user_name(string user_name);
 void set_pass_word(string pass_word);

 void display();
};

 

Student.cpp

#include <string>
#include <iostream>
#include "Student.h"
using namespace std;

Student::Student (string user_name, string pass_word)
{
 this->user_name = user_name;
 this->pass_word = pass_word;
}

string Student::get_user_name()
{
    return this->user_name;
}

string Student::get_pass_word()
{
 return this->pass_word;
}

void Student::set_user_name(string user_name)
{
 this->user_name  = user_name;
}

void Student::set_pass_word(string pass_word)
{
 this->pass_word = pass_word;
}

void Student::display()
{
 cout << "user_name: " << this->user_name << endl;
 cout << "pass_word: " << this->pass_word << endl;
}

 

Main.cpp

#include <iostream>
#include <string>
#include "Student.h"
using namespace std;

int main()
{
 Student student("xiaoming", "123");
 student.display();
 student.set_pass_word("456");
 cout << student.get_pass_word() << endl;

 return 0;
}


 

运行结果:

user_name: xiaoming
pass_word: 123
456
Press any key to continue


作此项目之前的准备:

画类图:

类图

Student

string user_name

string pass_word

student (string user_name,string pass_word)

string get_user_name()

string get_pass_word()

void set_name_word(string)

void set_pass_word(string)

void display()

这样可以使编码更为简单,不会出错

经验积累:

1.对数据成员赋初值时,所有参数必须与所对应的数据成员名称一致;

2.需要访问数据成员时用this指向;

3.画类图比写代码更为重要;

4.做一个项目最好多文件组织项目,良好的习惯需要逐步养成!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值