C/C ++ tips (2)

Source: Internet
Author: User

1. Diamond structure in c ++

Class {};
Class B: virtual public {};
Class C: virtual public {};
Class D: Public B, public C {};

2. Both long and float are represented in four bytes.

3. Role of the keyword volatile

A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Specifically, the optimizer must carefully re-read the value of this variable every time when using this variable, instead of storing the backup in the register.

4. Differences between abstract classes and interfaces

Abstract classes are special classes, but cannot be instantiated (classes that define pure virtual functions are called abstract classes). In addition, abstract classes have other features of classes; the important thing is that abstract classes can contain abstract classes and abstract methods. This is not a common class, but it can also contain common methods. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them. Although you cannot define an instance of an abstract class, you can define its pointer, which is the focus of the abstract class implementation interface.

An interface is a concept. It is implemented in C ++ using abstract classes and interfaces in C # and Java.

The interface is of the reference type. It is similar to a class and has three similarities with the abstract class.

1) it cannot be instantiated.

2) include unimplemented methods and statements

3) The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only methods, including other members)

In addition, interfaces have the following features:

In addition to methods, interfaces can also contain attributes, indexers, and events. These members are defined as common members. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members.

5. Differences between sizeof and strlen

Sizeof is a single-object operator, while strlen is a function.

The operands of sizeof can be data types, functions, variables, and expressions.

Strlen is not as widely used as sizeof. The parameter must be a char * pointer.

6. What is a wild pointer and under what circumstances will it generate a wild pointer? How can this problem be avoided?

The wild pointer is a pointer to the unavailable memory area. Operations on the wild pointer usually result in unpredictable errors.

There are three scenarios for the generation of wild pointers:

1) the pointer variable is not initialized.

2) the pointer in the dynamic memory is released by free or delete and is not set to null.

3) pointer operations go beyond the scope of variables.

Avoid: We need to initialize the pointer to null and assign null to it after use.

Avoid shortest copy during copy Construction

7. Three elements and five principles of object-oriented

Three elements: inherited encapsulation Polymorphism

Encapsulation: combines the abstracted data with the method to form an organic whole. users do not have to understand the specific implementation details, but use class members through external interfaces and specific access permissions.

Inheritance: inheritance enables sub-classes to have various attributes and methods of the parent class without having to write the same code.

Polymorphism: different objects call the same function to produce different results, allowing pointers of the subclass type to be assigned to pointers of the parent class.

RMB 5: single Responsibility Principle

There should be only one reason for the change.

Open and closed Principle

Open to extension, closed to Modification

Rys replacement principle

Sub-classes can expand the features of the parent class, but cannot change the features of the parent class.

Dependency inversion principle

This reduces the coupling between business and implementation modules.

Interface isolation principle

It is better to use multiple dedicated interfaces than to use a single interface.

8. class-related sizeof operations

Define an empty class without any member variables or member functions. Evaluate sieof for this type. What is the result?

The answer is 1. An empty instance does not contain any information. sizeof should be 0, but when we declare an instance of this type, it must occupy a certain amount of space in the memory; otherwise, these instances cannot be used. The compiler determines how much memory is used. Each empty instance in Visual Studio occupies 1 byte of space.

If a constructor and destructor are added to this type and sizeof is obtained for this type, what is the result?

As before, it is still 1. To call constructor and destructor, you only need to know the address of the function. The address of these functions is only related to the type, but not to the type instance, the compiler does not add additional information to the instance because of the two functions.

What if I mark the Destructor as a virtual function?

The C ++ compiler generates a virtual function table for a type that contains virtual functions, and adds a pointer to the virtual function table to each instance of the type. On 32-bit machines, a pointer occupies 4 bytes of space.

9. Copying constructor values is not allowed

If the copy constructor is allowed to pass values, it will call the copy constructor in the copy constructor, and an endless recursive call will be formed, resulting in stack overflow.

Related Article

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.