C++类设计1(Class without pointer members)

class complex{
public:
complex (double r = 0, double i = 0):re(r), im(i){} //inline
complex& operator += {const complex&};
double real() const{return re;} //inline
double imag() const{return im;} //inline
private:
double re,im;
friend complex& _doap1 (complex*, const complex);
};
inline double imag(const complex& x){ //inline
return x.imag();
}

3.访问级别
private: 数据部分,封装,不被外界(class外)访问。
public: 可以被外界访问(class外)的。
定义时不一定集中在两段,可以根据实际情况交替使用两个关键字。
4 构造函数(ctor)
创建对象是调用构造函数,函数名与类名相同,无返回值类型。
下述三种创建对象方式均调用构造函数

class complex{
public:
complex (double r = 0, double i = 0):re(r), im(i) {} //可以带参数,参数可以有默认值,即未指明时使用默认值
//初始化列表 : re(r),im(i) 能使用初始化列表时尽量使用
complex& operator += {const complex&};
double real() const{return re;}
double imag() const{return im;}
private:
double re,im;
friend complex& _doap1 (complex*, const complex);
};
{
complex c1(2,1); //三种方式均调用构造函数
complex c2;
complex *p = new complex(4);
...
}

4.2 构造函数在private 区域
不能被外界调动,例如在单例模式中使用

class A{
public:
static A& getInstance(); //静态类型变量
setup(){}
private:
A(); //构造函数在private区域
A(const A& ths);
...
};
A& A::getInstance(){
static A a;
return a;
}
A::getInstance().setup(); //使用时利用类的getInstace()函数

5 重载(overloading)
同名函数可以存在,判断是否可以重载即判定编译器是否能区分两种函数使用。
double real() const {return re;}
void real(double r) {re = r;} //正确重载
complex(double r= 0, double i = 0): re(r),im(i) {};
complex() : re(r), im(i){}; // 错误,编译器无法判断, 如: complex c1; 不知道调用哪个。
6 const member functions
注意const functions中 const 的位置: 函数名之后,函数体之前
double real() const{return re;}
double imag() const{return im;}
//不改变数据内容,则一定要加const,否则可能引起错误,如:
//外界调用该函数时 使用:const complex c1(1,2);
//声明常对象,常对象不可调用非常成员函数,出现错误
7 pass by value or pass by reference (to const)
传值会造成开销较大(基本类型可以传值),能传引用时尽量传递引用(开销仅4bytes)。
如果不想给予对方修改权限,则使用const关键字 如 const complex& 为常见形式。

class complex{
public:
complex (double r = 0, double i = 0):re(r), im(i){} //pass by value
complex& operator += {const complex&}; //pass by reference
double real() const{return re;}
double imag() const{return im;}
private:
double re,im;
friend complex& _doap1 (complex*, const complex&); //pass by reference
};

8 return by value or return by reference
尽量传递引用,不可以情况参考后 8.1 和临时变量章节
complex& operator += {const complex&};
friend complex& __doapl (complex*, const complex&);
8.1 什么时候可以return by reference
函数运算返回结果
1)必须在函数内新创建对象,不能返回reference,因为函数结束时,对象消亡,引用指向的本体已经不在。
2)当返回结果是已经存在的对象,可以返回引用,常见情况this指针

inline complex&
__doapl(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex&
complex::operator += (const complex& r)
{
return __doapl (this, r);
}

8.2 pass/ return by reference 语法分析
传递者无需知道接收端以何种形式进行接收;
如下例,返回传递仅需传递原值,接收者自己决定接收引用或接受值(调用拷贝构造)。

inline complex&
__doapl(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}

9. 友元(friend)

friend complex& __doapl (complex*, const complex&); inline complex&
_doap1 (complex* ths, const complex& r){
ths->re += r.re; //自由取得friend的private变量,但破坏封装
ths->im +=r.im;
return *ths;
}

9.1 相同类(class)的各个对象(objects)之间互为友元

class complex{
public:
complex (double r = 0, double i = 0):re(r),im(i){
}
int func(const complex& param){
return param.re + param.im;
} //直接拿param私有变量,可以用相同类的各个对象之间互为友元解释
private:
double re, im;
};

10. 运算符重载1 ,成员函数(operator overloading)
二元操作符被编译器看待的形式:(注意是看待形式,this存在可以使用,但参数列表中this不可写出)this指向调用者

实际代码如下:

inline complex&
__doapl(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex&
complex::operator += (const complex& r) //operator += 在程序员使用时可能采用c1 += c2 += c3形式
// 故需要返回complex&类型,不能是void, 类似还有重载cout返回类型
{
return __doapl (this, r)
}

11. 运算符重载2 非成员函数(无this指针)
全局函数,无this指针.
例如此复数类,重载加减等符号时,存在复数加减实数情况,故不宜使用成员函数。

inline complex
operator + (const complex& x, const complex& y)
{
return complex (real (x) + real (y),
imag (x) + imag (y)); //返回的是函数内临时变量(local object),函数结束后消亡,故不可使用return by reference
}
inline complex
operator + (const complex& x, double y)
{
return complex (real (x) + y, imag (x));
} inline complex
operator + (double x, const complex& y)
{
return complex (x + real (y), imag (y));
}

流操作符重载:
#include
ostream&
operator << (ostream& os, const complex&){ //两个参数为 << 左边和右边,如 cout << c1; cout类型为ostream,
return os << '(' << real(x) << ','
<< imag(x) << ')';
}
12 complex 类完整代码 与测试代码
complex.h
complex_testC++类设计1(Class without pointer members)的更多相关文章
- C++类设计2(Class with pointer members)
二 Class with pointer members(Class String) 1. 测试代码(使用效果) int main() { String s1(), String s2("h ...
- iOS控制器之基类设计
题记 在进入新公司后.经过这一个月的重构项目,终于把项目做到了个人相对满意的程度(还有一种不满意的叫老板的需求,提过多次意见也没用= =!).在这次重构中按照以前的思路设计出了个人觉得比较适用的一个基 ...
- 水果项目第1集-想法>需求->功能->数据库设计->类设计
懒,懒人,我是个懒人. 懒人想做点事,总是拖拖拉拉,迟迟没有开始. 很久很久以前,就想做属于自己的产品,但是至今还没有一个属于自己的产品. 两年前,终于想好,要做一个网上卖水果的系统,手机上点点,水果 ...
- [theWord] 一种英文字典的基类设计
theWord --- 一种英文字典的基类设计 使用场景 想写一个应用,来记录自己背单词时候,对每个单词的记忆状况之类的东西.至于为什么做这个,试过了一些背单词软件,并不觉得好用,自己做一个吧. 那么 ...
- YTU 2602: 熟悉题型——类设计( 矩形类定义【C++】)
2602: 熟悉题型--类设计( 矩形类定义[C++]) 时间限制: 1 Sec 内存限制: 128 MB 提交: 183 解决: 119 题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标 ...
- Java 类设计技巧
摘自<Java核心技术>卷I:基础知识 p140 第4章对象与类 - 类设计技巧 1)一定将数据设计为私有. 最重要的是:绝对不要破坏封装性.有时候,需要编写一个访问器方法或更改器方法,但 ...
- 字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变
字体图标 ''' fa框架: http://fontawesome.dashgame.com/ 下载 => 引入css文件 引入字体图标库 <link rel="styleshe ...
- Java11-java基础语法(十)类设计综合案例
Java11-java语法基础(十)类设计综合案例 一.类综合设计方法 1.类设计步骤 (1)分析数据成员 (2)分析成员方法和构造方法 (3)画出类图 (4)编码测试 2.具体问题 1)分析数据成员 ...
- JFreeChart绘制XY折线图(工具类设计)
准备用Java写通信的仿真平台作为毕业设计,相比matlab绘图,Java绘图需要自己去写很多工具类,博主在这采用了JFreeChart的开源解决方案,摸索着自己写了一个XY折线图工具类,话不多说贴源 ...
- iOS 基于MVC设计模式的基类设计
iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00
随机推荐
- Solr的SolrCloud与Master-slave主从模式对比
第一印象 SolrCloud是Solr4.0引入的,主要应对与商业场景.它很像master-slave,却能自动化的完成以前需要手动完成的操作.利用ZooKeeper这个工具去监控整个Solr集群,以 ...
- 满足java对redis的所有操作,token,验证码过期时间等
很实用 链接在此 https://www.cnblogs.com/edisonfeng/p/3571870.html System.out.println("=============== ...
- css与JS知识整理
CSS知识 CSS选择器:基础中的核心,选择器指明了{}中“样式”作用的对象,也就是“样式”作用于网页中的哪些元素. CSS选择器类型:一共三大类. 基础选择器 属性选择器 组合选择器 ...
- jquery测试解析
1.下列获取元素范围大小顺序错误的是 (选择一项) 1 A: B: C: D: 本题选择D 解析: 获取元素范围大小顺序依次为: $(#one).siblings("div")&g ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天(非原创)
文章大纲 一.课程介绍二.Redis基础实战三.Redis之高可用.集群.云平台搭建实战四.淘淘商城Jedis整合spring五.项目源码与资料下载六.参考文章 一.课程介绍 一共14天课程(1) ...
- kafka基础六
kafka中的高可用HA 1.replication副本 同一个partition会有一个leader和多个副本,这些副本存储的内容与leader相同,可以通过 server.properties 配 ...
- Windows下Apache+PHP+MySQL开发环境的搭建(WAMP)
准备工作: 1.下载apache服务器安装包,官网http://www.apache.org/,下载地址:http://httpd.apache.org/download.cgi 2.下载MySQL, ...
- selenium-Python之定位下拉框选择
1.通过select 进行定位下拉框 下拉框如图所示 通过代码定位 #通过index进行选择Select(driver.find_element_by_id("cardType") ...
- vue+element ui项目总结点(三)富文本编辑器 vue-wangeditor
1.参考 https://www.npmjs.com/package/vue-wangeditor 使用该富文本编辑器 <template> <div class="egi ...
- vue.js学习总结
下面使用的命令工具为git bash 使用命令行工具搭建vue.js项目 vue.js官网命令行工具安装 为了提升安装速度,建议将 npm 的注册表源设置为国内的镜像 1.输入命令:npm insta ...