拾人牙慧,浅记一些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: '已打包'; ...
随机推荐
- jquery自定义插件——window实现
该示例实现弹出窗口效应: 1.jquery.show.js /* * 开发人员:lzugis * 开发时间:2014年6月10日 * 实现功能:点击在鼠标位置显示div * 版本号序号:1.0 */ ...
- C#内存分配学习
CLR内存分配分三大块区域:栈.GC堆.大对象堆. 一.线程堆栈(栈) 用于分配值类型实例.栈由操作系统进行管理,不受GC管理,当值类型不在其作用域(主要是指其所在函数内)时,其所占栈空间自动释放.栈 ...
- Java Math的floor,round,ceil函数小结
转自 http://blog.csdn.net/foart/article/details/4295645 floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数( ...
- debian软件安装基础(同tomcat案件)
基本介绍 笔者是一个Linux盲.一旦只在虚拟机上载通过Ubantu-图形版本,我看着接口.打了几场比赛卸载的光盘上. 往下看,在过去的几天.试想想,在Linux关于建设nexus(mavenPW)玩 ...
- 移动端 iphone touchmove滑到边界(浏览器地址拦及以上) touchend失效解决办法
在移动端h5页面:尤其那些全屏幕的盒展示切换页面,当用户无意中将手指滑到了 浏览器地址拦以上(中国移动这快区域):此时,手指已经离开屏幕了,但是ios上无法监听到touchend 事件:touchen ...
- Gradle sourceCompatibility has no effect to subprojects(转)
I have Java 6 and 7 installed on my machine. Gradle uses 1.7 (checked using gradle -v). But I need t ...
- Android开发自学笔记(基于Android Studio1.3.1)—1.环境搭建(转)
一.引言 本套学习笔记的开发环境是Windows 10 专业版和Android Studio 的最新版1.3.1. Android Studio 是一个Android开发环境,基于Intelli ...
- 例如找出令人信服的权威C++中间malloc与new
例如找出令人信服的权威C++中间malloc与new 问题: 非常多人都知道malloc与new都是用来申请空间用的,开辟空间来源于堆中. 可是在C++中却非常少用malloc去申请空间,为什么? 以 ...
- SendRedirect和forward差分
(1)重定向JSP实现JSP/Servlet跳转到目标资源的方法中,基本的想法是:server目标资源完成URL通过HTTP 在回答本报发client浏览器.收到的浏览器URL更新到地址栏后,而目标资 ...
- 军医王-moTestin云测试看好移动医疗行业
看医生汪谟军:Testin云測在移动医疗产业大有可为 2014/10/21 · Testin · 开发人员訪谈 日常生活可能常常碰到这种情况:突然遇上头疼脑热.小病小痛,去医院又不方便:非常想了解家人 ...