目录
【PS】本篇文章基本上用于自己学习的备份。如果有任何理解有问题的地方请在评论中指出,非计算机专业学生,错误还请多多谅解,谢谢!
本篇文章的引文基本均出自Starting Out with C++ From Control Structures Through Objects 8th Global Edition 如果有兴趣的可以去自己学习研究。
1. 面对对象的语言(object-oriented programming (OOP))
与C语言不同,C++是面对对象的语言。与其相对的,是面向过程的语言©。顾名思义,面对对象的语言是围绕"对象(Obejct)"进行的编程。
a. 为什么要使用面对对象的语言?
Procedural programming has worked well for software developers for many years. However, as programs become larger and more complex, the separation of a program’s data and the code that operates on the data can lead to problems. For example, the data in a procedural program are stored in variables, as well as more complex structures that are created from variables. The procedures that operate on the data must be designed with those variables and data structures in mind. But, what happens if the format of the data is altered? Quite often, a program’s specifications change, resulting in redesigned data structures. When the structure of the data changes, the code that operates on the data must also change to accept the new format. This results in additional work for programmers and a greater opportunity for bugs to appear in the code.
面对过程的语言对于数据是直接通过过程(procedure/method)对数据进行处理。这样带来的结果是,如果数据的格式发生了改变,则整个处理过程的框架都会发生改变,修改这整个过程框架就会产生漏洞。下图是面对过程的语言编程的大致架构。
面对对象的语言则是通过接口与外部进行链接,因此如果出现原始的数据结构改变时,外部的整体框架并不会发生改变,只需要改变这一个对象即可,因此大大减少了维护的复杂度。在大型的项目和复杂额程序中,OOP的优势即可显现。下图是面对对象的语言的大致架构。
b. 对象 (Object) 是干什么的 ?
An object is not a stand-alone program, but is used by programs that need its service. For example, Sharon is a programmer who has developed an object for rendering 3D images. She is a math whiz and knows a lot about computer graphics, so her object is coded to perform all the necessary 3D mathematical operations and handle the computer s video hardware. Tom, who is writing a program for an architectural firm, needs his application to display 3D images of buildings. Because he is working under a tight deadline and does not possess a great deal of knowledge about computer graphics, he can use Sharon s object to perform the 3D rendering.
对象不是一个独立的程序,而是被需要它服务的程序所使用。
例如,Sharon是一名程序员,她开发了一种用于渲染3D图像的对象。她是数学天才,对计算机图形学非常了解,所以她的目标是执行所有必要的3D数学运算,并处理计算机的视频硬件。汤姆正在为一家建筑公司编写程序,他需要他的应用程序来显示建筑物的3D图像。因为他的工作时间很紧,而且没有大量的计算机图形学知识,所以他可以使用Sharon的对象来执行3D渲染。
因此对象是服务于程序的。
2. 类(Class)
a. 类是什么?
A class is code that specifies the attributes and member functions that a particular type of object may have. So, a class is not an object, but it is a description of an object.
类是指定特定类型对象可能具有的属性和成员函数的代码。所以,类是一个描述对象的东西,而非对象本身。
形象一点的说法是,”类“就好比一个个房子,它可以供人居住和使用(供程序调用)。而”对象“就是里面的客厅,厨房,卧室等等房间(有各自的功能)。这些房间都有特定的用处,整个房子也是由这些有用的房间组成的。当然,这个房子里有些地方是可以让人随意进出的(类中的对象是公共的);也有些地方是上锁的(类中的对象是可以私有的)。
相比于C语言中的struct,C++中的类可以包含函数,这种称为Method,即类提供的操作方法。所以在类中,包含有”成员变量(member variable)“和”成员函数(member function)“。每个成员有有可能是公共的/外部可访问的 (public) 或者是自己的/不可外部访问的 (private)。
b. 类的定义
在C++中,最简单的定义一个类:
class class_name
{
declaration;
// ... more declarations
// may follow...
};
//例如
class Rectangle
{
double width;
double length;
};
//有分号结尾 跟struct一样
但是对于类而言,为了数据的保护,会分为两种类型,private和public,这两种类型中,public是可以通过外部直接访问的成员,而private是不可以外部直接访问的,但是类的内部可以访问和调用(使用类内其他成员调用private成员)。
对于类里直接定义的变量来说,默认为private。因此,为了注明每一个成员是private还是public的,我们一般这样定义class:
class ClassName
{
private:
// Declarations of private
// members appear here.
public:
// Declarations of public
// members appear here.
};
//例如
class Rectangle
{
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
private:
double width;
double length;
};
其中,在函数后面添加const,意思是在这个函数不会更改存储在调用对象中的任何数据。例如这种函数一般只输出等。
如果成员函数需要输入,则只需在括号内添加数据类型即可。例如要两个输入:
void setWidth(double, double)
通常情况下,为