面向过程 :C语言

优点:

  1. 程序结构简单
  2. 把问题简单化
  3. 对整个逻辑结构清晰

缺点:

  1. 数据与操作往往是分离的
  2. 数据不具有封装性
  3. 多人写作开发时不清楚别人的逻辑

面向对象:c++

  1. 将数据和行为结合在一起
  2. 封装性、继承、多态
  3. 代码维护方便、拓展性好、代码重用技术
  4. 高内聚、低耦合

vector 容器

基本特性:

  • 动态大小vector 的大小可以根据需要自动增长和缩小。
  • 连续存储vector 中的元素在内存中是连续存储的,这使得访问元素非常快速。
  • 可迭代vector 可以被迭代,你可以使用循环(如 for 循环)来访问它的元素。
  • 元素类型vector 可以存储任何类型的元素,包括内置类型、对象、指针等。

使用场景:

  • 当你需要一个可以动态增长和缩小的数组时。
  • 当你需要频繁地在序列的末尾添加或移除元素时。
  • 当你需要一个可以高效随机访问元素的容器时。

e.g. std::vector<int> myVector; // 创建一个存储整数的空 vector
就是帮我们封装好了,不用自己搞加加减减扩容什么的了

class 类定义

类是一种用户自定义数据类型,可以看作 C 语言结构体的升级版。

//跟C语言差不多,不过是可以在里面定义函数了
/*定义方式:
class 类名 {
访问属性:
	类中成员,可以定义变量或函数;
};
通过类定义的变量叫对象
*/
 
class Student {
public:
	int id;
	int score;
private:
	char name[];
};//函数没有分号,结构体和类有分号
class Class {
	std::vector<Student> array;
	int getSum {
		int sum = 0;
		for (int i = 0; i< array.size(); i++) {
			sum+=array[i].score;
		}
	}
};
 
//封装一个字符串
class Str {
	char* array;//不能直接定义一个char array[],string应当能自动扩容
	void init(const char* str) {
		array = new char[strlen(str) + 1];//申请内存空间,"+1"是为了包含\0
		for (int i = 0; i < strlen(str); i++){
			array[i]= str[i]
		}
		array[strlen(str)]= '\0';
	}
};
 
//使用时跟结构体差不多,但不用把 class Str 名字中 class 也带上,直接写 Str 名字就行
int main {
	Str str;
	str.init("123");//用.调用
}

类成员访问属性 :

  • public 公有属性:类外和类中都可以直接访问,类中直接用,类外. 出来用对象访问
  • protected 保护属性
  • private 私有属性:在类外不能直接访问但可以通过函数间接拿到
class Student {
private:
	int id;
	int score;
public:
	int getScore() {
		return score;
	}
};//通过调用public的函数间接取得值
Student stu;
std::cout << Stu.getScore();

类中定义的数据和函数可以在类的任意位置使用,比如调用一个变量写在前面了,变量的声明在后面是没关系的。

权限修饰符的范围是从当前开始到下一个结束。

习惯:

  • 数据成员一般放在 private 权限下
  • 函数一般放在 public 权限下

类的成员访问

  • 类对象用”.”访问
student stu;
stu.fun();
  • 类指针用”->“访问
student *p=new student;
p ->fun();

C++类和 C 语言结构体区别

  • C++类中有权限修饰符
  • C++类中可以定义函数
    C++类和 C++ 结构体区别
  • 结构体的默认访问是 public,类是 private
  • 类能写的结构体里能写
  • 习惯:如果类中只有数据成员,一般把它改为 struct

alt+enter 快速修复

简单库存管理系统

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
 
class Product {
private:
	char* name;
	double price;
	int stock;
public:
	void init(const char* _name, double _price, int _stock) {
		name = const_cast<char*> (_name);//c++版本另一种的强制转换类似 name=(char*)_name 
		price = _price;
		stock = _stock;
	}
	void display() {
		std::cout << "名称:" << name<<"\t"
			<< "价格:" << price << "\t"
			<< "库存量:" << stock << "\t\n";//一条语句可以占多行,反正是以分号结尾
	}
	char* getName() {
		return name;
	}
	void setStock(int count) {//count传正数表示加库存负表示减少
		if (stock + count < 0) {
			printf("库存不能为复数\n");
			return;
		}
		stock = stock + count;
	}
};
class Store {
private:
	std::vector<Product>array;
	int count=0;
public:
	void addProduct(Product& p) {
		array.push_back(p);//添加商品
	}
	void sellProduct(const char* name,int _count) {
		//找出对应商品
		int findIndex = -1;
		for (int i = 0; i < array.size(); i++) {
			if (strcmp(array[i].getName(), name) == 0) {//strcmp 如果返回值等于 0,则表示 str1 等于 str2。
				findIndex = i;
				break;
			}
		}
		//销售
		if (findIndex == -1) {
			printf("商品不存在\n");
		}
		else {
			array[findIndex].setStock(-1 * _count);
		}
	}
	void listProduct() {
		//printf("名称\t价格\t库存量\t");
		for (int i = 0; i < array.size(); i++) {
			array[i].display();
		}
	}
};
 
int main() {
	Product p1, p2, p3;
	p1.init("皮革",14, 30);
	p2.init("西瓜", 1.5, 40);
	p3.init("机械零件", 89, 43);
	Store s;
	s.addProduct(p1);
	s.addProduct(p2);
	s.addProduct(p3);
	s.listProduct();
	s.sellProduct("皮革", 50);
	s.listProduct();
}