Trend C ++ interview questions

Source: Internet
Author: User
1.
Class {
Public:
A () {func (0 );};
Virtual void func (INT data) {printf ("A1: % d \ n", data );}
Virtual void func (INT data) const {printf ("A2: % d \ n", data );}
Void func (char * Str) {printf ("A3 :( % s) \ n", STR );}
};

Class B: Public {
Public:
Void func () {printf ("B1: % s \ n ","");}
Void func (INT data) {printf ("B2: % d \ n", data );}
Void func (char * Str) {printf ("B3 :( % s) \ n", STR );}
};

Int main ()
{
A * pA;
B;
Const A * PCA;

Pa = & B;
Pa-> func (1 );
Pa-> func ("test ");
A (). func (1 );
PCA = & B;
PCA-> func (2 );
Return 0;
}

Running result:
A1: 0
B2: 1
A3 :( test)
A1: 0
A1: 1
A2: 2

1) base class Pointer Points to the object of the derived class: the pointer can only call the function defined by the base class, but if the function is a virtual function, it calls its own member function of the derived class. (B2: 1)
2) if a pointer from a derived class is directed to a base class object, you must perform obvious transformation operations in advance, but this approach is very dangerous.

2.
Template
Void func (const Int & T)
{
Cout <t + 100 <Endl;
}

Template
Void func (const T & T)
{
Cout <t;
}

Int main ()
{
Func (10.3 );
Func (1000 );

Return 0;
}

Program running result:
10.3
1000

If the above function is changed
Void func (const Int & T)
{
Cout <t + 100 <Endl;
}

Template
Void func (const T & T)
{
Cout <t <Endl;
}

Int main ()
{
Func (10.3 );
Func (1000 );

Return 0;
}

The running result of the program is:
10.3
1100

If you use a function in a non-template form, you cannot add the template keyword before it.

3. Correction:
Class Klass
{
Public:
Klass (){}
PRIVATE:
~ Klass (){}
Void func (int n ){
Cout <"Klass !! "<Endl;
}
Public:
Void test ()
{
Func (100 );
}
};

Int main ()
{
Klass K;
K. Test ();

Return 0;
}
After running, the program displays error c2248: 'klass ::~ Klass ': cannot access private member declared in class 'klass'
The property of the Destructor must be public.
However, if you change Klass K to Klass * PK; PK = new Klass; PK-> test (); the program passes, but Klass cannot be released.

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.