GitHub Repo:coderZsq.github.io
Follow: coderZsq · GitHub
Resume: https://coderzsq.github.io/coderZsq.webpack.js/#/
日常扯淡
本期没什么好扯淡的, 这篇文章留着自己看.
C++ 作者的建议
- 在C++中几乎不需要用宏, 用const和enum定义显式的常量, 用inline避免函数调用的额外开销, 用模板去刻画一组函数或类型, 用namespace去避免命名冲突.
- 不要在你需要变量之前去声明, 以保证你能立即对它进行初始化.
- 不要用malloc, new运算会做的更好.
- 避免使用void*, 指针算数, 联合和强制, 大多数情况下, 强制类型转换是设计错误的指示器.
- 尽量少用数组和C风格的字符串, 标准库中的string和vector可以简化程序.
- 更加重要的是, 试着将程序考虑为一组由类和对象表示的互相作用的概念年, 而不是一堆数据结构和一些可以拨弄的二进制.
inheritance
class A {
public:
A(){}
~A(){}
A(const A &){}
A & operator=(const A &){ return *this; }
};
class Human {
public:
void eat(string food) {
cout<<food<<endl;
}
};
class Teacher: public Human {
public:
void teach(string course) {
cout<<"i am a teacher i am teaching "<<course<<endl;
}
};
class Student: public Human {
public:
void study(string course) {
cout<<"i am student i am learing "<<course<<endl;
}
};
class Worker: public Human {};
int main(int argc, const char * argv[]) {
Teacher t;
t.teach("C++");
t.eat("红烧牛肉面");
Student s;
s.study("C++");
s.eat("老坛酸菜牛肉面");
return 0;
}
#include <iostream>
#include <typeinfo>
using namespace std;
class A {
public:
A() {
cout<<"A()"<<endl;
cout<<"&a: "<<&a<<endl;
cout<<"A-this: "<<this<<endl;
cout<<"type A this: "<<typeid(this).name()<<endl;
}
int a;
};
class B: public A {
public:
B() {
cout<<"B()"<<endl;
cout<<"&b: "<<&b<<endl;
cout<<"B-this: "<<this<<endl;
cout<<"type B this: "<<typeid(this).name()<<endl;
}
int b;
};
class C: public B {
public:
C() {
cout<<"C()"<<endl;
cout<<"&c: "<<&c<<endl;
cout<<"C-this: "<<this<<endl;
cout<<"type C this: "<<typeid(this).name()<<endl;
}
int c;
};
int main(int argc, const char * argv[]) {
C cc;
cout<<"&cc: "<<&cc<<endl;
return 0;
}
A()
&a: 0x7ffeefbff5f0
A-this: 0x7ffeefbff5f0
type A this: P1A
B()
&b: 0x7ffeefbff5f4
B-this: 0x7ffeefbff5f0
type B this: P1B
C()
&c: 0x7ffeefbff5f8
C-this: 0x7ffeefbff5f0
type C this: P1C
&cc: 0x7ffeefbff5f0
Program ended with exit code: 0
subclass constructor
class C {
public:
C(int k) {
cout<<"C()"<<endl;
c = k;
};
int c;
};
class A {
public:
A(int i): a(i) {
cout<<"A()"<<endl;
}
int a;
};
class B: public A {
public:
B(int i, int j, int k): A(i), b(j), c(k) {
cout<<"B()"<<endl;
}
int b;
C c;
};
int main(int argc, const char * argv[]) {
B b(3, 5, 10);
cout<<b.a<<endl;
cout<<b.b<<endl;
cout<<b.c.c<<endl;
return 0;
}
A()
C()
B()
3
5
10
Program ended with exit code: 0
//class Birthday {
//public:
// Birthday(int y, int m, int d):_year(y), _month(m), _day(d) {}
// void disBirth() {
// cout<<"year: "<<_year<<endl;
// cout<<"month: "<<_month<<endl;
// cout<<"day: "<<_day<<endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
class Student {
public:
Student(string sn, char cs, float fs):_name(sn), _sex(cs),_score(fs) {}
void dis() {
cout<<"name: "<<_name<<endl;
cout<<"sex: "<<_sex<<endl;
cout<<"score: "<<_score<<endl;
}
private:
string _name;
char _sex;
float _score;
};
class Graduate: public Student {
public:
Graduate(string sn, char cs, float fs, float fsa/*, int y, int m, int d*/)
: Student(sn, cs, fs)/*, birth(y, m, d)*/ {
_salary = fsa;
}
void dump() {
dis();
// birth.disBirth();
cout<<"salary: "<<_salary<<endl;
}
private:
float _salary;
// Birthday birth;
};
class Doctor: public Graduate {
public:
Doctor(string sn, char cs, float fs, float fsa, string st):Graduate(sn, cs, fs, fsa) {
_title = st;
}
void disDump() {
dump();
cout<<"title: "<<_title<<endl;
}
private:
string _title;
};
int main(int argc, const char * argv[]) {
// Graduate g("zhangsan", 's', 99, 1000, 1989, 9, 9);
// g.dump();
Doctor d("zhaosi", 's', 99, 3000, "doctor zhao");
d.disDump();
return 0;
}
subclass copy constructor
//当子类, 不自实现时, 默认调用父类的拷贝构造
//若自实现, 不作特殊处理, 此时只会调用父类的构造器, 此时失去了拷贝构造的意义
//特殊处理 显示的调用父类的拷贝构造, 子类对象赋给父类的引用, 赋值兼容.
class A {
public:
A(int x = 99) {
cout<<"A()"<<endl;
a = x;
}
A(const A & another) {
this->a = another.a;
cout<<"const A & another"<<endl;
}
int a;
};
class B: public A {
public:
B(int x = 0, int y = 0): A(x) {
cout<<"B()"<<endl;
b = y;
}
B(const B & another): A(another) {
this->b = another.b;
cout<<"const B & another"<<endl;
}
int b;
};
int main(int argc, const char * argv[]) {
B b(199, 200);
B bb(b);
cout<<bb.a<<endl;
cout<<bb.b<<endl;
return 0;
}
class Birthday {
public:
Birthday(int y, int m, int d):_year(y), _month(m), _day(d) {}
Birthday(const Birthday & another) {
this->_year = another._year;
this->_month = another._month;
this->_day = another._day;
}
void disBirth() {
cout<<"year: "<<_year<<endl;
cout<<"month: "<<_month<<endl;
cout<<"day: "<<_day<<endl;
}
private:
int _year;
int _month;
int _day;
};
class Student {
public:
Student(string sn, char cs, float fs):_name(sn), _sex(cs),_score(fs) {}
Student(const Student & another) {
this->_name = another._name;
this->_sex = another._sex;
this->_score = another._score;
}
void dis() {
cout<<"name: "<<_name<<endl;
cout<<"sex: "<<_sex<<endl;
cout<<"score: "<<_score<<endl;
}
private:
string _name;
char _sex;
float _score;
};
class Graduate: public Student {
public:
Graduate(string sn, char cs, float fs, float fsa, int y, int m, int d)
: Student(sn, cs, fs), birth(y, m, d) {
_salary = fsa;
}
Graduate(const Graduate & another): Student(another), birth(another.birth) {
this->_salary = another._salary;
}
void dump() {
dis();
birth.disBirth();
cout<<"salary: "<<_salary<<endl;
}
private:
float _salary;
Birthday birth;
};
int main(int argc, const char * argv[]) {
Graduate g("zhaosi", 's', 99, 1000, 1992, 06, 19);
g.dump();
Graduate gg(g);
gg.dump();
return 0;
}
subclass operator overload
class C {
public:
C() {
cout<<"C()"<<endl;
}
C & operator= (const C & another) {
cout<<"C & operator= (const C & another)"<<endl;
return *this;
}
int c;
};
class A {
public:
A(int x): a(x) {
cout<<"A()"<<endl;
}
A & operator= (const A & another) {
if (this == &another) {
return *this;
}
cout<<"A & operator= (const A & another)"<<endl;
this->a = another.a;
return *this;
}
int a;
};
class B: public A {
public:
B(int x, int y): A(x) {
b = y;
cout<<"B()"<<endl;
}
B & operator= (const B & another) {
if (this == &another) {
return *this;
}
A::operator=(another);
cout<<"B & operator= (const B & another)"<<endl;
this->b = another.b;
c = another.c;
return *this;
}
int b;
C c;
};
int main(int argc, const char * argv[]) {
B b(1, 2);
B bb(3, 4);
b = bb;
cout<<b.a<<b.b<<endl;
return 0;
}
class Birthday {
public:
Birthday(int y, int m, int d):_year(y), _month(m), _day(d) {}
Birthday(const Birthday & another) {
this->_year = another._year;
this->_month = another._month;
this->_day = another._day;
}
void disBirth() {
cout<<"year: "<<_year<<endl;
cout<<"month: "<<_month<<endl;
cout<<"day: "<<_day<<endl;
}
private:
int _year;
int _month;
int _day;
};
class Student {
public:
Student(string sn, char cs, float fs):_name(sn), _sex(cs),_score(fs) {}
Student(const Student & another) {
this->_name = another._name;
this->_sex = another._sex;
this->_score = another._score;
}
void dis() {
cout<<"name: "<<_name<<endl;
cout<<"sex: "<<_sex<<endl;
cout<<"score: "<<_score<<endl;
}
private:
string _name;
char _sex;
float _score;
};
class Graduate: public Student {
public:
Graduate(string sn, char cs, float fs, float fsa, int y, int m, int d)
: Student(sn, cs, fs), birth(y, m, d) {
_salary = fsa;
}
Graduate(const Graduate & another): Student(another), birth(another.birth) {
this->_salary = another._salary;
}
Graduate & operator=(const Graduate & another) {
if (this == &another) {
return *this;
}
Student::operator=(another);
this->_salary = another._salary;
birth = another.birth;
return *this;
}
void dump() {
dis();
birth.disBirth();
cout<<"salary: "<<_salary<<endl;
}
private:
float _salary;
Birthday birth;
};
int main(int argc, const char * argv[]) {
Graduate g("zhaoqi", 's', 99, 1000, 1992, 06, 19);
g.dump();
Graduate gg("xxx", 'x', 9, 9999, 1111, 11, 11);
g = gg;
g.dump();
return 0;
}
subclass friend
class Student {
friend void operator<<(ostream & out, Student & stu);
public:
Student(int i, int j): a(i), b(j) {}
private:
int a;
int b;
};
void operator<<(ostream & out, Student & stu) {
out<<stu.a<<endl;
out<<stu.b<<endl;
}
class Graduate: public Student {
friend void operator<<(ostream & out, Graduate & gra) {
out<<(Student&)gra;
out<<gra.c<<endl;
}
public:
Graduate(int i, int j, int k):Student(i, j), c(k) {}
private:
int c;
};
int main(int argc, const char * argv[]) {
Graduate g(1, 2, 3);
cout<<g;
return 0;
}
multiple inheritance
class Sofa {
public:
void sit() {
cout<<"take a sit have a rest"<<endl;
}
};
class Bed {
public:
void sleep() {
cout<<"have a sleep ......"<<endl;
}
};
class SofaBed: public Sofa, public Bed {
};
int main(int argc, const char * argv[]) {
Sofa sf;
sf.sit();
Bed bd;
bd.sleep();
SofaBed sb;
sb.sit();
sb.sleep();
return 0;
}
//1. 数据冗余的问题, 2. 访问方便
//三角转四角的问题
//1. 提取各父类, 相同的成员, 构成祖父类
//2. 让各父类, 继承祖父类
//3. 虚继承是一种继承的扩展, virtual 继承祖父类
//4. 虚继承也是一种设计的结果
class A {
protected:
int _data;
};
class X: virtual public A {
public:
X(int d) {
_data = d;
}
void setData(int d) {
_data = d;
}
};
class Y: virtual public A {
public:
Y(int d) {
_data = d;
}
int getData() {
return _data;
}
};
class Z: public X, public Y {
public:
Z(int i, int j):X(i), Y(j) {}
void dis() {
cout<<_data<<endl;
}
};
int main(int argc, const char * argv[]) {
Z z(100, 200);
z.dis();
z.setData(100000);
cout<<z.getData()<<endl;
z.dis();
return 0;
}
//虚基类, 被虚继承的类就叫虚基类;
//虚继承, 是一种对继承的扩展;
class A {
protected:
int _data;
};
class B: virtual public A {
public:
B(int i = 0) {
cout<<"B(int i)"<<endl;
_data = i;
}
};
class C: virtual public A {
public:
C(int i = 0) {
cout<<"C(int i)"<<endl;
_data = i;
}
};
class D: public B, C {
public:
D(int d) {
cout<<"D(int i)"<<endl;
_data = d;
}
void dis() {
cout<<_data<<endl;
}
};
int main(int argc, const char * argv[]) {
D d(10);
d.dis();
return 0;
}
class Furniture {
public:
void descript() {
cout<<"_weight :"<<_weight<<endl;
cout<<"_color :"<<_color<<endl;
}
protected:
float _weight;
int _color;
};
class Sofa: virtual public Furniture {
public:
Sofa(float w = 0, int c = 1) {
_weight = w;
_color = c;
}
void sit() {
cout<<"take a sit have a rest"<<endl;
}
};
class Bed: virtual public Furniture {
public:
Bed(float w = 0, int c = 1) {
_weight = w;
_color = c;
}
void sleep() {
cout<<"have a sleep ......"<<endl;
}
};
class SofaBed: public Sofa, public Bed {
public:
SofaBed(float w, int c) {
_weight = w;
_color = c;
}
};
int main(int argc, const char * argv[]) {
SofaBed sb(1000, 2);
sb.sit();
sb.sleep();
sb.descript();
return 0;
}
polymorphism
//1. 派生类的对象可以赋值给基类对象
//2. 派生类的对象可以初始化基类的引用
//3. 派生类对象的地址可以赋值给指向基类的指针
class Shape {
public:
Shape(int x = 0, int y = 0):_x(x), _y(y) {
cout<<"Shape: "<<this<<endl;
cout<<typeid(this).name()<<endl;
}
void draw() {
cout<<"draw shape from "<<"("<<_x<<", "<<_y<<")"<<endl;
}
protected:
int _x;
int _y;
};
class Circle: public Shape {
public:
Circle(int x = 0, int y = 0, int r = 0):Shape(x, y), _radius(r) {
cout<<"Circle: "<<this<<endl;
cout<<typeid(this).name()<<endl;
}
void draw() {
cout<<"draw shape from "<<"("<<_x<<", "<<_y<<", "<<_radius<<")"<<endl;
}
protected:
int _radius;
};
class Rect: public Shape {
};
int main(int argc, const char * argv[]) {
// Shape s(1, 2);
// s.draw();
Circle c(4, 5, 6);
// s = c;
// s.draw();
// Shape & rs = c;
// rs.draw();
Shape * ps = &c;
ps->draw();
return 0;
}
//多态形成的条件有3个
//1. 父类中有虚函数, 加virtual 声明型关键字
//2. 子类override了父类的虚函数, 子类中同名, 同参, 同返回, 才能构成override
//3. 子类对象赋给父类的指针, 通过父类的指针, 调用虚函数, 形成多态
class Shape {
public:
Shape(int x = 0, int y = 0):_x(x), _y(y) {
cout<<"Shape: "<<this<<endl;
cout<<typeid(this).name()<<endl;
}
virtual void draw() {
cout<<"draw shape from "<<"("<<_x<<", "<<_y<<")"<<endl;
}
protected:
int _x;
int _y;
};
class Circle: public Shape {
public:
Circle(int x = 0, int y = 0, int r = 0):Shape(x, y), _radius(r) {
cout<<"Circle: "<<this<<endl;
cout<<typeid(this).name()<<endl;
}
virtual void draw() {
cout<<"draw shape from "<<"("<<_x<<", "<<_y<<", "<<_radius<<")"<<endl;
}
protected:
int _radius;
};
class Rect: public Shape {
public:
Rect(int x = 0, int y = 0, int w = 0, int l = 0): Shape(x, y), _width(w), _length(l) {}
virtual void draw() {
cout<<"draw rect from "<<"("<<_x<<", "<<_y<<", "<<_width<<", "<<_length<<")"<<endl;
}
protected:
int _width;
int _length;
};
int main(int argc, const char * argv[]) {
Circle c(3, 4, 5);
Rect r(7, 8, 9, 10);
Shape * ps;
while (1) {
int choice;
cin>>choice;
switch (choice) {
case 1:
ps = &c;
break;
case 2:
ps = &r;
break;
default:
break;
}
ps->draw();
}
return 0;
}
virtual function
//virtual 声明型关键字, 用于声明一个函数, 虚, 子类override的函数, 也是virtual
//override 同名, 同参, 同返回
//虚函数在子类中, 访问属性并不影响多态, 具体需求, 要看子类
class A {
public:
virtual void foo() {
cout<<"A: foo"<<endl;
}
};
class B: public A {
private:
virtual void foo() override {
cout<<"B: foo"<<endl;
}
};
int main(int argc, const char * argv[]) {
B b;
A * pa = &b;
pa->foo();
return 0;
}
pure virtual function
class A {
public:
virtual void foo() {
cout<<"A: foo"<<endl;
}
};
class B: public A {
};
class C: public B {
public:
virtual void foo() {
cout<<"C: foo"<<endl;
}
};
int main(int argc, const char * argv[]) {
C c;
B * bp = &c;
bp->foo();
A * ap = &c;
ap->foo();
return 0;
}
//纯虚函数, virtual 没有实现体, 被"初始化"为0
//纯接口类, 才配具备, 拥有纯虚函数, 含有纯虚函数的类, 称为抽象基类 Interface
//抽象基类不能实例化
//子类中若无override, 则依然为纯虚, 依然不能实例化
class A {
public:
virtual void foo() = 0;
};
class B: public A {
};
class C: public B {
public:
virtual void foo() override {
cout<<"C: foo"<<endl;
}
};
int main(int argc, const char * argv[]) {
C c;
B * bp = &c;
bp->foo();
A * ap = &c;
ap->foo();
return 0;
}
virtual destructor
class Animal {
public:
Animal();
~Animal();
virtual void voice() = 0;
};
class Dog: public Animal {
public:
Dog();
~Dog();
virtual void voice();
};
class Cat: public Animal {
public:
Cat();
~Cat();
virtual void voice();
};
Animal::Animal() {
cout<<"Animal::Animal()"<<endl;
}
Animal::~Animal() {
cout<<"Animal::~Animal()"<<endl;
}
Dog::Dog() {
cout<<"Dog::Dog()"<<endl;
}
Dog::~Dog() {
cout<<"Dog::~Dog()"<<endl;
}
void Dog::voice() {
cout<<"wang wang wang"<<endl;
}
Cat::Cat() {
cout<<"Cat::Cat()"<<endl;
}
Cat::~Cat() {
cout<<"Cat::~Cat()"<<endl;
}
void Cat::voice() {
cout<<"mew mew mew"<<endl;
}
int main(int argc, const char * argv[]) {
Cat c;
Dog d;
Animal *pa = &c;
pa->voice();
return 0;
}
Animal::Animal()
Cat::Cat()
Animal::Animal()
Dog::Dog()
mew mew mew
Dog::~Dog()
Animal::~Animal()
Cat::~Cat()
Animal::~Animal()
Program ended with exit code: 0
dependency inversion
class IReader {
public:
virtual string getContents() = 0;
};
class Book: public IReader {
public:
string getContents() {
return "从前有座山, 山里有座庙, 庙里有个小和尚"
"听老和尚讲故事, 从前有座山";
}
};
class EBook: public IReader {
public:
string getContents() {
return "郭文贵, 在美国瞎bb";
}
};
class NewsPaper: public IReader {
public:
string getContents() {
return "Trump 要在墨西哥边境建一堵墙";
}
};
class Mother {
public:
void tellStory(IReader * pi) {
cout<<pi->getContents()<<endl;
}
};
int main(int argc, const char * argv[]) {
Mother m;
Book b;
EBook eb;
NewsPaper n;
m.tellStory(&b);
m.tellStory(&eb);
m.tellStory(&n);
return 0;
}
func template
class MyType {
public:
MyType(int x, int y):_x(x), _y(y){}
int _x;
int _y;
};
//类型参数化, 设置默认类型
template<typename T>
void myswap(T & a, T & b) {
T t = a;
a = b;
b = t;
}
//函数模板 实例化模板函数 模板函数的调用
//myswap -> myswap<int> -> myswap<int>(1, 2)
int main(int argc, const char * argv[]) {
int a = 3; int b = 4;
myswap(a, b);
cout<<"a: "<<a<<", b: "<<b<<endl;
string s1 = "china"; string s2 = "america";
myswap(s1, s2);
cout<<"s1: "<<s1<<", s2: "<<s2<<endl;
double d1 = 3.1; double d2 = 4.1;
myswap(d1, d2);
cout<<"d1: "<<d1<<", d2: "<<d2<<endl;
MyType mt1(1, 2); MyType mt2(3, 4);
myswap(mt1, mt2);
cout<<mt1._x<<mt2._x<<endl;
return 0;
}
template <typename T>
void quickSort(T * p, int low, int high) {
if (low < high) {
int l = low, h = high;
T pivot = p[l];
while (l < h) {
while (p[h] >= pivot && l < h) {
h--;
}
p[l] = p[h];
while (p[l] <= pivot && l < h) {
l++;
}
p[h] = p[l];
p[l] = pivot;
quickSort(p, low, l - 1);
quickSort(p, l + 1, high);
}
}
}
int main(int argc, const char * argv[]) {
string arr[8] = {"1", "2", "3", "8", "7", "6", "4", "5"};
// sort(arr, arr + 8, [](int x, int y) {return x < y;});
quickSort(arr, 0, 7);
for (auto i: arr) {
cout<<i<<endl;
}
return 0;
}
template <typename T>
int compare(T a, T b) {
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
}
template <> int compare<const char *>(const char * a, const char * b) {
cout<<"template <> int compare<const char *>"<<endl;
if (strcmp(a, b)) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
}
int main(int argc, const char * argv[]) {
int a = 10, b = 20;
cout<<compare(a, b)<<endl;
string sa = "abc", sb = "Abc";
cout<<compare(sa, sb)<<endl;
const char *ca = "abc", *cb = "Abc";
cout<<compare(cb, ca)<<endl;
return 0;
}
class template
template <typename T>
class Stack {
public:
Stack(int size = 1024);
~Stack();
bool isEmpty();
bool isFull();
void push(T data);
T pop();
private:
T * space;
int top;
};
template <typename T>
Stack<T>::Stack(int size) {
space = new T[size];
top = 0;
}
template <typename T>
Stack<T>::~Stack() {
delete []space;
}
template <typename T>
bool Stack<T>::isEmpty() {
return top == 0;
}
template <typename T>
bool Stack<T>::isFull() {
return top == 1024;
}
template <typename T>
void Stack<T>::push(T data) {
space[top++] = data;
}
template <typename T>
T Stack<T>::pop() {
return space[--top];
}
int main(int argc, const char * argv[]) {
Stack<string> s;
s.push("aaa");
s.push("bbb");
s.push("ccc");
while (!s.isEmpty()) {
cout<<s.pop()<<endl;
}
return 0;
}
iostream
class A {
public:
A() {}
~A() {}
operator void* () const {
cout<<"operator void* () cast to void*; ";
return (void *)this;
}
bool operator!() const {
cout<<"bool operator!() return bool; ";
return true;
}
};
int main(int argc, const char * argv[]) {
A a;
while(a) {
cout<<"first"<<endl;
break;
}
if (!a) {
cout<<"second"<<endl;
}
return 0;
}
operator void* () cast to void*; first
bool operator!() return bool; second
Program ended with exit code: 0
最后
更多新鲜文章可以关注并Star
, 我们一起学习.
GitHub Repo:coderZsq.github.io
Follow: coderZsq · GitHub