类 对象
主要内容
数据成员
成员函数
构造函数
析构函数
对象复制
对象赋值
对象数组
对象指针
this指针
从面向过程 —> 面向对象
访问限定符
public
protected
private
对象实例化
从栈中
从堆中
对象成员的访问
从栈中 .
从堆中 ->
代码
#include <iostream>
using namespace std;
class Coordinate {
public:
int x;
int y;
void printX() {
cout << x << endl;
}
void printY() {
cout << y << endl;
}
};
int main() {
Coordinate coor;
coor.x = 10;
coor.y = 20;
coor.printX();
coor.printY();
Coordinate *p = new Coordinate();
if (NULL == p) {// 做判断
return;
}
p->x = 100;
p->y = 200;
p->printX();
p->printY();
delete p;
p = NULL;
return 0;
}
char[]操作频繁,但是麻烦 —> string
初始化方式
属性
方法
代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "请输入你的名字:";
getline(cin, name);// 可以读到空串
if(name.empty()) {
cout << "输入为空" << endl;
return 0;
}
if("admin" == name) {
cout << "管理员" << endl;
}
cout << "hello " + name << endl;
cout << "你名字的长度:" << name.size() << endl;
cout << "名字的第一个字母:" << name[0] << endl;
return 0;
}
封装代码
m_strName; //成员变量不初始化是随机值 命名规则
m_iAge;
public写在前,调用者想看
private写在后
代码
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
void setName(string name) {
m_strName = name;
}
string getName(){
return m_strName;
}
void setGender(string gender) {
m_strGender = gender;
}
string getGender() {
return m_strGender;
}
int getScore() {
return m_iScore;
}
void initScore() {
m_iScore = 0;
}
void study(int score) {
m_iScore += score;
}
private:
string m_strName;
string m_strGender;
int m_iScore;
};
int main() {
Student stu;
stu.initScore();
stu.setName("zhangsan");
stu.setGender("女");
stu.study(5);
stu.study(3);
cout << stu.getName() << " " << stu.getGender() << " " << stu.getScore() << endl;
return 0;
}
内联函数
直接替换代码,而不是去调用
类内定义
函数优先编译成内联函数
类外定义
同文件类外定义
定义和实现都在一个文件
代码
#include <iostream>
#include <string>
using namespace std;
class Teacher {
public:
string getName();
void setName(string);
int getAge();
void setAge(int);
void teach();
private:
string m_strName;
int m_iAge;
};
string Teacher::getName() {
return m_strName;
}
void Teacher::setName(string name) {
m_strName = name;
}
int Teacher::getAge() {
return m_iAge;
}
void Teacher::setAge(int age) {
m_iAge= age;
}
void Teacher::teach() {
cout << "正在上课~" << endl;
}
int main() {
Teacher t;
t.setAge(30);
t.setName("孔子");
cout << t.getName() << " " << t.getAge() << endl;
t.teach();
return 0;
}
分文件类外定义
大多都这样写
Teacher.h:定义
#ifndef TEACHER_H
#define TEACHER_H
#include <string>
#include <iostream>
using namespace std;
class Teacher {
public:
string getName();
void setName(string);
int getAge();
void setAge(int);
void teach();
private:
string m_strName;
int m_iAge;
};
#endif // TEACHER_H
Teacher.cpp:实现
#include "Teacher.h"
string Teacher::getName() {
return m_strName;
}
void Teacher::setName(string name) {
m_strName = name;
}
int Teacher::getAge() {
return m_iAge;
}
void Teacher::setAge(int age) {
m_iAge= age;
}
void Teacher::teach() {
cout << "正在上课~" << endl;
}
对象构造
内存分区
栈区:系统来控制
堆区:程序员自己管理
全局区:全局变量和static
常量区:字符串,常量
代码区:编译后的二进制代码 只有一份
构造函数
#ifndef TEACHER_H
#define TEACHER_H
#include <string>
#include <iostream>
using namespace std;
class Teacher {
public:
Teacher();
Teacher(string name, int age = 20);
Teacher::Teacher() {
m_strName = "Guo";
m_iAge = 5;
cout << "Teacher()" << endl;
}
Teacher::Teacher(string name, int age) {
m_strName = name;
m_iAge = age;
cout << "Teacher(string, int)" << endl;
}
Teacher t1;
Teacher t2("Merry", 15);
Teacher t3("Cheng");
默认构造函数
不需要传递构造函数
初始化列表
先与构造函数
只能用于构造函数
可以初始化多个
private:
string m_strName;
int m_iAge;
const int m_iMax;
Teacher::Teacher(string name, int age, int m):m_strName(name), m_iAge(age), m_iMax(m) {
cout << "Teacher(string, int)" << endl;
}
拷贝构造函数
拷贝函数时,自动调用
Teacher(const Teacher &);
Teacher::Teacher(const Teacher &tea) {
cout << "Teacher(const Teacher &tea)" << endl;
}
void test(Teacher t) {
}
int main() {
Teacher t1;
Teacher t2 = t1;// 新的内存空间
Teacher t3(t1);
test(t1);
return 0;
}
析构函数
对象销毁时,自动调用
~Teacher();
Teacher::~Teacher() {
cout << "~" << endl;// 用于释放new出来的内存空间
}