C++
I,Pencil
这个作者很懒,什么都没留下…
展开
-
多继承与虚基类
多继承多继承中的二义性问题虚基类虚基类的构造函数原创 2021-01-22 10:23:21 · 165 阅读 · 0 评论 -
C++(OOP)命名空间
命名空间的定义每个命名空间是一个作用域命名空间可以是不连续的接口和现实的分离嵌套命名空间命名空间成员的使用using std::vector;using primer = cplusplus_primer;using namespace std;原创 2021-01-20 17:25:04 · 166 阅读 · 2 评论 -
C++(OOP)标准异常
exceptionruntime_errorrang_erroroverflow_errorunderflow_errorlogic_errordomain_errorinvalid_argumentlength_errorout_of_range原创 2021-01-20 15:45:24 · 164 阅读 · 0 评论 -
C++(OPP)异常层次结构
异常层次结构异常是类 可以自己创建异常派生异常中的数据:数据成员按引用传递异常在异常中使用虚函数原创 2021-01-19 16:32:47 · 166 阅读 · 0 评论 -
C++(OOP)异常
传统处理方法C++处理异常方法trycatchthrow异常类型数字字符串类对象#include <stdio.h>#include <iostream>using namespace std;class BadSrcFile{};class BadDestFile{};class BadCopy{};int my_cp(const char * src_file, const char * dest_file){ FILE *.原创 2021-01-15 22:13:01 · 2600 阅读 · 5 评论 -
C++(OOP)函数模板
函数模板 → 实例化 → 函数模板形参四个例子原创 2021-01-15 21:09:09 · 464 阅读 · 0 评论 -
C++(OOP)模板与泛型编程
两种模板类模板、函数模板泛型编程主要用于容器、迭代器、算法 -> C++ STL 标准模板库示例1.普通队列2.C++中的泛型容器:vector、list、set 等等3.顺序队列4.链式队列原创 2021-01-14 17:03:27 · 212 阅读 · 0 评论 -
C++(OOP)纯虚函数与抽象类
纯虚函数与抽象类虚函数抽象类 抽象数据类型任何包含一个或多个纯虚函数的类都是抽象类不要创建这个类的对象,应该继承它务必覆盖从这个类继承的纯虚函数实现纯虚函数C++接口只包含纯虚函数的抽象基类...原创 2021-01-13 10:53:03 · 110 阅读 · 0 评论 -
C++(OOP)静态成员与继承
基类中的static成员,在整个继承层次中只有一个实例在派生类中访问static成员的方法基类名::成员名子类名::成员名对象.成员名指针->成员名成员名原创 2021-01-12 09:12:58 · 180 阅读 · 0 评论 -
C++(OOP)友元与继承
1原创 2021-01-12 08:53:58 · 188 阅读 · 0 评论 -
C++(OOP)派生类构造函数和析构函数
#include <iostream>using namespace std;class Base{public: Base() { b1 = b2 = 0; } Base(int i, int j); ~Base(); void Print() { cout << b1 << ", " << b2 << ", "; }private: int b1, b2;};Base::Base(int i, int原创 2021-01-10 19:00:42 · 138 阅读 · 0 评论 -
C++(OOP)三种继承
三种继承公有、私有、受保护继承class B : public Aclass B : private Aclass B : protected A修改继承访问using A::a1原创 2021-01-04 11:45:57 · 95 阅读 · 0 评论 -
C++(OOP)动态绑定
动态绑定多态从派生类到基类的转换只有通过引用或指针调用虚函数才会发送动态绑定引用或指针既可以指向基类对象,也可以指向派生类对象原创 2021-01-04 10:40:34 · 123 阅读 · 1 评论 -
C++(OOP):定义基类和派生类
#include <iostream>using namespace std;// 基类class Animal{ // 成员略};// 继承 or 派生class Dog: public Animal{ // 成员略};// 继承 or 派生class Cat: public Animal{ // 成员略};int main(){ Animal A; Dog B; Cat C; return 0;}#include <i原创 2021-01-03 15:12:12 · 360 阅读 · 0 评论 -
C++(OOP):转换类型操作符
转换类型操作符必须是成员函数不能是指定返回类型形参表必须是空必须显示的返回一个指定类型的值不应该改变被转换对象,通常定义为const#include <iostream>#include <string>using namespace std;class Dog{public: Dog(string n, int a, double w):name(n),age(a),weight(w){} // 转换操作符 // 必须是成员函数 // 不能原创 2021-01-03 14:10:44 · 85 阅读 · 0 评论 -
C++(OOP)重载操作符
重载操作符的定义#include <iostream>using namespace std;class Person{ // 重载操作符: 加法public: // 重载函数必须重载 类类型 // 定义重载操作符 void operator+(const Person& lhs) { cout << "执行了重载的加法操作" << endl; } // 最好不要重载 && || & *++(OOP)}原创 2020-12-28 15:37:02 · 137 阅读 · 0 评论 -
C++(OOP)深复制、浅复制
案例#include <iostream>#include <string.h>using namespace std;class Demo{public: Demo(int pa, char *cstr) { this->a = pa; this->str = new char[1024]; strcpy(this->str, cstr); }public: int a; char *str;};int main().原创 2020-12-24 16:30:06 · 116 阅读 · 2 评论 -
基数排序(C++实现)
基数排序#include <iostream>#include <list>using namespace std;int maxdigit(int data[], int n){ int d = 1; // 计位器: 1 1位数 2 两位数 int p = 10; // 倍数 // 求数组中最高位的的位数 for (int i = 0; i < n; ++i) { while (data[i] >= p) { p *= 10;原创 2020-12-24 08:59:24 · 1715 阅读 · 0 评论 -
C++(OOP)析构函数
析构函数构造函数用来创造资源,析构函数用来释放资源没有自定义析构函数,C++自动创建析构函数,但创建的析构函数功能有限调用过程#include <iostream>#include <string>using namespace std;class NoName{public: // 构造函数 NoName():pstring(new std::string),i(0),d(0){ cout << "构造函数被调用了" <<原创 2020-12-23 16:21:44 · 63 阅读 · 1 评论 -
C++(OOP)复制构造函数和赋值操作符
赋值构造函数如果没有定义,C++会给我们定义使用自定义构造函数的情况: 类成员中存在指针赋值操作符如果没有定义,C++会给我们定义使用自定义赋值操作符的情况: 类成员中存在指针#include <iostream>#include <string>#include <vector>using namespace std;class Sales_item{public: // 公有成员 // 构造函数 Sales_item():un原创 2020-12-23 11:48:12 · 83 阅读 · 0 评论 -
链式队列(C++实现)
/* LinkQueue.h */#ifndef LINKQUEUE_H_#define LINKQUEUE_H_template<class T>class Queue{public: Queue(); ~Queue(); bool isEmpty() const; const T & getFront() const; // 查看队头 void enqueue(const T & x); // 入队 T dequeue(); // 出队 voi原创 2020-12-21 15:33:33 · 300 阅读 · 0 评论 -
C++(OOP)static 类成员
static 类成员的优点static 静态类成员定义以后所有这个类的对象都能使用,比全局变量安全,比私有成员方便易用管理#include <iostream>#include <string>using namespace std;class Account {public: Account(std::string name, double money): owner(name),amount(money){} double getAmount() c原创 2020-12-21 14:47:28 · 109 阅读 · 0 评论 -
链栈(C++实现)
/* LinkStack.h */#ifndef LINKSTACK_H#define LINKSTACK_Htemplate<class T> class LinkStack;template<class T>class ChainNode{public: friend class LinkStack<T>;private: ChainNode(const T& theData, ChainNode *n = 0):data(theDat原创 2020-12-17 17:23:42 · 198 阅读 · 0 评论 -
C++(OOP)友元
类的友元函数普通函数无法使用类的私有成员友元函数可以使用类的私有成员#include <iostream>#include <string>using namespace std;class Person{public: Person(int ag, std::string nm):name(nm),age(ag){}; /* 定义友元函数 */ /* 友元函数可以调用类的私有变量 */ friend int getAge(Person &);p原创 2020-12-17 16:12:05 · 96 阅读 · 0 评论 -
C++(OOP)构造函数
设计一个类的时候必须写上一个构造函数#include <iostream>#include <string>using namespace std;class Person{public: /* 设计一个类的时候必须写上一个构造函数 */ Person(const std::string &nm, int a):name(nm),age(a){}private: std::string name; int age;};int main(){原创 2020-12-15 13:32:11 · 102 阅读 · 0 评论 -
C++(OOP)类的作用域
类的作用域#include <iostream>using namespace std;class First{public: /* 一般情况下数据成员不要写在public里面 */ /* 这里是为了举例子 */ int number; void doA() { number = 22; }};int main(){ /* number的作用域是在类内 */ //number = 11; // ERROR! First obj; obj.nu原创 2020-12-13 11:19:09 · 103 阅读 · 0 评论 -
C++(OOP)隐含的this指针
this指针的用法:在类中使用this可以让代码更加清晰易读#include <iostream>#include <string>using namespace std;/* 定义一个Person类 */class Person{public: Person(const std::string &name, const std::string address) { /* this指代当前对象 可以省略 */ /* 如果写上的话代码看起来更加清原创 2020-12-12 13:47:40 · 99 阅读 · 0 评论 -
C++(OOP)类定义详解(3)
#include <iostream>#include <string>using namespace std;class Screen; // 只是类声明 class LinkScreen{ Screen *window; LinkScreen *next; LinkScreen *prev;};class Y;class X{ // 各种成员忽略private: Y *ptr;};class Y{ // 各种成员略private:原创 2020-12-11 09:14:07 · 82 阅读 · 0 评论 -
C++(OOP)类定义详解(2)
类里面可以 - 定义成员函数类里面可以 - 使用别名简化#include <iostream>#include <string>using namespace std;/* 定义一个Screen类 */class Screen{public: /* 类中可以使用别名 作用域是整个类 */ typedef std::string::size_type index; Screen (index ht = 0, index wd = 0):contents(ht *原创 2020-12-10 11:29:50 · 91 阅读 · 0 评论 -
C++(OOP)类定义详解(1)
1原创 2020-12-10 10:27:34 · 143 阅读 · 0 评论 -
C++详解(7) 字符串流
基本使用#include <iostream>#include <sstream>#include <string>using namespace std;int main(){ // 字符串流是在内存内进行操作 ostringstream oss; // 定义一个字符串输入流 oss << "hello world!" << endl; cout << oss.str() << endl;.原创 2020-12-08 15:44:06 · 314 阅读 · 0 评论 -
c++实践小函数经验积累(一)
读取文件输入并判断是否合法// get.cc# include "get.h"std::istream& get(std::istream& in){ int ival; // 类型可以替换 可以修改成模板函数 while(in >> ival, !in.eof()) { if (in.bad()) throw std::runtime_error("IO stream corrupted"); if (in.fail()) { st.原创 2020-12-07 23:27:17 · 93 阅读 · 0 评论 -
顺序队列(C++实现)
// Queue.h#ifndef _QUEUE_H#define _QUEUE_Htemplate<class T>class Queue{ public: Queue(int queueCapacity=10); bool IsEmpty() const; // 判空 T& Front() const; // 头索引 T& Rear() const; // 尾索引 void Push(const T& item);原创 2020-12-07 16:53:10 · 327 阅读 · 0 评论 -
C++详解(6) 文件模式
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ // 提前创建好file1.txt文件并添加一些内容 string s; ifstream ifs("file1.txt", ifstream::in); // 同 ifstream ifs("file1.txt"); ifs >> s; // 显示file1..原创 2020-12-06 23:54:53 · 351 阅读 · 0 评论 -
C++详解(5) 文件流对象的使用
写文件#include <iostream>#include <fstream>using namespace std;int main(){ ofstream outfile("test.txt"); // 创建一个名为test.txt的文件 outfile << "hello file"; // 向文件中写入hello file outfile.close(); // 关闭文件流对象 return 0;}读文件#include.原创 2020-12-03 15:55:16 · 1096 阅读 · 0 评论 -
C++详解(4) 标准IO库:IO流的条件状态
IO流的条件状态// 省略头文件int main(){ // cin的4中状态 cout << "检查cin的状态" << endl; // cin.bad() if (cin.bad()) // 系统级的错误才会bad cout << "cin bad()" << endl; else cout << "cin is not bad()" << endl; // cin.fail() if (ci.原创 2020-12-01 15:49:05 · 304 阅读 · 0 评论 -
归并排序算法(C++实现)
1原创 2020-11-30 18:16:24 · 267 阅读 · 0 评论 -
C++详解(3) 标准IO库:面向对象(IO流)的标准库
基本使用// 省略头文件int main(){ std::cout << "cout是标准输入库ostream输入流对象" << std::endl; // 文件头加入 using namespace std // 可以省略 命名空间std cout << "cout是标准输入库ostream输入流对象" << endl; return 0;}IO对象不支持赋值// 省略头文件int main(){ std::of.原创 2020-11-30 16:05:31 · 223 阅读 · 0 评论 -
C++详解(2) 指向函数的指针
// 省略头文件bool compare(const int &i1, const int &i2){ return i1 == i2;}int main(){ bool (*pf)(const int &, const int &); // pf 是一个指针,指向函数的指针 pf = &compare; // 这里可以去掉取址符& pf = compare; /原创 2020-11-27 10:12:43 · 185 阅读 · 0 评论 -
快速排序算法(C++实现)
#include <iostream>using namespace std;// left和right代表数组的下标template<class T>void QuickSort(T *a, const int left, const int right){ if (left < right) { // 选枢轴划分 int i = left; int j = right + 1; // 为什么要加1 int pivot = a[left];原创 2020-11-25 16:03:37 · 128 阅读 · 0 评论