![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
文章平均质量分 55
C++
入木
抬头看 天空是蓝色的
展开
-
多态1...
多态的概念通俗来说就是多种形态,具体就是去完成某个行为,当不同的对象去完成时会产生出不同的状态多态两个条件:1、子类重写父类的虚函数2、必须是父类的指针或者引用去调用虚函数(指向谁就调用谁的虚函数)class Person{public: virtual void BuyTicket()//虚函数 { cout << "正常排队-全价买票" << endl; }protected: int _age; string _name;};class Stu原创 2022-03-04 15:05:43 · 207 阅读 · 0 评论 -
继承2...
继承与友元友元关系不能被继承,父类和子类的友元要分别去处理class Student;class Person{public: friend void Display(const Person& p, const Student& s);protected: string _name; // 姓名};class Student : public Person{ friend void Display(const Person& p, const Stud原创 2022-03-04 13:39:11 · 642 阅读 · 0 评论 -
继承...
赋用赋用访问私有成员变量不好处理,所以面向对象有了继承的方式1、 继承基类(父类)成员访问方式的变化#include<iostream>#include <string>using namespace std;class Person{public: void Print() { cout << "name:" << _name << endl; cout << "age:" << _age原创 2022-02-23 21:22:22 · 247 阅读 · 0 评论 -
函数 / 类模板--模板2
这样N开20000满足st2但是对st1太大,st1只需要100则st1会浪费很多空间#include <iostream>#include <array>#include <vector>#include <queue>#include <functional>using namespace std;// 静态的栈--不需要capacity#define N 20000template<class T>clas..原创 2021-12-10 21:13:02 · 405 阅读 · 0 评论 -
priority_Queue优先级队列-- 容器适配器
priority_Queue–没有template模板单纯实现随意形成大堆和小堆#pragma once#include <vector>namespace bit{ template<class T> class Less//反函数--重载一个运算符 { public: //重载运算符()--函数名(形参表) bool operator()(const T& x, const T& y) { return x < y; .原创 2021-12-09 22:53:35 · 694 阅读 · 0 评论 -
栈和队列--容器适配器
stack.cpp#include <iostream>#include <stack>#include <queue>#include <forward_list>#include <algorithm>#include <list>using namespace std;//栈--后进先出void test_stack(){ stack<int> st; st.push(1); st.push(原创 2021-12-08 20:14:14 · 698 阅读 · 0 评论 -
List的模拟实现
insert迭代器需不需要实现析构、拷贝构造、赋值重载不需要默认生成就好了,因为节点属于链表的,不是迭代器的原创 2021-12-07 20:55:59 · 495 阅读 · 0 评论 -
List..
List的使用test.cpp#include <iostream>#include <list>#include <vector>using namespace std;namespace std{ void test_list1() { //初始化 list<int> lt1; //10个5 list<int> lt2(10, 5); //自己的迭代器来初始化 list<int> lt3(l原创 2021-11-11 22:23:11 · 694 阅读 · 0 评论 -
vector的模拟实现
vector.h#pragma oncenamespace bit{ template<class T> class vector { public: //迭代器 typedef T* iterator; typedef const T* const_iterator; const_iterator begin() const { return _start; } const_iterator end() const { return .原创 2021-11-11 21:30:41 · 2697 阅读 · 0 评论 -
vector
vector的重点构造函数vector就是一个顺序表vector是一个顺序容器,是一个大小(size)可以改变的数组遍历void test_vector1(){ vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); //遍历 // 1、下标+[] for (size_t i = 0; i < v.size(); ++i)原创 2021-11-06 21:38:55 · 763 阅读 · 0 评论 -
string的模拟实现
string.h#pragma once#include<assert.h>namespace bit{ class string { public: //默认 /*string() :_str(new char[1]) , _size(0) , _capacity(0) { *_str = '\0'; }*/ //全缺省 //string(char* str = nullptr)--err,当什么都不传的时候下面strlen会对空指针解引用原创 2021-11-05 16:17:33 · 91 阅读 · 0 评论 -
深、浅拷贝
浅拷贝string.h#pragma oncenamespace bit{ class string { public: string(char* str) :_str(new char[strlen(str) + 1]) { strcpy(_str, str); } ~string() { delete[] _str; _str = nullptr; } char& operator[](size_t pos) { r.原创 2021-11-04 21:00:55 · 98 阅读 · 0 评论 -
String重点接口
SLT的六大组件编码String多看文档官网可代替官网1、char和wchar_t#include <string>int main(){ cout << sizeof(char) << endl; cout << sizeof(wchar_t) << endl;}2、String构造#include<string.h>int main(){ string s1; string s2("原创 2021-10-30 10:22:55 · 111 阅读 · 0 评论 -
函数模板+类模板
函数模板Swap模板函数模板实例化过程模板的推演,推演T的具体类型是什么再实例化生成具体的函数库里有Swap函数以后直接用//template<class T>//template<class T1, class T2, class T3>//template<typename T>template<class T>void Swap(T& x1, T& x2){ //T tmp = x1; T tmp(x1);原创 2021-10-24 17:04:33 · 80 阅读 · 0 评论 -
动态内存管理(new / delete)
c/c++中程序内存区域划分这几个区域堆是很大从图里面看栈和堆差不多大,实际上不是。栈很小,linux下一般8M,所以递归调用深度太深,会导致栈溢出数据段和代码段也不是多大,因为没有多少数据malloc、calloc、realloc的区别calloc等价于malloc+memset(0)即开空间+初始化realloc是对malloc或calloc的空间进行扩容void Test (){int* p1 = (int*) malloc(sizeof(int));free(p1);原创 2021-10-24 15:55:22 · 97 阅读 · 0 评论 -
初始化列表+static+友元函数+内部类--类和对象5
只能初始化-b(缺省值,给了就不用)为1,-a为0class A{public: A(int a = 0)//不写=0会导致B自定义类型出错 { _a = a; } }private: int _a;};class B{public:private: int _b = 1; // 内置类型 A _aa; // 自定义类型};int main(){ B b; return 0;}初始化为任意值class A{public: A(int a =.原创 2021-10-22 18:46:20 · 180 阅读 · 0 评论 -
日期加减天数得到规范的天数--类和对象的运用4
判断日期是否合法Date.h#include <iostream>#include <assert.h>using namespace std;class Date{public: // 获取某年某月的天数 int GetMonthDay(int year, int month) { assert(month > 0 && month < 13); //频繁调用后被重复定义需要开很多空间,但加了static放到静态区就可以不用重原创 2021-10-18 16:18:54 · 239 阅读 · 0 评论 -
赋值运算符重载 --类和对象3
运算符重载–函数实现函数名:operator+运算符参数:操作符的操作数有几个就有几个参数,参数类型你要操作的类型对象返回值:看运算符运算后的返回值是什么运算符默认都是给内置类型变量用的, 自定义类型的变量想用这些运算符,得自己进行运算符重载运算符重载的意思就是我们的自己去写一个函数定义实现这里运算符的行为#include<iostream>using namespace std;class Date{public: Date(int year = 0, int mon原创 2021-10-14 17:02:41 · 362 阅读 · 0 评论 -
构造函数+析构函数+拷贝构造函数 --类和对象2
空类// 没有成员变量的类class B{public: void Print() {}};// 空类class C{};int main(){ cout << sizeof(B) << endl; cout << sizeof(C) << endl; C c1, c2; return 0;}原创 2021-10-12 18:52:23 · 153 阅读 · 0 评论 -
类和对象1
c++兼容c原创 2021-10-12 18:08:45 · 94 阅读 · 0 评论 -
内联+auto(基础) --c++基础知识点3
内联c–宏缺点:语法复杂、注意的细节多,易出错;没有类型安全的检查;不能调试;代替宏:const、enum、inline原创 2021-10-10 20:34:35 · 142 阅读 · 0 评论 -
引用 --C++基础知识点2
extern “c”引用引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间。#include <iostream>using namespace std; // C++标准库的东西int main(){ int a = 10; int& b = a; return 0;}形参变量的改变要影响实参–指针或者引用#include <iostream>using names原创 2021-10-08 09:44:27 · 227 阅读 · 0 评论 -
命名空间、参数缺省、函数重载--C++基础知识点1
命名空间1. 普通的命名空间namespace N1N1为命名空间的名称{ // 命名空间中的内容,既可以定义变量,也可以定义函数namespace N1 // N1为命名空间的名称{ int a; int Add(int left, int right) { return left + right; } }2. 命名空间可以嵌套namespace//2. 命名空间可以嵌套namespace N2{ int a; int b; int Add(int l原创 2021-09-27 12:38:19 · 148 阅读 · 0 评论