本次上课继续讲解了 [ ] 、-> 等运算符重载的具体例子

也讲解了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)【第十一次上课笔记】的更多相关文章

  1. 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】

    这是本门<面向对象程序设计>课最后一次上课,刚好上完了这本<Thinking in C++> :) 这节课首先讲了流 Stream 的概念 平时我们主要用的是(1)在屏幕上输入 ...

  2. 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】

    Sadly, 这节课带过去的笔记本没电了 T^T 导致没有一行 Code, Sorry 笔记如下: Shape * p1; //使用指针创建对象的方法 p = new Circle (2.0); Sh ...

  3. 面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】

    这节课继续讲解了 static 作为静态数据成员 / 成员函数的用法 具体详解我都已注释出来了,大家可以慢慢看 有任何问题都可以在这篇文章下留言我会及时解答 :) //static 静态数据成员 // ...

  4. Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?

    什么是面向对象程序设计? 我们称为OOP(Object  Oriented  Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...

  5. [c++] Operator overloading

    c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...

  6. Type Conversion

    文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本文原更新于作者的github博客,这里给出链接. 什么是转换 转换是接受一个类型的值并使用它作为另一个类型的等价值的过程.转换 ...

  7. (C/C++学习笔记) 十七. 面向对象程序设计

    十七. 面向对象程序设计 ● 面向对象程序设计的基本概念 ※ 类实际上是一种复杂的数据类型,它不仅包含不同类型的数据,还包含对这些数据的一些必要的操作. 而对象则是这种复杂的数据类型的一个变量. 类是 ...

  8. 201871010132-张潇潇-《面向对象程序设计(java)》第六-七周学习总结

    201871010132-张潇潇-<面向对象程序设计(java)>第六-七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh ...

  9. [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程

    [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程 本节导读:本节主要介绍什么是.NET反射特性,.NET反射能为我们做些什么,最后介绍几种常用的 ...

随机推荐

  1. C++ 面向对象学习1

    #include "stdafx.h" #include <iostream> //不要遗漏 否则不能使用cout using namespace std; class ...

  2. Protel99se教程四:将SCH转为PCB文件

    本节课,我们介绍,如何快速的将绘制好的SCH文件转为PCB文件,首先,我们打开刚开始时我们绘制的SCH原理图,我们可以使用protel99se菜单栏的view-Fit All Objects命令,以查 ...

  3. mongodb 学习初探

    1.去mongodb 官方下载 http://www.mongodb.org/downloads 2.下载php的mongodb扩展 http://files.cnblogs.com/lsl8966/ ...

  4. 数据结构- 串的模式匹配算法:BF和 KMP算法

      数据结构- 串的模式匹配算法:BF和 KMP算法  Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...

  5. js遍历对象的属性并且动态添加属性

    var person= { name: 'zhangsan', pass: '123' , 'sni.ni' : 'sss', hello:function (){ for(var i=0;i< ...

  6. PHP移动互联网开发(1)——环境搭建及配置

    原文地址:http://www.php100.com/html/php/rumen/2014/0326/6702.html 一.PHP5.4环境搭配基本流程 Apache:Web服务提供者.官网:ww ...

  7. 字符串String类

    1. String类是一个密封类.用关键字sealed修饰: 2. 字符串的两个特性:     ·不可变性:string类型变量,一旦声明就表明它是不会被改变的.因此,string中的方法对strin ...

  8. BZOJ 4143 The Lawyer

           这道题看起来很吓人,但事实上看懂后会发现,其根本没有任何技术含量,做这道题其实要考虑的就是每天最早结束的一场的结束时间以及最晚开始的一场的开始时间,如果结束时间早于开始时间,那么OK就这 ...

  9. 杭电oj An easy problem

    </pre><h1 style="color: rgb(26, 92, 200);">An easy problem</h1><stron ...

  10. Java基础之编程语法(一)

    1.基本格式 所有Java代码都应该在一个class中. Java是严格区分大小写的. Java是一种自由格式的语言.Java代码分为结构定义语句和功能执行语句,功能执行语句最后必须以分号结束. 2. ...