C + + Interview questions Summary 1

Source: Internet
Author: User
Tags abstract definition

1.C What are the main differences from C + +?

Answer: 1. The C + + language includes the procedural language part and the class part, the procedural language part and C do not have the essential difference, the class part is not in the C language, it is the object-oriented programming body.

2. The programming method has moved from structured programming to object-oriented programming.

2. structural programming and the concept of object-oriented programming

The main idea of structured programming is to decompose function and to seek refinement gradually. Data is stored separately from the program, and the main technique of programming is to track which functions call which functions and which data changes.

The essence of object-oriented programming is to treat data and process data as a whole----object. (An object is a special variable----like a struct variable, and an associated action is added)

The implementation of object-oriented programming requires encapsulation and data hiding technology, inheritance and reuse technology, polymorphism technology .

3. What are classes and objects?

A class is a program description of a set of objects of the same nature, consisting of data and functions that summarize the common nature of a set of objects. is the basic unit of encapsulation.

An object is an independent unit of the world that has its own static characteristics (state) and dynamic characteristics (operations). A static feature is a feature that can be described with some kind of data, a dynamic feature that is the behavior or function that an object behaves.

Class-To-Object relationships: The class gives an abstract definition of all the objects that belong to the class, and the object is an entity that conforms to that definition. Therefore, an object is also known as an instance of the class (instance). An object is an instance of a class that defines the common attributes of all objects belonging to that class.

4. What are the three main features of object-oriented programming?

1. Encapsulation (encapsulation): encapsulation is an important principle of the object-oriented approach. It has two meanings: the first implication is to combine all the attributes of an object with all the services to form an indivisible independent unit (that is, an object). The second implication, also known as "information concealment", is to conceal the inner details of the object as far as possible, to form a boundary (or form a barrier) to the outside, leaving only a limited external interface to make it externally connected. This mainly refers to the object's external cannot directly access the object property, only through a few allow the external use of the service to contact with the object.

2. Inheritance (inheritance): (Single and multiple inheritance) subclasses can inherit properties and operations from a parent class, or define their own properties and actions

3. Polymorphism (polymorphism): After a property or operation defined in a generic class is inherited by a special class, it can have different data types or behave differently. This allows the same attribute or operation name to have different semantics in the generic class and in each of its special classes.

5. The composition of the class:

Class is a keyword that defines a class. < class name > is an identifier that uniquely identifies a class. A pair of curly braces is a description part of the class that describes all the members of the class. The members of the class include the data member and the member function two parts. The members of a class have the following three categories of access rights: public, private, and protected (protected), which defaults to private permissions.

1. Members of the public can be accessed by any code in the program;

2. Private members can only be accessed by member functions of the class itself and member functions of the Friend class, and the member functions of other classes, including the member functions of their derived classes, cannot access them;

3. A protected member is similar to a private member, except that members of the class itself and the member functions of the friend class can access the protected member, which is also accessible to a member of the class's derived class.

6. What is the difference between a struct and a class?

The only difference between structs and classes is that when no access is specified, members in the struct are defaulted to public and members in the class are defaulted to private.

7. defining methods for class objects and methods for representing object members

Object Definition method: < class name >< object Name Table >;

Object member Representation method:< Object name >.< member name > here "." is an operator that is capable of representing the members of an object. Or a member of a pointer to an object represents the following:< object pointer name >->< member name >

8. Constructors and destructors

(1) A constructor is a special member function for creating objects, and when an object is created, the system automatically calls the constructor and cannot be called directly in the program. The constructor name is the same as the class name, a class can have multiple constructors (overloads), a constructor can have any type of argument, but it cannot have a return type.

(2) The destructor name is the symbol "~" plus the class name, and the destructor has no parameters and return values. Only one destructor can be defined in a class, so destructors cannot be overloaded. The default destructor is an empty function

(3) Restrictions on the use of constructors: cannot be inherited, cannot be described as virtual functions, cannot be explicitly called, cannot take the address of a constructor.

9.c++ Memory layout of the program

The memory landscape of a C + + program is typically divided into four zones:

(1) Global data area: Holds global variables, static data, constants.

(2) Code area: holds class member functions, other function code.

(3) Stack area: Store local variables, function parameters, return data, return address.

(4) Heap area: free Storage area.

Ten. copy Constructors

When a constructor's argument is a reference to its own class, this constructor is called a copy constructor. The function of a copy constructor is to initialize a similar object that is being built with an existing object.

11.this Pointers

When a member function is called, it is automatically passed an implied argument, which is a pointer to the object where the function member resides. The This keyword can be used in member functions to refer to the pointer. The type of this pointer is the type of the class to which the member function belongs. When a member function is called, it is initialized to the address of the class instance where the called function resides.

The this pointer can only be used in a member function of a class, which points to the object to which the member function is called. This pointer is typically used to return the current object itself. This pointer is heavily used in operator overloading member function design. The static member function does not have the this pointer.

operator overloading

There are two overloaded forms of an operator: overloading a member function for a class and overloading a friend function for a class.

< function type > operator < operator > (< formal parameter list >) or friend < function type > operator < operator > (< formal parameter list >)

When an operator is overloaded with a member function of a class, the number of arguments to the function is one less than the number of operands (except for the suffix + + 、--), and when overloaded with the friend function of a class, the number of arguments is the same as the number of the original operands.

The monocular operator is best overloaded as a member function, while the binocular operator is best overloaded as a friend function.

In C + +, the monocular operator has + + and--(suffix has an int parameter, and the prefix does not)

What is the difference between function overrides and overloads in C + +?

Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overloaded method is to change the type of the return value.

How to Inherit

Base class Properties

Derived class attributes

Public inheritance

Public

Public

Protected

Protected

Private

Private

Private inheritance

Public

Private

Protected

Private

Private

Not accessible

Protect inheritance

Public

Protected

Protected

Protected

Private

Not accessible

. Basic knowledge of inheritance

① single-inheritance is defined in the following format:

class< derived class name >:< inheritance >< base class name >

{public:members;//A new defined member of a derived class

Private:members;

Protected:members;

};

(1) In the case of public inheritance, the object of the derived class can access the public member in the base class, and the member functions of the derived class can access the public and protected members of the base class. Here, it is important to distinguish between objects of derived classes and member functions in derived classes that have different access to the base class.

(2) In private inheritance, a member of a base class can only be accessed by a direct derived class and cannot be inherited further down.

(3) for the protection of inheritance, this inherits in the same way as the private inheritance. The difference between the two is that the members of the derived class have different accessibility to the base class members.

(4) A private member in a base class can only be accessed by a member function and a friend function in the base class, and cannot be accessed by another function.

② derived classes can have different access methods to base class Members:

Derived classes can override base class members

Derived classes cannot access a private member of a base class

The public and protected segment member access rights of the base class remain unchanged for derived classes (public inheritance)

The public and protected segment members of the base class become private members of the derived class (private inheritance)

③ constructors and destructors in single inheritance

A Constructors cannot be inherited. The invocation order of the derived class constructor is as follows:

(1) Call the constructor of the base class, in the order in which they are inherited.

(2) Call the constructor of the child object class, in the order in which they are described in the class.

(3) The contents of the derived class constructor body.

B Destructors cannot be inherited, so destructors for the base class are also called when the destructor for the derived class is executed. The order of execution is to execute the destructor of the derived class first, and then the destructor of the base class, in the same order as the constructor was executed.

④ assignment Compatibility rules

<!--[if!supportlists]--> (1) <!--[endif]--> derived class objects can be assigned to objects of the base class.

<!--[If!supportlists]--> (2) <!--[endif]--> derived class objects can initialize a reference to a base class

<!--[If!supportlists]--> (3) <!--the address of an object of a derived class can be assigned to a pointer to a base class.

the . Polymorphism and virtual functions

The polymorphism of C + + is embodied in two aspects of running and compiling, while the polymorphism of the program runtime is embodied by inheritance and virtual function, and polymorphism in the program compile is embodied in the overloading of functions and operators.

Overloads (same function name, different parameters, different parameter types, no effect on return value)

A virtual function is a member function that is prefixed with the keyword virtual in the base class. It provides an interface interface. Virtual functions can be redefined in one or more derived classes.

The overloaded function of a virtual function is still a virtual function.

When you redefine a virtual function in a derived class, you must have the same function prototype, including the return type, the function name, the number of arguments, and the order of the parameter types. A virtual function must be a member function of a class. A destructor can be a virtual function, but a constructor cannot be a virtual function.

Pure virtual function: In many cases, in the base class can not give a meaningful definition of virtual function, at this point, it may be described as pure virtual function, the definition of its left to the derived class to do. The general form of defining a pure virtual function is:

Class Name {Virtual return value type function name (parameter table) = 0;};

Abstract class: If there is at least one pure virtual function in a class, then this class is an abstract class. Abstract classes include not only pure virtual functions, but also virtual functions. A pure virtual function in an abstract class may be defined in an abstract class, or it may be inherited and redefined from its abstract base class.

An important feature of an abstract class is that an abstract class must be used as a base class for deriving other classes, and cannot be used to create object instances directly. Abstract classes cannot create objects directly because one or more of these functions are not defined, but you can still use pointers to abstract classes to support run-time polymorphism.

- . Implementation of exception Handling in C + +

The basic idea of C + + language exception handling mechanism is to separate the detection and processing of exceptions.

Implemented with 3 reserved words: Throw, try, and catch.

The called function detects the presence of the exception condition directly and throws an exception with throw (note that the C + + language exception is caused by the programmer's control, not by the computer hardware or the program running environment); use try in the upper call function to detect whether a function call throws an exception. The various exceptions detected are captured by catch and handled accordingly.

17. The difference between a function pointer and a pointer function

A function pointer is a pointer to a function, and the pointer function simply means that he is a function that returns a pointer to a value.

1, in the program operation, the function code is the program's algorithm instruction part, they and the group also occupies the storage space, has the corresponding address. You can use pointer variables to point to the first address of an array, or you can use a pointer variable to point to the first address of a function code, which is called a function pointer to the first address of the function code. Its function pointer is defined as follows: function type (* pointer variable name) (parameter list);

int    (*f) (int  x); Double (*PTR) (double x)

When defining a function pointer, Note the following :

1, the function pointer and the function it points to the number and type of arguments should be-to

2. The type of the function pointer and the return value type of the function must also be consistent.

Examples of use of function pointers:

int func (int x);     /*  */int (*f) (int x);    /*  */f=func;              /* */

2, a function can not only bring back the value of an integer data, the character type value and the value of the real type, you can also bring back the data of the pointer type, so that it points to an address cell. A function that returns a pointer is called a pointer function. The pointer function is defined as follows: type identifier * Function name (parameter table)

int *f (x, y);

Pointer functions use an example:

example copies the string 1 (str1) to String 2 (STR2) and outputs the string 2. #include"stdio.h"Main () {Char*ch (Char*,Char*); Charstr1[]="I am glad to meet you!"; Charstr2[]="welcom to study c!"; printf ("%s", CH (str1,str2));}Char*ch (Char*STR1,Char*str2) {inti; Char*p; P=str2if(*str2==null) Exit (-1);  Do{*str2=*str1; STR1++; STR2++; } while(*str1!=NULL); return(P);}

18. Difference between non-parametric and void parameters

For functions without any parameters (note: No arguments and void arguments are different concepts), C and C + + have different rules. For example, a parameter void F () {}
1. In the C language, functions without any parameters in a function prototype are considered to be able to go in with any parameters called simultaneous (yes, exactly the opposite), that is, F (), F (1), F ("ABCD"), all of which can be compiled.
2. In the C + + language, functions without any parameters in a function prototype are considered to be a function with void parameters. That is, void F () {} is actually considered void F (void) {} and is not allowed to pass arguments when actually called.

C + + Interview questions Summary 1

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.