面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】
本次上课继续讲解了 [ ] 、-> 等运算符重载的具体例子
也讲解了C++单个参数的类的类型转换的案例
最后稍微提到了 static 的第三种作用:静态数据成员
具体详解我都已注释出来了,大家可以慢慢看
有任何问题都可以在这篇文章下留言我会及时解答 :)
#include <iostream>
#include <cmath> using namespace std; class myArray {
private:
float * p;
unsigned int size;
public:
myArray (unsigned int len = );
~myArray ();
unsigned int getSize () const;
double moudar () const; const float & operator [] (int index) const; //下标运算符只能被重载在成员函数中
float & operator [] (int index); //Only member function
}; myArray::myArray (unsigned int len) {
float * tmp = new float[len];
if (NULL == tmp) { //Check whether new work OK
cout << "memory allocation error!" << endl;
p = NULL;
size = ;
} else {
p = tmp;
size = len;
for (int i = ; i < len; ++i) {
p[i] = 0.0f;
}
}
} myArray::~myArray () {
if (NULL != p) {
delete [] p;
size = ;
}
p = NULL; //保证析构后 p 指针为空
} unsigned int myArray::getSize () const {
return size;
} double myArray::moudar () const {
//float sum = 0.0f;
double sum = 0.0;
for (int i = ; i < size; ++i) {
float a = p[i];
sum += a * a;
}
sum = sqrt (sum);
return sum;
} const float & myArray::operator [] (int index) const {
return p[index];
} float & myArray::operator [] (int index) {
return p[index];
} ostream & operator << (ostream & out, const myArray & ma) {
int size = ma.getSize ();
out << "{";
for (int i = ; i < size - ; ++i) {
out << ma[i] << ", ";
}
out << ma[size - ] << "}" << endl;
return out;
} int main() { myArray ma(); //init one object
double length = ma.moudar (); float element = ma[]; //Get the element
ma[] = 12.5f; //Update the element value
ma[] = 3.8f; cout << ma << endl; //!! Only golbal function return ;
}
// = () [] -> ->* Only overloaded by member function
// << 在表达输出的时候,只能重载为全局函数;表达移位运算的时候,都可以
#include <iostream>
using namespace std;
class myClassA {
public:
int i;
myClassA (int k = ) : i(k) { cout << "myClassA init" << endl; };
};
class myClassB { //类B负责对其子对象创建,如果什么都不写,就是用默认
//构造函数创建,否则,需要在成员初始化列表写
public:
int m;
myClassA a; //对象成员,创建B对象的时候,会先对A对象构造
myClassA * operator -> ();
//myClassB * operator -> ();
myClassB (int j) : a(j), m() { cout << "myClassB init" << endl; };
};
/*
myClassB * myClassB::operator->() {
cout << "myClassB * myClassB::operator->() " << endl;
return this;
}
*/
myClassA * myClassB::operator->() {
cout << " myClassA * myClassB::operator->() " << endl;
return &a;
}
int main() {
myClassB b ();
cout << b->i << endl;
//cout << b->m << endl;
return ;
}
//C++单个参数的类的类型转换
//类的转化 #include <iostream> using namespace std; //class A; class Integer {
int i;
//A a;
public:
//explicit 的作用 : 告诉编译器,如果要转化,一定是显式 explicit Integer (int k = ) : i (k) { cout << "Integer (int k = 0)" << endl; }
Integer & operator = (const Integer & obj);
friend ostream & operator << (ostream & out, const Integer & ing); //operator A() { return a; }
explicit operator int () { return i; } //类型转换函数
explicit operator double () { return i + 45.51; } //类型转换函数
}; ostream & operator << (ostream & out, const Integer & ing) {
out << ing.i << endl; return out;
} Integer & Integer::operator = (const Integer & obj) {
if (this == &obj) return *this; //检查自赋值
i = obj.i;
cout << "operator = (const Integer & obj) " << endl;
return *this;
} int main () { Integer a (), b;
b = Integer ();
cout << b << endl; int m = int (b); //将Integer 类型转换成整形
cout << m << endl; double d = double (b);
cout << d << endl; return ;
}
//static 静态数据成员
#include <iostream>
using namespace std;
class Integer {
//static int i;
static int number; //整个类只有一个版本,所有对象共享
public:
Integer (int k = ) { ++number; }
static int getNumber () const { return number; }
};
int Integer::number = ;
int main () {
Integer Zhao, jin, wei, shi, tian, cai;
cout << Zhao.getNumber () << endl;
cout << Integer::getNumber()<< endl;
return ;
}
面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】的更多相关文章
- 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】
这是本门<面向对象程序设计>课最后一次上课,刚好上完了这本<Thinking in C++> :) 这节课首先讲了流 Stream 的概念 平时我们主要用的是(1)在屏幕上输入 ...
- 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】
Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...
- 面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】
这节课继续讲解了 static 作为静态数据成员 / 成员函数的用法 具体详解我都已注释出来了,大家可以慢慢看 有任何问题都可以在这篇文章下留言我会及时解答 :) //static 静态数据成员 // ...
- Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
什么是面向对象程序设计? 我们称为OOP(Object Oriented Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
- Type Conversion
文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本文原更新于作者的github博客,这里给出链接. 什么是转换 转换是接受一个类型的值并使用它作为另一个类型的等价值的过程.转换 ...
- (C/C++学习笔记) 十七. 面向对象程序设计
十七. 面向对象程序设计 ● 面向对象程序设计的基本概念 ※ 类实际上是一种复杂的数据类型,它不仅包含不同类型的数据,还包含对这些数据的一些必要的操作. 而对象则是这种复杂的数据类型的一个变量. 类是 ...
- 201871010132-张潇潇-《面向对象程序设计(java)》第六-七周学习总结
201871010132-张潇潇-<面向对象程序设计(java)>第六-七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...
- [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程
[.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程 本节导读:本节主要介绍什么是.NET反射特性,.NET反射能为我们做些什么,最后介绍几种常用的 ...
随机推荐
- php 通过referer防盗链(以图片为例)
1.在网页里访问站外图片时,服务器如何知道是在站外引用的呢? (1)对比本服务器请求与跨服务器请求 图一——本服务器请求 图二——显示盗链的referer信息 通过对比也就知道referer显示的是引 ...
- 用wfastcgi在IIS下部署Django&Flask
Django跟Flask在Linux底下都可以很方便地以FastCGI模式部署,貌似IIS下面不很好配置,而且IIS也缺少一个像PHPmanager一样的全自动配置工具,在公司服务器上部署起来颇费周折 ...
- js学习笔记第一课(js基础知识)
1.js代码在浏览器中执行. 2.js代码直接插入网页中需包含在 <script language="javascript"> js代码 </script> ...
- 链队列之C++实现
链队列时建立在单链表的基础之上的.由于是动态分配节点内存,所以无需判满. 链队列的形式如下: 1.队列空 2.队列存在数据 下面介绍下C++实现的链队列,VC6下调试通过. 1.文件组织 2.lq.h ...
- 用Mediawiki做百科网站资源大参考
MediaWiki简易安装教程**关于mediawiki 一些好的资料: http://codex.wordpress.org.cn/Mediawiki%E5%BB%BA%E7%AB%99%E7%BB ...
- poj 2051 Argus(优先队列)
题目链接: http://poj.org/problem?id=2051 思路分析: 优先级问题,使用优先队列求解:当执行某个任务后,再增加一个任务到队列中, 该任务的优先级为执行任务的时间加上其时间 ...
- USB设备在连接PC时的reset从何而来?
近期在做烧写工具的优化工作,有一些关于USB的内容须要总结一下当中包含设备的初始化过程和枚举过程. 在枚举的过程中,设备会一直等PC端的状态,当等到reset命令时会对设备进行又一次枚举.可是这个re ...
- [每日一题] 11gOCP 1z0-052 :2013-09-15 Enterprise Manager Support Workbench..................B9
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11715219 正确答案:ABD EnterpriseManger Support Work ...
- ps快速删除圆角图片旁白的白色区域方法
简单实用5招的ps快速删除圆角图片旁白的白色区域方法 1.图像-模式-rgb颜色 2.双击背景取消图层锁定 3.用魔棒工具点击要删除的区域 4.delete删除 5.另存为png图片
- Canvas使用渐变之-线性渐变详解
在canvas里面,除了使用纯色,我们还能把填充和笔触样式设置为渐变色:线性渐变和径向渐变. 线性渐变 createLinearGradient(x0,y0,x1,y1) 返回 CanvasGrad ...