[关闭]
@skysbjdy 2016-10-09T06:56:04.000000Z 字数 6347 阅读 1547

OOP

Interview


Basic concepts

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming paradigm where the complete software operates as a bunch of objects talking to each other. An object is a collection of data and methods that operate on its data.

Why OOP?

The main advantage of OOP is better manageable code that covers following.
1. The overall understanding of the software is increased as the distance between the language spoken by developers and that spoken by users.
2. Object orientation eases maintenance by the use of encapsulation. One can easily change the underlying representation by keeping the methods same.

OOP paradigm is mainly useful for relatively big software. See this for a complete example that shows advantages of OOP over procedural programing.

What are main features of OOP?

Encapsulation
Polymorphism
Inheritance

What is Inheritance? What is the purpose?

The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class.
The purpose of inheritance is Code Reuse.

What is Abstraction?

The first thing with which one is confronted when writing programs is the problem. Typically we are confronted with “real-life” problems and we want to make life easier by providing a program for the problem.

However, real-life problems are nebulous and the first thing we have to do is to try to understand the problem to separate necessary from unnecessary details: We try to obtain our own abstract view, or model, of the problem. This process of modeling is called abstraction.

C++ Concepts

What are the differences between C and C++?

  1. C++ is a kind of superset of C.
  2. C is a procedural programming language, but C++ supports both procedural and Object Oriented programming.
  3. Since C++ supports object oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, friend functions. These features are absent in C.
  4. C++ supports exception handling at language level, in C exception handling is done in traditional if-else style.
  5. C++ supports references, C doesn’t. (C里面 函数声明是参数是pointer, 调用函数时,参数取地址 addrss of operator)

    void fun(int *x, int *y){ /* ... */ }
    fun(&a, &b);
    
  6. In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is standard input stream and cout is standard output stream.

What are the differences between references and pointers?

Similarities:

Both references and pointers can be used to change local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain.

Differences:

Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. In Java, references don’t have above restrictions, and can be used to implement all data structures. References being more powerful in Java, is the main reason Java doesn’t need pointers.

What are virtual functions – Write an example?

Virtual functions are used with inheritance, they are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual.
Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions)
1) A base class and a derived class.
2) A function with same name in base class and derived class.
3) A pointer or reference of base class type pointing or referring to an object of derived class.

  1. #include<iostream>
  2. using namespace std;
  3. class Base {
  4. public:
  5. virtual void show() { cout<<" In Base \n"; }
  6. };
  7. class Derived: public Base {
  8. public:
  9. void show() { cout<<"In Derived \n"; }
  10. };
  11. int main(void) {
  12. Base *bp = new Derived;
  13. bp->show(); // RUN-TIME POLYMORPHISM
  14. return 0;
  15. }

Output:

In Derived

For example, in the following program bp is a pointer of type Base, but a call to bp->show() calls show() function of Derived class, because bp points to an object of Derived class.

What is this pointer?

The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

Can we do “delete this”?

See http://www.geeksforgeeks.org/delete-this-in-c/

What are VTABLE and VPTR?

Compiler adds additional code at two places to maintain and use vtable and vptr.

  1. Code in every constructor. This code sets vptr of the object being created. This code sets vptr to point to vtable of the class.
  2. Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference (In the above example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注