C++ Primer Summary

About Header

We use headers to access classes defined for our own applications.

  • Conventionally, header file names are derived from the name of a class defined in that header.
  • The standard library headers typically have no suffix at all. Compilers usually don’t care about the form of header file names, but IDEs sometimes do.
  • Headers should have guards, even if they aren’t (yet) included by another header.
  • Headers should not include using declarations.

#About class
A major design of goal of C++ is to let the programmers define their own types that are as easy to use as the built-in types.

#About Primitive Built-in Types

  • Use int for integer arithmetic. short is usually too small and in practice, long often has the same size of int. If you data values are larger than the minimum guaranteed size of an int, then use long long.
  • Use double for floating-point computations; float usually does not have enough precision. The precision offered by long double usually is unnecessary.
  • Expression that mix signed and unsigned values can yield surprising results when the signed value is negative.
  • Initialization in C++ is a surprisingly complicated topic.
  • List initialization, When used with variables of built-in type,this form of initialization has one important property: the compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information.
	int units_sold{0};//list initialization
	long double  ld = 3.1415926535;//this is copy initialization
	int a{ld}; //error:narrowing conversion required
	int c(ld);//ok:but value will be truncated +this is also an example of direct initialization

#About Container

  • use empty() instead of size()==0 to get if the container is empty.
  • use at(i) instead of [i] to get the i-th element;
  • use contain::size_type to get the index of element. size_type is one machine-independent manner. And size_type is an unsigned type.
  • The most common way of using vectors is to define an initially empty vector to which elements are added as their values become at run time.The exception to this rule is if all the elements actually need same values. If differing the element values are needed , it is usually more efficient to define an empty vector.
  • If the vector holds elements of a built-in value type, such as int, then the element initializer has a value of 0. or string as empty string; However, if our vector holds objects that we cannot default initialize, then we must supply an initial element value.
	vector<OwnType>(size,ownTypeElement);
  • C++ programmers use != as a matter of habit.They do so for the same reason that they use iterators rather than subscripts.
  • The prefix version avoids unnecessary work. It increments the value and returns the incremented version.However, the postfix operator must store the original value so that it can return the unincremented value as its result.

#About Function

  • Programmers accustomed to programming in C often use pointer parameters to access objects outside a function. In C++,programmers generally use reference parameters instead. And reference parameters that are not changed inside a function should be references to const.
  • Default arguments ordinarily should be specified with the function declaration in an appropriate header.And what’s more is defaults can be specified only if all parameters to the right already have defaults.
  • In general, the inline mechanism is meant to optimize small, straight-line functions that are called frequently.(A 75-line function will almost surely not be expanded inline.)And inline functions normally are defined in headers.

#About Member Functions

  • 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值