C ++ Primer Plus Chapter 10 objects and classes

1. What is the interface?

For the program for the class, the user can not directly access the public class to operate, but can be called class methods, the method is part of an interface between the user and the public class, so the user can use to class member variable, have to write methods in the class, in which the interface.
In addition, we generally interface (class definition) in header files, and realize (the code class method) in the source code, why should this way have written in a previous blog.

Why 2. #ifndef behind the header file name to uppercase

To distinguish macros and ordinary variables.
This macro is for pre-Used, usually pretreated with variable capital, so these macros are capitalized. For convenience, generally capitalized as a pre-processor variable file name, in fact, what all can be added later #ifndef, with the file name just to distinguish good. It is only used to define a preprocessor variable.

3. What is the definition of the class include?

The class definition includes the definition of member variables, function declarations, etc., usually set to private member variables private, public function is generally set to public, The reason for this is to let users direct access to modify the member variables, and access by calling the method.
Further members of a class type is not provided, the default type of private members.

4. scope resolution operator ::

In c ++ may not be accessed through the object method, but may be operated by a corresponding function call scope resolution operator class directly.
This static members and methods Java and C # are similar, but not directly by calling the object.

The outside member functions defined in the class declaration makes inline

class Stock
{
private:
	...
	void set_tot();
public:
	...
};

inline void Stock::set_tot()
{
	total_val = shares * share_val;
}

As described above, as long as the keyword to use the inline function definition.

6. How to use the constructor?

1. explicitly call

Stock food = Stock(“World Cabbage”,250,12.5);

2, an implicit call

Stock food(“World Cabbage”,250,12.5);

3.new dynamic allocation

Stock *food = new Stock(“World Cabbage”,250,12.5);

4. list initialization

Stock food ={“World Cabbage”,250,12.5};
Stock food {“World Cabbage”,250,12.5};

7. The relationship between the object and the constructor

You can not use the object to call the constructor, because before the constructor to construct an object, the object does not exist. Thus constructor is used to construct the object, and the object can not be invoked.

8. The difference between the two kinds of statement

Stock stock2 = Stock("Boffo Objects",2,2.0);
stock1 = Stock("Nifty Foods",10,50.0)	//temporary object

The first statement is initialized, a new stock2 object. The second sentence is to use the constructor to create a new temporary object, and assign the temporary object to stock1.
and also

Stock stock2 = stock3
stock2 = stock3	

The first statement is the initialization stock2 objects, assigned to the stock3 stock2, statements are next day using techniques operator overloading, the stock3
assigned to already existing object stock2, these two statements are not the same.

9. after the function definition statement added const What does it mean?

such as

void Stock::Show() const

Class function declaration and definition in this way is called const member functions, which means that this function does not modify its call to the class object.

10.this pointer

this pointer to an object used to call the member function, rather than to class. Each member function (including constructors and destructors) this has a pointer that is implicitly defined.

Published 22 original articles · won praise 2 · Views 462

Guess you like

Origin blog.csdn.net/weixin_42709632/article/details/104086716