The first class after reading the section7.1 <<C++ primer>>

Today,  I start learning the class in C++.. It's really hard to get it.So slow my step into it...

I decided to make some records at first.

The books mention a simple class on Page 73:

struct Sales_data{
 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};

then on Page256. after introducing the function. the author give a new version class.

struct Sales_data {
 // new members operations on Sales_data objects
 std::string isbn() const {return bookNo}
 Sales_data& combine (const Sales_data&);
 double avg_price() const;
 // data members are unchanged from $2.6.1(p.72)
 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};
// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istreams&, Sales_data&);

Author mentions "Functions defined in the class are implicitly inline".(p.238)

Defining Member Functions: Although every member must be declared inside its class, we can define a member function's body either inside or outside of the class body. In Sales_data, isbn is defined inside the class; combine and avg_price will be defined elsewhere.

In this class, there are three new concepts. " std::string isbn() const {return bookNo}"  the definition of isbn() behave a weird way. after the parameter list, there is a "const". what's hell the purpose of "const". Is it declarate the "{}". obviously say "no". a ghost parameter "this". "this" is a implicit behavior. In this call, when isbn returns bookNo, it's implicitly returning name.bookNo.(assume we had declarate an object <"Sales_data name">).  Member functions access the object on which they were called through an extra, implicit parameter named this. When we call a member function, this is initialized with the address of the object on which the function was invoked. For example, when we call

 name.isbn()

the compiler passes the address of "name" to the implicit thisparameter in isbn. it looks like

Sales_data:: isbn(&name)

Inside a member function, we can refer directly to the member of the object on which the function was called. we don't have to use the member access operator to use the member of the object to which this points. Any direct use of a member of the class is assumed to be an implicit reference through this. so the author tell us we can define it like that form.

std::string isbn() const{ return this->bookNo;}

It can work normally as the above definition.

the blue "const" need to catch our attention. the purpose of that const is to modify the type of the implicit this pointer.

The books give us an explanation

"""

By default, the type of this is a const pointer to the nonconst version of the class type. For example, by default, the type of this a Sales_data member function is Sales_data *const. Although this is implicit, it follows the normal initialization rules, which means that (by default) we cannot bind this to a const object. This fact, in return, means that we cannot call an ordinary member function on a const object.

If isbn were an ordinary function and if this were an ordinary pointer parameter, we would declare this as const Sales_data *const. After all, the body of isbn doesn't change the object ot which this points, so our function would be more flexible if this were a pointer to const.

However, this is implicit and does not appear in the parameter list. There is no place to indicate that this should be a pointer to const. The language resolves this problem by letting us put const after the parameter list of a memeber to const. A const following the parameter list indicates that this is a pointer to const. Member functions that use const in this way are const member functions.

"""

an interested example from the book:

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
    units_sold += rhs.units_sold; // add the member of rhs into
    revenue += rhs.revenue; // the members of "this" object
    return *this;   // return the object on which the function was called.
}

The books mentions a insterested part:

"""

The interesting ar about this function is its return type and the return statement. Ordinarily, when we define a function that operates like a built-in operator, our function should minic the behavior of that operator. The built-in assignment operators return their left-hand operand as an lvalue. To return an lvalue, our combine function must return a reference. Because the left-hand operand is a Sales_data object, the return type is Sales_data&.

As we've seen, we do not need to use the implicit this pointer to access the members of the object on which a member function is executing. However, we do need to use this to access the object as a whole:

return *this; // return the object on which the funtion was called

Here the return statement dereferences this to obtain the object on which the function is executing. That is, for the call above, we return a reference to total.

"""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java自带的DOM解析器或者第三方的解析库,比如JDOM或者DOM4J来解析XML数据。 以下是使用Java自带的DOM解析器来解析XML数据的示例代码: ```java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLParser { public static void main(String[] args) { try { File inputFile = new File("F:/test/XML/purchaseOrders.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); System.out.println("Root element: " + doc.getDocumentElement().getNodeName()); NodeList nodeList = doc.getElementsByTagName("purchaseOrder"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println("\nCurrent Element: " + node.getNodeName()); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("ID: " + element.getAttribute("id")); System.out.println("OrderDate: " + element.getAttribute("orderDate")); Element shipTo = (Element) element.getElementsByTagName("shipTo").item(0); System.out.println("ShipTo Name: " + shipTo.getElementsByTagName("name").item(0).getTextContent()); System.out.println("ShipTo Street: " + shipTo.getElementsByTagName("street").item(0).getTextContent()); System.out.println("ShipTo City: " + shipTo.getElementsByTagName("city").item(0).getTextContent()); System.out.println("ShipTo State: " + shipTo.getElementsByTagName("state").item(0).getTextContent()); System.out.println("ShipTo Zip: " + shipTo.getElementsByTagName("zip").item(0).getTextContent()); Element billTo = (Element) element.getElementsByTagName("billTo").item(0); System.out.println("BillTo Name: " + billTo.getElementsByTagName("name").item(0).getTextContent()); System.out.println("BillTo Street: " + billTo.getElementsByTagName("street").item(0).getTextContent()); System.out.println("BillTo City: " + billTo.getElementsByTagName("city").item(0).getTextContent()); System.out.println("BillTo State: " + billTo.getElementsByTagName("state").item(0).getTextContent()); System.out.println("BillTo Zip: " + billTo.getElementsByTagName("zip").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } ``` 运行以上代码,输出如下: ``` Root element: purchaseOrders Current Element: purchaseOrder ID: 1 OrderDate: 2004-06-28 ShipTo Name: Alice Smith ShipTo Street: ShipTo City: Cambridge ShipTo State: MA ShipTo Zip: 12345 BillTo Name: Robert Smith BillTo Street: 8 Oak Avenue BillTo City: Cambridge BillTo State: MA BillTo Zip: 12345 ``` 以上代码解析了XML文件,遍历了每个purchaseOrder元素,输出了其中的ID、orderDate、shipTo和billTo元素的内容。你可以根据自己的需求,扩展或修改以上代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值