C++面向对象
从C的角度去学习C++,主要是了解C++,能读懂C++代码,掌握面向对象的方法
Trstary
每天掉一根,知识涨一截。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
十五.模板(动态类型)(C++)
内容参考于《21天学通C++》(第八版)十五.模板1. 普通模板示例#include<iostream>#include<string>using namespace std;template < typename Type>const Type& GetMax(const Type& value1, const Type& value2){ if (value1 > value2) return value1; .原创 2020-09-21 14:37:48 · 468 阅读 · 0 评论 -
十四.C++的类型转换(C++)
内容参考于《21天学通C++》(第八版)十四.C++的类型转换由于不喜欢C风格类型转换,所以多了一套方案1. static_cast编译阶段检查并转换Base* objBase = new Derived ();Derived* objDer = static_cast<Derived*>(objBase); // ok!// class Unrelated is not related to BaseUnrelated* notRelated = static_cast&l.原创 2020-09-21 14:37:33 · 199 阅读 · 0 评论 -
十三.双目运算符重载(C++)
内容参考于《21天学通C++》(第八版)十三.双目运算符重载以类成员的方式实现的双目运算符只接受一个参数, 其原因是第二个参数通常是从类属性获得的。1. 示例#include <iostream>using namespace std;class Date{private: int day, month, year; string dateInString;public: Date(int inMonth, int inDay, int inYear) : mo.原创 2020-09-21 14:37:23 · 513 阅读 · 0 评论 -
十二.单目运算符重载(C++)
内容参考于《21天学通C++》(第八版)十二.单目运算符重载1. 示例#include <iostream>using namespace std;class Date{private: int day, month, year;public: Date(int inMonth, int inDay, int inYear) : month(inMonth), day(inDay), year(inYear) {}; Date & operator ++.原创 2020-09-21 14:36:57 · 946 阅读 · 0 评论 -
十一. 封装的public与private与protected 、友元、继承的public与private与protected(C++)
内容参考于《21天学通C++》(第八版)十一. 封装的public与protected与private 、友元、继承的public与protected与private1. public与protected与privateC++让您能够将类属性和方法声明为公有的,这意味着有了对象后就可获取它们;也可将其声明为私有的, 这意味着只能在类的内部(或其友元)中访问。 作为类的设计者, 您可使用 C++关键字 public和 private 来指定哪些部分可从外部(如 main( ))访问,哪些部分不能。.原创 2020-09-21 14:36:45 · 381 阅读 · 0 评论 -
十.override和final的使用和继承的注意事项(C++)
内容参考于《21天学通C++》(第八版)十.override和final的使用表明覆盖意图的限定符 override使用 final 来禁止覆盖函数1. 一个复杂的示例#include <iostream>using namespace std;class Fish{public: virtual Fish* Clone() = 0; virtual void Swim() = 0; virtual ~Fish() {};};class Tuna : pub.原创 2020-09-18 13:46:34 · 201 阅读 · 0 评论 -
九.继承的菱形问题(C++)
内容参考于《21天学通C++》(第八版)九.继承的菱形问题鸭嘴兽具备哺乳动物、鸟类和爬行动物的特征,这意味着 Platypus 类需要继承Mammal、 Bird 和 Reptile。然而,这些类都从同一个类—Animal 派生而来。实例化 Platypus 时,结果将如何呢?对于每个 Platypus 实例,将实例化多少个 Animal 实例呢?1. 示例-异常现象#include <iostream>using namespace std;class Animal{.原创 2020-09-18 13:46:06 · 172 阅读 · 0 评论 -
八.抽象基类和纯虚函数(C++)
内容参考于《21天学通C++》(第八版)八.抽象基类和纯虚函数不能实例化的基类被称为抽象基类, 这样的基类只有一个用途, 那就是从它派生出其他类。 在 C++中,要创建抽象基类,可声明纯虚函数。1. 示例#include <iostream>using namespace std;class Fish{public: // define a pure virtual function Swim virtual void Swim() = 0;};class Tuna.原创 2020-09-18 13:45:40 · 196 阅读 · 0 评论 -
七.多态-虚函数表(C++)
内容参考于《21天学通C++》(第八版)七.多态-虚函数表1. 示例原创 2020-09-18 13:45:22 · 197 阅读 · 0 评论 -
六.多态(C++)
内容参考于《21天学通C++》(第八版)六.多态1. 示例1#include <iostream>using namespace std;class Fish{public: void Swim() { cout << "Fish swims! " << endl; }};class Tuna :public Fish{public: // override Fish::Swim void Swim() { cout <.原创 2020-09-18 13:44:57 · 195 阅读 · 0 评论 -
五.继承(C++)
内容参考于《21天学通C++》(第八版)二.继承1. 示例#include <iostream>using namespace std;class Fish{public: bool isFreshWaterFish; void Swim() { if (isFreshWaterFish) cout << "Swims in lake" << endl; else cout << "Swims in sea" &l.原创 2020-09-18 13:44:36 · 158 阅读 · 0 评论 -
四.sizeof用于类(C++)
内容参考于《21天学通C++》(第八版)四.sizeof用于类1. 示例-只算成员变量大小,不算方法#include <iostream>#include <string.h>using namespace std;class MyString{private: char* buffer;public: MyString(const char* initString) // default constructor { buffer = NULL; .原创 2020-09-18 13:44:15 · 389 阅读 · 0 评论 -
三.类的复制(C++)
内容参考于《21天学通C++》(第八版)三.类的复制1. 示例1-浅复制及其存在的问题#include <iostream>#include <string.h>using namespace std;class MyString{private: char* buffer;public: MyString(const char* initString) // Constructor { buffer = NULL; if (initString .原创 2020-09-18 13:43:41 · 2880 阅读 · 0 评论 -
二.析构函数(C++)
内容参考于《21天学通C++》(第八版)二.析构函数1. 示例1#include <iostream>#include <string.h>using namespace std;class MyString{private: char* buffer;public: MyString(const char* initString) // constructor { if (initString != NULL) { buffer = ne.原创 2020-09-18 13:42:57 · 225 阅读 · 0 评论 -
一.构造函数(C++)
内容参考于《21天学通C++》(第八版)一.构造函数1. 示例1:构造函数是可以重载#include <iostream>#include <string>using namespace std;class Human{private: string name; int age;public: Human() // default constructor { age = 0; // initialized to ensure no junk val.原创 2020-09-17 15:25:34 · 190 阅读 · 0 评论
分享