亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? apa.htm

?? 21天學(xué)會VC的英文版
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
objects of class Lot_size are declared. On lines 36 through line 38, the values ofthe objects are set using the set() method. An important point to note is that theclass Tax_assessment has no objects in the main() method, so you cannot access anydata member or method of this class from main().</P><P>On line 41, the area of the Lot_size is output by operating the get_area() methodon an object of class Lot_size. On line 42, the city taxes are output by operatingthe method get_data2 on an object of Lot_size. This approach is required becausethe city_tax is a member data of class Tax_assessment, which cannot be operated ondirectly in the main() method. You use the method get_data2, which is a method ofLot_size and has access to the object taxes, which in turn can be accessed via get_city_tax.</P><P><H2><A NAME="Heading9"></A>Inheritance</H2><P>One of the advantages of programming in C++ or any other object-oriented languageis taking a global to local approach. Suppose you need to develop a program thatcomprehends all metals and their characteristics. If you take the class approachof the previous section, you would probably have one class named metals. The datamembers of metals would probably be density and volume. You could have another classnamed gold and one for aluminum. The data members describing gold and aluminum wouldneed all the properties of metals in addition to their own data members such as colorand shine. If you could devise a hierarchy of classes such that the classes for goldand aluminum would have only their individual data members but inherit the genericproperties from the parent metals class--then you would be using inheritance.</P><P>Inheritance is also called derivation. The new class inherits the functionalityof an existing class. The existing class is called the base class, and the new classis called the derived class. A similar inheritance can be derived for animals, mammals,and dogs.</P><P>To derive a new class from a base class, you use the colon (:) operator:</P><P><PRE>class human : public mammal</PRE><P>In this example, the new class human is derived from the base class mammal. Thederived class human would have all the functionality of the base class mammal. Inaddition, the human class can have other functionality, such as the ability to drivea car and work for food. The example in Listing A.13 shows how to create the objectsof type human and access its data and functions.</P><P><H4>LISTING A.13. Inherit1.cpp.</H4><PRE> 1: // Workspace Name:  Inherit 2: // Program Name :  Inherit1.cpp 3:  4: #include &lt;iostream.h&gt; 5: enum GENDER { MALE, FEMALE }; 6:  7: class Mammal 8: { 9: public:10:   // constructors11:   Mammal():itsAge(35), itsWeight(180){}12:   ~Mammal(){}13: 14:   int GetAge()const { return itsAge; }15:   void SetAge(int age) { itsAge = age; }16:   int GetWeight() const { return itsWeight; }</PRE><PRE>17:   void SetWeight(int weight) { itsWeight = weight; }</PRE><PRE>18: 19: 20: protected:21:   int itsAge;22:   int itsWeight;23:  };24: 25: class Human : public Mammal26: {27: public:28: 29:   // Constructors30:   Human():itsGender(MALE){}31:   ~Human(){}32: 33:   GENDER GetGender() const { return itsGender; }34:   void SetGender(GENDER gender) { itsGender = gender; }35: 36:   void Drive() { cout &lt;&lt; &quot;Driving to work...\n&quot;; }37:   void Work() { cout &lt;&lt; &quot;working...\n&quot;; }38: 39: private:40:   GENDER itsGender;41: };42: 43: void main()44: {45:   Human John_doe;46:   John_doe.Drive();47:   John_doe.Work();48:   cout &lt;&lt; &quot;John_doe is &quot; &lt;&lt; John_doe.GetAge() &lt;&lt; &quot; years old\n&quot;;49: cout &lt;&lt; &quot;And weighs &quot; &lt;&lt;John_doe.GetWeight() &lt;&lt; &quot; lbs \n&quot;;50: }</PRE><P>The output from Listing A.13 is shown in Figure A.12.</P><P><A HREF="javascript:popUp('0afig12.gif')"><B>FIGURE A.12.</B></A><B> </B><I>Inheritanceoutput.</I></P><P>On line 5, a new keyword enum is defined. Enumerate defines a new data type witha list of identifiers. The identifiers are fixed values that increment automatically.In this example, the variable MALE has a value 0 and the variable FEMALE has a value1 by default. You could also specify a value:</P><P><PRE>enum Alphabets ( A, B, C=5, D=1)</PRE><P>In this case, A has a value 0, B is 1, C is 5, and D is 1. If you did not specifyany values, then</P><P>A is 0</P><P>B is 1</P><P>C is 2</P><P>D is 3</P><P>On line 20, another new keyword protected is defined in the class Mammal. Youcovered public and private in the section on classes where all the data members weredefined under the private keyword. When the data member is defined as private, thederived class cannot access them. For the derived classes to be able to access thedata members and methods of a class, they must be defined as protected. The protectedkeyword restricts the access only to the derived classes. Another alternative isto define these methods and members as public, in which case all classes have freeaccess. Although this is a solution, it is not a desired solution because it movesyou away from encapsulation.</P><P>Line 7 declares the base class Mammal. The constructor is on line 11, and thedestructor is on line 12. In classes, whenever an object of the class is created,the class constructor is called. The constructor class performs an additional functionof initializing its member data, itsAge(35) and itsWeight(180). This could have beenaccomplished by initializing the member data in the body of the constructor, as shownin the following:</P><P><PRE>Mammal(){itsAge = 35;itsWeight = 180;};</PRE><P>The technique of initializing the data members in the constructor declaration(as shown on line 11 of Listing A.14) is far more efficient due to the internal initializationof classes in C++. Use this technique whenever possible because it increases codeefficiency.</P><P>With derived classes, when an object is created in the derived class, the constructorof the base class is called first and then the constructor of the derived class iscalled. In this example, when the object John_doe is created for the first time,the constructor of the base class Mammal is called. The object John_doe is not createduntil both the base constructor and derived class constructor are called. With destructors,the reverse order is followed; when the object John_doe ceases to exist, the derivedclass destructor is called before the base class destructor. On line 25, you definethe name of the derived class and its relevant base class.</P><P>Line 48 and line 49 are critical in terms of how the data is accessed and output.On lines 48 and 49, the Human object John_doe accesses information directly fromthe base class of Mammal. Remember from the example class3.cpp, to output data froma nested class, you had to use indirect access to the class Tax_assessment.</P><P>Inheritance is a significant tool in object-oriented programming, and if it'sused effectively, it provides code reusability. The inherit1.cpp program gave youan overall flavor of inheritance and its properties. However, when programs are writtenin real life, they are structured more efficiently. The next program involves a morelogical and formal process of writing a program.</P><P>Assume you are writing a program for the automobile market. The automobile marketconsists of cars, trucks, minivans, and SUVs (sport utility vehicles). automobileis the parent or base class, and the others are the derived classes. Let's startby defining the automobile class. Listing A.14 shows the code.</P><P><H4>LISTING A.14. Auto.h.</H4><PRE> 1: // Workspace name: Inherit2 2: // Program name:  Auto.h 3:  4: #ifndef AUTO_H 5: #define AUTO_H 6:  7: class automobile  8: { 9: protected:10:  int miles_per_gallon;</PRE><PRE>11:  float fuel_capacity;</PRE><PRE>12: public:13:  void initialize(int in_mpg, float in_fuel);14:  int get_mpg(void);15:  float get_fuel(void);16:  float travel_distance(void);17: }18: 19: #endif</PRE><P>Lines 4 and 5 include directives to the preprocessor. The directive on line 4is covered in detail toward the end of this section. On line 7, the class automobileis defined. This class has two data members and four methods. The class is includedin the header file only. The definition of the methods of this class are containedin the Auto.cpp file in Listing A.15.</P><P><H4>LISTING A.15. Auto.cpp.</H4><PRE> 1: // Workspace name : Inherit2 2: // Program name :  Auto.cpp 3: #include &quot;auto.h&quot; 4:  5:  6: void automobile::initialize(int in_mpg, float in_fuel) 7: { 8:  miles_per_gallon = in_mpg; 9:  fuel_capacity = in_fuel;10: }11: 12:        // Get the rated fuel economy - miles per gallon13: int automobile::get_mpg()14: {15:  return miles_per_gallon;16: }17: 18:        // Get the fuel tank capacity19: float automobile::get_fuel()20: {21:  return fuel_capacity;22: }23: 24:        // Return the travel distance possible25: float automobile::travel_distance()26: {27:  return miles_per_gallon * fuel_capacity;28: }</PRE><P>The method get_mpg provides the value for the miles per gallon for a particularvehicle. The get_fuel<I> </I>method provides the gas tank capacity. Next, you definethe first derived class, a car, in Listing A.16.</P><P><H4>LISTING A.16. Car.h.</H4><PRE> 1: // Workspace name: Inherit2 2: // Program name:  Car.h  3:  4: #ifndef CAR_H 5: #define CAR_H 6:  7: #include &quot;auto.h&quot; 8:  9: class car : public automobile 10: {11:  int Total_doors;12: public:13:  void initialize(int in_mpg, float in_fuel, int door = 4);14:  int doors(void);15: };16: 17: #endif</PRE><P>The class car is a derived class from the automobile class. Because it is a derivedclass, it has access to all of the methods of the base class automobile. In addition,this class has a data member for the number of doors in the car. The methods of thisclass are defined in Car.cpp in Listing A.17.</P><P><H4>LISTING A.17. Car.cpp.</H4><PRE> 1: // Workspace name: Inherit2 2: // Program name:  Car.cpp  3:  4: #include &quot;car.h&quot; 5:  6: void car::initialize(int in_mpg, float in_fuel, int door) 7: { 8:  Total_doors = door;</PRE><PRE> 9:  miles_per_gallon = in_mpg;</PRE><PRE>10:  fuel_capacity = in_fuel;11: }12: 13: 14: int car::doors(void)15: {16:  return Total_doors;17: }</PRE><P>The initialization method is defined in lines 6 through 11. It is important tonote that the base class of the automobile (auto.h) also had an initialization method.The initialization in the car class overrides the base class initialization. Lastbut not least is the main() definition. The main() method is defined in Allauto.cppin Listing A.18.</P><P><H4>LISTING A.18. Allauto.cpp.</H4><PRE> 1: // Workspace name: Inherit2 2: // Program name:  Allauto.cpp  3:  4: #include &lt;iostream.h&gt; 5: #include &quot;auto.h&quot; 6: #include &quot;car.h&quot; 7:  8: int main() 9: {10: 11: car sedan;12: 13:  sedan.initialize(24, 20.0, 4);14:  cout &lt;&lt; &quot;The sedan can travel &quot; &lt;&lt; sedan.travel_distance() &lt;&lt; 15:                           &quot; miles.\n&quot;;16:  cout &lt;&lt; &quot;The sedan has &quot; &lt;&lt; sedan.doors() &lt;&lt; &quot; doors.\n&quot;;17: 18:  return 0;19: }</PRE><P>The main() definition has only one object defined. On line 11, an object of classcar is declared. The initialization is on line 13. The initialization passes thefuel efficiency of the car (miles per gallon) and the tank capacity. This informationis used to access the method travel_distance in the base class define in auto.cpp.The derived class has access to the methods of the base class. Additionally, thederived class passes information to its own data member about the number of doorsin the vehicle. The result of executing this program is shown in Figure A.13.</P><P><A HREF="javascript:popUp('0afig13.gif')"><B>FIGURE A.13.</B></A><B> </B><I>Vehicleclass results.</I></P><P>You can now add more classes for other vehicle types. You can make your own classesfor a truck and minivan and derive them from the base class exactly like the carclass.</P><P>If you add another class for trucks, it is important to include the preprocessordirectives from Listing A.14's lines 4 and 19. These lines are listed again in thefollowing:</P><P><PRE>4    #ifndef AUTO.H5    #define AUTO.H...19    #endif</PRE><P>Because the truck class is derived from the parent class automobile, it must includethe file Auto.h in Truck.h. The header of the car class, Car.h, already includesAuto.h for the same reason. Now, if you create a method that uses both the truckand car classes, you could potentially include Auto.h twice, which would generatein a compiler error. To prevent this, you add lines 4 and 5 of Listing A.14. Line4 issues a command to the compiler to verify whether the class AUTO.H has been defined;if it hasn't been defined, the program jumps to line 5 and defines it, and if ithas been defined, the program jumps to line 19 and ends.</P><P><H2><A NAME="Heading10"></A>Summary</H2><P>Congratulations! You have covered almost all of the features and properties ofC++. You should now have a sol

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
91亚洲资源网| 久久久久国产精品厨房| 久久综合色鬼综合色| 亚洲高清免费观看高清完整版在线观看| 国产成人h网站| 久久久久久9999| 国产精品一区二区久激情瑜伽| 欧美调教femdomvk| 亚洲综合无码一区二区| 色综合夜色一区| 一区二区三区在线观看欧美| 国产精品一区二区在线观看网站| 在线电影一区二区三区| 亚洲在线视频网站| 欧美三级电影网站| 日本三级亚洲精品| 日韩写真欧美这视频| 天堂va蜜桃一区二区三区| 91丨九色porny丨蝌蚪| 中文字幕一区二区不卡| 国产成人免费在线观看| 国产精品护士白丝一区av| 91在线丨porny丨国产| 亚洲伊人色欲综合网| 3d成人h动漫网站入口| 亚洲国产日韩av| 欧美一级欧美一级在线播放| 一区二区国产视频| 欧美色视频一区| 国产精品成人午夜| 色婷婷综合激情| 日韩av一级片| 欧美日韩综合不卡| 精品午夜久久福利影院| 国产欧美日韩精品一区| 91福利精品视频| 日本美女一区二区三区| 国产欧美日韩另类一区| 国产电影精品久久禁18| 国产精品色呦呦| 懂色av一区二区在线播放| 久久女同性恋中文字幕| 91欧美激情一区二区三区成人| 亚洲午夜成aⅴ人片| 欧美大片国产精品| 91免费看视频| 美女视频免费一区| 欧美国产成人在线| 3d动漫精品啪啪1区2区免费| 国产成人小视频| 中文在线资源观看网站视频免费不卡| 一本久久综合亚洲鲁鲁五月天| 婷婷综合另类小说色区| 国产日韩影视精品| 高清国产午夜精品久久久久久| 国产精品久久精品日日| 欧美精品免费视频| 99在线视频精品| 国精产品一区一区三区mba视频| 亚洲欧美日韩精品久久久久| 精品国产1区二区| 91高清视频免费看| 国产91精品露脸国语对白| 国产无人区一区二区三区| 欧美日韩精品电影| 白白色 亚洲乱淫| 中文字幕一区二区三区av| 色综合色综合色综合| 亚洲欧美在线另类| 久久伊人中文字幕| 欧美一区二区性放荡片| 91丝袜国产在线播放| 首页国产欧美日韩丝袜| 尤物在线观看一区| 国产精品超碰97尤物18| 日韩视频一区二区在线观看| 国产成人在线视频免费播放| 日韩不卡一二三区| 亚洲午夜成aⅴ人片| 亚洲色图制服诱惑 | 国产91精品一区二区麻豆网站| 日韩av电影天堂| 亚洲成av人片在线| 亚洲激情一二三区| 久久一区二区三区四区| 日韩欧美成人激情| 欧美精品粉嫩高潮一区二区| 成人激情开心网| 亚洲动漫第一页| 亚洲美女在线国产| 国产精品私人自拍| 亚洲国产精品ⅴa在线观看| 久久久久久久久久久黄色| 日韩精品一区二区三区中文精品| 欧美日韩免费观看一区三区| av电影在线观看一区| 成人h精品动漫一区二区三区| 国产高清无密码一区二区三区| 久久99国产精品久久| 蜜桃av噜噜一区二区三区小说| 亚洲乱码国产乱码精品精可以看| 日韩一区二区三区视频在线| 日韩一区二区视频在线观看| 欧美一区二区福利视频| 欧美三级中文字| 在线播放中文字幕一区| 日韩欧美亚洲国产另类| 欧美精品一区二区蜜臀亚洲| 欧美一区日韩一区| 日韩精品一区二区三区中文精品| 欧美一区欧美二区| 7777精品伊人久久久大香线蕉 | 国产精品第一页第二页第三页| 欧美激情中文字幕| 精品理论电影在线观看| 欧美精品成人一区二区三区四区| 欧美一区二区三区婷婷月色 | 性欧美大战久久久久久久久| 五月婷婷综合激情| 美女www一区二区| 国产成人免费视频网站高清观看视频| 99热在这里有精品免费| 成人激情黄色小说| 国产精品乱码妇女bbbb| 欧美电影在哪看比较好| 欧美刺激午夜性久久久久久久| 国产亚洲欧美中文| 国产亚洲精品资源在线26u| 中文字幕字幕中文在线中不卡视频| 亚洲综合免费观看高清在线观看| 日韩精品一卡二卡三卡四卡无卡| 国产在线视频精品一区| 91蝌蚪porny成人天涯| 欧美日韩免费电影| 欧美电影精品一区二区| 日本一区二区三区国色天香| 中文字幕免费一区| 日本成人中文字幕在线视频| 国产91精品一区二区麻豆网站| 欧美三级中文字幕在线观看| 久久久久综合网| 亚洲一区免费视频| 暴力调教一区二区三区| 精品欧美一区二区久久| 亚洲人妖av一区二区| 国产精品不卡视频| 午夜a成v人精品| 91网上在线视频| 久久久久久久久久看片| 一区二区在线电影| 成人精品国产一区二区4080| 欧美一个色资源| 亚洲高清视频在线| 99久久99久久久精品齐齐| 精品国产污污免费网站入口| 亚洲国产精品一区二区www| 国产麻豆精品theporn| 欧美日韩精品免费| 亚洲老司机在线| 国产成人午夜99999| 91精品国产综合久久精品性色| 亚洲女同一区二区| 成人妖精视频yjsp地址| 日韩精品资源二区在线| 日韩黄色一级片| 日韩亚洲欧美中文三级| 蜜臀国产一区二区三区在线播放| 91精品国产综合久久精品app| 亚洲成人黄色小说| 欧美日本国产视频| 日韩激情中文字幕| 日韩你懂的在线播放| 毛片基地黄久久久久久天堂| 日韩欧美黄色影院| 国产一区二区三区久久久 | 精品国产乱码91久久久久久网站| 蜜臀久久99精品久久久久久9| 日韩女优毛片在线| 国产在线精品视频| 久久久久久久久久久黄色| 粉嫩一区二区三区在线看| 国产精品美女久久久久久久久| www.在线成人| 亚洲一二三区不卡| 91精品国产综合久久久久久漫画| 久久国内精品自在自线400部| 国产午夜精品理论片a级大结局| 成人18精品视频| 一片黄亚洲嫩模| 日韩一区二区三区视频| 国产精品一区专区| 自拍视频在线观看一区二区| 欧美午夜免费电影| 久久99精品一区二区三区| 国产精品美女久久久久av爽李琼| 不卡的av在线| 天天综合天天综合色| 久久久久国产精品麻豆ai换脸| 97精品超碰一区二区三区| 一区二区欧美精品|