C + + Classic Test questions exercise (1)

Source: Internet
Author: User

Practice of classic C + + test questions

1, there is a function definition:
void Test (int a) {}
void Test (float a) {}
Then the error with the downgrade is: D
A. Test (1);
B. Test (' C ');
C. Test ("d ')
D. Test (0.5) "Default const double cannot match int and float"

2. struct S
{
int x:3; "Data structure: bit field!" X, Y, Z is a 3 bit field of type int, so it is calculated as int (4 bytes) "
int y:4;
int z:5;
Double A;
}
Ask sizeof (s): Answer 16 memory aligned! Memory Alignment! Memory Alignment! Need to fill a 4 byte, make up 8-8 byte alignment

3, the following program output
<p class="p0">
#include "stdio.h"
class A
{
public:
A()【构造函数】
{
printf("1");
}
A(A &a)【拷贝构造函数】
{
printf("2");
}
A &operator=(const A &a)【重载的赋值函数】
{
printf("3");
return *this;
}
};
int main()
{
A a;
A b = a;
}
</p>

Answer: 12
A, define an object, no doubt call the constructor
A b=a, which defines object B and initializes with a to B, calls the copy constructor. If it is written as a a;a b;b=a, it is called an assignment function that is overloaded later, which should output 113. This topic mainly examines the difference between assignment and initialization.

4, MFC Class Library, why CObject's destructor is virtual? Why can't a constructor be virtual?
Answer: 1. If the destructor of the CObject is set to virtual, all its derived classes ' destructors will automatically become virtual, which guarantees that in all cases there will be no memory leaks caused by the destructor of the derived class being called (such as pointers to derived class objects defined by the base class). 2. A virtual call is a mechanism that can work with only a subset of the information, specifically allowing us to invoke a function that knows only the interface and does not know its exact object type. However, when creating an object, you must know the exact type of the object, so the constructor cannot be virtual.
(in addition, the use of virtual functions is a cost, each virtual function object must maintain a V table, the use of virtual functions will incur overhead, so the small class does not want to derive other classes, there is no need to use virtual functions).

5. About copy Constructors

6. Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its sub-objects, and the parent object can operate differently depending on the attributes of the child object that is currently assigned to it. It is mainly realized by virtual function. Overriding is a subclass of the practice of redefining a virtual function of a parent class. Overloading refers to allowing multiple functions with the same name, and the parameter tables for those functions are different. Overloading is independent of polymorphism, and overrides are related to polymorphism. Multi-State purpose--interface reuse. For example: Define a plane's base class, the helicopter and the sub-class of the jet, to let a global function plane_fly take off all kinds of aircraft, only need plane_fly (const ppplane:plane) on the line, no matter what aircraft, will take off according to the characteristics of the aircraft, That is parent=child.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Classic Test questions exercise (1)

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.