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 are the differences between a C++ struct and C++ class?

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

struct is just like 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 are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specified and private base-class

inheritance.

Q: How do you 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 will allow you to delete a dynamic object through a caller to a base class object. If the destructor is non-virtual, then wrong destructor will 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's origin.

Q: What is "this" pointer?

A: The this pointer is 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. Static member functions do not have a this pointer. When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call

myDate.setMonth( 3 );

can be interpreted this way:

setMonth( &myDate, 3 );

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

Q: What happens when you make call "delete this;"?

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

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 are all the implicit member functions of the class? Or what are 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 are the types of container classes?

A: A container class is a class that is 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 is 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 is holding a group of objects that are all the same, the container is called a homogeneous container.

Q: What is Overriding?

A: To override a method, a subclass of the class that 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 have same method name.

· Must have same data type.

· Must have same argument list.

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

Q: How do 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 are 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 then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are 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: What
is a local class? Why can it be useful?

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

member function or a free
function. For example:

// Example 2: Local class

//

int f()

{

class LocalClass

{

// ...

};

// ...

};

Like nested classes, local
classes can be a useful tool for managing code dependencies.

Q: What
a derived class can add?

A: New data members

New member functions

New constructors and destructor

New friends

Q: What
happens when a derived-class object is created and destroyed?

A: Space is allocated (on the stack
or the heap) for the full object (that is, enough space to store the data
members inherited from the base class plus the data members defined in the derived

class itself) The base class's
constructor is called to initialize the data members inherited from the base
class The derived class's constructor is then called to initialize the data
members added in the derived

class The derived-class object
is then usable When the object is destroyed (goes out of scope or is deleted)
the derived class's destructor is called on the object first Then the base
class's destructor is called on the object Finally the allocated space for the
full object is reclaimed

Q: How
do I create a subscript operator for a Matrix class?

A: Use operator() rather than
operator[].

When you have multiple
subscripts, the cleanest way to do it is with operator() rather than with operator[].
The reason is that operator[] always takes exactly one parameter, but
operator() can take any number of parameters (in the case of a rectangular
matrix, two parameters are needed).

For example:

class Matrix {

public:

Matrix(unsigned rows, unsigned
cols);

double& operator() (unsigned
row, 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= (const
Matrix& 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("Matrix
constructor 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("Matrix
subscript out of bounds");

return data_[cols_*row + col];

}

inline

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

{

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

throw BadIndex("const
Matrix subscript out of bounds");

return data_[cols_*row + col];

}

Then you can access an element
of 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/C++) Interview in English - Class的更多相关文章

  1. (C++) Interview in English. - Constructors/Destructors

    Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...

  2. (C/C++) Interview in English - Threading

    Q. What's the process and threads and what's the difference between them? A.  A process is an execut ...

  3. (C/C++) Interview in English - Basic concepts.

    Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...

  4. (C/C++ )Interview in English - Virtual

    Q: What is virtual function?A: A virtual function or virtual method is a function or method whose be ...

  5. (C/C++) Interview in English - Points.

    Q: What is a dangling pointer? A: A dangling pointer arises when you use the address of an object af ...

  6. (C/C++) Interview in English. - Memory Allocation/Deallocation.

    Q: What is the difference between new/delete and malloc/free? A: Malloc/free do not know about const ...

  7. step 1:begin to identify something in english(to becaome a baby again)

    long long ago , i think if i want to improve my english especially computer english . i must do so m ...

  8. English interview!

    Q1:Why are you interested in working for our company?为什么有兴趣在我们公司工作?A1:Because your company has a goo ...

  9. Enginering English for interview (1)

    I was lucky to work in a foreign company, Here is an overview of the interview test : 1.Because of t ...

随机推荐

  1. activiti工作流数据库表详细介绍 (23张表)

    Activiti的后台是有数据库的支持,所有的表的表名都以ACT_开头,表名的第二部分是用来表示表的用途的两个字母标识. 用途也和服务的API对应. ACT_RE_*: 'RE'表示repositor ...

  2. HDU-4455 Substrings(DP)

    题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...

  3. 编码规范(二)之Code Templates的设置(转)

    http://swiftlet.net/archives/1199 编码规范(二)之Code Templates的设置(转) 文件(Files)注释标签:/** * @Title: ${file_na ...

  4. linux下常用文件传输命令 (转)

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  5. OSPF

    Ospf OSPF(开放最短路径优先协议)是一种无类内部网关协议(IGP):是一种链路状态路由选择协议: 入门: 可以把整个网络(一个自治系统AS)看成一个王国,这个王国可以分成几个 区(area), ...

  6. GOOGLE的专业使用方法(转)

    搜索引擎命令大全! 1.双引号 把搜索词放在双引号中,代表完全匹配搜索,也就是说搜索结果返回的页面包含双引号中出现的所有的词,连顺序也必须完全匹配.bd和Google 都支持这个指令.例如搜索: “s ...

  7. EXT3_DX_ADD_ENTRY: DIRECTORY INDEX FULL!

    inode问题故障1例故障关键字:ext3_dx_add_entry: Directory index full! 线上业务的一台服务器无缘无故突然挂了让机房帮忙连接显示器后发现报错 http://i ...

  8. 有关<table>的几个问题

    1)实现任意一行下边框的颜色设置: 单元格边距(表格填充)(cellpadding) -- 代表单元格外面的一个距离,用于隔开单元格与单元格空间 单元格间距(表格间距)(cellspacing) -- ...

  9. 自然语言处理1——语言处理与Python(内含纠错)

    学习Python自然语言处理,记录一下学习笔记. 运用Python进行自然语言处理需要用到nltk库,关于nltk库的安装,我使用的pip方式. pip nltk 或者下载whl文件进行安装.(推荐p ...

  10. Xcode 7安装KSImageNamed失败解决方法

    ## How do I use it? Build the KSImageNamed target in the Xcode project and the plug-in will automati ...