- 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.
-
- 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: