about class (updating)
类的初始化、构造函数的定义
当未定义构造函数时会自动生成一个构造函数,默认为空
#include<iostream>
using namespace std;
class fun{
public:
fun();
void show_it();
private:
int a;
int b;
int c;
}; //注意分号结尾
fun::fun(): a(1),b(2),c(3){} //不用分号结尾
void fun::show_it(){
cout << a << ' ' << b << ' ' << c << endl;
}
int main(){
fun temp;
temp.show_it();
return 0;
}
关于构造函数的花括号使用
#include<iostream>
#include<cstring>
using namespace std;
class fun{
public:
fun(int i,char j,char k[]);
void show_it();
private:
int a;
char b;
char c[];
}; //注意分号结尾
fun::fun(int i,char j,char k[]): a(i),b(j){strcat(c,k);} //不用分号结尾,花括号内是需要执行的函数,若无函数需要执行可不写内容
void fun::show_it(){
cout << a << ' ' << b << ' ' << c << endl;
}
int main(){
fun temp(1,'1',"123");
fun alls[2] = {
fun(2,'2',"213"), //注意是逗号
fun(3,'3',"321") //注意没有分号结束
};
temp.show_it();
alls[0].show_it();
alls[1].show_it();
return 0;
}
析构函数
~ + 类名 表示析构函数,清理内存空间
先构造的后析构
#include<iostream>
using namespace std;
class stu{
public:
stu(){
cout << "start here" << endl;
}
~stu(){
cout << "end here" << endl;
}
};
int main(){
stu a;
cout << "pause" << endl;
return 0;
}
类的复制和赋值
#include<iostream>
using namespace std;
class Box{
public :
void show(){cout << "Hi the B is : " << B << " and the A is : " << A << endl;}
Box(int a,int b);
private :
int A;
int B;
};
Box::Box(int a,int b):A(a), B(b){cout <<"Done" << endl;}
int main(){
Box box1(1,2);
cout << "Here is box1" << endl;
box1.show();
Box box2(box1); //直接复制box1
//这里的构造函数是自带的复制构造函数,不是上面定义的构造函数
or
Box box2 = box1; //box1赋值给box2
cout << "Here is box2" << endl;
box2.show();
return 0;
}
运算符重载
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
Box operator+(const Box&);
声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:
Box operator+(const Box&, const Box&);
例子:
#include <iostream>
using namespace std;
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
// 重载 + 运算符,用于把两个 Box 对象相加
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 程序的主函数
int main( )
{
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 把体积存储在该变量中
// Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
重载
#include <iostream>
using namespace std;
class complex{
public:
complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
public:
friend complex operator+(const complex & A, const complex & B);
friend complex operator-(const complex & A, const complex & B);
friend complex operator*(const complex & A, const complex & B);
friend complex operator/(const complex & A, const complex & B);
friend istream & operator>>(istream & in, complex & A);
friend ostream & operator<<(ostream & out, complex & A);
private:
double m_real; //实部
double m_imag; //虚部
};
//重载加法运算符
complex operator+(const complex & A, const complex &B){
complex C;
C.m_real = A.m_real + B.m_real;
C.m_imag = A.m_imag + B.m_imag;
return C;
}
//重载减法运算符
complex operator-(const complex & A, const complex &B){
complex C;
C.m_real = A.m_real - B.m_real;
C.m_imag = A.m_imag - B.m_imag;
return C;
}
//重载乘法运算符
complex operator*(const complex & A, const complex &B){
complex C;
C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;
C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;
return C;
}
//重载除法运算符
complex operator/(const complex & A, const complex & B){
complex C;
double square = A.m_real * A.m_real + A.m_imag * A.m_imag;
C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;
C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;
return C;
}
//重载输入运算符
istream & operator>>(istream & in, complex & A){
in >> A.m_real >> A.m_imag;
return in;
}
//重载输出运算符
ostream & operator<<(ostream & out, complex & A){
out << A.m_real <<" + "<< A.m_imag <<" i ";;
return out;
}
int main(){
complex c1, c2, c3;
cin>>c1>>c2;
c3 = c1 + c2;
cout<<"c1 + c2 = "<<c3<<endl;
c3 = c1 - c2;
cout<<"c1 - c2 = "<<c3<<endl;
c3 = c1 * c2;
cout<<"c1 * c2 = "<<c3<<endl;
c3 = c1 / c2;
cout<<"c1 / c2 = "<<c3<<endl;
return 0;
}
不可重载的运算符:
- .:成员访问运算符
- ., ->:成员指针访问运算符
- :::域运算符
- sizeof:长度运算符
- ?::条件运算符
-
:预处理符号