ArticleDirectory
- 1. Which class member functions are generated by C ++ hollow classes by default?
- 2. Can there be constructor, destructor, and member functions in the structure? If yes, what is the difference between the structure and the class?
- 3. What are the output results of the following program?
- 4. Is the following class declaration correct? Why?
- 5. The Destructor can be virtual, but the constructor cannot. Why?
- 6. If the virtual function is very effective, can we declare every function as a virtual function?
- 7. See the following program:
- 8. Write string-like constructor, destructor, and value assignment function.
- 9. What is the difference between heavy load and coverage?
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 meaning of the struct keyword in C ++ is to make CProgramMembers have a sense of belonging, in order to make the C ++ compiler compatible with previous projects developed in C.
3. What are the output results of the following program?
# Include <iostream> using namespace STD; class base {PRIVATE: int m_ I; int m_j; public: Base (int I): m_j (I), m_ I (m_j) {} base (): m_j (0), m_ I (m_j) {} int get_ I () {return m_ I;} int get_j () {return m_j ;}}; int main () {base OBJ (98); cout <obj. get_ I () <Endl <obj. get_j () <Endl; return 0 ;}
Resolution:The answer to this question 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?
Class A {const int size = 0 ;};
Resolution:This program has a 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:
Class A {A () {const int size = 1 ;}};
Or:
Class A {static const int size = 1 ;};
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:
# Include <iostream> using namespace STD; Class B {PRIVATE: int data; public: B () {cout <"defualt constructor" <Endl ;}~ B () {cout <"destructed" <Endl;} B (int I): Data (I) {cout <"Constructed by parameter" <data <Endl ;}; B play (B) {return B ;}int main () {B temp = play (5); Return 0 ;}
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:
Constructed by parameter // when B: B (int I) destructed/play (5) is called by implicit type conversion in play (5, the parameter destructor is called by destructed // temp. The temp constructor calls the copy constructor that survive 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:
Class string {public: string (const char * STR = NULL); // common constructor string (const string & other); // copy constructor ~ String (void); // destructor string & operate = (const string & other); // value assignment function PRIVATE: char * m_data; // used to save the string };
Answer:
1. String destructor:
String ::~ String (void) {Delete [] m_data ;}
2. String constructor:
String: string (const char * Str) {If (null = Str) {m_data = new char [1]; * m_data = '\ 0 ';} else {int length = strlen (STR); m_data = new char [Length + 1]; strcpy (m_data, STR );}}
3. string copy constructor:
String: string (const string & Other) {int length = strlen (Other. m_data); m_data = new char [Length + 1]; strcpy (m_data, other. m_data );}
4. String Value assignment function:
String & string: operate = (const string & Other) {If (this = & Other) // check the self-replication {return * This;} Delete [] m_data; // release the original memory resource int length = strlen (Other. m_data); // allocate new memory resources and copy the content m_data = new char [Length + 1]; strcpy (m_data, other. m_data); return * This; // return the reference of this object}
9. What is the difference between heavy load and coverage?
Answer:
A virtual function is always rewritten in a 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.