ToB企服应用市场:ToB评测及商务社交产业平台

标题: C++学习路线(二十四) [打印本页]

作者: 用户云卷云舒    时间: 2024-10-27 13:08
标题: C++学习路线(二十四)
静态成员函数

类的静态方法:
1.可以直接通过类来访问【更常用】,也可以通过对象(实例)来访问。
2.在类的静态方法中,不能访问普通数据成员和普通成员函数(对象的数据成员和成员函数)
1)静态数据成员
可以直接访问“静态数据成员”对象的成员函数(没有static的成员函数)内部,类的静态成员函数(有static的成员函数)内部,可以直接访问“静态数据成员”即:所有的成员函数,都可以访问静态数据成员。类不能直接访问普通的静态数据成员(Human::humanCount非法)
2)静态成员函数
对象可以直接访问静态成员函数
类可以直接访问静态成员函数(Human::getHumanCount())在类的静态成员函数(类的静态方法)内部,不能直接访问this指针和对象的数据成员!在类的静态成员函数(类的静态方法)内部,只能访问类的数据成员。
const数据成员

定义为const数据范例(常量数据成员)const数据成员的初始化方式
1.使用类内值(C++11支持)


我们猜一猜 类内初始值=2 然后再类外进行初始值定义 以哪个为准?
答案是报错,多次初始化

2.使用构造函数的初始化列表(如果同时使用这两种方式,以初始化列表中的值为最终初始化结果)
注意:不能在构造函数或其他成员函数内,对const成员赋值!


const static int count 可以类内赋值 ,但是去掉了const 就不可以了,得在类外赋值
组合和聚合

说明:组合和聚合,不是C++的语法要求,是应用中的常用手段
组合

构建一个计算机类,一台计算机,由CPU芯片,硬盘,内存等组成。
CPU 芯片也使用类来表示。
CPU.h
  1. #pragma once
  2. #include <string>
  3. class CPU {
  4. public:
  5.         CPU(const char* brand = "intel", const char* version = "i5-7500");
  6.         ~CPU();
  7. private:
  8.         std::string brand;
  9.         std::string version;
  10. };
复制代码
CPU.cpp
  1. #include "CPU.h"
  2. #include <iostream>
  3. CPU::CPU(const char* brand, const char* version) {
  4.         this->brand = brand;
  5.         this->version = version;
  6.         std::cout << __FUNCTION__ << " called" << std::endl;
  7. }
  8. CPU::~CPU() {
  9.         std::cout << __FUNCTION__ << " called" << std::endl;
  10. }
复制代码
Computer.h
  1. #pragma once
  2. #include "CPU.h"
  3. class Computer {
  4. public:
  5.         Computer(const char* cpuBrand , const char* cpuVersion , int harDisk , int memory);
  6.         ~Computer();
  7. private:
  8.         CPU cpu;
  9.         int hardDisk;
  10.         int memory;
  11. };
复制代码
Computer.cpp
  1. #include <iostream>
  2. #include "Computer.h"
  3. Computer::Computer(const char* cpuBrand, const char* cpuVersion, int hardDisk, int memory) : cpu(cpuBrand, cpuVersion) {
  4.     this->hardDisk = hardDisk;
  5.     this->memory = memory;
  6.     std::cout << __FUNCTION__ << " called" << std::endl;
  7. }
  8. Computer::~Computer() {
  9.     std::cout << __FUNCTION__ << " called" << std::endl;
  10. }
复制代码
main.cpp
  1. #include <iostream>
  2. #include "Computer.h"
  3. int main() {
  4.         Computer computer1("intel" , "i9" , 1000 , 8);
  5.        
  6. }
复制代码

聚合

给计算机配备一台音响
Computer.h
  1. #pragma once
  2. #include "CPU.h"
  3. class VoiceBox {
  4. public:
  5.         VoiceBox() {}
  6.         ~VoiceBox() {}
  7.         void VioceBoxOn() {
  8.                 std::cout << "Voice box is on." << std::endl;
  9.         }
  10. };
  11. class Computer {
  12. public:
  13.         Computer(const char* cpuBrand , const char* cpuVersion , int harDisk , int memory);
  14.         ~Computer();
  15.         void addVoiceBox(VoiceBox* vb);
  16. private:
  17.         CPU cpu;
  18.         int hardDisk;
  19.         int memory;
  20.         VoiceBox *voiceBox;
  21. };
复制代码
Computer.cpp
  1. #include <iostream>
  2. #include "Computer.h"
  3. Computer::Computer(const char* cpuBrand, const char* cpuVersion, int hardDisk, int memory) : cpu(cpuBrand, cpuVersion) {
  4.     this->hardDisk = hardDisk;
  5.     this->memory = memory;
  6.     std::cout << __FUNCTION__ << " called" << std::endl;
  7. }
  8. Computer::~Computer() {
  9.     std::cout << __FUNCTION__ << " called" << std::endl;
  10. }void Computer::addVoiceBox(VoiceBox* voiceBox) {    this->voiceBox = voiceBox;    std::cout << __FUNCTION__ << " called" << std::endl;}
复制代码
main.cpp
  1. #include <iostream>
  2. #include "Computer.h"
  3. int main() {
  4.         VoiceBox voicebox1;
  5.         {
  6.                 Computer computer1("intel", "i9", 1000, 8);
  7.                 computer1.addVoiceBox(&voicebox1);
  8.         }
  9.         voicebox1.VioceBoxOn();
  10. }
复制代码

可以看到Computer这个类不能左右VoiceBox的构造和析构。
聚合不是组成关系,被包含的对象,也大概被其他对象包含拥有者,不需要对被拥有的对象的生命周期负责。
项目-相亲系统

Girl.h
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. class Boy;
  6. class Girl {
  7. public:
  8.         Girl();
  9.         Girl(std::string name, int age, int beauty);
  10.         ~Girl();
  11.         int getAge() const;
  12.         std::string getName() const;
  13.         int getBeauty() const;
  14.         std::string Description() const;
  15.         bool satisfy(const Boy& boy) const;
  16.         static void inputsGirls(std::vector<Girl>& girls);
  17. private:
  18.         std::string name;
  19.         int age;
  20.         int beauty;
  21. };
复制代码
Boy.h
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. class Girl;
  5. class Boy {
  6.         Boy();
  7.         Boy(std::string name, int age, int beauty);
  8.         ~Boy();
  9.         int getAge() const;
  10.         int getBeauty() const;
  11.         std::string Description() const;
  12.         bool satisfy(const Boy& other);
  13.         static void inputsBoys(std::vector<Boy>& boys);
  14. private:
  15.         std::string name;
  16.         int age;
  17.         int beauty;
  18. };
复制代码
Girl.cpp
  1. #include "girl.h"
  2. #include "boy.h"
  3. #include <string>
  4. #include <sstream>
  5. #define YANZHI_FACTOR 1.1
  6. Girl::Girl(){}
  7. Girl::Girl(std::string name, int age, int beauty) {
  8.     this->name = name;
  9.     this->age = age;
  10.     this->beauty = beauty;
  11. }
  12. Girl::~Girl(){}
  13. int Girl::getAge() const {
  14.     return age;
  15. }
  16. std::string Girl::getName() const {
  17.     return name;
  18. }
  19. int Girl::getBeauty() const {
  20.     return beauty;
  21. }
  22. std::string Girl::Description() const {
  23.     std::stringstream ss;
  24.     ss << name << "颜值" << beauty << " 年龄" << age;
  25.     return ss.str();
  26. }
  27. bool Girl::satisfy(const Boy& boy) const {
  28.     if(boy.getSalary() >= beauty * YANZHI_FACTOR) return true;
  29.     return false;
  30. }
  31. void Girl::inputsGirls(std::vector<Girl>& girls) {
  32.     int age;
  33.     std::string name;
  34.     int beauty;
  35.     int n = 1;
  36.     while (1) {
  37.         std::cout << "请输入第" << n << "位女孩的信息(输入0结束输入):" << std::endl;
  38.         std::cout << "年龄:" << std::endl;
  39.         std::cin >> age;
  40.         if (age == 0) break;
  41.         std::cout << "姓名:" << std::endl;
  42.         std::cin >> name;
  43.         std::cout << "颜值:" << std::endl;
  44.         std::cin >> beauty;
  45.         n++;
  46.         girls.push_back(Girl(name, age, beauty));
  47.     }
  48. }
复制代码
Boy.cpp
  1. #include <sstream>
  2. #include <string>
  3. #include "boy.h"
  4. #include "girl.h"
  5. #define SALARY_FACTOR 0.005
  6. Boy::Boy(){}
  7. Boy::Boy(std::string name, int age, int salary) {
  8.     this->name = name;
  9.     this->age = age;
  10.     this->salary = salary;
  11. }
  12. Boy::~Boy(){}
  13. std::string Boy::getName() const {
  14.     return name;
  15. }
  16. int Boy::getAge() const {
  17.     return age;
  18. }
  19. int Boy::getSalary() const {
  20.     return salary;
  21. }
  22. std::string Boy::Description() const {
  23.     std::stringstream ss;
  24.     ss << "Name: " << name << ", Age: " << age << ", Salary: " << salary;
  25.     return ss.str();
  26. }
  27. void Boy::inputsBoys(std::vector<Boy>& boys) {
  28.     int n = 1;
  29.     std::string name;
  30.     int age;
  31.     int salary;
  32.     while (1) {
  33.         std::cout << "请输入第" << n << "位男孩的信息(输入0结束输入):" << std::endl;
  34.         std::cout << "年龄:" << std::endl;
  35.         std::cin >> age;
  36.         if (age == 0) break;
  37.         std::cout << "姓名:" << std::endl;
  38.         std::cin >> name;
  39.         std::cout << "工资:" << std::endl;
  40.         std::cin >> salary;
  41.         boys.push_back(Boy(name, age, salary));
  42.         n++;
  43.     }
  44. }
  45. bool Boy::satisfy(const Girl& girl) {
  46.     if (girl.getBeauty() >= salary * SALARY_FACTOR) {
  47.         return true;
  48.     }
  49.     else return false;
  50. }
复制代码
main.cpp
  1. #include <string>
  2. #include <iostream>
  3. #include "boy.h"
  4. #include "girl.h"
  5. using namespace std;
  6. void autoPair(vector<Boy>& boys, vector<Girl>& girls) {
  7.         for (Boy& b : boys) {
  8.                 for (Girl& g : girls) {
  9.                         if (b.satisfy(g) && g.satisfy(b)) {
  10.                                 cout << b.Description() << " and " << g.Description() << endl;
  11.                         }
  12.                 }
  13.         }
  14. }
  15. int main() {
  16.         vector<Boy> boys;
  17.         vector<Girl> girls;
  18.         Boy::inputsBoys(boys);
  19.         Girl::inputsGirls(girls);
  20.         cout << "pairs\n" << endl;
  21.         autoPair(boys, girls);
  22. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4