(c + +) Interview in English-class

Source: Internet
Author: User
Tags shallow copy

Q: What is a class?

A: A class is an expanded concept of a data structure:instead of holding only data, it can hold both data and functions.

Q: What is the differences between a C + + struct and C + + class?

A: The default member and base class access specifies is different. This is one of the commonly misunderstood aspects of C + +. Believe it or not, many programmers think that a C + +

Struct is just as a C struct, while a C + + class has inheritance, Access specifes, member functions, overloaded operators , and so on. Actually, the C + + struct has all the features of the

Class. The only differences is a struct defaults to public member access and public base class inheritance, and a class Def Aults to the private access specified and private Base-class

Inheritance.

Q: How does know that your class needs a virtual destructor?

A: If your class has at least one virtual function, you should make a destructor for this class virtual. This would allow you to delete a dynamic object through a caller to a base class object. If the destructor is non-virtual and then wrong destructor would be invoked during deletion of the dynamic object.

Q: What is encapsulation?

A: Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data ' origin.

Q: What is the "This" pointer?

A: The this pointer are a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. The Static member functions do not has a this pointer. When a nonstatic member function was called for a object, the address of the object was passed as a hidden argument to the function. For example, the following function call

Mydate.setmonth (3);

Can is interpreted this by:

Setmonth (&mydate, 3);

The object's address is available from within, the member function as the this pointer. It is a legal, though unnecessary, to use the this pointer when referring to members of the class.

Q: What happens when do call "delete this;"?

A: The code has a built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program would probably crash as Soon as the DELETE statement executes. There is no portable-a-object to-tell-it was instantiated on the heap, so the class cannot asserts that it O Bject is properly instantiated. Second, when a object commits suicide this is, the using program might isn't know about its demise. As far as the instantiating program was concerned, the object remains in scope and continues to exist even though the OBJEC T did itself in. Subsequent dereferencing of the pointer can and usually does leads to disaster. You should never does this. Since compiler does not know whether the object is allocated on the stack or in the heap, "delete this" could cause a dis Aster.

Q: What is assignment operator?

A: Default assignment operator handles assigning one object to another of the same class. Member to Member copy (shallow copy)

Q: What is all the implicit member functions of the class? Or what is all the functions which compiler implements for us if we don ' t define one?

A:

(a) Default ctor

(b) Copy ctor

(c) Assignment operator

(d) Default destructor

(e) Address operator

Q: What is a container class? What is the types of container classes?

A: A container class is a class was used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a wellknown

Interface. A container class is a supporting class whose purpose are to hide the topology used for maintaining the list of objects in Memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; When the container was holding a group of objects that was all the same, the container was called a homogeneous container.

Q: What is overriding?

A: To override a method, a subclass of the class originally declared the method must declare a method with the same name , return type (or a subclass of that return type), and same parameter

List. The definition of the method overriding is:

· Must has same method name.

· Must has same data type.

· Must has same argument list.

Overriding a method means that replacing a method functionality in the child class. To imply overriding functionality we need the parent and child classes. The child class is define the same method signature as one defined in the parent class.

Q: How does you access the static member of a class?

A: ::

Q: What is a nested class? Why can it be useful?

A: A nested class is a class enclosed within the scope of another class. For example:

Example 1:nested Class

//

Class Outerclass

{

Class NestedClass

{

// ...

};

// ...

};

Nested classes is useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; So, in Example 1, if NestedClass is

Public and any code can name it as Outerclass::nestedclass. Often nested classes contain private implementation details, and is therefore made private; In Example 1, if NestedClass

is private, then only Outerclass's members and friends can use NestedClass. When you instantiate as outer class, it won ' t instantiate inside class.

Q: Whatis a local class? Why can it be useful?

A: Local class is a class Definedwithin the scope of a function _ any function, whether a

member function or a freefunction. For example:

Example 2:local Class

//

int F ()

{

Class Localclass

{

// ...

};

// ...

};

Like nested classes, localclasses can is a useful tool for managing code dependencies.

Q: Whata derived class can add?

A: New data members

NEW member functions

New Constructors and destructor

New Friends

Q: Whathappens When a Derived-class object is created and destroyed?

A: Space is allocated (on the Stackor of the heap) for the full object (so is, enough space to store the DataMembers inherited The base class plus the data members defined in the derived

Class itself) the base class ' Sconstructor is called to initialize the data members inherited from the BaseClass the derive D class ' s constructor is then called to initialize the datamembers added in the derived

Class The Derived-class Objectis then usable when the object was destroyed (goes out of scope or is deleted) the derived CLA SS ' s destructor is called on the object first and then the BaseClass's destructor is called on the object Finally the Allocate D space for Thefull object is reclaimed

Q: HOWdo I Create a subscript operator for a Matrix class?

A: Use operator () rather thanoperator[].

When you have multiplesubscripts, the cleanest-on-the-operator () rather than with operator[]. The reason is this operator[] always takes exactly one parameter, Butoperator () can take any number of parameters (in the Case of a Rectangularmatrix, and parameters is needed).

For example:

Class Matrix {

Public

Matrix (unsigned rows, unsignedcols);

double& operator () (Unsignedrow, unsigned col); Subscript operators often come in pairs

Double operator () (unsigned row,unsigned col) const; Subscript operators often come in pairs

...

~matrix (); destructor

Matrix (const matrix& m); Copy Constructor

matrix& operator= (constmatrix& m); Assignment operator

...

Private

unsigned rows_, Cols_;

Double* Data_;

};

Inline

Matrix::matrix (unsigned rows,unsigned cols)

: Rows_ (rows)

, Cols_ (cols)

Data_ <--initialized below (after the ' if/throw ' statement)

{

if (rows = = 0 | | cols = = 0)

Throw Badindex ("Matrixconstructor has 0 size");

Data_ = new double[rows * cols];

}

Inline

Matrix::~matrix ()

{

Delete[] Data_;

}

Inline

double& matrix::operator () (unsigned row, unsigned col)

{

if (row >= Rows_ | | col >=cols_)

Throw Badindex ("Matrixsubscript Out of Bounds");

return Data_[cols_*row + col];

}

Inline

Double Matrix::operator () (unsigned row, unsigned col) const

{

if (row >= Rows_ | | col >=cols_)

Throw Badindex ("Constmatrix subscript out of Bounds");

return Data_[cols_*row + col];

}

Then you can access an elementof Matrix m using m (i,j) rather than m[i][j]:

int main ()

{

Matrix m (10,10);

M (5,8) = 106.15;

Std::cout << m (5,8);

...

}

(c + +) Interview in English-class

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.