Pencil-and-paper test finishing

Source: Internet
Author: User

1. The difference between C's struct and C + + struct BODY

(1) C in the structure of the body does not allow the existence of functions, C + + allows the internal member functions, and allows the function is a virtual function. So the struct of C is no constructor, destructor, and this pointer.

(2) C's struct can only have public access to internal member variables, while C + + allows public,protected,private of three.

(3) C language structure can not inherit, C + + structure can be inherited from other structures or classes.

These are the differences between the surface, the actual difference is the process-oriented and object-oriented programming thinking of the difference:

The structure of C simply wraps the data variables and does not involve the algorithm.

C + + is to encapsulate data variables and related algorithms for these data variables, and give different access to the data and classes.

There is no concept of class in C language, but C language can create function pointers through structure body to realize object-oriented thought.

2. How do I define and initialize a constant member in a class? Answer: The const member can only be initialized in the initialization list, as follows:

class Cbook {public:    constdouble  m_price;    Cbook (): M_price (8.8) {}};

3. What is the purpose of using the mutable keyword when defining member functions of a class? Answer: When you need to modify the data members of an object in the Const method, you can use the Mutable keyword before the data member to prevent compilation errors. Examples are as follows:

classCbook { Public: mutable DoubleM_price;//If you don't add it, it will go wrong .Cbook (DoublePrice ): M_price (Price) {}DoubleGetPrice ()Const;//defining a const method};DoubleCbook::getprice ()Const{ M_price = 9.8; returnM_price;}

4. What is the role of the explicit keyword in C + +?

= 9.8 Such an implicit conversion, using explicit to prevent this conversion from occurring.

5. Another constructor problem for the constructor call class:

In C + +, if you determine the creation process for a constructor, If you call other overloaded constructors in that constructor,

It does not initialize the specified this.

Examples illustrate the following:

class Cbook {public:  double  m_price;  Cbook () {    cbook (8.8);  }  Cbook (double price ): M_price (Price) {}}; int Main () {  cbook C;   << c.m_price << Endl;  // the ideal 8.8} is not output at this time

6. Virtual function Principle Test center, for example, what is the output of the following program?

classA { Public:  Virtual voidFuna (); Virtual voidFunb (); voidfunc (); Static voidFund (); Static intsi;Private:  inti; Charc;};

Q: sizeof (A) =?

Answer:

There are a few things to note about the memory space that the class occupies:

(1) If the class contains virtual functions, the compiler needs to build a virtual function table for the class, the class needs to store a pointer to the first address of the virtual function table , note that no matter how many virtual functions, only one table is created, all the virtual function addresses are present in this table, Only one pointer in the class is required to point to the first address of the virtual function table.

(2) A static member of a class is shared by all instances of the class, and it does not count toward the space calculated by sizeof

(3) ordinary functions or static normal functions in a class are stored in the stack, not in the space calculated by sizeof

(4) Class members allocate space in byte-aligned ways

Answer: 12 (32-bit system) or 16 (64-bit system)

7. What is the role of virtual inheritance?

In multiple inheritance, subclasses may have multiple parent classes at the same time, and if they have the same parent class (ancestor Class), then there will be multiple ancestor classes in the subclass. For example, Class B and Class C both inherit from Class A, and if Class D is derived from B and C, then there will be two copies of a in Class D. To prevent duplicate parent classes in multiple-inheritance subclasses, you can use virtual functions when inheriting from a parent class, that is, using the virtual keyword when class B and Class C inherit Class A, for example:

Class B:virtual Public A

Class C:virtual Public A

Note: Because multiple inheritance can lead to many complex problems, use caution.

Pencil-and-paper test finishing

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.