`
standalone
  • 浏览: 615221 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

c++问答

    博客分类:
  • c++
阅读更多

1.       Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?
There is nothing like Virtual Constructor. The Constructor cant be virtual as the constructor is a code which is responsible for creating a instance of a class and it cant be delegated to any other object by virtual keyword means.


2.       What about Virtual Destructor?
Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object baller is balling to , proper destructor will be called.


3.       What is Pure Virtual Function? Why and when it is used ?
The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error.
This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.


4.       What is problem with Runtime type identification?
The run time type identification comes at a cost of performance penalty. Compiler maintains the class.


5.       How Virtual functions call up is maintained?
Through Look up tables added by the compile to every class image. This also leads to performance penalty.


6.       Can inline functions have a recursion?
No.
Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.


7.       How do you link a C++ program to C functions?
By using the extern "C" linkage specification around the C function declarations.
Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions.


8.       Explain the scope resolution operator?
It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.


9.       How many ways are there to initialize an int with a constant?
1. int foo = 123;
2. int bar(123);


10.    What is your reaction to this line of code? delete this;
It is not a good programming Practice.
A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious.
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 baller can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter.


11.    What is the difference between a copy constructor and an overloaded assignment operator?
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.


12.    When should you use multiple inheritance?
There are three acceptable answers:- "Never," "Rarely," and "When the problem domain cannot be accurately modeled any other way."

Consider an Asset class, Building class, Vehicle class, and CompanyCar class. All company cars are vehicles. Some company cars are assets because the organizations own them. Others might be leased. Not all assets are vehicles. Money accounts are assets. Real estate holdings are assets. Some real estate holdings are buildings. Not all buildings are assets. Ad infinitum. When you diagram these relationships, it becomes apparent that multiple inheritance is a likely and intuitive way to model this common problem domain. The applicant should understand, however, that multiple inheritance, like a chainsaw, is a useful tool that has its perils, needs respect, and is best avoided except when nothing else will do.


13.    What is a virtual destructor?
The simple answer is that a virtual destructor is one that is declared with the virtual attribute.
The behavior of a virtual destructor is what is important. If you destroy an object through a baller or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be comple


14.    Can a constructor throw a exception? How to handle the error when the constructor fails?
The constructor never throws a error.


15.    What are the debugging methods you use when came across a problem?
Debugging with tools like :

GDB, DBG, Forte, Visual Studio.

Analyzing the Core dump.

Using tusc to trace the last system call before crash.

Putting Debug statements in the program source code.


16.    How the compilers arranges the various sections in the executable image?
The executable had following sections:-

Data Section (uninitialized data variable section, initialized data variable section )

Code Section

Remember that all static variables are allocated in the initialized variable section.


17.    Explain the ISA and HASA class relationships. How would you implement each in a class design?
A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class.
This relationship is best implemented by embedding an object of the Salary class in the Employee class.


18.    When is a template a better solution than a base class?
When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generality) to the designer of the container or manager class.


19.    What are the differences between a C++ struct and C++ class ?
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 specifies, 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.


20.    How do you know that your class needs a virtual destructor?
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 baller to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object.


21.    What is the difference between new/delete and malloc/free?
Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory.


22.    What happens when a function throws an exception that was not specified by an exception specification for this function?
Unexpected() is called, which, by default, will eventually trigger abort().


23.    Can you think of a situation where your program would crash without reaching the breakball, which you set at the beginning of main()?
C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.


24.    What issue do auto_ptr objects address?
If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown.


25.    Is there any problem with the following:
char *a=NULL; char& p = *a;?
The result is undefined. You should never do this. A reference must always refer to some object.


26.    Why do C++ compilers need name mangling?
Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name.


27.    Is there anything you can do in C++ that you cannot do in C?


No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C

 

  • What are the major differences between C and C++?
    • What are the differences between new and malloc ?
    • What is the difference between delete and delete[] ?
    • What are the differences between a struct in C and in C++?
    • What are the advantages/disadvantages of using #define ?
    • What are the advantages/disadvantages of using inline and const ?
  • What is the difference between a baller and a reference?
    • When would you use a baller? A reference?
    • What does it mean to take the address of a reference?
  • What does it mean to declare a function or variable as static ?
  • What is the order of initalization for data?
  • What is name mangling/name decoration?
    • What kind of problems does name mangling cause?
    • How do you work around them?
  • What is a class?
    • What are the differences between a struct and a class in C++?
    • What is the difference between public, private, and protected access?
    • For class CFoo { }; what default methods will the compiler generate for you>?
    • How can you force the compiler to not generate them?
    • What is the purpose of a constructor? Destructor?
    • What is a constructor initializer list?
    • When must you use a constructor initializer list?
    • What is a:
      • Constructor?
      • Destructor?
      • Default constructor?
      • Copy constructor?
      • Conversion constructor?
    • What does it mean to declare a...
      • member function as virtual ?
      • member function as static ?
      • member function as static ?
      • member variable as static ?
      • destructor as static ?
    • Can you explain the term "resource acquisition is initialization?"
    • What is a "pure virtual" member function?
    • What is the difference between public, private, and protected inheritance?
    • What is virtual inheritance?
    • What is placement
new?
  •  
    • What is the difference between
operator new

and the

new

operator?

  • What is exception handling?
    • Explain what happens when an exception is thrown in C++.
    • What happens if an exception is not caught?
    • What happens if an exception is throws from an object's constructor?
    • What happens if an exception is throws from an object's destructor?
    • What are the costs and benefits of using exceptions?
    • When would you choose to return an error code rather than throw an exception?
  • What is a template?
  • What is partial specialization or template specialization?
  • How can you force instantiation of a template?
  • What is an iterator?
  • What is an algorithm (in terms of the STL/C++ standard library)?
  • What is
std::auto_ptr?
  • What is wrong with this statement?
std::auto_ptr ptr(new char[10]);
  • It is possible to build a C++ compiler on top of a C compiler. How would you do this?

 

分享到:
评论

相关推荐

    C++问答题整理——together1

    【C++异常处理机制】 C++引入异常处理机制是为了改善传统错误处理方式的不足,尤其是在发现异常位置和处理异常位置不一致的情况。在C++中,异常处理允许程序员在程序的不同部分定义和捕获错误,使得代码的逻辑更加...

    C++ Programming HOW TO

    有用的链接包括C++官方文档、Stack Overflow上的C++问答等。 ##### 18.3 C++ Quick-Reference 快速参考手册提供了C++语言的关键概念和API速查表。 ##### 18.4 C++ Usenet Newsgroups Usenet新闻组是一种早期的...

    C++基础的一些问答题

    【C++基础问答详解】 1. **VC MFC程序中的输出**:在Visual C++的MFC(Microsoft Foundation Classes)程序中,不能直接使用`cout`进行输出,因为MFC是一个面向Windows API的库,它使用`CDC`(Device Context Class...

    C/C++单元测试问答(摘要)

    C/C++单元测试问答(摘要)软件测试《C/C++单元测试问答》提出了一些新的、颇有实用意义的单元测试思路,为了方便阅读,笔者整理了这篇摘要,完整内容请浏览http://www.KaileSoft.cn。为什么要进行单元测试?●单元测试...

    C++builder初学问答

    【C++Builder初学问答】 C++Builder是一个集成开发环境,专为使用C++语言开发Windows应用程序而设计。它提供了一套丰富的控件和API,使得开发者能够方便地创建各种类型的用户界面。以下是对C++Builder中一些基础...

    C++ 常见问题问答 .

    本篇将基于"C++常见问题问答"这一主题,深入探讨一些常见的C++编程问题,并提供相应的解答。 1. **基本语法问题** - **C++与C的区别**:C++是C语言的超集,增加了类、模板、异常处理等面向对象特性。 - **命名...

    课程设计基于C++ QT的问答系统源码+资料齐全(高分课程设计项目)

    课程设计基于C++ QT的问答系统源码+资料齐全(高分课程设计项目)课程设计基于C++ QT的问答系统源码+资料齐全(高分课程设计项目)课程设计基于C++ QT的问答系统源码+资料齐全(高分课程设计项目)课程设计基于C++ ...

    VISUAL C++经典问答

    **Visual C++经典问答** 在深入探讨MFC(Microsoft Foundation Classes)编程的问答集之前,首先需要理解MFC是什么。MFC是微软提供的一套C++类库,它基于面向对象的设计,为开发者提供了构建Windows应用程序的框架...

    C++常见问答

    为了成为真正的 OO 程序员,在学 C++ 之前,我需要先学一门纯 OO 语言吗? 标准化: 为何 C++ 没有图形用户接口? 为何 C++ 不支持线程? C++0x 会是什么样的? 书籍: 何时会有新的 ARM? 其它语言: 您怎么看C#?...

    C++知识问答题目.txt

    C++知识问答题目.txt

    课程设计基于C++和QT的问答系统源码+资料齐全(高分优秀项目)

    课程设计基于C++和QT的问答系统源码+资料齐全(高分优秀项目)课程设计基于C++和QT的问答系统源码+资料齐全(高分优秀项目)课程设计基于C++和QT的问答系统源码+资料齐全(高分优秀项目)课程设计基于C++和QT的问答...

    CV算法岗知识点及面试问答汇总(主要分为计算机视觉、机器学习、图像处理和 C++基础四大块).zip

    CV算法岗知识点及面试问答汇总(主要分为计算机视觉、机器学习、图像处理和 C++基础四大块).zip CV算法岗知识点及面试问答汇总(主要分为计算机视觉、机器学习、图像处理和 C++基础四大块).zip CV算法岗知识点及...

    c++题库-1000.cpp

    c++题库1000题

    编程及C/C++初学者 FAQ问答集 pdf

    编程及C/C++初学者 FAQ问答集 pdf 这本教程是作者在从事C、C++编程时在技术论坛所发帖子的精品收集,意在集中解决新手学习C/C++语言时将遭遇到的各类问题,经过整理后上传至网上,网友反馈普通良好,本次重新集合...

    C++电子书集锦B

    4. **《MFC经典问答》**: MFC是微软提供的一个用于开发Windows应用程序的类库,基于C++。本书可能是以问题解答的形式,解决MFC开发过程中常见的问题,包括窗口创建、消息处理、控件使用、数据库访问等。对于正在...

    c++编程详解PPT

    第6章"面向对象技术复习题汇总"提供了大量练习题,帮助巩固前面章节学到的知识,题目可能包括理论问答、代码实现和分析等,以检验读者对C++面向对象编程的理解和应用能力。 文档中的“面向对象技术复习题汇总.doc”...

    Visual C++学习资料

    3. **在线教程**:除了官方文档外,还可以参考Stack Overflow、Cplusplus.com等网站上的教程和问答,解决实际编程中遇到的问题。 4. **实战项目**:通过GitHub上的开源项目参与实际的C++项目开发,可以更好地将理论...

    c++0x 问答

    ### C++0x 问答知识点概览 #### 关于C++0x的总体理解与展望 - **C++0x的定位与价值**:C++0x被看作是C++语言的重要里程碑,旨在增强语言的功能性和易用性,解决早期版本中存在的问题,并引入现代编程实践所需的新...

    C++中国象棋人机对弈的实现

    使用C++语言制作的中国象棋人机对弈程序,附带论文及源码

    最全的常见C++面试题.rar

    所包含文件: 1、华为C++内部培训材料 2、130道面试题.doc 3、C++试题.htm 4、C-C++ 程序设计员应聘常见面试试题深入剖析...12、英语面试问答.htm 13、最全的C-C++试题集和答案1.txt 14、最全的C-C++试题集和答案2.txt

Global site tag (gtag.js) - Google Analytics