
C++编程思想
文章平均质量分 67
chosen13
这个作者很懒,什么都没留下…
展开
-
基础的了解
#include // 这个是C语言的编写,#include class Book{ // 类的成员,数据成员,成员函数,};int main(){ int a; // int 是整形类型,a 是叫做变量, Book b; // Book 是类,b是对象, system("pause"); // 这个是暂停键, return 0;}原创 2016-04-28 21:31:28 · 682 阅读 · 0 评论 -
名称空间
#ifndef TEST_H_#define TEST_H_namespace ThinkingInCppDemoLib // 这个就是一个名称空间,ThinkingInCppDemoLib这个是名称空间的名字, // 在这里使用名称空间,为了避免在其它的人编写时使用相同的类 class f 类,{ class t // 这个类在名称空间里,这个地方也可以是全局函数\变量名称、全局原创 2016-05-08 20:23:19 · 412 阅读 · 0 评论 -
多态性与虚函数2
#include #include using namespace std;class A{public: virtual int f() const { cout << "A::f()" << endl; return 1; } virtual void f(string) const {} virtual void g() const {}};class B原创 2016-05-18 21:45:03 · 240 阅读 · 0 评论 -
多态性和虚函数
#include #include using namespace std;enum note{ middleC, Csharp, Eflat};class Instument // 这个是一个纯抽象类因为它所有的抽象函数都是纯虚函数,只有一个纯虚函数就是普通的抽象类,不能用来创建对象可以用来做接口,{public: virtual void play(note) c原创 2016-05-18 20:55:35 · 274 阅读 · 0 评论 -
函数内部静态变量,( \\Visual C++\\ CLR \\ windows窗体应用程序 编写C++)
#pragma oncenamespace demo2 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace Syste原创 2016-05-07 14:36:00 · 686 阅读 · 0 评论 -
访问函数
#ifndef TIME_H_#define TIME_H_#include class Time{ std::time_t t; // time_t是时间的起始时间, std::tm local; // tm 就是时间的记录, char asciiRep[26]; unsigned char lflag, aflag; // 这个就是为了做标记, void updat原创 2016-05-07 13:32:33 · 404 阅读 · 0 评论 -
继承中的向上类型转换
#include using namespace std;enum note{ middleC, Csharp, Cflat};class Instrument{public: void play(note) const {}};class Flute : public Instrument{};void tune(Instrument& i){ i.pl原创 2016-05-17 19:20:00 · 528 阅读 · 0 评论 -
继承中构造函数与析构函数
#include using namespace std;class Y{private: int i;public: Y(){} Y(int ii) : i(ii) {}};class X : public Y // 这个是继承,在继承的时候,X 类中含有的函数与Y 函数相同,则将Y类的此函数屏蔽掉,无论参数是否相同,只要函数一样就将其屏蔽掉,{private:原创 2016-05-17 18:15:54 · 266 阅读 · 0 评论 -
错误检查
#ifndef COS_H_#define COS_H_#include #include #include inline void require(bool requirement, const std::string& msg = "Requirement failed"){ if (!requirement) { fputs(msg.c_str(), stderr);原创 2016-05-05 21:09:06 · 288 阅读 · 0 评论 -
静态成员
#ifndef ACCOUNT_H_#define ACCOUNT_H_#include class Account{public: Account(std::string name, double money); double getAccount() const; void deposit(double money); void applyint(); static原创 2016-05-08 21:33:44 · 337 阅读 · 0 评论 -
单例设计模式
# include using namespace std;class Egg // 这个就是单例模式,只有一个对象,单例设计模式{private: static Egg e; // 只有这么一个对象, int i; Egg(int ii) : i(ii){} Egg(const Egg&); // 这个做成私有的防止copy 构造函数,public: static E原创 2016-05-09 21:15:26 · 192 阅读 · 0 评论 -
模板迭代器
#include #include #include using namespace std;templateclass Stack{private: T stack[ssize]; int top;public: Stack() : top(0){} void push(const T& i) { if (top <ssize)// 这个是判断条件, s原创 2016-05-23 18:36:18 · 381 阅读 · 0 评论 -
堆栈里的迭代器
#include using namespace std;class IntStack{private: enum {ssize = 100}; int stack[ssize]; int top;public: IntStack() : top(0){} //~IntStack(){} void push(int i) { if (top <ssize)// 这个原创 2016-05-23 18:05:17 · 722 阅读 · 0 评论 -
简单的迭代器
#include using namespace std;class IntStack{private: enum {ssize = 100}; int stack[ssize]; int top;public: IntStack() : top(0){} //~IntStack(){} void push(int i) { if (top <ssize)// 这个原创 2016-05-23 16:30:35 · 248 阅读 · 0 评论 -
模板化的指针Stash
#ifndef STASH_H #define STASH_H #include namespace ThinkingInCppDemoLib{ template class PStash { private: int quantity; int next; T** storage; void inflate(int increase = incr);原创 2016-05-23 15:42:56 · 352 阅读 · 0 评论 -
模板2
#include #include using namespace std;// template 这就是模板,template // 这个就是模板中的常量,class Array{private: //enum { size = 100 }; int A[size];public: T& operator[](T& index) { index > 0 &&原创 2016-05-23 15:19:43 · 188 阅读 · 0 评论 -
多态与虚函数 4
#include using namespace std;class Matrix; // 前置声明,class Scalar;class Vector;class Math{public: virtual Math& operator*(Math& rv) = 0; // 虚的运算符重载, virtual Math& multiply(Matrix*) = 0;原创 2016-05-20 16:30:43 · 215 阅读 · 0 评论 -
多态与虚函数3
#include using namespace std;class Y{private: int i;public: Y(){} Y(int ii) : i(ii) {}};class X : public Y // 这个是继承,在继承的时候,X 类中含有的函数与Y 函数相同,则将Y类的此函数屏蔽掉,无论参数是否相同,只要函数一样就将其屏蔽掉, {priva原创 2016-05-20 14:30:37 · 210 阅读 · 0 评论 -
替代链接 extern “C”
#ifndef ACCOUNT_H_#define ACCOUNT_H_#ifdef __cplusplus // 下边的设计就是无论是C 或者 C++ 都可以直接的调用,只需要加上此头文件就可以,extern "C"{#endiffloat f(int a, int b);void p();void t();#ifdef __cplusplus}#endif原创 2016-05-09 21:56:23 · 616 阅读 · 0 评论 -
继承与组合
#include using namespace std;class X{private: int i;public: X() { i = 0; // 这个只是初始化, } void set(int ii) { i = ii; } int read() const { return i; } int permute() { return i *原创 2016-05-16 21:09:01 · 177 阅读 · 0 评论 -
内联函数
#include using namespace std;#define SRUARE (x) (x)*(x) // 这个是C语言的预定义宏,是计算x*x的,它有缺点。总是出错,inline double square(double x) // 在函数前边加上inline就是内联函数,内联函数在编译的时候执行,{ double result; result = x * x;原创 2016-05-04 22:08:32 · 206 阅读 · 0 评论 -
C++ 构造函数与析构函数
#include using namespace std;const int sz = 20;class Pointer;class Holder{private: int a[sz];public: Holder(); ~Holder(); friend class Pointer;};Holder::~Holder(){ }class Point原创 2016-05-01 21:33:32 · 206 阅读 · 0 评论 -
句柄类
#ifndef TEST_H_#define TEST_H_class Demo{private: class Cheshire; // 将私有的成员隐藏在Cheshire里边.这个就是句柄类把private部分隐藏起来,还可以减少重复编译。 Cheshire* smile;public: void initialize(); int read(); void change(原创 2016-05-01 20:22:09 · 271 阅读 · 0 评论 -
友元
#include using namespace std;const int sz = 20;class Pointer;class Holder{private: int a[sz];public: void initialize(); friend class Pointer;};class Pointer{private: Holder* h; i原创 2016-05-01 19:49:48 · 162 阅读 · 0 评论 -
用C++写Stash
class Stash{ int size; int quantity; int next; unsigned char* storage;public: void initialize(int size); int add(const void* element); void inflate(int increase); int count(); // 计算Stash里的数原创 2016-04-30 20:44:32 · 736 阅读 · 0 评论 -
CStash 的例子
typedef struct CStashTag{ int size; int quantity; int next; unsigned char* storage;}CStash;void initialize(CStash* s, int size);int add(CStash* s, const void* element);void inflate(CStash* s原创 2016-04-29 21:38:57 · 542 阅读 · 0 评论 -
运算符重载
#include using namespace std;class Dog{public: int a;};class Cat{public: int c;};class Person{public: Cat operator+(const Dog& d) // 这个就是成员函数重载, { Cat cat; cat.c = b + d.a;原创 2016-05-11 10:41:34 · 299 阅读 · 0 评论 -
拷贝构造函数
#include #include using namespace std;class Person{public: Person(std::string st, int a, double ht) : name(st), age(a), height(ht) { cout << " normal constructor" << endl; } Person(const原创 2016-05-11 10:40:16 · 191 阅读 · 0 评论 -
引用
#include using namespace std;int x = 12;int y = 80;int *p = &y; // & 就是引用,int &r = y; // 引用就是别名相当于y 又有了一个别名r,r 和 y 是同一个变量,对r 进行操作就是对y 进行操作,// 创建引用的时候必须初始化,引用不能改变、也没有NULL引用,// 指针可以在任何时候初始化,指针原创 2016-05-11 10:38:53 · 1066 阅读 · 0 评论 -
运算符重载2
#ifndef BYTE_H#define BYTE_H#include class Byte{ unsigned char b;public: Byte(unsigned char bb = 0) : b(bb){} void print() const { std::cout << (int)b << std::endl; } const Byte& opera原创 2016-05-12 20:10:28 · 238 阅读 · 0 评论 -
Union 联合
#ifndef SUPERVAR_H_#define SUPERVAR_H_class SuperVar { enum // 枚举 后边的标识符可以不写即为匿名的, { character, integer, floating_point }varType; // 这里的varType 是class的成员变量, union // 这个就是使原创 2016-05-02 19:13:22 · 247 阅读 · 0 评论 -
volatile 限定符
#include using namespace std;class Dog{private: const volatile unsigned char byte; // volatile 可以通过外部接口修改byte, volatile unsigned char flag; // volatile是限定符,可以用数据成员,成员函数和对象本身使用volatile, enum {原创 2016-05-04 19:45:22 · 362 阅读 · 0 评论 -
const
#include using namespace std;class Dog{private: int x; const int y; // 在这里不能进行初始化,只有静态的const才能进行初始化。在运行时分配内存,初始化列表进行初始化, static const int z = 200; // 这个是静态的const数据成员,是共有的,这个是可读的不能够进行修改了。这个是编原创 2016-05-04 19:00:33 · 584 阅读 · 0 评论 -
指针的Stash 将void** 删除指针
#ifndef STASH_H#define STASH_H#include namespace ThinkingInCppDemoLib{ class PStash { private: int quantity; int next; void ** storage; void inflate(int increase); public: PStash()原创 2016-05-15 17:55:36 · 258 阅读 · 0 评论 -
动态创建对象与delete 使用
#include #include using namespace std;class Circle{private: int r;public: Circle(){} ~Circle(){} Circle(int rr) :r(rr){} void print() { cout << "r = " << r << endl; }};Circle *pCir原创 2016-05-15 16:20:24 · 624 阅读 · 0 评论 -
重载赋值运算符--引用计数
#include #include using namespace std;class Dog{private: string nm; int refcount; // 引用计数, Dog(const string& name) : nm(name), refcount(1) { cout << "Creating Dog: " << *this << endl; }原创 2016-05-14 19:35:00 · 282 阅读 · 0 评论 -
重载赋值运算符2
#include #include using namespace std;class Dog{private: string nm;public: Dog(const string& name) : nm(name) { cout << "Creating Dog: " << *this << endl; } ~Dog() { cout << "Deleti原创 2016-05-14 10:31:40 · 198 阅读 · 0 评论 -
重载赋值运算符
#include using namespace std;class Dog{private: int k;public: // C++自动创建赋值运算符, Dog(int kk) : k(kk) { cout << "这是构造函数," << endl; } Dog(const Dog& dog) : k(dog.k) { cout << "这里是拷贝构造函数。"原创 2016-05-13 21:08:57 · 229 阅读 · 0 评论 -
默认参数与占位符参数
#include using namespace std;const int ArSize = 80;char* left(const char* str, int n = 2); // 提取字符串str里边左边n 个,void f(int a, int b, float c); // 函数中声明的变量x y z 可以跟函数定义中的变量不一样,函数声明的变量也可以不写,int原创 2016-05-02 20:05:45 · 1884 阅读 · 0 评论 -
函数模板
#include using namespace std;template // 这个就是函数模板,void Swap(T &a, T &b) // C++里边已经有一个swap了,里边的s就是小写,参数a b 前面必须要用引号,{ T temp; temp = b; b = a; a = temp;}int main(){ int a(2), b(8); S原创 2016-05-23 18:58:41 · 190 阅读 · 0 评论