这两天没事又翻了翻Primer,发现自己上岁数了,记单词能力开始下降,索引把一些简单的例子记下来,把一些肥肉剔除,剩一下骨头,方便今后Ctrl+F。

    在此感谢:

     http://ticktick.blog.51cto.com/823160/194307/

http://www.cnblogs.com/gaojun/archive/2010/09/10/1823354.html

     http://www.cnblogs.com/uniqueliu/archive/2011/08/02/2125590.html

一、类的初始化--构造函数。

#include<iostream>
#include<memory>
#include<unistd.h> using namespace std; class Tree
{
public:
int data;
int data_arr[3];
double hello;
//无参数构造函数
//编译系统对象分配内存空间,并自动调用该构造函数->由构造函数完成成员的初始化工作
Tree(void)
{
hello=9.823;
data=22;
}
//一般构造函数
Tree(double a)
{
hello=a;
}
//复杂构造函数
Tree(const Tree &c)
{
hello=c.hello;
}
//等号运算符重载构造函数
Tree &operator=(const Tree &rt)
{
if(this==&rt)
{
return *this;
}
this->hello=rt.hello;
this->data=rt.data; return *this;
} private:
int price;
int num; }; int main()
{
//一般构造
Tree a(233.33);
cout<<a.hello<<endl;
//复杂构造
Tree b(a);
cout<<b.hello<<endl;
//等号运算构造
Tree c=a;
cout<<c.hello<<endl;
}

  

二、深、浅拷贝--防止DELETE二次错误。

//深拷贝和浅拷贝
#include<iostream>
#include<memory>
#include<string.h>
#include<stdio.h> using namespace std; class Tree
{
public:
Tree(char *pN)
{
m_pName = new char[strlen(pN) + 1];
if(m_pName)
{
strcpy(m_pName ,pN);
}
} Tree(Tree &p)
{
m_pName=new char[strlen(p.m_pName)+ 1];
if(m_pName)
{
strcpy(m_pName ,p.m_pName);
}
} ~Tree()
{
delete m_pName;
}
private:
char *m_pName;
}; int main()
{
Tree man("lujun");
Tree woman(man); }

  

三、友元---我的世界你不懂,你的世界我能懂

#include<iostream>
#include<memory>
#include<string.h>
#include<stdio.h> using namespace std; class TV
{
public:
friend class Control;
TV():volume(20){}
void Show(TV &t)const;
private:
int volume;
}; class Control
{
public:
bool VolumeUp(TV &t);
bool VolumeDown(TV &t);
void Show(TV &t)const;
}; bool Control::VolumeUp(TV &t)
{
t.volume++;
} bool Control::VolumeDown(TV &t)
{
t.volume--;
} void Control::Show(TV&t)const
{
cout<<"经遥控器调整的音量大小为:"<<t.volume<<endl;
} void TV::Show(TV&t)const
{
cout<<"TV自身音量大小为:"<<t.volume<<endl;
} int main()
{
Control c1;
TV t1;
c1.VolumeUp(t1);
c1.Show(t1);
c1.VolumeUp(t1);
c1.Show(t1);
c1.VolumeDown(t1);
c1.Show(t1);
t1.Show(t1);
}

四、基类、派生、虚、多态----剪不断理还乱

#include <iostream>

using namespace std;

//原始书籍类
class Quote
{
public:
string isbn() const;
virtual double net_price(int n) const; //返回实际销售价格
Quote(void)
{
price=9.987;
}
protected:
double price;
}; string Quote::isbn() const
{
string aa="abcd,world";
cout<<aa<<endl;
return aa;
} double Quote::net_price(int n) const
{
cout<<n+20<<endl;
return n+20;
} //打折书籍类
class BulkQuote:public Quote
{
public:
BulkQuote()=default;
double net_price(int n) const ; //返回改动后的价格+自动覆盖
}; double BulkQuote::net_price(int n) const
{
cout<<n+10<<endl;
cout<<"n_price="<<n*price<<endl;
return n+10;
} void PrintAll(const Quote &book); //根据实际传的类类型,进行动态鉴别 int main()
{
Quote Father,*f;
BulkQuote Son,*s; Father.isbn();
Father.net_price(100);
cout<<endl; Son.isbn();
Son.net_price(100);
cout<<endl; PrintAll(Father);
PrintAll(Son); } void PrintAll(const Quote &book)
{
book.net_price(1000);
};

五、类模板、容器模板--真心方便

#include <iostream>
#include <vector>
#include <list> using namespace std; template<typename T1,typename T2>
class Tree
{
private:
T1 I;
T2 J;
public:
Tree(T1 a,T2 b);
void show();
}; template <typename T1,typename T2>
Tree<T1,T2>::Tree(T1 a,T2 b):I(a),J(b){} template <typename T1,typename T2>
void Tree<T1,T2>::show()
{
cout<<"I="<<I<<",J="<<J<<endl;
} template <typename X>
void print(X v)
{
typename X::iterator itor;
for (itor = v.begin(); itor != v.end(); ++itor)
{
cout<<*itor<<endl;
}
} int main()
{
Tree<int,int> t1(33,55);
t1.show();
Tree<int,string> t2(99,"wenzhang");
t2.show();
Tree<double,string> t3(3.414,"mayili");
t3.show();
vector<int> v1;
v1.push_back(2);
v1.push_back(33);
v1.push_back(44);
print(v1);
}

六、tuple类型--自己也能MongoDB

#include <iostream>
#include <tuple>
#include <vector>
#include <list> using namespace std; int main()
{
tuple<string,vector<double>,int,list<int> > aa("头条",{1.1,3.4},42,{3,4,5,6,7,9});
auto item=make_tuple("play",3,99.239);
cout<<get<0>(item)<<endl;
}

拾人牙慧,浅记一些C++的类的更多相关文章

  1. C# 中堆与栈的浅记

    C# 中堆与栈的浅记 什么是堆和栈? 简言之.堆和栈是驻留在内存中的区域,它们的作用是帮助我们运行代码.在.Net Framework 环境下,当我们的代码运行时,内存中的堆和栈便存储了这些代码,并包 ...

  2. C#中值类型和引用类型的差别浅记

    C#中值类型和引用类型的差别浅记         在C#中,变量的类型分为两种.各自是值类型和引用类型.         值类型的变量直接存储值,说得更详细一些,就是值类型变量在内存中直接存储它们自身 ...

  3. hybrid浅记

    目前首次接触hybrid项目,故根据翻阅了解后,浅记对它的认识. hybrid是携程推出的一个项目框架,其优点是:跨平台.开发效率高.开发成本相对较低,其不足是:体验不如Native hybrid设计 ...

  4. 浅谈Java的匿名类

    在实际的项目中看到一个很奇怪的现象,Java可以直接new一个接口,然后在new里面粗暴的加入实现代码.就像下面这样.那么问题来了,new出来的对象没有实际的类作为载体,这不是很奇怪吗? 思考以下代码 ...

  5. 浅谈JAVA中“增强”类的某个方法的几个中方法!

    一.继承 使用场景:能够控制这个类的构造的时候,才可以使用继承. 优点:简单容易使用, 缺点:耦合性大大的增强,不利于后期的维护,所以对于继承这种方法,谨慎使用.  代码实现:二.装饰者模式 使用场景 ...

  6. ES6躬行记(21)——类的继承

    ES6的继承依然是基于原型的继承,但语法更为简洁清晰.通过一个extends关键字,就能描述两个类之间的继承关系(如下代码所示),在此关键字之前的Man是子类(即派生类),而在其之后的People是父 ...

  7. ES6躬行记(20)——类

    ES6正式将类(Class)的概念在语法层面标准化,今后不必再用构造函数模拟类的行为.而ES6引入的类本质上只是个语法糖(即代码更为简洁.语义更为清晰),其大部分功能(例如继承.封装和复用等)均可在E ...

  8. 浅记初次使用expect、scp中出现的一些小问题

    以前也学过一些shell,不过学得并不是很深入,动手写的代码的时间也不是很多.前不久将shell比较细的过了一遍,leader布置了任务让用shell写一个脚本将redis源码压缩包从一个服务器上传到 ...

  9. 浅谈css常用伪类用法

    着重写一下after和before的用法: css样式搞定:标签元素+伪类after a.'class名':after{//我的样式名称是这个,可以写成你自己的样式名称 content: '已打包'; ...

随机推荐

  1. charles抓包

    charles使用教程指南 charles使用教程指南 前言 移动APP抓包 PC端抓包 查看模式 其他功能 问题汇总 1. 前言: Charles是一款抓包修改工具,相比起burp,charles具 ...

  2. openstack临时存储后端

    声明: 本博客欢迎转发.但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 眼下openstack提供了 ...

  3. bat(批处理文件)初步 第一篇 基本符号

    近期我使用的一款软件中须要大量的环境变量设置,而我又不想讲这些变量都加入到系统的环境变量中,一方面是由于有一些同名的库文件的版本号却不一样,都 写在系统环境中会相互干扰:还有一方面则是大部分的路径仅仅 ...

  4. Cocos2d-x3.0游戏实例《不要救我》第十篇(结束)——使用Json配置数据类型的怪物

    如今我们有2种类型的怪物,并且创建的时候是写死在代码里的,这是要作死的节奏~ 所以.必须可配置.不然会累死人的. ; i < size; ++i) { int id = root[i][&quo ...

  5. Atitit.web三编程模型 Web Page Web Forms 和 MVC

    Atitit.web三编程模型 Web Page    Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2. W ...

  6. 对于C11中的正則表達式的使用

    Regular Expression Special Characters "."---Any single character(a "wildcard") & ...

  7. Linux网络编程一站式学习

    提要 学过非常多遍计算机网络,依旧不会网络编程. 看完这篇文章之后就不会是这样了. 环境:Ubuntu14.04 64bit 何为Socket 是基于TCP/IP的网络应用编程中使用的有关数据通信的概 ...

  8. oracle触发农产品证明文件号码

    CREATE OR REPLACE TRIGGER TRG_KC_SPRK_i     BEFORE INSERT ON KC_SPRK     FOR EACH ROW DECLARE     vS ...

  9. Ubuntu升级后apache所有的失败,以解决虚拟文件夹的设置

    问题描述: 将Ubuntu离12.04升级到14.04后,出现apache配置的虚拟文件夹所有失效.所有站点域名所有定向到根文件夹.无法分别訪问! 尝试方法: 開始以为是升级后Apache的问题.已经 ...

  10. 【剑指offer】数字数组中只出现一次(2)

    转载请注明出处:http://blog.csdn.net/mmc_maodun/article/details/27800577 题目:一个int数组中有三个数字a.b.c仅仅出现一次,其它数字都出现 ...