C++ model - OO, OB

王朝c/c++·作者佚名  2006-01-09
宽屏版  字体: |||超大  

The C++ programming model directly supports three programming paradigms:

The procedural model as programmed in C, and, of course, supported within C++. An example of this is string manipulation using character arrays and the family of str* functions defined in the Standard C library: char boy[] = "Danny";

char *p_son;

...

p_son = new char[ strlen( boy ) + 1 ];

strcpy( p_son, boy );

...

if ( !strcmp( p_son, boy ))

take_to_disneyland( boy );

The abstract data type (ADT) model in which users of the abstraction are provided with a set of operations (the public interface), while the implementation remains hidden. An example of this is a String class: String girl = "Anna";

String daughter;

...

// String::operator=();

daughter = girl;

...

// String::operator==();

if ( girl == daughter )

take_to_disneyland( girl );

The object-oriented (OO) model in which a collection of related types are encapsulated through an abstract base class providing a common interface. An example of this is a Library_materials class from which actual subtypes such as Book, Video, Compact_Disc, Puppet, and Laptop are derived: void

check_in( Library_materials *pmat )

{

if ( pmat->late() )

pmat->fine();

pmat->check_in();

if ( Lender *plend = pmat->reserved() )

pmat->notify( plend );

}

C++ supports polymorphism through class pointers and references. This style of programming is called object-oriented.

C++ also supports a concrete ADT style of programming now called object-based (OB)—nonpolymorphic data types, such as a String class.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有