本次上课继续讲解了 [ ] 、-> 等运算符重载的具体例子
也讲解了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 ;
}