面向对象程序设计-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程序员到RAW开发~
RAW是一款PHP网站开发系统,即使不懂PHP,也可以使用RAW进行PHP程序开发,当然如果已经掌握了PHP,那么使用RAW开发将会是如虎添翼! 怎么理解“如虎添翼”:我们平时要做一个项目的话,我们要 ...
- C语言(2)--数据类型
C语言中提供多种不同的数据类型,用以存放不同的数据. 1.常见的基本类型有:int-->整型 float-->浮点型 double-->双精度浮点型 char-->字符型 NO ...
- MySQL FEDERATED 存储引擎
MySQL中针对不同的功能需求提供了不同的存储引擎.所谓的存储引擎也就是MySQL下特定接口的具体实现. FEDERATED是其中一个专门针对远程数据库的实现.一般情况下在本地数据库中建表会在数据库目 ...
- JAVA中SSH面试问题
1.阐述struts2的执行流程. Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件.核心控制器Filter ...
- 关于QT中evaluateJavaScript()函数返回值的处理问题
关于QT中evaluateJavaScript()函数返回值的处理问题 - 寒风问雪的专栏 - 博客频道 - CSDN.NET 关于QT中evaluateJavaScript()函数返回值的处理问题 ...
- 为什么webview.loadUrl("javascript:function() ")不执行?
这几天搞webview 但是常常有时候会出现webview.loadurl 没有反映的情况对现在的分析如下: 情况一:webview.loadurl 的加载是在另一个线程中执行必须要在webview加 ...
- nginx-lua实现简单权限控制
1,依赖软件:nginx(openresty) mysql(存储用户表)redis(存储用户登录token,有效期1周) create table account( uid integer not n ...
- 微信网页签名失败(invalid signature)
签名失败,建议按以下步骤排查 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验. 确认con ...
- 用Aspose.Cells控件读取Excel
Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...
- NSOperationQueue和GCD的区别
使用NSOperationQueue用来管理子类化的NSOperation对象,控制其线程并发数目.GCD和NSOperation都可以实现对线程的管理,区别是 NSOperation和NSOpera ...