拾人牙慧,浅记一些C++的类
这两天没事又翻了翻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++的类的更多相关文章
- C# 中堆与栈的浅记
C# 中堆与栈的浅记 什么是堆和栈? 简言之.堆和栈是驻留在内存中的区域,它们的作用是帮助我们运行代码.在.Net Framework 环境下,当我们的代码运行时,内存中的堆和栈便存储了这些代码,并包 ...
- C#中值类型和引用类型的差别浅记
C#中值类型和引用类型的差别浅记 在C#中,变量的类型分为两种.各自是值类型和引用类型. 值类型的变量直接存储值,说得更详细一些,就是值类型变量在内存中直接存储它们自身 ...
- hybrid浅记
目前首次接触hybrid项目,故根据翻阅了解后,浅记对它的认识. hybrid是携程推出的一个项目框架,其优点是:跨平台.开发效率高.开发成本相对较低,其不足是:体验不如Native hybrid设计 ...
- 浅谈Java的匿名类
在实际的项目中看到一个很奇怪的现象,Java可以直接new一个接口,然后在new里面粗暴的加入实现代码.就像下面这样.那么问题来了,new出来的对象没有实际的类作为载体,这不是很奇怪吗? 思考以下代码 ...
- 浅谈JAVA中“增强”类的某个方法的几个中方法!
一.继承 使用场景:能够控制这个类的构造的时候,才可以使用继承. 优点:简单容易使用, 缺点:耦合性大大的增强,不利于后期的维护,所以对于继承这种方法,谨慎使用. 代码实现:二.装饰者模式 使用场景 ...
- ES6躬行记(21)——类的继承
ES6的继承依然是基于原型的继承,但语法更为简洁清晰.通过一个extends关键字,就能描述两个类之间的继承关系(如下代码所示),在此关键字之前的Man是子类(即派生类),而在其之后的People是父 ...
- ES6躬行记(20)——类
ES6正式将类(Class)的概念在语法层面标准化,今后不必再用构造函数模拟类的行为.而ES6引入的类本质上只是个语法糖(即代码更为简洁.语义更为清晰),其大部分功能(例如继承.封装和复用等)均可在E ...
- 浅记初次使用expect、scp中出现的一些小问题
以前也学过一些shell,不过学得并不是很深入,动手写的代码的时间也不是很多.前不久将shell比较细的过了一遍,leader布置了任务让用shell写一个脚本将redis源码压缩包从一个服务器上传到 ...
- 浅谈css常用伪类用法
着重写一下after和before的用法: css样式搞定:标签元素+伪类after a.'class名':after{//我的样式名称是这个,可以写成你自己的样式名称 content: '已打包'; ...
随机推荐
- Swing动画之游戏角色
一.动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动. 二.实现原理: 1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果,动画就是更 ...
- 探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据
上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...
- VS2010或2012中,如何设置代码格式化?
ctrl + E,D菜单在 编辑-->高级 里面 第一个菜单项
- Nova创建虚拟机的底层代码分析
作为个人学习笔记分享.有不论什么问题欢迎交流! 在openstack中创建虚拟机的底层实现是nova使用了libvirt,代码在nova/virt/libvirt/driver.py. #image_ ...
- 看你的门-攻击服务器(4)-HTTP参数注入攻击
首先需要声明.这纯粹是没有远见和有点真才实学开发一个愚蠢的观点,只为web参考系统安全. 1.HTTP参数注入攻击 參数,被用做后端HTTP请求中的參数,这个时候就有可能会导致HTTP參数注入. 一个 ...
- js 滚轮事件 滚轮焦点图(轮播图)
利用滚轮,切换轮播图.附带mousewheel插件以及原生js写法: <!doctype html> <html> <head> <meta charse ...
- SpringMVC源代码深度分析DispatcherServlet核心的控制器(初始化)
SpringMVC是非常优秀的MVC框架,每一个框架都是为了我们提高开发效率,我们试图通过对SpringMVC的源码去了解这个框架,了解整个设计思想,框架要有扩展性,这里用的比較多是接口和抽象,是框架 ...
- android内置存储器memory和第三方外部存储disk管理
缓存管理这里 http://blog.csdn.net/intbird/article/details/38338713 图片处理在这里 http://blog.csdn.net/intbird/ar ...
- 【jQuery】使用JQ要准备的主要淡入淡出效果
jQuery是JavaScript 库.也就是JavaScript延期,加入满足不同效果的不断增长的需求.事实上质量JavaScript 下面写的一大JQ方案说明JQ. .基本目标 网页中有例如以下三 ...
- NSIS:简单按钮美化插件SkinButton,支持透明PNG图片。
原文 NSIS:简单按钮美化插件SkinButton,支持透明PNG图片. 征得作者贾可的同意,特发布按钮美化插件SkinButton. 插件说明: 使用GDI+库写的一个简单按钮美化插件,支持透明P ...