一、类的引入
· C语言中,”数据”和“函数”是分开的,语言本身并不支持“数据和函数”之间的关联性;所有的数据由一组“分布在各个以功能为导向的函数中”的算法所驱动,它们处理的是共同的外部数据。
· 在C++中,结构体内不仅可以定义变量,也可以定义函数;如下:
#define _CRT_SECURE_NO_WARNINGS //编译出错,strcpy不安全,加上这一行可以解决问题
#include<iostream>
using namespace std;
struct Student //结构体的定义,在C++中更喜欢用class来代替
{
char _name[20];
char _gender[3];
int _age;
void SetStudentInfo(const char* name, const char* gender, int age)
{
strcpy(_name, name);
strcpy(_gender, gender);
_age = age;
}
void PrintStudentInfo()
{
cout << _name << " " << _gender << " " << _age << " " << endl;
}
};
int main()
{
Student s1,s2,s3;
s1.SetStudentInfo("Peter", "男"<