面向对象只是更优秀的面向过程!
抽象、封装、继承、多态四个要素是面向对象的四要素(所以是c++是面向对象的编程)。
而c语言只有抽象、部分封装和一点点多态所以只能称得上是面向过程。
类是更合理的库!
因为相比之下支持了完整的抽象、封装、继承、多态的功能,变得更加合理。
所以就会有人问:
class和struct有什么本质的区别?
下面有两块代码:
struct people {
int a;
people()
{
a = 10;
}
int out(void)
{
return a;
}
};
int main()
{
people x[10];
cout << x[2].out() << endl;
return 0;
}
class people {
private:
int a;
public:
people() { a = 10; }
int out(void)
{
return a;
}
};
int main()
{
people x[10];
cout << x[2].out() << endl;
return 0;
}
甚至是这玩意!
struct people {
protected:
int a;
public:
people()
{
a = 10;
}
friend void out(people s[]);
};
void out(people s[])
{
for (int i = 0; i < 10; i++)
cout << s[i].a<<" ";
}
int main()
{
people x[10];
out(x);
return 0;
}
你会发现在现在最新的编译器上,似乎class和struct似乎成了一个玩意……
所以面向对象不是class和struct的区别,
只要是一个“数据集”支持抽象、封装、继承、多态的功能,那它就可以称得上是面向对象。