C/C ++ questions and answers: 6-10

Source: Internet
Author: User

Question 6: in which situations can B be implicitly converted to a for non-C ++ built-in types A and B?

(1) Class A {...}; Class B: Public {......} ; // The Public B inherits from a, which can be indirectly inherited

(2) Class {......}; class B {operator A () {return a: ();}......}; // B converts the data type to
(3) Class A {A (const B &) ;}; // a constructor that implements the non-explicit parameter as B (other parameters with default values can be available)
Avoid ambiguity when providing implicit conversion. For example, in the preceding methods, if (2) is defined and (3) is defined, there is a ambiguity. The compiler can convert B to a in two ways. One is to use the constructor of A, and the other is to use the type conversion operator of B. Best Practice: The best way to avoid ambiguity is to avoid writing pairs of classes that provide mutual implicit conversion. C ++ primer.

Question 7: what is the problem with this program?

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. PRIVATE:
  6. Int value;
  7. Public:
  8. A (int n) {value = N ;}
  9. A (a other) {value = Other. value ;}
  10. Void print () {cout <value <Endl ;}
  11. };
  12. Int main ()
  13. {
  14. A A = 10;
  15. A B =;
  16. B. Print ();
  17. Return 0;
  18. }

Compilation error. A (a other) {} the duplicate constructor is incorrectly defined. The form parameter of this function can only be referenced by objects of this class type. It is often used to modify C ++ primer by const. It should be changed to a (const A & Other) {value = Other. value ;}. The copy constructor can be used:
(1) display or implicitly Initialize an object of the same type based on another object
String S = "hello ";
First, a string constructor that accepts a C-style string parameter is called to create a temporary object, and then a copy constructor is called to initialize s as a copy of the temporary object.
(2) copy an object and pass it as a real parameter to a function.
Void Foo (string S );
(3) copy an object from the return result of the Function
String Foo ();
(4) initialize elements in the container.
Vector <string> SVEC (5 );
First, call the string default constructor to create a temporary value, and then copy the temporary value to every element of the SVEC, that is, call the string copy constructor.
(5) initialize array elements based on the element initialization list
For example, the following function calls the copy constructor when initializing array. The default constructor is called when array B is initialized. When initializing array C, because it has four elements and the initialization list has only two, the initialization of the first two elements calls the replication constructor, the default constructor is called for initialization of the last two elements.

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. Public:
  6. A () {value = 1; cout <"a default constructor" <Endl ;}
  7. A (INT v) {value = V; cout <"A copy constructor" <Endl ;}
  8. Int value;
  9. };
  10. Int main ()
  11. {
  12. A A [2] = {1, 2 };
  13. A B [2];
  14. A c [4] = {1, 2 };
  15. Return 0;
  16. }

Question 8: Can constructors, static functions, and inline functions be virtual functions?

Constructors and static functions are not allowed. inline functions are allowed.
Constructors cannot think of virtual functions: virtual functions are called through a virtual pointer, Which is set during the construction process.
Static functions cannot be virtual functions: you do not need instances to call static functions. Instead, you need to obtain the address of a function by obtaining a virtual pointer from an instance, to achieve dynamic binding.
Inline functions can: inline functions are called with code during compilation, while virtual functions are dynamically bound at runtime. Although yes, it generally does not. The cost is too high.

Question 9: must the Destructor be a virtual function?

The answer is that it is not necessary. If the class is designed to be inherited, The Destructor must be a virtual function. Otherwise, it cannot be a virtual function. Destructor are written as virtual functions to avoid Memory leakage during polymorphism. For example, in the following program, B's destructor will not be called. If a's destructor is written as a virtual function, B's destructor can be called.

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. Public:
  6. A () {cout <"a constructor \ n ";}
  7. ~ A () {cout <"A destructor \ n ";}
  8. };
  9. Class B: public
  10. {
  11. Public:
  12. B () {cout <"B constructor \ n ";}
  13. ~ B () {cout <"B destructor \ n ";}
  14. };
  15. Int main ()
  16. {
  17. A * pA = new B ();
  18. Delete Pa;
  19. Return 0;
  20. }

Question 10: What is the output of the program below?

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Struct point3d
  4. {
  5. Int X;
  6. Int y;
  7. Int Z;
  8. };
  9. Int main ()
  10. {
  11. Point3d * Ppoint = NULL;
  12. Int offset = (INT) (& (Ppoint-> Z ));
  13. Cout <OFFSET <Endl;
  14. Return 0;
  15. }

This program outputs 8, & (Ppoint-> Z) to get the address of the member variable Z, and does not need to access the memory, so you only need to add the address pointed to by Ppoint to the variable z offset in the class, the result is 8.

If it is & (Ppoint-> X) and & (Ppoint-> Y), the results are 0 and 4.

If Ppoint is not empty, for example, point3d * Ppoint = new point3d; the addresses of the three member variables are 4 different.

Question 6: in which situations can B be implicitly converted to a for non-C ++ built-in types A and B?

(1) Class A {...}; Class B: Public {......} ; // The Public B inherits from a, which can be indirectly inherited

(2) Class {......}; class B {operator A () {return a: ();}......}; // B converts the data type to
(3) Class A {A (const B &) ;}; // a constructor that implements the non-explicit parameter as B (other parameters with default values can be available)
Avoid ambiguity when providing implicit conversion. For example, in the preceding methods, if (2) is defined and (3) is defined, there is a ambiguity. The compiler can convert B to a in two ways. One is to use the constructor of A, and the other is to use the type conversion operator of B. Best Practice: The best way to avoid ambiguity is to avoid writing pairs of classes that provide mutual implicit conversion. C ++ primer.

Question 7: what is the problem with this program?

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. PRIVATE:
  6. Int value;
  7. Public:
  8. A (int n) {value = N ;}
  9. A (a other) {value = Other. value ;}
  10. Void print () {cout <value <Endl ;}
  11. };
  12. Int main ()
  13. {
  14. A A = 10;
  15. A B =;
  16. B. Print ();
  17. Return 0;
  18. }

Compilation error. A (a other) {} the duplicate constructor is incorrectly defined. The form parameter of this function can only be referenced by objects of this class type. It is often used to modify C ++ primer by const. It should be changed to a (const A & Other) {value = Other. value ;}. The copy constructor can be used:
(1) display or implicitly Initialize an object of the same type based on another object
String S = "hello ";
First, a string constructor that accepts a C-style string parameter is called to create a temporary object, and then a copy constructor is called to initialize s as a copy of the temporary object.
(2) copy an object and pass it as a real parameter to a function.
Void Foo (string S );
(3) copy an object from the return result of the Function
String Foo ();
(4) initialize elements in the container.
Vector <string> SVEC (5 );
First, call the string default constructor to create a temporary value, and then copy the temporary value to every element of the SVEC, that is, call the string copy constructor.
(5) initialize array elements based on the element initialization list
For example, the following function calls the copy constructor when initializing array. The default constructor is called when array B is initialized. When initializing array C, because it has four elements and the initialization list has only two, the initialization of the first two elements calls the replication constructor, the default constructor is called for initialization of the last two elements.

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. Public:
  6. A () {value = 1; cout <"a default constructor" <Endl ;}
  7. A (INT v) {value = V; cout <"A copy constructor" <Endl ;}
  8. Int value;
  9. };
  10. Int main ()
  11. {
  12. A A [2] = {1, 2 };
  13. A B [2];
  14. A c [4] = {1, 2 };
  15. Return 0;
  16. }

Question 8: Can constructors, static functions, and inline functions be virtual functions?

Constructors and static functions are not allowed. inline functions are allowed.
Constructors cannot think of virtual functions: virtual functions are called through a virtual pointer, Which is set during the construction process.
Static functions cannot be virtual functions: you do not need instances to call static functions. Instead, you need to obtain the address of a function by obtaining a virtual pointer from an instance, to achieve dynamic binding.
Inline functions can: inline functions are called with code during compilation, while virtual functions are dynamically bound at runtime. Although yes, it generally does not. The cost is too high.

Question 9: must the Destructor be a virtual function?

The answer is that it is not necessary. If the class is designed to be inherited, The Destructor must be a virtual function. Otherwise, it cannot be a virtual function. Destructor are written as virtual functions to avoid Memory leakage during polymorphism. For example, in the following program, B's destructor will not be called. If a's destructor is written as a virtual function, B's destructor can be called.

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. Public:
  6. A () {cout <"a constructor \ n ";}
  7. ~ A () {cout <"A destructor \ n ";}
  8. };
  9. Class B: public
  10. {
  11. Public:
  12. B () {cout <"B constructor \ n ";}
  13. ~ B () {cout <"B destructor \ n ";}
  14. };
  15. Int main ()
  16. {
  17. A * pA = new B ();
  18. Delete Pa;
  19. Return 0;
  20. }

Question 10: What is the output of the program below?

[CPP]
View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace STD;
  3. Struct point3d
  4. {
  5. Int X;
  6. Int y;
  7. Int Z;
  8. };
  9. Int main ()
  10. {
  11. Point3d * Ppoint = NULL;
  12. Int offset = (INT) (& (Ppoint-> Z ));
  13. Cout <OFFSET <Endl;
  14. Return 0;
  15. }

This program outputs 8, & (Ppoint-> Z) to get the address of the member variable Z, and does not need to access the memory, so you only need to add the address pointed to by Ppoint to the variable z offset in the class, the result is 8.

If it is & (Ppoint-> X) and & (Ppoint-> Y), the results are 0 and 4.

If Ppoint is not empty, for example, point3d * Ppoint = new point3d; the addresses of the three member variables are 4 different.

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.