[CPP] Classes, constructors and inline functions

Classes vs structs

classes and structs are (almost) the same thing in C++;
The difference is only in encapsulation
  • struct defaults to public, class to private
    If there is only data and no member functions in the code, we use a struct; if you add member functions then use a class.
  • Data and methods in a class have either public or private access.
  • public methods and data can be accessed by anything, like non-static global functions/data in a file.
  • private methods and data can only be accessed by other members of the same class
  • class members default to private access
  • struct members default to public access

    inheritance

    enter image description here
    To use inheritance, we use the
    class subclass : public superclass{};
  • As we can see in the above code, member functions can access the data in classes or structs using this-> not this..
  • In every getter function, there is a keyword “const”, means access only, no changes.
  • Also you might be curious about how the constructor works.

Constructor and destructors

Constructor
- called when an object is created
- the same as java, the same name as class name and no return type
Destructor
  • called when an object is destroyed
  • a function with name ~ then class name
  • e.g. ~DemoClass()
  • No return type
  • similar to java finalize
There are several ways of constructor, the most basic way is the same as java. We can write default constructor or constructor with parameter.
Another way is as above–constructor member with initializer lists

Initializer list

In this way, the default constructor, for example, can be written like this:
enter image description here
It’s the same as
public:
Something(){
    m_value1 = 1;
    m_value2 = 2.2;
    m_value3 = 'c';
}
Of course, constructors are more useful when we allow the caller to pass in the initialization values:
enter image description here
  • Uses the () form of initialization
  • Uses the : operator following the constructor parameters(before the opening brace)
  • Initialization lists are used a lot in C++, should be used in preference to member assignment

Inline functions

C++ offers a way to combine the advantages of functions with the speed of code written in-place: inline functions. The inline keyword is used to request that the compiler treat your function as an inline function.
Inline functions act exactly like normal functions but no function call is made.
Use the keyword “inline”, e.g.
inline int max(int a, int b)
{ return a > b ? a : b; }
  • Similar to a ‘safe’ marco expansion
  • Safely replaces the function call with the code(safer than marco #define)
  • Avoids the overhead of creating a stack frame
  • Very useful for small fast functions
  • It is advice only: compiler can decide to ignore you.

inline function example

enter image description here

评论

此博客中的热门博文

[MLE] W2 Multivariate linear regression

[MLE] W1 Introduction

[AIM] MetaHeuristics