C + + Fundamentals interview Selection 100 series (21-30) [C + + Basics]

Source: Internet
Author: User

"This article links"

Http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html

"topic 21"

Run the following code to output the results?

Code

C + + Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
version:1.0
Author:hellogiser
Blog:http://www.cnblogs.com/hellogiser
Date:2014/9/22
*/

#include "StdAfx.h"
#include<iostream>
using namespacestd;

classA
{
Public:
Virtual voidFun (intNumber =Ten)
{
Std::cout <<"A::fun with Number"<< number << Endl;
}
};

classB: PublicA
{
Public:
Virtual voidFun (intNumber = -)
{
Std::cout <<"B::fun with Number"<< number << Endl;
}
};

intMain ()
{
b b;
A &a = b;
A.fun ();
return 0; //virtual function Dynamic binding: B, the default argument is determined at compile time ... is 10
}

/*
B::fun with Number 10
*/

Analysis

Virtual functions are dynamically bound, but the default arguments are determined at compile time, so the result is b::fun with Number 10

"topic 22"

Indicate what is wrong with the following program and correct it.

Code

C + + Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespaceStd

classA
{
Public:
A ();
~a ();

inti =0; //ERROR
    Static intj =0; //ERROR
    Const intk =0; //ERROR
    Const Static Char*p ="Hello World"; //ERROR
    Static voidFun ();
};

A::a ()
{

}

A::~a ()
{

}

Static voidFun ()ERROR
{

}

"Correct code"

C + + Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
version:1.0
Author:hellogiser
Blog:http://www.cnblogs.com/hellogiser
Date:2014/9/22
*/

#include "StdAfx.h"
#include<iostream>
using namespacestd;

classA
{
Public:
A ();
~a ();

inti;//Error
    Static intJ;//Error
    Const intK; //Error
    Const Static Char*p;//Error
    Static voidFun ();
};

inta::j =0;
Const Char*a::p ="Hello World";

A::a (): I (0), K (0)
{

}

A::~a ()
{

}

voidA::fun ()
{

}

intMain ()
{
return 0;
}

"topic 23"

C + + Fundamentals interview Selection 100 series (21-30) [C + + Basics]

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.