📄 key_aspects_of_c++.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Key Aspects of C++,Classes,Functions,ACIS" /> <link rel="shortcut icon" href="/favicon.ico" /> <link rel="search" type="application/opensearchdescription+xml" href="/r18/opensearch_desc.php" title="DocR18 (English)" /> <link title="Creative Commons" type="application/rdf+xml" href="/r18/index.php?title=Key_Aspects_of_C%2B%2B&action=creativecommons" rel="meta" /> <title>Key Aspects of C++ - DocR18</title>
<style type="text/css" media="screen, projection">/*<![CDATA[*/
@import "/r18/skins/common/shared.css?97";
@import "/r18/skins/monobook/main.css?97";
/*]]>*/</style>
<link rel="stylesheet" type="text/css" media="print" href="/r18/skins/common/commonPrint.css?97" />
<!--[if lt IE 5.5000]><style type="text/css">@import "/r18/skins/monobook/IE50Fixes.css?97";</style><![endif]-->
<!--[if IE 5.5000]><style type="text/css">@import "/r18/skins/monobook/IE55Fixes.css?97";</style><![endif]-->
<!--[if IE 6]><style type="text/css">@import "/r18/skins/monobook/IE60Fixes.css?97";</style><![endif]-->
<!--[if IE 7]><style type="text/css">@import "/r18/skins/monobook/IE70Fixes.css?97";</style><![endif]-->
<!--[if lt IE 7]><script type="text/javascript" src="/r18/skins/common/IEFixes.js?97"></script>
<meta http-equiv="imagetoolbar" content="no" /><![endif]-->
<script type= "text/javascript">/*<![CDATA[*/var skin = "monobook";var stylepath = "/r18/skins";var wgArticlePath = "/r18/index.php/$1";var wgScriptPath = "/r18";var wgScript = "/r18/index.php";var wgServer = "http://doc.spatial.com";var wgCanonicalNamespace = "";var wgCanonicalSpecialPageName = false;var wgNamespaceNumber = 0;var wgPageName = "Key_Aspects_of_C++";var wgTitle = "Key Aspects of C++";var wgAction = "view";var wgRestrictionEdit = ["sysop"];var wgRestrictionMove = ["sysop"];var wgArticleId = "451";var wgIsArticle = true;var wgUserName = null;var wgUserGroups = null;var wgUserLanguage = "en";var wgContentLanguage = "en";var wgBreakFrames = false;var wgCurRevisionId = "1133";/*]]>*/</script>
<script type="text/javascript" src="/r18/skins/common/wikibits.js?97"><!-- wikibits js --></script>
<script type="text/javascript" src="/r18/index.php?title=-&action=raw&gen=js&useskin=monobook"><!-- site js --></script>
<style type="text/css">/*<![CDATA[*/@import "/r18/index.php?title=MediaWiki:Common.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";@import "/r18/index.php?title=MediaWiki:Monobook.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";@import "/r18/index.php?title=-&action=raw&gen=css&maxage=18000";/*]]>*/</style>
<!-- Head Scripts -->
<script type="text/javascript" src="/r18/skins/common/ajax.js?97"></script><style type="text/css">/*<![CDATA[*/.source-cpp {line-height: normal;}.source-cpp li {line-height: normal;}/** * GeSHi Dynamically Generated Stylesheet * -------------------------------------- * Dynamically generated stylesheet for cpp * CSS class: source-cpp, CSS id: * GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */.source-cpp .de1, .source-cpp .de2 {font-family: 'Courier New', Courier, monospace; font-weight: normal;}.source-cpp {}.source-cpp .head {}.source-cpp .foot {}.source-cpp .imp {font-weight: bold; color: red;}.source-cpp .ln-xtra {color: #cc0; background-color: #ffc;}.source-cpp li {font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;}.source-cpp li.li2 {font-weight: bold;}.source-cpp .kw1 {color: #0000ff;}.source-cpp .kw2 {color: #0000ff;}.source-cpp .kw3 {color: #0000dd;}.source-cpp .kw4 {color: #0000ff;}.source-cpp .kw5 {color: #000099;}.source-cpp .kw6 {color: #000099; font-weight:normal}.source-cpp .kw7 {color: #004D99;}.source-cpp .co1 {color: #006600; font-style: italic;}.source-cpp .co2 {color: #006600;}.source-cpp .coMULTI {color: #006600; font-style: italic;}.source-cpp .es0 {color: #666666; font-weight: bold;}.source-cpp .br0 {color: #B300D6;}.source-cpp .st0 {color: #999933;}.source-cpp .nu0 {color: #A30000;}.source-cpp .me1 {color: #993300;}.source-cpp .me2 {color: #993300;}@import "/r18/index.php?title=MediaWiki:Geshi.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";/*]]>*/</style> </head>
<body class="mediawiki ns-0 ltr page-Key_Aspects_of_C">
<div id="globalWrapper">
<div id="column-content">
<div id="content">
<a name="top" id="top"></a>
<h1 class="firstHeading">Key Aspects of C++</h1>
<div id="bodyContent">
<h3 id="siteSub">From DocR18</h3>
<div id="contentSub"></div>
<div id="jump-to-nav">Jump to: <a href="#column-one">navigation</a>, <a href="#searchInput">search</a></div> <!-- start content -->
<p><a href="/r18/index.php?title=ACIS&action=edit" class="new" title="ACIS">ACIS</a> is written in C++ and consists of a set of C++ <a href="/r18/index.php?title=Functions&action=edit" class="new" title="Functions">Functions</a> and <a href="/r18/index.php?title=Classes&action=edit" class="new" title="Classes">Classes</a> (including data and member functions, or <i>methods</i>). A developer uses these functions and classes to create an end user 3D modeling application. This section describes how ACIS uses features of C++.</p><p>ACIS is a solid modeler, but wireframe and surface models may also be represented in ACIS. ACIS integrates wireframe, surface, and solid modeling by allowing these alternative model representations to coexist naturally in a unified data structure, which is implemented as a hierarchy of C++ classes.</p><p>ACIS utilizes key features of C++, such as encapsulation, overloading, etc. to create a modular and maintainable interface. The API function interface remains stable from release to release, while the class interface may change.</p><table id="toc" class="toc" summary="Contents"><tr><td><div id="toctitle"><h2>Contents</h2></div><ul><li class="toclevel-1"><a href="#Data_Encapsulation"><span class="tocnumber">1</span> <span class="toctext">Data Encapsulation</span></a></li><li class="toclevel-1"><a href="#Overloading_Class_Constructors"><span class="tocnumber">2</span> <span class="toctext">Overloading Class Constructors</span></a></li><li class="toclevel-1"><a href="#Overloading_Class_Methods_and_Operators"><span class="tocnumber">3</span> <span class="toctext">Overloading Class Methods and Operators</span></a></li><li class="toclevel-1"><a href="#Overloading_Class_new_and_delete"><span class="tocnumber">4</span> <span class="toctext">Overloading Class new and delete</span></a></li><li class="toclevel-1"><a href="#Overloading_API_Functions"><span class="tocnumber">5</span> <span class="toctext">Overloading API Functions</span></a></li></ul></td></tr></table><script type="text/javascript"> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </script><a name="Data_Encapsulation"></a><h2> <span class="mw-headline"> Data Encapsulation </span></h2><p>A class provides not only data storage, but also methods (or member functions) that can access and manipulate that data. The concept of having private data and public methods for accessing data is common to ACIS classes. This is how ACIS uses C++ classes to provide data encapsulation and modularization. This permits reorganization of the internals of any component while maintaining the interface.</p><p>Although it is possible to make the class data members accessible to anyone (public), it is much more common to hide the data members by making them private. Access to private class data members is restricted to the public class methods. Not all class methods need to be exposed, so some methods are also private.</p><p>For example, consider the SPAposition class, which is used to define a position in space. The declaration for this class is shown in the following example. The internal data structure for this is an array of three real numbers and it has class methods for the <i>x</i>, <i>y</i>, and <i>z</i> components of the position: x, y, and z. At some later date, if the data structure for SPAposition were to use three separate real numbers instead of an array, this could be implemented and still be transparent to the users of this class, because the users can only access the public x, y, and z methods and not the private data elements directly.</p><blockquote class="templatequote"><div><p><i><b>Note:</b> ACIS globally defines the keyword real to be equivalent to a C++ double or float data type using a statement.</i></p></div></blockquote><pre class="source-cpp">C++ Example <span class="kw2">class</span> <span class="kw7">DECL_KERN</span> <span class="kw5">SPAposition</span> <span class="br0">{</span> real coord <span class="nu0">3</span> ; <span class="co1">// x, y and z coordinates of the position public: // Force creation of all positions to be by constructor. SPAposition() {} // Construct a position from three doubles. SPAposition( double xi, double yi, double zi ) { coord 0 = (real) xi; coord 1 = (real) yi; coord 2 = (real) zi; } // Construct a position from an array of three doubles. SPAposition( double p 3 ) { coord 0 = (real) p 0 ; coord 1 = (real) p 1 ; coord 2 = (real) p 2 ; } // Copy a position SPAposition( SPAposition const &p ) { coord 0 = p.coord 0 ; coord 1 = p.coord 1 ; coord 2 = p.coord 2 ; } // Extract a coordinate value. double x() const { return coord 0 ; } double y() const { return coord 1 ; } double z() const { return coord 2 ; } double coordinate( int i ) const { return coord i ; } // Extract a coordinate value for update. double &x() { return coord 0 ; } double &y() { return coord 1 ; } double &z() { return coord 2 ; } double &coordinate( int i ) { return coord i ; } // Set coordinate values. void set_x( double new_x ) { coord 0 = new_x; } void set_y( double new_y ) { coord 1 = new_y; } void set_z( double new_z ) { coord 2 = new_z; } void set_coordinate( int i, double new_c ) void set_coordinate( int i, double new_c ) { coord i = new_c; } // Position operators // Get displacement, that is, a vector, as difference of // two positions. friend DECL_KERN SPAvector operator-( SPAposition const &, SPAposition const & ); // Translate a position by a vector. friend DECL_KERN SPAposition operator+( SPAposition const &, SPAvector const & ); friend DECL_KERN SPAposition operator+( SPAvector const &, SPAposition const & ); SPAposition const &operator+=( SPAvector const &); friend DECL_KERN SPAposition operator-( SPAposition const &, SPAvector const & ); SPAposition const &operator-=( SPAvector const & ); // Transform a position. friend DECL_KERN SPAposition operator*( SPAmatrix const &, SPAposition const & ); friend DECL_KERN SPAposition operator*( SPAposition const &, SPAmatrix const & ); SPAposition const &operator*=( SPAmatrix const & ); friend DECL_KERN SPAposition operator*( SPAposition const &, SPAtransf const & ); friend DECL_KERN SPAposition operator*( SPAposition const &p, SPAtransf const *t ); SPAposition const &operator*=( SPAtransf const & ); // Output details of a position void debug( FILE * = debug_file_ptr ) const; };</span></pre><p>Example. Declaration of SPAposition Class</p><p>The following example shows a code fragment of two attempts to use the SPAposition class. As the declaration of the SPAposition class in Example. Declaration of SPAposition Class shows, this has private data which is an array of three real numbers, called <i>coord</i>. Attempting to access the private data member <i>coord</i> directly fails at compile time. However, accessing the public methods, such as x, is valid and will compile.</p><pre class="source-cpp">C++ Example <span class="co1">// Declare an instance of the class. SPAposition my_p1(10, 15, 20); // The compile returns an error in next statement because // "coord" is the private data member of this class. double my_x = my_p1.coord0; // The correct way to access the x coordinate of the position. double my_x = my_p1.x();</span></pre><a name="Overloading_Class_Constructors"></a><h2> <span class="mw-headline"> Overloading Class Constructors </span></h2><p>Overloading class constructors means that there is more than one way to create a class instance. In ACIS, additional constructors are made available as a convenience based on the type of data that an application programmer may have on hand at the time a class instance is to be created.</p><p>The decision of which constructor to use happens at compile time, based on the argument types passed into the constructor.</p><p>Consider again the SPAposition class declared in Example. Declaration of SPAposition Class. A SPAposition instance can be created from three real numbers, from an array of three real numbers, or from another SPAposition instance. This is illustrated in the following example.</p><pre class="source-cpp">C++ Example <span class="co1">// Create an instance of SPAposition using 3 real numbers. SPAposition p1 (3, 4, 5); // Create an instance of SPAposition using an array. double my_array3 = { 3, 4, 5}; SPAposition p2 (my_array); // Create an instance of SPAposition using a previously defined // instance of SPAposition. SPAposition p3 (p1);</span></pre><p>Example. Overloaded Constructors</p><a name="Overloading_Class_Methods_and_Operators"></a><h2> <span class="mw-headline"> Overloading Class Methods and Operators </span></h2><p>ACIS overloads operators, such as the minus operator (-) SPAvector class in the following example.</p><pre class="source-cpp">C++ Example <span class="kw5">SPAposition</span> p1 <span class="br0">(</span><span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">5</span><span class="br0">)</span>; <span class="kw5">SPAposition</span> p2 <span class="br0">(</span><span class="nu0">6</span>, <span class="nu0">7</span>, <span class="nu0">8</span><span class="br0">)</span>; <span class="kw5">SPAvector</span> v1 = p2 - p1;</pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -