1.声明
告诉编译器变量的类型和名字,不需要分配空间
2.定义
对这个变量和函数进行内存分配和初始化,需要分配空间
3.变量的声明和定义
3.1 变量的声明
extern int a;//声明变量a存在,但不创建
extern int b;//声明变量b存在,但不创建
3.2 变量的定义
int a = 10;//定义变量a,分配内存并初始化
int b;//定义变量b,分配内存但未初始话
extern int c = 1;//定义变量c,分配内存并初始化为1
4.函数的声明和定义
4.1 函数的声明
//声明函数add
int add(int x,int y);
//声明函数printMessage
void printMessage();
//声明函数calculate
double calculate(double a);
4.2 函数的定义
//定义函数add
int add(int x,int y){
return x + y;
}
//定义函数printMessage
void printMessage(){
cout << "hello world" << endl;
}
//定义函数calculate
double calculate(double a){
return a * 2.5;
}
5.类的声明和定义
5.1类的声明
//类声明:告诉编译器有这个类存在
class Student;//前向声明类 Student
5.2类的定义和成员函数的声明
// 定义类 Student
class Student{
public:
string name;
int age;
void study();//声明成员函数 study
void showInfo();//声明成员函数 showInfo
};
5.3成员函数的定义
void Student::study(){
cout << name << "正在学习" << endl;
}
void Student::showInfo(){
cout << "name:" << name << "age" << age << endl;
}
5.4调用成员成员函数
int main() {
Student student;
student.name = "张三";
student.age = 24;
student.study();
student.showInfo();
return 0;
}