面向对象程序设计-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反射能为我们做些什么,最后介绍几种常用的 ...
随机推荐
- 11.PHP 教程_PHP Switch 语句
switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. 语法 switch (n) { case labe ...
- springmvc附件上传核心代码
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.Comm ...
- maven安装scala插件
默认情况maven不支持scala 所以需要安装maven-scala插件 更新地址:http://alchim31.free.fr/m2e-scala/update-site/ (m2eclipse ...
- mybatis获取插入的语句主键(自增主键)
<insert id="insertUser" parameterType="User"> <selectKey keyProperty=&q ...
- Windows 8.1 IIS 8.5 远程管理 Windows 2008 R2 IIS 7.0
案例: Windows 8.1 x64 IIS 8.5 inetmgr_amd64_v1.1_en-US.msi Windows 2008 R2 x64 IIS 7.0 在Win8.1 通过IIS ...
- 一个int 数组,里面数据无任何限制,要求求出所有这样的数a[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现。
一个int数组, 比如 array[],里面数据无任何限制,要求求出 所有这样的数array[i],其左边的数都小于等于它,右边的数都大于等于它.能否只用一个额外数组和少量其它空间实现. 分析:这题很 ...
- perl5 第五章 文件读写
第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件 语法为open (filevar, file ...
- BZOJ 1088 扫雷Mine (递推)
题解:如果确定了第一排前两个数,那么剩下的数是唯一确定的,所以只要分情况讨论即可. #include <cstdio> #include <cstring> int n,a[1 ...
- Sicily-1028
一. 题意: 算出汉诺塔移动序列中对应位置的号码,数据规模很大,所以不能单纯递归,而是要找出汉诺塔序列的规律. 二. 汉诺塔数列 为了得出最少的移动步数,当n为偶数时,最上 ...
- hdoj 1166 敌兵布阵(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...