C++

  • Difference between mysql and mongodb:
    • MySQL: relational database management system (RDBMS). In MySQL, you pre-define your database schema, related information may be stored in separate tables, but associated through the use of joins. In this way, data duplication is minimized.
    • Mongdb: MongoDB stores data in JSON-like documents. MongoDB uses dynamic schemas. You can change the structure of records (which we call documents) simply by adding new fields or deleting existing ones. MongoDB was also designed with high availability and scalability in mind, and includes out-of-the-box replication and auto-sharding. Sharding is a type of database partitioning that separates very large databases the into smaller, faster, more easily managed parts called data shards. The word shard means a small part of a whole.
Difference.png
  • difference between C++ and C?

    • reference; object; namespace; malloc, calloc for allocating, free - new delete; exception handling; virtual and friend class
  • Abstract data type

    • struct & class
    • struct: undefined by default, all data are accessible, contains only data
    • class: always initialized, data encapsulation, contains data and functions, the compiler will call a constructor automatically
    • benefits:
      • Class internals are protected from inadvertent user-level errors, which might corrupt the state of the object.
      • The class implementation may evolve over time in response to changing requirements or bug reports without requiring change in user-level code.
  • what can you do with pointers?

    • simulate reference semantics
    • use objects across different scopes
    • enable subtype polymorphism
    • keep track of objects in dynamic memory
  • private, protected, public member access

    • A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.
  • private, protected, public inheritence

    • Public Inheritance − When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
    • Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
    • Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • Multiple Inheritance

    • class derived-class: access baseA, access baseB....
  • Operators Overloading in C++

    • Box operator+(const Box&); declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. In case we define above function as non-member function of a class then we would have to pass two arguments for each operand as follows −Box operator+(const Box&, const Box&);
  • polymorphism

    • C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
    • use virtual keyword to avoid the consequence of early binding/static linkage. the compiler then will look for the content of the pointer rather than the its type
    • pure virtual: when no meaningful definition for virtual function in the base class, we declare virtual int area() = 0;
  • Data encapsulation

    • Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
  • friend keyword

    • Making one class a friend of another exposes the implementation details and reduces encapsulation
  • abstract class

    • A class is made abstract by declaring at least one of its functions as pure virtual function
    • Abstract classes cannot be used to instantiate objects and serves only as an interface.
    • In contrast, classes that can be used to instantiate objects are called concrete classes.
    • This architecture also allows new applications to be added to a system easily, even after the system has been defined.
    • An object-oriented system might use an abstract base class to provide a common and standardized interface appropriate for all the external applications.
  • Error handling

try {
   // protected code
} catch( ExceptionName e1 ) {
   // catch block
} catch( ExceptionName e2 ) {
   // catch block
} catch( ExceptionName eN ) {
   // catch block
}
double division(int a, int b) {
   if( b == 0 ) {
      throw "Division by zero condition!";
   }
   return (a/b);
}
try {
   // protected code
} catch(...) {
  // code to handle any exception
}
  • stack and heap
    • The malloc() function from C, still exists in C++, but it is recommended to avoid using malloc() function. The main advantage of new over malloc() is that new doesn't just allocate memory, it constructs objects which is prime purpose of C++.
    • Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. Fast: because of the way that memory is allocated on the stack. Allocating memory on the stack is as simple as moving the stack pointer up.
    • Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory.
    • You can use the stack if you know exactly how much data you need to allocate before compile time and it is not too big. You can use heap if you don't know exactly how much data you will need at runtime or if you need to allocate a lot of data.
    • In a multi-threaded situation each thread will have its own completely independent stack but they will share the heap. Stack is thread specific and Heap is application specific. The stack is important to consider in exception handling and thread executions.
    • The stack is set to a fixed size, and can not grow past it’s fixed size (although some languages have extensions that do allow this). So, if there is not enough room on the stack to handle the memory being assigned to it, a stack overflow occurs. This often happens when a lot of nested functions are being called, or if there is an infinite recursive call. If the current size of the heap is too small to accommodate new memory, then more memory can be added to the heap by the operating system. This is one of the big differences between the heap and the stack.
    • The heap could have the problem of fragmentation, which occurs when the available memory on the heap is being stored as noncontiguous (or disconnected) blocks – because used blocks of memory are in between the unused memory blocks. When excessive fragmentation occurs, allocating new memory may be impossible because of the fact that even though there is enough memory for the desired allocation, there may not be enough memory in one big block for the desired amount of memory.
#include <iostream>
using namespace std;

class Box {
   public:
      Box() { 
         cout << "Constructor called!" <<endl; 
      }
      ~Box() { 
         cout << "Destructor called!" <<endl; 
      }
};
int main() {
   Box* myBoxArray = new Box[4];
   delete [] myBoxArray; // Delete array

   return 0;
}
  • namespace
    • In essence, a namespace defines a scope.
    • define a namespace
namespace namespace_name {
   // code declarations
}
  • the using directive
#include <iostream>
using namespace std;

// first name space
namespace first_space {
   void func() {
      cout << "Inside first_space" << endl;
   }
}

// second name space
namespace second_space {
   void func() {
      cout << "Inside second_space" << endl;
   }
}

using namespace first_space;
int main () {
   // This calls function from first name space.
   func();
   
   return 0;
}
  • templates
    • Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
    • The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.
template <typename T> // function template
inline T const& Max (T const& a, T const& b) { 
   return a < b ? b:a; 
}
template <class T> // class template
class Stack { 
   private: 
      vector<T> elems;    // elements 

   public: 
      void push(T const&);  // push element 
      void pop();               // pop element 
      T top() const;            // return top element 
      
      bool empty() const {      // return true if empty.
         return elems.empty(); 
      } 
}; 
  • preprocessor
    • There are number of preprocessor directives supported by C++ like #include, #define, #if, #else, #line, etc
#define PI 3.14159
#define MIN(a,b) (((a)<(b)) ? a : b)

// Conditional Compilation
#ifdef DEBUG
  cerr <<"Variable x = " << x << endl;
#endif

// The # operator causes a replacement-text token to be converted to a string surrounded by quotes.
#include <iostream>
using namespace std;
#define MKSTR( x ) #x

int main () {
  cout << MKSTR(HELLO C++) << endl;
  return 0;
}

// The ## operator is used to concatenate two tokens. Here is an example −
#include <iostream>
using namespace std;
#define concat(a, b) a ## b

int main() {
  int xy = 100;
  
  cout << concat(x, y);
  return 0;
}
  • Static Members of a C++ Class

    • A static member function can only access static data member, other static member functions and any other functions from outside the class.
  • copy constructor

    • If a copy constructor is not defined in a class, the compiler itself defines one.If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.
Line( int len );             // simple constructor
Line( const Line &obj);  // copy constructor
~Line();                     // destructor
Line line2 = line1; // This also calls copy constructor
  • friend class
    • A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
class Box {
   double width;
   
   public:
      double length;
      friend void printWidth( Box box );
      void setWidth( double wid );
};

// Note: printWidth() is not a member function of any class.
void printWidth( Box box ) {
   /* Because printWidth() is a friend of Box, it can
   directly access any member of this class */
   cout << "Width of box : " << box.width <<endl;
}

friend class ClassTwo;
  • inline functions

    • The C++ inline function provides an alternative. With inline keyword, the compiler replaces the function call statement with the function code itself (process called expansion) and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function, and then jump back as the code of the called function is already available to the calling program.
    • cons:
      • C++ inlining is resolved at compile time. Which means if you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated
      • It increases the executable size due to code expansion.
    • pros: Use inline function over macros(macros has some problems)
  • typedef, typename

  • static, dynamic casting

  • difference between Python and C++

  • OOD

  • design

  • stack and heap

  • include C in C++

  • private, public, protected, friend

  • abstract class, pure function

  • pass by &*value

  • difference betweeen JAVA and C++

    • Other languages like Java and .NET use garbage collection to automatically delete memory from the heap, without the programmer having to do anything..
    • Java uses the keyword abstract in order to declare abstract classes. An abstract class may or may not have abstract methods. But if there are any abstract methods in a class, then that class must absolutely be declared to be abstract.
  • synchronous and asynchronous design patterns

Reference:

  1. C++
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,740评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,437评论 0 23
  • 今年4月,到北京参加LIFE峰会算是比较全面地了解了PBL。6月,深入设计和实践。7月,将继续深入学习实践。 PB...
    颖子GO阅读 822评论 0 4
  • 发表于2013年9月26日 悲伤台风过境海岸线心情不定大雨的歌声盖过了几千万繁星老歌把昨天反复的沉淀痛苦全不见踪影...
    邝鉴萍阅读 184评论 0 2
  • 现在在机关办公室做文员工作,很多时候做的都是很琐碎的事情。比如管理桶装水,给其他部门的人发放各种办公用品,...
    遇见苏欣阅读 1,515评论 0 2