真∙面向对象
Object Oriented Programming
class class_name
{
instance varibles
public:
member_functions
private:
...
};
class_name obj_name()//generate_function
obj_name.xxx
特性
区别于Java,可以继承多个类
class C:public A,public B
{
};
接口(Interface)
class Base
{
public:
virtual int func();
}
class R: public Base
{
public:
int func()
{
//...
}
}
与结构体的区别
结构体可以看作一种特殊的类,而且成员默认情况下是公共的(public),类在默认情况下成员是私有的(private),当然结构体可以有public、private、protected变量,也可以有自己的函数,基本用在存储数据的轻量级对象。而类是采用面向对象的思想,除了有各种类型的变量外,也可以有成员函数、构造函数、析构函数,可以从别的类继承(extends),也可以被别的类继承,可以实现接口(implements),除了封装各种数据,更可以用于存在复杂的逻辑关系的情况(表现多级别的对象层次)
一个栗子(关于friend)
class fraction
{
public:
int m = 0;
int n = 1;
int getN(){return n;}
int getM(){return m;}
fraction(int N,int M):n(N),m(M){}
friend ostream& operator << (ostream& os,const fraction &a);
private:
};
ostream& operator << (ostream& os,const fraction &a)
{
return os << a.m << " / " << a.n << endl;
}
fraction f(1,2);
cout << f;
这里的重载有点像Java里的toString()
友元机制
友元函数(friend)
class A{
friend xx yy();
}
xx yy();
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类。
友元类
一个类 A 可以将另一个类 B 声明为自己的友元,类 B 的所有成员函数就都可以访问类 A 对象的私有成员。
class A
{
friend class B;
};
class B
{
};
类成员函数作为友元函数
class A
{
friend int B::func(A a);
};
class B
{
int func(A a);
};
能够提高效率,表达简单、清晰,但是破环了封装机制
构造函数与析构函数
class Box {
public:
// Default constructor
Box() {}
// Initialize a Box with equal dimensions (i.e. a cube)
explicit Box(int i) : m_width(i), m_length(i), m_height(i) // member init list
{}
// Initialize a Box with custom dimensions
Box(int width, int length, int height)
: m_width(width), m_length(length), m_height(height)
{}
int Volume() { return m_width * m_length * m_height; }
private:
// Will have value of 0 when default constructor is called.
// If we didn't zero-init here, default constructor would
// leave them uninitialized with garbage values.
int m_width{ 0 };
int m_length{ 0 };
int m_height{ 0 };
};
explicit
explicit关键字只能用来修饰构造函数。它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况下即声明为implicit(隐式)
使用explicit可以禁止编译器自动调用拷贝初始化,还可以禁止编译器对拷贝函数的参数进行隐式转换。
Box b;
b = 10;❎//隐式转换成b = Box temp(10)
Box b(10);✅
struct
#define MATRIX_SIZE 1001
using namespace std;
struct Matrix
{
int r,c;
int data[MATRIX_SIZE][MATRIX_SIZE];
Matrix(int C,int R):c(C),r(R)
{
memset(data, 0, sizeof(data));
}
int* operator [](int pos)
{
return data[pos];
}
Matrix operator +(Matrix A)
{
Matrix C(r,c);
for(int i = 1;i <= r;++i)
{
for(int j = 1;j <= c;++j)
{
C[i][j] = data[i][j] + A[i][j];
}
}
return C;
}
Matrix operator -(Matrix A)
{
Matrix C(r,c);
for(int i = 1;i <= r;++i)
{
for(int j = 1;j <= c;++j)
{
C[i][j] = data[i][j] - A[i][j];
}
}
return C;
}
Matrix operator *(int b)
{
Matrix C(r,c);
for(int i = 1;i <= r;++i)
{
for(int j = 1;j <= c;++j)
{
C[i][j] = b*data[i][j];
}
}
return C;
}
Matrix operator *(Matrix A)
{
Matrix C(r,A.c);
for(int i = 1;i <= r;++i)
{
for(int j = 1;j <= A.c;++j)
{
for(int k = 1;k <= c;++k)
{
C[i][j] += data[i][k] * A[k][j];
}
}
}
return C;
}
Matrix T()
{
Matrix C(c,r);
for(int i = 1;i <= r;++i)
{
for(int j = 1;j <= c;++j)
{
C[j][i] = data[i][j];
}
}
return C;
}
};