Q1: Override vs overload?
https://stackoverflow.com/questions/11912022/differentiate-between-function-overloading-and-function-overriding/23277110
In summary,
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there a is relationship between a superclass method and subclass method.
(b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
(c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
(d) Overloading must have different method signatures whereas overriding must have same signature.
Q2: The differences between C++ 11, C++ 14, and C++ 17, (even C++ 20) ?
https://www.quora.com/Why-is-C++14-better-than-C++11
One summary:
C++14 is more like a bug fix and minor enhancement of C++11 than a full new standard of C++. In fact C++17 is going to be the next major revision to the language. Many of the new features in C++14 should have been in C++11 itself. Some new stuffs include:
1. Generic Lambdas - lambda expression can infer auto type parameters.
2. Relaxed constexpr functions - now can contain conditionals, loops etc.
3. Type inference for all functions, not only for lambdas.
4. Template variables similar to template functions and classes.
Some details can be found here,
Q3: private, public, and protected?
Public, protected, private are three types of access modifiers in c++ which decides the accessibility of class members.
Public: The class members declared as public can be accessed by the functions both inside and outside the class.
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class.
Protected: The class members declared as protected can be accessed only by the functions inside the base class and derived classes.
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
IMPORTANT NOTE: Classes B, C and D all contain the variables x, y and z. It is just question of access.
Q4: what is the constexpr specifier in C++
constexpr is a feature added in C++ 11. The main idea is performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized be developer, it is run multiple times by users. The idea is to spend time in compilation and save time at run time.
In C++ 11, a constexpr function should contain only one return statement. C++ 14 allows more than one statements.
constexpr function should refer only constant global variables.
constexpr function can call only other constexpr function not simple function.
Function should not be of void type and some operator like prefix increment (++v) are not allowed in constexpr function.
constexpr vs inline functions:
Both are for performance improvements, inline functions are request to compiler to expand at compile time and save time of function call overheads. In inline functions, expressions are always evaluated at run time. constexpr is different, here expressions are evaluated at compile time.
constexpr vs const
They serve different purposes. constexpr is mainly for optimization while const is for practically const objects like value of Pi.
Both of them can be applied to member methods. Member methods are made const to make sure that there are no accidental changes by the method. On the other hand, the idea of using constexpr is to compute expressions at compile time so that time can be saved when code is run.
const can only be used with non-static member function whereas constexpr can be used with member and non-member functions, even with constructors but with condition that argument and return type must be of literal types.
No comments:
Post a Comment