zhuang@linux:~/notes/cpp/cpp_basics/$ cat

CPP Basics

$ grep tags cpp_basics.md

Overview of CPP Basics.

The function header describes the interface between a function and the function that calls it.

The list-initialization protects against narrowing.

Raw string use R"(...)".

Bit Fields in Structures

c++
struct torgle_register
{
    unsigned int SN : 4;    // 4 bits for SN value
    unsigned int : 4;       // 4 bits unused
    bool goodIn : 1;        // valid input
    bool goodTorgle: 1;     // successful torgling
};

Prefixing Versus Postfixing

The user defines a prefix function that works by incrementing a value and then returning it. But the postfix version works by first stashing a copy of the value, incrementing the value, and then returning the stashed copy.Thus, for classes, the prefix version is a bit more efficient than the postfix version. In short, for built-in types, it most likely makes no difference which form you use. For user-defined types having user-defined increment and decrement operators, the prefix form is more efficient.

The comma operator is a sequence point. C++ states that the value of a comma expression is the value of the second part of the expression.

cin.get(ch) Versus cin.get()

Propertycin.get(ch)ch = cin.get()
Method for conveying input characterAssign to argument chUse function return value to assign to ch
Function return value for character inputAn istream object; converts to true after bool conversionCharacter code represented as an int value
Function return value at EOFAn istream object; converts to false after bool conversionEOF

A reference is rather like a const pointer; you have to initialize it when you create it, and when a reference pledges its allegiance to a particular variable, it sticks to its pledge.

Rvalue reference, that can refer to an rvalue. It’s declared using &&:

c++
double && rref = std::sqrt(36.00);
double j = 15.0;
double && jref = 2.0* j + 18.5;
std::cout << rref << '\n';
std::cout << jref << '\n';

Storage Duration, Scope, and Linkage

Storage DescriptionDurationScopeLinkageHow Declared
AutomaticAutomaticBlockNoneIn a block
RegisterAutomaticBlockNoneIn a block with the keyword register
Static with no linkageStaticBlockNoneIn a block with the keyword static
Static with external linkageStaticFileExternalOutside all functions
Static with internal linkageStaticFileInternalOutside all functions with the keyword static

The following are the most important OOP features:

  • Abstraction
  • Encapsulation and data hiding
  • Polymorphism
  • Inheritance
  • Reusability of code

You can use only member functions to overload the following operators:

OperatorDescription
=Assignment operator
()Function call operator
[]Subscripting operator
->Class member access by pointer operator

Conversion Function

To convert to type typeName, you use a conversion function in this form:

c++
operator typeName();

Note the following points:

  • The conversion function must be a class method.
  • The conversion function must not specify a return type.
  • The conversion function must have no arguments.

Special Member Functions

C++ automatically provides the following member functions:

  • A default constructor if you define no constructors
  • A default destructor if you don’t define one
  • A copy constructor if you don’t define one
  • An assignment operator if you don’t define one
  • An address operator if you don’t define one
  • move constructor
  • move assignment operator

Virtual Function Mechanism

virtual function mechanism

Using virtual functions has the following modest costs in memory and execution speed:

  • Each object has its size increased by the amount needed to hold an address.
  • For each class, the compiler creates a table (an array) of addresses of virtual functions.
  • For each function call, there’s an extra step of going to a table to look up an address.

Member Function Properties

FunctionInheritedMember or FriendGenerated by DefaultCan Be VirtualCan Have a Return Type
ConstructorNoMemberYesNoNo
DestructorNoMemberYesYesNo
operator=NoMemberYesYesYes
operator&YesEitherYesYesYes
Conversion functionYesMemberNoYesNo
operator()YesMemberNoYesYes
operator[]YesMemberNoYesYes
operator->YesMemberNoYesYes
operator op=YesEitherNoYesYes
operator newYesStatic memberNoNovoid*
operator deleteYesStatic memberNoNovoid
Other operatorsYesEitherNoYesYes
Other membersYesMemberNoYesYes
FriendsNoFriendNoNoYes

Here, operator op= represents compound-assignment operators such as operator+=, operator-=, operator*=, and operator/=.

zhuang@linux:~/notes/cpp/cpp_basics/$ comments