Discipline eight years C ++ experience of object-oriented programming

Six years ago, I just love when (Object-Oriented) "object-oriented", remember breath nearly 10 definitions. Six years later, I Gunpa from hundreds of thousands of lines out of the program when ready to write about their feelings and experiences, but can not explain what "object-oriented", as that is not clear what is mathematics so. And "object-oriented design" usually refers to "needs analysis" and "System Design" link buzzword in software engineering "object oriented." "Object-oriented" There are a few university school, just like Buddha, Allah and God with their own way to define this world, and by leaving a pile of books to explain the world.

Some scholars have suggested this to find the "object": grammatical analysis of a sentence, identify nouns and verbs, nouns is the object, the object of the verb is the method (ie function).

To counter the KMT scholar Mao Zedong "Patio Spring Snow", specially invited survivors of the Qing Dynasty who wrote some neat antithesis of poetry, please look over Chiang Kai-shek. Chiang Kai-shek looked angry and cursed:. "Mother Xi horses, all there was a smell of carrion coffin" I read thousands of pages of information on software engineering, and finally found himself some "mentally handicapped" can not understand "object-oriented" the theory, while awakening to "programming is the last word."

Many object-oriented programming languages, such as Smalltalk, Ada, Eiffel, Object Pascal, Visual Basic, C ++, and so on. C ++ language most flattering because it is compatible with the C language, and with the performance of the C language. In recent years, called pure object-oriented language Java's smash hit, a lot of people shouting to use C ++ Java leather life. I think Java is like C ++ nephew, though not directly inherited, but also somewhat decent. Nephew spilled a urine while playing uncle who, two people should not quarrel for this purpose.

About C ++ programming books very much, do not speak in this chapter of the C ++ syntax, they talk about some small programming truth. If I can understand a few years ago these little truth, can greatly improve the quality of hundreds of thousands of lines of the program.

6.1 C ++ important early revolutionary concept of object-oriented program design movie has such a role, he said: "I am the party, the party I represent, I am the party." Then he brought disaster to the comrades.

I will use C ++ programmers must understand object-oriented programming it?

Not use C ++ programmers do not necessarily know how to do object-oriented programming?

Both are not. After scoundrel like to join the party may not be able to become a good man, a good man not to join the party may not become as bad guys.

I am not afraid to say something lying aroused public indignation: "C ++ is no master, only C language expert." After using C and C ++ programming for eight years, I deeply regret that they are not masters of the C language, but no one coaching me how sorry object-oriented programming. I and many C ++ programmers, while enjoying the benefits of C ++ syntax into thinking that he has to understand object-oriented programming. Like selling toothpaste squeezed out like toothpaste, it is really a throwaway ah.

People also do not understand Mandarin Pinyin, Pinyin know if it will be better to speak Putonghua. Do not understand object-oriented programming in C ++ programming can be, if you know how object-oriented programming will compile the C ++ program better. This section describes three very basic concepts: "object class", "Inheritance in combination with", "virtual functions and polymorphism." Understanding these concepts will help improve the quality of the program, in particular, to improve the "reusability" and "scalability."

6.1.1 Classes and Objects

Object (Object) is one example (Instance) class (Class) of. If the object is compared to a house, then the house is kind of design drawings. So the focus of object-oriented programming is the class of the design, rather than the object of design. Class data and functions may be packaged together, wherein the function represents the behavior of a class (or services). Class provides keywords public, protected and private statements for which data and functions are public, protected or private.

So we can achieve the purpose of hiding information that must be disclosed so that the class only to let the outside world know the content while hiding all other content. We can not abuse the class package, do not regard it as hot pot, throw everything inside.

Design is based on data type as the center, or to conduct centered?

Internal data structures advocated "data-centric 'that sent the class concerned, they are used on private data EDITORIAL type, and the public function of the type written on the back, as shown in Table 8.1 (a) shown in FIG.

Advocate "to conduct centered" focus like that sent should provide what services and interfaces, they are used on public EDITORIAL type of function, while the private type of data written on the back, as shown in Table 8.1 (b) Fig.


很多C++教课书主张在设计类时“以数据为中心”。我坚持并且建议读者在设计类时“以行为为中心”,即首先考虑类应该提供什么样的函数。Microsoft 公司的COM 规范的核心是接口设计,COM 的接口就相当于类的公有函数[Rogerson 1999]。在程序设计方面,咱们不要怀疑Microsoft 公司的风格。

设计孤立的类是比较容易的,难的是正确设计基类及其派生类。因为有些程序员搞不清楚“继承”(Inheritance)、“组合”(Composition)、“多态”( Polymorphism)这些概念。

6.1.2 继承与组合

如果A 是基类,B 是A 的派生类,那么B 将继承A 的数据和函数。示例程序如下:

class A
{
public:
void Func1(void);
void Func2(void);
};
class B : public A
{
public:
void Func3(void);
void Func4(void);
};
// Example
main()
{
B b; // B的一个对象
b.Func1(); // B 从A 继承了函数Func1
b.Func2(); // B 从A 继承了函数Func2
b.Func3();
b.Func4();
}

这个简单的示例程序说明了一个事实:C++的“继承”特性可以提高程序的可复用性。正因为“继承”太有用、太容易用,才要防止乱用“继承”。我们要给“继承”立一些使用规则:

一、如果类A 和类B 毫不相关,不可以为了使B 的功能更多些而让B 继承A 的功能。

不要觉得“白吃白不吃”,让一个好端端的健壮青年无缘无故地吃人参补身体。

二、如果类B 有必要使用A 的功能,则要分两种情况考虑:

(1)若在逻辑上B 是A 的“一种”(a kind of ),则允许B 继承A 的功能。如男人(Man)是人(Human)的一种,男孩(Boy)是男人的一种。那么类Man 可以从类Human 派生,类Boy 可以从类Man 派生。示例程序如下:

class Human
{

};
class Man : public Human
{

};
class Boy : public Man
{

};

(2)若在逻辑上A 是B 的“一部分”(a part of),则不允许B 继承A 的功能,而是要用A和其它东西组合出B。例如眼(Eye)、鼻(Nose)、口(Mouth)、耳(Ear)是头(Head)的一部分,所以类Head 应该由类Eye、Nose、Mouth、Ear 组合而成,不是派生而成。示例程序如下:

class Eye
{
public:
void Look(void);
};
class Nose
{
public:
void Smell(void);
};
class Mouth
{
public:
void Eat(void);
};
class Ear
{
public:
void Listen(void);
};
// 正确的设计,冗长的程序
class Head
{
public:
void Look(void) { m_eye.Look(); }
void Smell(void) { m_nose.Smell(); }
void Eat(void) { m_mouth.Eat(); }
void Listen(void) { m_ear.Listen(); }
private:
Eye m_eye;
Nose m_nose;
Mouth m_mouth;
Ear m_ear;
};
如果允许Head 从Eye、Nose、Mouth、Ear 派生而成,那么Head 将自动具有Look、Smell、Eat、Listen 这些功能:
// 错误的设计
class Head : public Eye, public Nose, public Mouth, public Ear
{
};

上述程序十分简短并且运行正确,但是这种设计却是错误的。很多程序员经不起“继承”的诱惑而犯下设计错误。

一只公鸡使劲地追打一只刚下了蛋的母鸡,你知道为什么吗?

因为母鸡下了鸭蛋。

本书3.3 节讲过“运行正确”的程序不见得就是高质量的程序,此处就是一个例证。


6.1.3 虚函数与多态

除了继承外,C++的另一个优良特性是支持多态,即允许将派生类的对象当作基类的对象使用。如果A 是基类,B 和C 是A 的派生类,多态函数Test 的参数是A 的指针。那么Test 函数可以引用A、B、C 的对象。示例程序如下:

class A
{
public:
void Func1(void);
};
void Test(A *a)
{
a->Func1();
}
class B : public A
{

};
class C : public A
{

};
// Example
main()
{
A a;
B b;
C c;
Test(&a);
Test(&b);
Test(&c);
};

以上程序看不出“多态”有什么价值,加上虚函数和抽象基类后,“多态”的威力就显示出来了。

C++用关键字virtual 来声明一个函数为虚函数,派生类的虚函数将(override)基类对应的虚函数的功能。示例程序如下:

class A
{
public:
virtual void Func1(void){ cout<< “This is A::Func1 /n”}
};
void Test(A *a)
{
a->Func1();
}
class B : public A
{
public:
virtual void Func1(void){ cout<< “This is B::Func1 /n”}
};
class C : public A
{
public:
virtual void Func1(void){ cout<< “This is C::Func1 /n”}
};
// Example
main()
{
A a;
B b;
C c;
Test(&a); // 输出This is A::Func1
Test(&b); // 输出This is B::Func1
Test(&c); // 输出This is C::Func1
};

如果基类A 定义如下:

class A
{
public:
virtual void Func1(void)=0;
};

那么函数Func1 叫作纯虚函数,含有纯虚函数的类叫作抽象基类。抽象基类只管定义纯虚函数的形式,具体的功能由派生类实现。

结合“抽象基类”和“多态”有如下突出优点:

(1)应用程序不必为每一个派生类编写功能调用,只需要对抽象基类进行处理即可。这一
招叫“以不变应万变”,可以大大提高程序的可复用性(这是接口设计的复用,而不是代码实现的复用)。

(2)派生类的功能可以被基类指针引用,这叫向后兼容,可以提高程序的可扩充性和可维护性。以前写的程序可以被将来写的程序调用不足为奇,但是将来写的程序可以被以前写的程序调用那可了不起。

6.2 良好的编程风格

内功深厚的武林高手出招往往平淡无奇。同理,编程高手也不会用奇门怪招写程序。良好的编程风格是产生高质量程序的前提。

6.2.1 命名约定

有不少人编程时用拼音给函数或变量命名,这样做并不能说明你很爱国,却会让用此程序的人迷糊(很多南方人不懂拼音,我就不懂)。程序中的英文一般不会太复杂,用词要力求准确。匈牙利命名法是Microsoft 公司倡导的[Maguire 1993],虽然很烦琐,但用习惯了也就成了自然。没有人强迫你采用何种命名法,但有一点应该做到:自己的程序命名必须一致。

以下是我编程时采用的命名约定:

(1)宏定义用大写字母加下划线表示,如MAX_LENGTH;

(2)函数用大写字母开头的单词组合而成,如SetName, GetName ;

(3)指针变量加前缀p,如*pNode ;

(4)BOOL 变量加前缀b,如bFlag ;

(5)int 变量加前缀i,如iWidth ;

(6)float 变量加前缀f,如fWidth ;

(7)double 变量加前缀d,如dWidth ;

(8)字符串变量加前缀str,如strName ;

(9)枚举变量加前缀e,如eDrawMode ;

(10)类的成员变量加前缀m_,如m_strName, m_iWidth ;

对于int, float, double 型的变量,如果变量名的含义十分明显,则不加前缀,避免烦琐。如用于循环的int 型变量i,j,k ;float 型的三维坐标(x,y,z)等。

6.2.2 使用断言

程序一般分为Debug 版本和Release 版本,Debug 版本用于内部调试,Release 版本发行给用户使用。断言assert 是仅在Debug 版本起作用的宏,它用于检查“不应该”发生的情况。以下是一个内存复制程序,在运行过程中,如果assert 的参数为假,那么程序就会中止(一般地还会出现提示对话,说明在什么地方引发了assert)。

//复制不重叠的内存块
void memcpy(void *pvTo, void *pvFrom, size_t size)
{
void *pbTo = (byte *) pvTo;
void *pbFrom = (byte *) pvFrom;
assert( pvTo != NULL && pvFrom != NULL );
while(size - - > 0 )
*pbTo + + = *pbFrom + + ;
return (pvTo);
}

assert 不是一个仓促拼凑起来的宏,为了不在程序的Debug 版本和Release 版本引起差别,assert 不应该产生任何副作用。所以assert 不是函数,而是宏。程序员可以把assert 看成一个在任何系统状态下都可以安全使用的无害测试手段。

很少有比跟踪到程序的断言,却不知道该断言的作用更让人沮丧的事了。你化了很多时间,不是为了排除错误,而只是为了弄清楚这个错误到底是什么。有的时候,程序员偶尔还会设计出有错误的断言。所以如果搞不清楚断言检查的是什么,就很难判断错误是出现在程序中,还是出现在断言中。幸运的是这个问题很好解决,只要加上清晰的注释即可。这本是显而易见的事情,可是很少有程序员这样做。这好比一个人在森林里,看到树上钉着一块“危险”的大牌子。但危险到底是什么?树要倒?有废井?有野兽?除非告诉人们“危险”是什么,否则这个警告牌难以起到积极有效的作用。难以理解的断言常常被程序员忽略,甚至被删除。[Maguire 1993]

以下是使用断言的几个原则:

(1)使用断言捕捉不应该发生的非法情况。不要混淆非法情况与错误情况之间的区别,后者是必然存在的并且是一定要作出处理的。

(2)使用断言对函数的参数进行确认。

(3)在编写函数时,要进行反复的考查,并且自问:“我打算做哪些假定?”一旦确定了的
假定,就要使用断言对假定进行检查。

(4)一般教科书都鼓励程序员们进行防错性的程序设计,但要记住这种编程风格会隐瞒错误。当进行防错性编程时,如果“不可能发生”的事情的确发生了,则要使用断言进行报警。

6.2.3 new、delete 与指针

在C++中,操作符new 用于申请内存,操作符delete 用于释放内存。在C 语言中,函数malloc 用于申请内存,函数free 用于释放内存。由于C++兼容C 语言,所以new、delete、malloc、free 都有可能一起使用。new 能比malloc 干更多的事,它可以申请对象的内存,而malloc 不能。C++和C 语言中的指针威猛无比,用错了会带来灾难。对于一个指针p,如果是用new申请的内存,则必须用delete 而不能用free 来释放。如果是用malloc 申请的内存,则必须用free 而不能用delete 来释放。在用delete 或用free 释放p 所指的内存后,应该马上显式地将p 置为NULL,以防下次使用p 时发生错误。示例程序如下:

void Test(void)
{
float *p;
p = new float[100];
if(p==NULL) return;
…// do something
delete p;
p=NULL; // 良好的编程风格
// 可以继续使用p
p = new float[500];
if(p==NULL) return;
…// do something else
delete p;
p=NULL;
}

我们还要预防“野指针”,“野指针”是指向“垃圾”内存的指针,主要成因有两种:

(1)指针没有初始化。

(2)指针指向已经释放的内存,这种情况最让人防不胜防,示例程序如下:

class A
{
public:
void Func(void){…}
};
void Test(void)
{
A *p;
{
A a;
p = &a; // 注意a 的生命期
}
p->Func(); // p 是“野指针”,程序出错
}

6.2.4 使用const

在定义一个常量时,const 比#define 更加灵活。用const 定义的常量含有数据类型,该常量可以参与逻辑运算。例如:

const int LENGTH = 100; // LENGTH 是int 类型

const float MAX=100; // MAX 是float 类型

#define LENGTH 100 // LENGTH 无类型

#define MAX 100 // MAX 无类型

除了能定义常量外,const 还有两个“保护”功能:

一、强制保护函数的参数值不发生变化

以下程序中,函数f 不会改变输入参数name 的值,但是函数g 和h 都有可能改变name的值。

void f(String s); // pass by value
void g(String &s); // pass by referance
void h(String *s); // pass by pointer
main()
{
String name=“Dog”;
f(name); // name 的值不会改变
g(name); // name 的值可能改变
h(name); // name 的值可能改变
}

对于一个函数而言,如果其‘&’或‘*’类型的参数只作输入用,不作输出用,那么应当在该参数前加上const,以确保函数的代码不会改变该参数的值(如果改变了该参数的值,编译器会出现错误警告)。因此上述程序中的函数g 和h 应该定义成:

void g(const String &s);
void h(const String *s);

二、强制保护类的成员函数不改变任何数据成员的值

以下程序中,类stack 的成员函数Count 仅用于计数,为了确保Count 不改变类中的任何数据成员的值,应将函数Count 定义成const 类型。

class Stack
{
public:
void push(int elem);
void pop(void);
int Count(void) const; // const 类型的函数
private:
int num;
int data[100];
};
int Stack::Count(void) const
{
++ num; // 编译错误,num 值发生变化
pop(); // 编译错误,pop 将改变成员变量的值
return num;
}

6.2.5 其它建议

(1)不要编写一条过分复杂的语句,紧凑的C++/C 代码并不见到能得到高效率的机器代码,却会降低程序的可理解性,程序出错误的几率也会提高。

(2)不要编写集多种功能于一身的函数,在函数的返回值中,不要将正常值和错误标志混在一起。

(3)不要将BOOL 值TRUE 和FALSE 对应于1 和0 进行编程。大多数编程语言将FALSE定义为0,任何非0 值都是TRUE。Visual C++将TRUE 定义为1,而Visual Basic 则将TRUE定义为-1。示例程序如下:

BOOL flag;

if(flag) { // do something } // 正确的用法

if(flag==TRUE) { // do something } // 危险的用法

if(flag==1) { // do something } // 危险的用法

if(!flag) { // do something } // 正确的用法

if(flag==FALSE) { // do something } // 不合理的用法

if(flag==0) { // do something } // 不合理的用法

(4)小心不要将“= =”写成“=”,编译器不会自动发现这种错误。

(5)不要将123 写成0123,后者是八进制的数值。

(6)将自己经常犯的编程错误记录下来,制成表格贴在计算机旁边。

6.3 小结

C++/C 程序设计如同少林寺的武功一样博大精深,我练了8 年,大概只学到二三成。所以无论什么时候,都不要觉得自己的编程水平天下第一,看到别人好的技术和风格,要虚心学习。本章的内容少得可怜,就象口渴时只给你一颗杨梅吃,你一定不过瘾。我借花献佛,推荐一本好书:Marshall P. Cline 著的《C++ FAQs》[Cline 1995]。你看了后一定会赞不绝口。会编写C++/C 程序,不要因此得意洋洋,这只是程序员基本的技能要求而已。如果把系统分析和系统设计比作“战略决策”,那么编程充其量只是“战术”。如果指挥官是个大笨蛋,士兵再勇敢也会吃败仗。所以我们程序员不要只把眼光盯在程序上,要让自己博学多才。我们应该向北京胡同里的小孩们学习,他们小小年纪就能指点江山,评论世界大事。

发布了30 篇原创文章 · 获赞 2 · 访问量 5万+

Guess you like

Origin blog.csdn.net/khzide/article/details/499768