Core C++ Programming Concepts and Data Principles
Data Collection Fundamentals
Data collection is the process of measuring and gathering information on desired variables to answer specific research questions. It is a common feature of study in various disciplines, such as marketing, statistics, economics, sciences, and more. While the methods of collecting data may vary according to the subject, the ultimate aim of the study and honesty in data collection are equally important in all matters of study.
Primary vs. Secondary Data
Primary data is collected by researchers on their own and for the first time in a study. There are various ways of collecting primary data.
Secondary data refers to data that is not collected by the researcher personally or for the first time. In fact, secondary data is already available and needs to be collected from various existing sources.
Understanding Multithreading
Multithreading is a specialized form of multitasking, which is the feature that allows your computer to run two or more programs concurrently. In general, there are two types of multitasking:
- Process-based multitasking: Handles the concurrent execution of entire programs.
- Thread-based multitasking: Deals with the concurrent execution of pieces of the same program.
A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Before C++11, there was no built-in support for multithreaded applications; instead, it relied entirely upon the operating system to provide this feature.
C++ Exception Handling
An exception is an unexpected problem that arises during the execution of a program, causing the program to terminate suddenly with some errors or issues. Exceptions occur during the running of the program (runtime).
Types of C++ Exceptions
There are two types of exceptions in C++:
- Synchronous: Exceptions that happen when something goes wrong because of a mistake in the input data or when the program is not equipped to handle the current type of data it’s working with, such as dividing a number by zero.
- Asynchronous: Exceptions that are beyond the program’s control, such as disk failure or keyboard interrupts.
C++ Virtual Functions
A virtual function (also known as virtual method) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the method. Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for the function call.
Purpose of Virtual Functions
Virtual functions are mainly used to achieve runtime polymorphism. Functions are declared with a virtual
keyword in a base class, and the resolving of a function call is done at runtime.
Rules for Virtual Functions
The rules for virtual functions in C++ are as follows:
- Virtual functions cannot be
static
. - A virtual function can be a
friend
function of another class. - Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism.
- The prototype of virtual functions should be the same in the base as well as the derived class.
- They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define) the virtual function; in that case, the base class version of the function is used.
C++ Inline Functions
The C++ programming language provides various important and fascinating methods and built-in functions to its users. When it comes to enhancing the performance of the program, the inline function proves to be an excellent concept. An inline function in C++ is an enhancement feature that improves the execution time and speed of the program. A main advantage of inline functions is that you can use them with C++ classes as well.
When an instruction of a function call is encountered during the compilation of a program, its memory address is stored by the compiler. The function arguments are copied onto the stack, and after the execution of the code, control is transferred back to the calling instruction. This process can sometimes cause overhead in function calls, especially if the function is small and its execution time is less than the switching time. This issue is resolved by using inline functions. These functions overcome the overhead and also make the program faster by reducing the execution time.
Overloading vs. Overriding in C++
Feature | Overloading | Overriding |
---|---|---|
Polymorphism Type | Compile-time | Run-time |
Usage | Increases the readability of the program. | Grants the specific implementation of the method already provided by its parent class or superclass. |
Need of Inheritance | It may or may not | Always |
Methods Have | Same name and different signatures | Same name and same signature |
Return Type | Can or cannot be the same | Must be the same or co-variant |
Binding Type | Static | Dynamic |
Performance | Good | Poor |
Overload Private and Final Methods | Yes | No |
Argument List | Different | Same |