C + + classes (1)

Source: Internet
Author: User

_c++ Primer plus_chap10_compiler-devc++_

I. Understanding the function of the book (p.343)

①stock.h_ defining classes in a header file

1#ifndef Stock00_h_//ifndef Prevention of double definition2 #defineStock00_h_3 4#include <string>5 6 classStock//The stock is the new class type name7 {8     Private://programs that use this class must have access to the private section through the public member function (or friend function, see CHAP11)9STD::stringCompany ;Ten         LongShares//number of shares One         DoubleShare_val;//Stock price A         DoubleTotal_val;//Stock price -         voidSet_tot () {total_val=shares*Share_val;} -      Public://programs that use this class have direct access to the public section the         voidAcquire (ConstSTD::string& Co,LongNDoublePR); -         voidBuyLongNumDoublePrice ); -         voidSellLongNumDoublePrice ); -         voidUpdateDoublePrice ); +         voidShow (); -};//Don't forget the class curly braces are followed by ";" +  A #endif at  - /*There is also a third type of access control keyword protected, see follow Chap13*/ 

Implementation of ②STOCK.CPP_ class member functions

1 /** * Implementation of CLASS member functions * **/2 /*The class member function header uses the scope operator resolution (::) to identify the attribution*/3 /*function header such as update () member function: void Stock::update (double price), identifier update () has class scope*/ 4 /*This representation means that another tired member function can be named Update () with the same name*/5 /*Other member functions of the same class as update () can be accessed without (::) access to it, using update outside the Class () requires additional measures (to be continued)*/6 /** * Implementation of the class * **/7 8#include <iostream>9#include"stock.h"Ten  One /*Aquire managing the first purchase of a company's shares*/  A voidStock::acquire (ConstSTD::string& Co,LongNDoublePR) - { -company=Co; the     if(n<0) -     { -std::cout<<"Number of shares can ' t be negative," -<<company<<"shares set to 0.\n"; +shares=0; -     } +     Else Ashares=N; atShare_val=PR; - Set_tot (); - }  -  - /*buy and sell manage to increase and reduce holdings of stocks*/ - voidStock::buy (LongNumDoublePrice ) in { -     if(num<0) to     { +std::cout<<"Number of shares perchased can ' t be negative," -<<"Transaction is aborted.\n"; the     } *     Else $     {Panax Notoginsengshares+=num; -Share_val=Price ; the Set_tot (); +     } A } the  + /*Sell Manage sold stocks*/ - voidStock::sell (LongNumDoublePrice ) $ { $     usingstd::cout; -     if(num<0) -     { thecout<<"Number of shares sold can ' t be negative." -<<"Transaction is aborted.\n";Wuyi     } the     Else if(num>shares) -cout<<"You can ' t sell more than." Wu<<"Transaction is aborted.\n";  -     Else About     { $shares-=num; -Share_val=Price ; - Set_tot (); -     } A } +  the /**/  - voidStock::update (DoublePrice ) $ { theShare_val=Price ; the Set_tot (); the } the  - /**/ in voidstock::show () the { thestd::cout<<"Company :"<< Company About<<"Shares:"<<shares<<'\ n' the<<"Share price:$"<<Share_val the<<"Total worth:$"<<total_val<<'\ n'; the } + 

→ Note that the member function "Set_tot" in the preceding class is defined in the header file and called in the following four other member functions in Stock.cpp. This involves the knowledge of inline functions:

① for defining functions that are in a class declaration, they will automatically generate an inline function, and stock::set_tot is an inline function. Inline functions can also be defined outside the class declaration, at which point the inline -defined character can be used when defining it;

The ② inline function does not make a real call at compile time, and the compiler pastes the inline function code in the invocation (formally called the call), where it is inserted in every place where it is used instead of the actual call to pass the function address.

The ③ inline function is usually a function of small memory, its function--set_tot () in the example above, saves the formula of resetting total in each member function that calls it, effectively saving time and improving efficiency.

③usestock.cpp_ using classes

1#include <iostream>2#include"stock.h"3 intMain ()4 {5 Stock MC;6Mc.acquire ("NanoSmart", -,12.50);7 mc.show ();8Mc.buy ( the,18.125);9 mc.show ();TenMc.sell ( -,20.00); One mc.show (); AMc.buy (300000,40.125); - mc.show (); -Mc.sell (300000,0.125); the mc.show (); -     return 0; -}

④ Running Results

Like books.

C + + classes (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.