[C ++ Interview Questions] object-oriented

Source: Internet
Author: User

 

When it comes to object-oriented, the first response should be its three major features: encapsulation, inheritance, and polymorphism. Let's take a brief look at these three features:

 

(1) Encapsulation: encapsulate objective things into abstract classes, and classes can only operate trusted classes or objects on their own data and methods, hide untrusted information.

 

In C ++, the attributes of the members are public, protected, and private. The access permissions of these three attributes are reduced in turn.

 

(2) Inheritance: Inheritance refers to the ability to use all functions of the existing class and extend these functions without re-writing the original class.

 

(3) polymorphism: polymorphisn is a technique that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. There are two methods to achieve polymorphism: overwrite and reload.

 

Overwrite refers to the method by which the subclass redefines the virtual function of the parent class.

 

Overload means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both are different ).

 

Next we will start learning today.

 

1. Which class member functions are generated by C ++ hollow classes by default?

Answer:

 

For an empty class, the compiler generates four member functions by default:

 

(1) default constructor

 

(2) destructor

 

(3) copy constructor

 

(4) value assignment function

 

 

2. Can there be constructor, destructor, and member functions in the structure? If yes, what is the difference between the structure and the class?

Answer:

 

The difference is that the variables in the class are private by default, and the variables in the struct are public by default. By default, class inheritance is private inheritance, while struct inheritance is public inheritance by default. Struct can have constructor, destructor, and inheritance between them or even multiple inheritance. In C ++, struct actually has the same meaning as class. The only difference is that the default access control in struct is public and the default access control in class is private. The unique significance of the existence of the struct keyword in C ++ is to give the C programmer a sense of belonging and to make the C ++ compiler compatible with projects previously developed using C.

 

 

3. What are the output results of the following program?

01 # include <iostream>

 

02 using namespace std;

 

03

 

04 class base

 

05 {

 

06 private:

 

07 int m_ I;

 

08 int m_j;

 

09 public:

 

10 base (int I): m_j (I), m_ I (m_j ){}

 

11 base (): m_j (0), m_ I (m_j ){}

 

12 int get_ I () {return m_ I ;}

 

13 int get_j () {return m_j ;}

 

14 };

 

15

 

16 int main ()

 

17 {

 

18 base obj (98 );

 

19 cout <obj. get_ I () <endl <obj. get_j () <endl;

 

20 return 0;

 

21}

 

Resolution: The expected result is "98, 98 ". However, the declaration of the member variables is m_ I, followed by m_j; the initialization variable sequence of the initialization list is executed according to the declaration sequence of the member variables. Therefore, m_ I is initialized first, however, m_j has not been initialized yet, And m_ I will be assigned a random value. Change the Declaration sequence of member variables to get the expected results.

 

Answer:

 

The first in the output result is a random number, and the second is 98.

 

 

4. Is the following class declaration correct? Why?

1 class

 

2 {

 

3 const int Size = 0;

 

4 };

 

Analysis: this program has the member variable problem. A constant must be initialized in the initialization list of the constructor or set to static.

 

Answer:

 

The correct procedure is as follows:

 

 

1 class

 

2 {

 

3 ()

 

4 {

 

5 const int Size = 1;

 

6}

 

7 };

 

 

Or:

 

1 class

 

2 {

 

3 static const int Size = 1;

 

4 };

 

 

5. The Destructor can be virtual, but the constructor cannot. Why?

Answer:

 

Virtual functions use a virtual call method. Virtual call is a mechanism that can work with only partial information. In particular, it allows us to call a function that only knows the interface but does not know its exact object type. However, to create an object, you must know the exact type of the object. Therefore, the constructor cannot be virtual.

 

 

6. If the virtual function is very effective, can we declare every function as a virtual function?

Answer:

 

No, this is because virtual functions have a price: because each virtual function object must maintain a v table, a system overhead is generated when using virtual functions. If it is only a small class and cannot derive other classes, there is no need to use virtual functions.

 

 

7. See the following program:

01 # include <iostream>

 

02 using namespace std;

 

03

 

04 class B

 

05 {

 

06 private:

 

07 int data;

 

08 public:

 

09 B ()

 

10 {

 

11 cout <"defualt constructor" <endl;

 

12}

 

13

 

14 ~ B ()

 

15 {

 

16 cout <"destructed" <endl;

 

17}

 

18

 

19 B (int I): data (I)

 

20 {

 

21 cout <"constructed by parameter" <data <endl;

 

22}

 

23 };

 

24

 

25 B Play (B B)

 

26 {

 

27 return B;

 

28}

 

29

 

30 int main ()

 

31 {

 

32 B temp = Play (5 );

 

33 return 0;

 

34}

 

Problem:

 

(1) What is the output result of the program? Why is there such output?

 

(2) B (int I): data (I). What is the terminology for this usage?

 

(3) Play (5): The parameter type is a class, and 5 is a constant. Is this write legal? Why?

 

Answer:

 

(1) the output result is as follows:

 

1 constructed by parameter // in Play (5), 5 calls B: B (int I) through implicit type conversion)

 

2 destructed // Play (5) the destructor of the parameter is called

 

3. destructed // The temp destructor is called. The temp constructor calls the copy constructor of the compiler.

 

(2) The constructor to be parameter, followed by the member variable initialization list (member initialization list)

 

(3) Valid. If you do not add the explicit keyword to a constructor of a single parameter, an implicit type conversion is defined. Adding the explicit keyword will eliminate this implicit conversion.

 

8. Write String-like constructor, destructor, and value assignment function.

The prototype of the known class String is:

 

01 class String

 

02 {

 

03 public:

 

04 String (const char * str = NULL); // common Constructor

 

05 String (const String & other); // copy the constructor

 

06 ~ String (void); // destructor

 

07 String & operate = (const String & other); // value assignment function

 

08 private:

 

09 char * m_data; // used to save strings

 

10 };

 

Answer:

 

1. String destructor:

 

1 String ::~ String (void)

 

2 {

 

3 delete [] m_data;

 

4}

 

2. String constructor:

 

01 String: String (const char * str)

 

02 {

 

03 if (NULL = str)

 

04 {

 

05 m_data = new char [1];

 

06 * m_data = '\ 0 ';

 

07}

 

08 else

 

09 {

 

10 int length = strlen (str );

 

11 m_data = new char [length + 1];

 

12 strcpy (m_data, str );

 

13}

 

14}

 

3. String copy constructor:

 

1 String: String (const String & other)

 

2 {

 

3 int length = strlen (other. m_data );

 

4 m_data = new char [length + 1];

 

5 strcpy (m_data, other. m_data );

 

6}

 

4. String Value assignment function:

 

01 String & String: operate = (const String & other)

 

02 {

 

03 if (this = & other) // check self-replication

 

04 {

 

05 return * this;

 

06}

 

07 delete [] m_data; // release the original memory resource

 

08 int length = strlen (other. m_data); // allocate new memory resources and copy the content

 

09 m_data = new char [length + 1];

 

10 strcpy (m_data, other. m_data );

 

11 return * this; // return the reference of this object

 

12}

 

 

 

 

9. What is the difference between heavy load and coverage?

Answer:

 

Virtual functions are always rewritten in the derived class, which is called override ).

 

Override refers to the virtual function that the derived class overrides the base class. The rewritten function must have the same parameter table and return value. The word Override seems to have no appropriate Chinese vocabulary. Some people translate "overwrite" into "overwrite", which is also appropriate.

 

Overload conventions are translated as "overload", which refers to writing a function with the same name as an existing function but different from the parameter table. For example, a function can either take an integer as a parameter or a floating point as a parameter. Overloading is not an object-oriented programming, but a syntax rule. Overloading is not directly related to polymorphism.

 

Author: IT stupid

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.