理解 C++ 基础面向对象编程
先决条件
- 您的计算机上已安装 C++ 编译器
- C++基础知识
- 集成开发环境 (IDE) 或文本编辑器
- 时间
- 开放的心态
介绍
面向对象编程是一种编程范式,它在代码中表示现实生活中的对象或实体。
首先,你需要理解面向对象编程中的两个基本但至关重要的概念,即类和对象。
什么是类?
类是定义对象属性和行为的蓝图。假设我们要创建一个名为“人类”的新类,我们可以指定一些人类特有的属性。
...
class human {
public:
string name;
int age;
};
...
在上面的代码片段中,我们创建了一个具有属性的类name。age
什么是物体?
对象是类的实例,之所以称之为实例,是因为它可以拥有类的任何属性或特性。举例来说,我们人类就是我们基因的实例。
利用我们之前创建的类,我们可以创建一个对象,然后为其分配属性name和age值。
#include <iostream>
using namespace std;
class human {
public:
string name;
int age;
};
int main () {
human exhibitA;
exhibitA.name = "Michael";
exhibitA.age = 25;
cout <<"Exhibit A's Name is "<< exhibitA.name << endl;
cout <<"Exhibit A's Age is "<< exhibitA.age << endl;
return 0;
}
在上面的代码片段中,我们已经成功创建了一个类,并且创建了一个对象,并将父类的属性分配给了该对象。
至此,你应该已经了解了类和对象的概念。请注意,一个类并不局限于一个对象,你可以创建任意数量的对象。
#include <iostream>
using namespace std;
class human {
public:
string name;
int age;
};
int main () {
human exhibitA;
human exhibitB;
exhibitA.name = "Michael";
exhibitA.age = 25;
exhibitB.name = "Nerfetiti";
exhibitB.age = 24;
cout <<"Exhibit A's Name is "<< exhibitA.name << endl;
cout <<"Exhibit A's Age is "<< exhibitA.age << endl;
cout <<"Exhibit B's Name is "<< exhibitB.name << endl;
cout <<"Exhibit B's Age is "<< exhibitB.age << endl;
return 0;
}
如上文代码片段所示,创建了一个新对象,并根据父类中的定义为其分配了属性。
什么是行为?
我们要探讨的另一个概念是行为,行为是指物体可以执行的动作。例如,人可以跑步、吃饭、睡觉等等。
#include <iostream>
using namespace std;
class human {
public:
string name;
int age;
void run () {
cout << name <<" is running" << endl;
}
};
int main () {
human exhibitA;
exhibitA.name = "Michael";
exhibitA.run();
return 0;
}
如上面的代码片段所示,创建了一个函数,用于传递公共名称变量以打印“Michael is running”。根据分配给该函数/行为的对象,它将通过点号表示法传递该函数/行为所需的参数。
什么是过载?
函数重载是一种允许你使用同一个函数名,根据传递给它的参数执行不同操作的概念。在这个例子中,我们将创建一个新的行为eat(),分别接受无参数、一个参数和两个参数。
#include <iostream>
using namespace std;
class human {
public:
string name;
int age;
void eat () {
cout << name <<" is eating" << endl;
}
};
int main () {
human exhibitA;
exhibitA.name = "Michael";
exhibitA.eat();
return 0;
}
如上所示,我们创建了一个名为 name 的行为eat,它没有参数,应该返回“Michael is eating”或你声明的任何名称exhibitA。
接下来,我们将eat通过要求另一个也名为 eat 的行为的参数来加重该行为。
#include <iostream>
using namespace std;
class human {
public:
string name;
int age;
void eat () {
cout << name <<" is eating" << endl;
}
void eat (string food1) {
cout << name <<" is eating "<< food1 << endl;
}
};
int main () {
human exhibitA;
exhibitA.name = "Michael";
exhibitA.eat();
exhibitA.eat("Rice");
return 0;
}
上面的代码片段有第二个eat函数,但需要一个参数,该参数根据分配给它的对象传递给“Michael is eating Rice”这个函数。
现在,让我们最后创建一个eat接受两个参数的新行为。
#include <iostream>
using namespace std;
class human {
public:
string name;
int age;
void eat () {
cout << name <<" is eating" << endl;
}
void eat (string food1) {
cout << name <<" is eating "<< food1 << endl;
}
void eat (string food1, string food2) {
cout << name <<" is eating "<< food1 << " and " << food2 << endl;
}
};
int main () {
human exhibitA;
exhibitA.name = "Michael";
exhibitA.eat();
exhibitA.eat("Rice");
exhibitA.eat("Rice", "Beans");
return 0;
}
该代码片段的最终输出是“Michael 正在吃米饭和豆子”,最后一次调用触发了human 类中声明的两个参数的eat重载行为,因为我们恰好传递了两个参数。eat
你会发现,eat无论eat在之前或之后声明其他行为,所有其他已声明的行为仍然有效。
结论
现在你应该对面向对象编程的工作原理有所了解了,而且这个概念不仅适用于 C++,它也适用于其他编程语言,只是语法有所不同。我漏掉了一些概念,比如继承、多态、数据抽象和接口。我会在另一篇文章中尝试讲解这些概念,祝你学习愉快。
文章来源:https://dev.to/ichtrojan/understanding-basic-object-orient-programming-with-c-3d0a





