123mcz / blog

0 stars 0 forks source link

面向对象 #3

Open 123mcz opened 1 week ago

123mcz commented 1 week ago

include

using namespace std;

include

include"Student.h"

int main() { //.h头文件 cpp代码文件 //h里面一般写类的声明,里面即使有函数,也只是函数的声明 //cpp里面写类里面函数的定义也就是实现 //类似现实中的书,h相当于书的目录 cpp相当于根据目录找到的详细内容

//面试题: 包含头文件<>和""有什么区别 //<>是已经在编辑器上配置好的系统的库文件 //""是当前项目中的源代码文件 Student zhangsan(2555, "洒出",22356); zhangsan.introduce(); }

Student.h:

pragma once

include

using namespace std; class Student { public: int id = 0; string name; int score = 0;

Student(int _id, string _name, int _score);
void introduce();

};

Student.cpp:

include "Student.h"

Student::Student(int _id, string _name, int _score) { id = _id; name = _name; score = _score; }

void Student::introduce() { cout << "我叫" << name << endl; cout << "我考了" << score << endl; }