C + + Review questions

Source: Internet
Author: User

(1) An object of the standard input stream Cin:istream class. (2) An object of the standard output stream Cout:ostream class.

(3) Non-buffered standard error stream Cerr : ostream the object of the class. (4) buffer type standard error stream clog: object of Ostream class

(10) The following description of the virtual base class, the error is

A) The purpose of setting the virtual base class is to eliminate ambiguity

B) The constructor of the virtual base class is called after the non-virtual base class

C) If multiple virtual base classes are contained in the same layer, the constructors of these virtual base classes are called in the order they are described

D) If the virtual base class is derived from a non-virtual base class, the base class constructor is still called before the constructor of the derived class is called

extern is used to declare an external variable . Register declaration type variable

Polymorphism has two kinds of static polymorphism and dynamic polymorphism , and static polymorphism refers to the invocation of a function with the same name, because the parameter

Different calls to different functions with the same name, and dynamic polymorphism refers to different objects calling the same function, because different objects call different

function with the same name. Polymorphism must have the same function name ,

A member of a derived class is one from the base class, one from itself, so the derived class is an extension of the base class and is also the base

Classes are materialized and specialized, and derived classes are extended to the base class

substr takes a substring, the first 1 parameter indicates where to intercept the substring in the string, 2 a representation of how many to take characters.

to Output this->x=x the value of an expression is enclosed in parentheses.

[Edit] cout<< ( this->x=x ) <<endl;

#include <iostream.h>

Class Test

{int x, y;

Public

Test (int i,int j=0)

{x=i;y=j;}

int get (int i,int j)

{return i+j;}

};

void Main ()

{Test T1 (2), T2 (4,6);

Int (Test::* P) (int,int=10);

P=test::get;

cout<< (T1.*p) (5) <<endl;

Test *p1=&t2;

cout<< (p1->*p) (7,20) <<endl;

}

Answer: the

[Parse] the use of pointers to class member functions, *p Pointing Test A pointer to a function with two parameters in the class.

P = Test::get. this P just and Get has been contacted. (T1.*p) (5) is equivalent to a get function that invokes a parameter .

9. the access rights of the public members of the base class in the derived class are determined by ___ .

Answer: Access control mode or inheritance method

The class template is used to express The set of template class objects with ___.

Answer: Same treatment Method

The template feature is the abstraction of different data with the same processing method.

The source file name extension for C + + programs is ___.

Answer: CPP

[Parse] source program *.cpp, the target file is *.obj, executable program *.exe.

15. The header file contained in the # include command can be either a system-defined header file or a header file for ___.

Answer: Custom

[Parsing]#include loading files in two ways <> and "", one is the system, one is a custom file.

#include <iostream.h>

void Main ()

{int *p1;

int **p2=&p1;

int b=20;

p1=&b;

cout<<**p2<<endl;

}

Answer: 20

[Parse]p1 points to B, and P points to the address of P1. *P2 represents the address of P1, P1 's address is &b, that is *p2 is &b, so

**P2 is the value of the B variable.

#include <iostream>

#include <string.h>

using namespace Std;

Class MyString

{public:

char * pdata;

mystring (int len)

{pdata=new char[len+1];

}

~mystring ()

{Delete pdata;}

void Show () {Cout<<pdata<<endl;}

};

void Fun (mystring** array,int len)

{Mystring*old=*array;

_______;

memcpy (*array, old, Len);

}

void Main ()

{mystring str (20);

mystring*pstr=&str;

mystring**ppstr=&pstr;

strcpy (Str.pdata, "Hello,andylin");

Fun (PPSTR, 20);

_______

}

Answer: *array=new mystring (len), (**PPSTR). Show (); or str.show ();

[parsing] calls the constructor of the MyString class to open up space, after which the character is copied. The output can be directly using STR or

Use a level two pointer.

#include <iostream>

#include <string>

using namespace Std;

Template<class t>

void Swap (t& a,t& B)

{T temp;

Temp=a,a=b,b=temp;

}

void Main ()

{int a=5,b=9;

Char s1[]= "Hello", s2[]= "HI";

Swap (A, b);

Swap (S1,S2);

cout<< "a=" <<a<< ", b=" <<b<<endl;

cout<< "s1=" <<s1<< ", s2=" <<s2<<endl;

}

Answer: Char s1[]= "Hello", s2[]= "HI"; Swap (S1,S2) is the address used for the interchange. Character Pointers as Real

Parameter values change and the arguments change.

[Modify]char *s1= "Hello", *s2= "HI";

ios::binary , in binary form, theIos::app is positioned to the end of the file.

for int *pa[5], the description of the correct is ()

A. PA is a pointer to an array that points to an array of 5 int elements

B. PA is a pointer to the 5th element in an array, which is a variable of type int

C. Pa[5] Represents the value of the 5th element of a number group

D. PA is an array of pointers with 5 elements, each of which is an int type pointer

Answer: D

Parse: pointer array: Array elements are pointers of the same type, and pointers of the same type are the objects that the pointer points to

types are the same. For example, statement int *pa[5]; Defines an array of pointers. In the definition of an array of pointers, there are two operations

Operators:* and [], operator [] have precedence above *, so *pa[5] is equivalent to * (Pa[5]), Pa[5] represents a

arrays, while * means that the following objects are pointer variables, together *PA[5] represents an array of pointers. The array consists of 5

element, each element is a pointer to the int type . So select the D option.

3. Set Class A with member function void f (void); To define a pointer to a class member function, a variable PF to point to F, the

The declaration statement for the pointer variable is: ___.

Answer: Void (A::* pf) (void) =&a::f;

[Parse]void (A::* pf) (void) =&a::f; a pointer to a member function that is equivalent to two statements

: void (A::* pf) (void); and pf=&a::f;.

Overloaded operators keep their original ___, precedence, and binding intact.

Answer: operand

IStream and Ostream's direct base class is iOS.

Define the vector list Vector<int>a (10,1), use two parameters, 10 for length, and 1 for numeric values.

in C + +, there are two ways to pass parameters: Pass values and ___.

Answer: Pass Reference

[Parse] (1) The Call of value is divided into data value Call and address value call.

#include <iostream.h>

void Main ()

{int a,b,c;

Char ch;

cin>>a>>ch>>b>>c;//Enter 1.5xcx10x20,x from the keyboard to represent a space

cout<<a<<endl<<ch<<endl<<b<<endl<<c<<endl;

}

Answer: 1

.

5

0

[Parse] The problem of inputting input characters using CIN. 1-->a,.-->ch,5-->b, the space conversion to zero gives C.

The number of arguments contained in the function call Func ((EXP1,EXP2), (EXP3,EXP4,EXP5)) is ()

A. 1

B. 2

C. 4

D. 5

Answer: B

Parse: (EXP1,EXP2), (EXP3,EXP4,EXP5) is a two comma expression, the value is the last value, the equivalent of two

Parameters. So the number of arguments is 2.

A normal member function can call a static function, whereas a static function cannot call a normal member function, as with a normal

The function is the same as the constant member function.

in the compilation instruction, the macro defines which instruction () to use

A. #if

B. #include

C. #define

D. #error

Answer: C

Parse: #if条件编译, # include file contains, #error error handling.

: Except for the generic relational operator ".", the member pointer operator ". *", The scope operator "::", the sizeof operation

And the three-mesh operator "?:", all operators in C + + can be overloaded.

in the C + + language, the problem that data encapsulation solves is ()

A. Normalization of data

B. facilitates data conversion

C. Avoid data loss

D. preventing illegal access to data between different modules

Answer: D

Parsing: encapsulation refers to the combination of object properties and operations to form a separate unit, its internal information to the outside world is

Concealed, does not allow the outside world directly accesses the object the attribute, can only through the limited interface and the object occurrence contact. Classes are data encapsulation

The object is the implementation of the encapsulation. The access control mechanism of a class is reflected in the members of the class that can have public members, private members, and

Protect members. For the outside world, you only need to know the external behavior of the object, without having to know the internal implementation details. Packaging

The "Information concealment and Localization principle" is embodied in the object-oriented method.

C + + derived classes use two basic object-oriented techniques: the first is called a nature constraint, which restricts the nature of the base class

The second is called ___, which increases the nature of the derived class.

Answer: Nature Extension

Derived classes can obtain members from a base class through inheritance, or they can customize members.

6. the object-oriented four basic features are polymorphism, inheritance, and encapsulation ___.

Answer: Abstract

Answer: (*this). x=x; (*this) .y=y;,cout<<x<< "+" <<y<< "=" <<x+y<<endl;

The [parse] parameter has the same name as the data member and is accessed using this.

#include <iostream.h>

Class A

{public:

A (int i=10) {x=i;cout<< "A:" <<X<<ENDL;}

int x;

};

Class B:public A

{public:

b (int i): A (i) {x=i;cout<< "B:" <<x<< "," <<A::X<<ENDL;}

Private

A;

int x;

};

void Main ()

{b b (5);

}

Answer: A:10

A:5

b:5,10

[Parse] Define object B, call the base class constructor first, use a (i) in the B constructor, note case, not

is a (i), which means that there is no argument value when calling the constructor of the base class, so the default value is used, and the class member A is initialized.

A (i), i=5, so the input is a:5, and finally the constructor for Class B, x=5, from the base class x=10, Output b:5,10.

(1) Global variables: variables defined outside the function are called global variables and are scoped to: starting from the position where the variable is defined

To the end of the source program. Global variables increase the channel in which data is linked between functions, and functions within the scope of global variables can be used

, modify the value of the global variable, but use global variables to reduce the understandable nature of the program, and software engineering advocates to avoid making

Use global variables.

(2) local variable: A variable defined inside a function is called a local variable, and its scope is: start with the position where the variable is defined

To the end of the function. A local variable contains an automatic variable (auto) static variable and a function parameter.

The auto variable means that the allocation and release of the variable's storage space is automatic. Specifiers auto can be omitted. function in

Local variables are stored in the stack space. When the function begins to run, the local variable is allocated a memory unit, and when the function ends, the local change

The amount of the internal deposit element is released. Therefore, local variables in either function can have the same name because they occupy different memory units without affecting

Use. This facilitates the modularization of software development.

A static variable is a variable that is defined in the body of a function, stored in a static store, without a stack space, and its value does not exist with

The storage space is released and disappears.

operator, the prefix lets the variable change first. Call ++a, which is equivalent to a.operator++ (), note that no reference

The form. Suffix words a++, equivalent to A.operator (0), with formal parameters, formal parameter names can be saved.

8. solutions to the problem of defining ambiguity are ()

A. only use the scope resolution operator

B. using a scope-resolved operator or member name qualification

C. using a scope resolution operator or a virtual base class

D. using member names to qualify or assign compatible rules

Answer: B

Analysis: The two main methods to solve the two semantic problems are: (1) Assignment compatibility rule, (2) virtual base class.

CIN, cout, Cerr, and clog are used for input, cout for output, Cerr, and clog error handling.

int a=32;

Double c=32;

COUT.SETF (Ios::hex);

cout<< "hex:a=" <<a<< ", c=" <<c<<endl;

COUT.UNSETF (Ios::hex);

The output of the program is ___.

Answer: hex:a=20,c=32

Parsing can only output integer data in hexadecimal, and cannot convert other types of data to 16-binary data output. The

With a double type, the constant is still a (double type).

Class members, including member functions and data members, can be repaired using access rights public, private, and protected.

And normal variables cannot be described using access rights.

The following statements about templates and inheritance are correct ()

A. both templates and inheritance can derive a class system

B. from the members of the class system, the members of the template class system are more stable than the members of the inheriting class system.

C. in terms of dynamic performance, the Inheritance class system has more dynamic characteristics than the template class system.

D. different instances of the same class template are generally not linked, and the derived classes have sibling relationships between the various classes

Answer: D

Parsing: classes are abstractions of the same type of things, with different operations. and templates are different types of things, exactly the same

The abstract of the operation. After instantiating a class template, the individual objects have no relation. and class objects are derived, inherited, and so on.

The relationship.

The class template is used to express the set of template class objects with ___.

Answer: Same Treatment method

The template feature is the abstraction of different data with the same processing method.

If you derive a series of classes from the same base class, these classes are always called ___.

Answer: class family

[Parse] A number of classes derived from a single inheritance method.

The four basic features of object-oriented are polymorphism, inheritance, and encapsulation of ___.

Answer: Abstract

String str ("helloc++");

Cout<<str.substr (5,3);

The output of the program is ___.

Answer: C + +

C + + Review questions

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.