⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code_c++_intro.html

📁 有趣的模拟进化的程序 由国外一生物学家开发 十分有趣
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html><head>   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">   <meta name="GENERATOR" content="Mozilla/4.77 [en] (X11; U; Linux 2.4.3-20mdk i686) [Netscape]">   <title>An Introduction to C++</title></head><body text="#000000" bgcolor="#FFFFFF" link="#0000AA" vlink="#000044" alink="#0000FF"><center><h2>A Conceptual Introduction to C++</h2></center>This file will cover in more detail some of the concepts needed to understandC++. My goal here is <i>not</i> to make you be able to sit down and writeyour own program from scratch.<h3>Objects and Classes</h3>An <b>object</b> in C++ is a single, conceptual unit that contains <b>data</b>(information about the state of that object) and <b>methods</b> (functionsassociated with that class of objects) by which the object is modifiedor can interact with other objects. The data in an object can be eithernormal variables (<i>e.g.</i> characters, floating point numbers, or integers)or previously-defined objects. A category of objects is a <b>class</b>;an object is a single instance of a class.<p>For example, in Avida one of the most important classes is called<tt>cOrganism</tt>-- it is the class that all organism objects belong to. Here is an abbreviatedversion of the cOrganism class declaration (explained further below), colorcoded to aide understanding.<center><table CELLPADDING=4 WIDTH="90%" ><tr><td WIDTH="50%"><font color="#886600"><b>Comments</b> are in BROWN</font><br><font color="#008800">Names of <b>methods</b> are in GREEN</font></td><td WIDTH="50%"><font color="#880000">Names of <b>classes</b> are in RED</font><br><font color="#0000AA">Names of <b>objects</b> are in BLUE</font></td></tr></table></center><table CELLPADDING=8 ><tr><td><pre>class <font color="#880000">cOrganism</font> {private:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#886600">// Data in this class cannot be directly accessed from outside.</font>&nbsp; const <font color="#880000">cGenome</font><font color="#0000AA"> genome;</font>&nbsp;&nbsp;&nbsp;&nbsp; <font color="#886600">// The initial genome that this organism was born with.</font>&nbsp; <font color="#880000">cPhenotype</font><font color="#0000AA"> phenotype;</font>&nbsp;&nbsp;&nbsp;&nbsp; <font color="#886600">// Maintains the status of this organism's phenotypic traits.</font>&nbsp; <font color="#880000">cPopulationInterface</font><font color="#0000AA"> pop_interface;</font>&nbsp; <font color="#886600">// Interface back to the population.</font>&nbsp; <font color="#880000">cGenotype </font><font color="#000000">*</font> <font color="#0000AA">genotype;</font>&nbsp;&nbsp;&nbsp;&nbsp; <font color="#886600">// A pointer to the genotype that this organism belongs to.</font>&nbsp; <font color="#880000">cHardwareBase </font><font color="#000000">*</font> <font color="#0000AA">hardware;</font> <font color="#886600">// The virtual machinery that this organism's genome is run on.</font>public:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#886600"> // The methods are accessible to other classes.</font>&nbsp; <font color="#886600">// This classes' constructor requires an initial genome, an interface to the population,&nbsp; // and the environment the organism is in.&nbsp; NOTE: These are defined elsewhere.</font>&nbsp; <font color="#008800">cOrganism(</font><font color="#000000">const</font> <font color="#880000">cGenome</font> <font color="#000000">&amp;</font> <font color="#0000AA">in_genome</font><font color="#000000">,</font><font color="#008800">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">const</font> <font color="#880000">cPopulationInterface</font> <font color="#000000">&amp;</font> <font color="#0000AA">in_interface</font><font color="#000000">,</font><font color="#008800">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">const</font> <font color="#880000">cEnvironment</font> <font color="#000000">&amp;</font> <font color="#0000AA">in_environment</font><font color="#008800">)</font><font color="#000000">;</font><font color="#008800">&nbsp; ~cOrganism()</font><font color="#000000">;</font>&nbsp; <font color="#886600">// This batch of methods involve interaction with the population to resolve.</font>&nbsp; <font color="#880000">cOrganism</font> <font color="#000000">*</font><font color="#008800"> GetNeighbor() </font><font color="#000000">{</font> <font color="#000000">return</font> <font color="#0000AA">pop_interface</font><font color="#008800">.GetNeighbor()</font><font color="#000000">; }</font><font color="#008800">&nbsp; </font><font color="#000000">int</font><font color="#008800"> GetNeighborhoodSize() </font><font color="#000000">{</font> <font color="#000000">return</font> <font color="#0000AA">pop_interface</font><font color="#008800">.GetNumNeighbors()</font><font color="#000000">; }</font><font color="#008800">&nbsp; </font><font color="#000000">void</font><font color="#008800"> Rotate(</font><font color="#000000">int direction</font><font color="#008800">) </font><font color="#000000">{</font> <font color="#0000AA">pop_interface</font><font color="#008800">.Rotate(direction)</font><font color="#000000">; }</font><font color="#008800">&nbsp; </font><font color="#000000">int</font><font color="#008800"> GetInput() </font><font color="#000000">{ return</font> <font color="#0000AA">pop_interface</font><font color="#008800">.GetInput()</font><font color="#000000">; }</font><font color="#008800">&nbsp; </font><font color="#000000">void</font><font color="#008800"> Die() </font><font color="#000000">{</font> <font color="#0000AA">pop_interface</font><font color="#008800">.Die()</font><font color="#000000">; }</font>&nbsp; <font color="#886600">// Input &amp; Output Testing -- see if any environment reactions are triggered.</font>&nbsp; <font color="#000000">void</font><font color="#008800"> DoInput</font><font color="#000000">(const</font> <font color="#000000">int</font><font color="#008800"> </font><font color="#000099">value</font><font color="#008800">, </font><font color="#880000">tBuffer&lt;</font><font color="#000000">int</font><font color="#880000">></font> <font color="#000000">&amp;</font><font color="#008800"> </font><font color="#000099">input_buf</font><font color="#008800">, </font><font color="#880000">tBuffer&lt;</font><font color="#000000">int</font><font color="#880000">></font> <font color="#000000">&amp;</font><font color="#008800"> </font><font color="#000099">output_buf</font><font color="#000000">);</font><font color="#008800">&nbsp; </font><font color="#000000">void</font><font color="#008800"> DoOutput</font><font color="#000000">(const</font> <font color="#000000">int</font><font color="#008800"> </font><font color="#000099">value</font><font color="#008800">, </font><font color="#880000">tBuffer&lt;</font><font color="#000000">int</font><font color="#880000">></font> <font color="#000000">&amp;</font><font color="#008800"> </font><font color="#000099">input_buf</font><font color="#008800">, </font><font color="#880000">tBuffer&lt;</font><font color="#000000">int</font><font color="#880000">></font> <font color="#000000">&amp;</font><font color="#008800"> </font><font color="#000099">output_buf</font><font color="#000000">);</font>&nbsp; <font color="#886600">// Accessors -- these are used to gain access to private data.</font>&nbsp; const <font color="#880000">cGenome</font> &amp; <font color="#008800">GetGenome()</font> const { return <font color="#0000AA">genome</font>; }&nbsp; <font color="#880000">cPhenotype</font> &amp; <font color="#008800">GetPhenotype()</font> { return <font color="#0000AA">phenotype</font>; }&nbsp; <font color="#880000">cGenotype</font> * <font color="#008800">GetGenotype()</font> { return <font color="#0000AA">genotype</font>; }&nbsp; <font color="#880000">cHardwareBase</font> * <font color="#008800">GetHardware()</font> { return <font color="#0000AA">hardware</font>; }&nbsp; <font color="#886600">// Some special methods used during saving and loading.</font>&nbsp; <font color="#000000">void</font><font color="#008800"> SaveState</font><font color="#000000">(</font><font color="#880000">ofstream</font><font color="#008800"> &amp; fp</font><font color="#000000">);</font><font color="#008800">&nbsp; </font><font color="#000000">void</font><font color="#008800"> LoadState</font><font color="#000000">(</font><font color="#880000">ifstream</font><font color="#008800"> &amp; fp</font><font color="#000000">);</font>};</pre></td></tr></table><h3>Style and Syntax Guide</h3>Don't worry too much about how the syntax works. The code presented aboveis a definition of a class in C++. It is broken into two parts; one labeled"<tt>private:</tt>" for those portions of the definition that can onlybe interacted with from within the class, and another labeled "<tt>public:</tt>"which defines the interface to the outside. In this case, we've made allof the variables private and the methods public.<p>A variable is defined by a description of the type of variable (sucha "<tt>cPhenotype</tt>") and then the name of this particular instanceof the variable. In this case, since organisms only have one phenotype,we called it merely "<tt>phenotype</tt>".<p>Methods are slightly more complex. The declaration of a method startswith the type of data the method returns (such as "<tt>int</tt>" for integer),or else lists "<tt>void</tt>" if there is no return value. Then the methodname is given, followed by a set of parenthesis (which are what indicatesto C++ that you are declaring a method). Inside of those parentesis, canbe<b>arguments</b>, which are variables that must be given to the methodin order for it to operate correctly. The declaration can stop at thispoint (ending in a semi-colon) if the method <b>body</b> is defined elsewhere.The body of the method is the sourcecode that details how the method operates,and can be included immediately after the declaration (within braces) orbe placed elsewhere in the code. Typically short method bodies are includedin the class definition, while longer ones are placed outside of it. Amethod is performed on an object, by listing the object name, followedby a dot ('.'), and then the name of the method to be called with all necessaryarguments included. This is explained further below.<p>The C++ language will accept variable names, class names, and methodnames of any alpha-numeric sequence as long as all names begin with a letter.&nbsp;The only other character allowed in a name is the underscore ('_').&nbsp;To make reading code easier, we have adopted certain conventions.<br>&nbsp;<table CELLSPACING=3 CELLPADDING=3 NOSAVE ><tr VALIGN=TOP NOSAVE><td>&nbsp;</td><td NOSAVE><font color="#0000AA">an_example_variable</font></td><td>Variable names (including object names) are always all in lowercaseletters, with individual words separated by underscores. Variables areeither user-defined classes, numbers (integers, boolean values, floatingpoint numbers, etc.) or characters (single symbols)</td></tr><tr VALIGN=TOP NOSAVE><td>&nbsp;</td><td NOSAVE><font color="#008800">ExampleMethod</font></td><td>Method names always have the first letter of each word capitalized,with the remainder of the word in lowercase. The one exception to thisis Constructors and Destructors, which must have the same name as the class(see below).</td></tr><tr VALIGN=TOP NOSAVE><td>&nbsp;</td><td NOSAVE><font color="#880000">cExampleClass</font></td><td>Classes use a similar format to methods, but always begin with a single,lowercase 'c'. Some other specialized types also used this format, butwith a different initial letter. For example, an initial 't' indicatesa template, which is a special type of class.</td></tr><tr VALIGN=TOP NOSAVE><td>&nbsp;</td><td NOSAVE>CONSTANT_VALUE</td><td>Any constant values (that is, numerical values that will never changeduring the course of the run) are given in all upper-case letters, withindividual words separated by underscores.</td></tr></table><p>Different software projects will each use their own style conventions;these are the ones you'll end up working with in Avida. Some exceptionsdo exist. For example, the C++ language itself does not follow many stylerules; built-in C++ names are all lowercase letters, regardless of whatthey represent.<h3>Description of Data Elements</h3>The section labeled <tt>private</tt> above lists those data that are uniqueto each organism; these are objects and pointers that exist <i>inside</i>of an organism object. First, <font color="#0000AA">genome</font> keepsthe initial state of the organism. Since we never want this genome to changeover the organism's life, we place a <tt>const</tt> directive in frontof it. The <tt>const</tt> command exists so that C++ knows to warn theprogrammer if they accidentally try to change an object (or variable) thatis not supposed to be altered.<p>The internal <font color="#0000AA">phenotype</font> object is used torecord the behaviors and abilities that the organism demonstrates duringits life. This class has variables to track everything from the tasks performedto the gestation time of the organism and the number of offspring it hasever produced. The <font color="#0000AA">pop_interface</font> allows anorganism to communicate with the population (object of class<tt><font color="#880000">cPopulation</font></tt>)that it is part of. This is used, for example, when an organism is readyto finish replicating and needs its offspring to be placed into the population.If an organism is being run on a test CPU rather than in a proper populationobject, then this population interface will cause the statistics aboutoffspring to be recorded for later use instead of activating it.<p>Next, we have two <b>pointers</b>. A pointer is a value that represents("points to") a location in the physical memory of the computer. A pointercan be identified by the asterisk ('*') that follows the type name. Thecode "<tt>cGenotype * genotype</tt>" indicates that the variable<font color="#0000AA">genotype</font>points to a location in memory where an object of class <font color="#880000">cGenotype</font>is stored. In this case, all of the organisms that are of a single genotypeall point to the <i>same</i> cGenotype object so that the genotypic information

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -