目录
类的声明
声明一个类的语法:
class 类名
{
private://私有的成员
public://公有的成员
};
注意分号不能丢
1.vim student.h
#ifndef _STUDENT_H
#define _STUDENT_H
class Student
{
piblic:
int id;
char name[20];
double score;
void printf();//类成员函数
};//分号不能丢
#endif
类的定义
主要是对类中的成员函数进行定义
定义类成员函数的语法:
返回值类型 类名::函数名(参数列表)
{
//函数体
}
当类成员函数定义在类的外部时,一定要在函数名前加上“类名::”,以此来表示该函数是类的成员函数。
在类的成员函数中可以自由的访问该类的其他成员属性和成员函数。
类成员函数的定义一般写在与头文件对应的.cpp文件中。
2.vi Student.cpp
#include"Student.h"
#include<iostream>
using namespace std;
void Student::print()//定义类成员函数
{
cout<<"id "<<id<<endl;
cout<<"name "<<name<<endl;
cout<<"score "<<score<<endl;
}
3.g++ -c Student.cpp -o Student.o -g -Wall