(This page has no text content)
(This page has no text content)
(This page has no text content)
© Copyright 2021 - All rights reserved. The content contained within this book may not be reproduced, duplicated, or transmitted without direct written permission from the author or the publisher. Under no circumstances will any blame or legal responsibility be held against the publisher, or author, for any damages, reparation, or monetary loss due to the information contained within this book, either directly or indirectly. Legal Notice: This book is copyright protected. It is only for personal use. You cannot amend, distribute, sell, use, quote or paraphrase any part, or the content within this book, without the consent of the author or publisher. Disclaimer Notice: Please note the information contained within this document is for educational and entertainment purposes only. All effort has been executed to present accurate, up to date, reliable, complete information. No warranties of any kind are declared or implied. Readers acknowledge that the author is not engaging in the rendering of legal, financial, medical, or professional advice. The content within this book has been derived from various sources. Please consult a licensed professional before attempting any techniques outlined in this book. By reading this document, the reader agrees that under no circumstances is the author responsible for any losses, direct or indirect, that are incurred as a result of the use of the information contained within this document, including, but not limited to, errors, omissions, or inaccuracies.
Table of Contents C++ A Comprehensive Beginner’s Guide to Learn About the Realms of C++ From A-Z Introduction Chapter 1: Writing a C++ Program General structure of a simple C++ code! Editing, Compiling and Running a Program Variations of Writing Our Simple Program Chapter 2: Variables and Values Integer Values Variables and Assignment Identifiers Additional Integer Types Floating-Point Types Constants Other Numeric Types Characters Enumerated Types Chapter 3: Arithmetic and Expressions Expressions Mixed Type Expressions Operator Precedence and Associativity Comments Formatting Errors and Warnings Integers vs. Floating-Point Numbers
Bitwise Operators Algorithms Chapter 4: Conditional and Iterative Statements Conditional Execution Boolean Expression The Simple IF Statement Compound Statements The IF/ELSE Statement Nested Conditionals Iteration Nested Loops Abnormal Loop Termination Infinite Loops Chapter 5: Using, Writing and Managing Functions and Data Introduction to Using Functions Standard Mathematic Function Maximum and Minimum Clock Function Character Function Random Numbers Writing Functions Function Basics Using Functions Commenting Functions Managing Functions and Data Overloaded Function Default Arguments Recursion Chapter 6: Sequences Vectors Declaring and Using Vectors Traversing a Vector Vector Methods
Vectors and Functions Multidimensional Vectors Arrays Code 6.6 Copying an Array Multidimensional Arrays C Strings Chapter 7: Sorting and Searching Sorting Flexible Sorting Search Binary Search Vector Permutations Randomly Permuting a Vector Chapter 8: Standard C++ Classes String Objects Input/Output Streams Complex Numbers Better Pseudorandom Number Generation Chapter 9: Memory Management Memory Available to C++ Programs Manual Memory Management Linked Lists Resource Management Smart Pointers Chapter 10: Generic Programming Function Templates Class Templates Conclusion C++ Simple and Effective Tips and Tricks
to learn C++ Programming Effectively Introduction Chapter 1: The Fundamentals of C++ The Fundamental Characteristics of C++ Object-Oriented Programming Translating and Creating a C++ Program Chapter 2: The Basic Data Types, Constants, and Variables Used in C++ The Fundamental Data Types The Fundamental Constants The Fundamental Variables Constant and Volatile Objects Chapter 3: Functions and Classes in C++ Declaring Functions Function Calls Functions Without Return Values or Arguments Header Files Using Classes in C++ Chapter 4: Operators For Fundamental Types Binary Arithmetic Operators Unary Arithmetic Operators Assignments Relational Operators Logical Operators Chapter 5: Controlling the Flow of a Program The ‘While’ Statement The ‘For’ Statement The ‘do-while’ Statement Selections of ‘If-Else’ Statements Else-If Chains The Conditional Operators The ‘Switch’ Statements Chapter 6: Arithmetic Data Type Conversions
Implicit Type Conversions Performing Some of the Usual Arithmetic Type Conversions Implicit Type Conversions with Assignment Operators Some Other Type Conversions Chapter 7: The Use of References and Pointers in C++ Defining References References as Parameters References as Return Values Expressions with Reference Types Defining Pointers The Indirection Operator Pointers as Parameters Chapter 8: The Basics of File Input and File Output in C++ The Basic Concept of Files File Stream Classes Creating Files through a C++ Program Modes when Opening Files Closing Files Read and Write Operation on Blocks Conclusion References C++ Advanced Guide to Learn C++ Programming Effectively Introduction Chapter 1: Using Pointers in C++ Introduction to Pointers How to Use Pointers in C++ Types of Pointers Chapter 2: References in C++ Difference Between References and Pointers
How to Create References Chapter 3: Introduction to Data Structures in C++ The Struct Statement How to Access Members Using Structures as Arguments Using Pointers Typedef Keyword Chapter 4: Introduction to Object-Oriented Programming in C++ Definition of Classes Defining Class Objects How to Access the Class Members Classes and Objects Chapter 5: Differences Between Classes and Structures Chapter 6: Encapsulation in C++ Chapter 7: Understanding Inheritance Introduction to Base and Derived Classes Inheritance and Access Inheritance Types Multiple Inheritance Chapter 8: Overloading in C++ Introduction to Function Overloading Introduction to Operator Overloading Chapter 9: Polymorphism in C++ Understanding Virtual Functions Chapter 10: Abstraction in C++ Benefits How to Enforce Abstraction Example Why Use Abstraction? Chapter 11: Abstract Classes or Interfaces Chapter 12: Constructors in C++
Constructor Types Chapter 13: Copy Constructors in C++ Definition When Do You Call a Copy Constructor? When Should You Define a Copy Constructor? Assignment Operators Versus Copy Constructors Example Where You Use Copy Constructors What Happens When You Remove a Copy Constructor From the Code? Chapter 14: Destructors in C++ Properties When Do You Call a Destructor? Difference Between Destructors and Member Functions Chapter 15: Virtual Destructors in C++ Pure Virtual Destructors Chapter 16: Introduction to Private Destructors Chapter 17: Exception Handling in C++ Importance of Exception Handling Exception Handling Examples Chapter 18: Stack Unwinding Chapter 19: Identifying Exceptions in Base and Derived Classes Differentiating Between Block and Type Conversions Chapter 20: Object Destruction and Error Handling Chapter 21: Searching Algorithms Linear Search Binary Search Jump Search Chapter 22: Sorting Algorithms Bubble Sort Selection Sort Insertion Sort Quicksort
Chapter 23: Tips to Optimize Code in C++ Using the Appropriate Algorithm to Optimize Code Optimizing Code Using Input and Output Operators Optimizing the Use of Operators Optimization of Conditional Statements Dealing with Functions Optimizing Loops Optimizing Data Structures Sequential or Binary Search? Optimizing the Use of Arrays Chapter 24: Debugging and Testing Definition Conditions for Debugging Debugging Requirements Debugging Principles Functionalities to Use Techniques for Debugging How to Correct the Errors In Your Code Conclusion References
C++ A Comprehensive Beginner’s Guide to Learn About the Realms of C++ From A-Z BENJAMIN SMITH
Introduction C++ is a computer programming language, and as from one perspective, a computer program language is a sequence of instruction fed into a computer that dictate to the computer what to do. These instructions are executed through the flow of electrical impulses that affects the computer’s memory through interaction with the input and output devices. There are different types of computer programs playing different roles; one program might let a computer perform the role as a financial calculator, while another computer program could transform the computer into a worthy chess opponent. In the mid-1980s, Bjarne Stroustrup of AT and T Bell Labs developed the C++ programming language. C++ is an extension of the previously developed C programming language by AT and T Labs in the early 1970s. Originally, AT and T developed the C programing language to write UNIX operating systems, system-level software, and embedded system development. Initially, following after the development of the C++ programming language, it provided object-oriented programming features, and later on, generic programming capacities were added. The C++ programming language is useful in both commercial and industrial software development. C++ is a powerful programming language used for developing complex engineering, sciences, and business systems. Some common software written in C++ include Adobe Creative Suites, macOS, Microsoft Office, Microsoft Windows 8, and so on. The C++ programming language is complex itself so that it can meet up with the needs of commercial software development. Experienced software developers can accomplish great things with C++, but beginners may have a difficult time with it because it is closer to machine language than human language. We’re keen on understanding in this book, and so, to fully understand everything you need to know about C++ programing language, there are some development tools you need to acquit yourself to how they work.
This book is not aimed to cover all facets of the programming language, but to guide you on all, you ought to know about the C++ programming language. Despite the plethora of things you need to know about C++, this book will bring as much of it into ten concise chapters letting you learn all you need to know about C++ from A-Z.
Chapter 1 Writing a C++ Program C++ has a particular structure of writing them. The syntax of the code much be correct. If not, the compiler would give an error message, and it would not be executable. In this chapter, we shall be introducing C++ with simple executable examples you can try. Most C++ codes are executable in a lot of other programming languages like C, Ada, C#, and Java but with a slightly different syntax. The General Structure of a Simple C++ Code Below is a one of the simplest C++ code that does something: #include <iostream> int main() { std::cout << “General structure of a simple C++ code!\n”; } The above code is what a simple C++ code looks like. You type it into an editor, and you run it through a compiler, the program will print the message: General structure of a simple C++ code! This simple C++ code above is a four non-blank lines of code which includes: ● #include <iostream> This coding line is a preprocessing directive. The preprocessing directive in the C++ source code always starts with a # symbol. The symbols direct the preprocessor, so it adds a predefined source code to an existing one before compiling the process. This process is automatic. The iostream in this line means we want to use an object from the iostream library. The iostream is a
collection of pre-compiled C++ codes executable by the C++ program. The iostream library also contains elements that handle the input and output (I/O) like getting information from the keyboard, dealing with files, and printing a display on the screen. In the example, std::cout is not a part of the C++ code itself. It is one of those things related to output and input the C++ compiler develop and store in the iostream library. Now the compiler needs to be aware of these iostream items so it can compile the program. So, that is why the #include directive is used to specifies a file called a header, which contains the library code specifications. So, the compiler would use the std::cout to cross-check the specification in the <iostream> header. A lot of C++ programs make use of the #include <iostream> directive, although there are other programming codes headers as well. ● int main () { This programming line specifies the actual beginning of the program. In this line, we’re declaring a function named main. Any C++ program containing this function is executable. The int means integer, which is a fundamental variable a compiler used to assign numeric variables with whole numbers. The opening of the curly brace at the end of the line is the beginning of the main program. It’s a rule of thumb that the body of a function must contain the statement of the function to execute. ● std::cout << “General structure of a simple C++ code!\n”; The body of the main function in this code is only one statement. The statement is an executable program that directs to print the message “General structure of a simple C++ code!” on the screen. A statement simply means a fundamental unit of execution in the C++ program. Functions, for example, contains statements a compiler can translate into executable machine language. There are so many kinds of statements which we will explore more in the next chapter. Take note that statements in C++ ends with a semicolon (;). ● } The closing curly brace is what programmers use to mark the end of a particular function. So, both the opening and closing curly brace are needed for defining every function.
Editing, Compiling and Running a Program When it comes to the best development environment, C++ programmers have two options. The first option includes the use of a command-line environment, together with a collection of independent tools. The second option includes the use of an IDE like Virtual Studio, which groups the compilation steps into a process called build. These powerful IDEs have a myriad of features and configuration options that can be bewildering to those still learning how to program. In a typical command-line environment, the developer only needs to type in some simple commands in the console window to edit, compile, and eventually execute the program. Some programmers prefer the flexibility and simplicity of a command line of build environments, most especially in less complex projects. An example of a common command-line building system is the GNU Compiler Collection (GCC). The GCC C++ compiler is one of the most conforming compilers available for C++ standards. The GCC++ is supported by Linux, Apple Mac, and Microsoft Windows platforms, and it is a free, open-source software. The following are important tools that useful in editing, compiling, and running a program: Editors An editor is a tool that allows software developers to enter the program source code and save it into files. Most editor tools out there enhances the programmer’s productivity, such that it helps highlight language features with colors. Programmers must follow the strict syntax rules when creating computer programs. The syntax of a language is the way the different pieces and components of the sentence are arranged to form a sentence the computer would understand. For instance, the sentence “the short girl runs quickly to the window” makes use of proper English syntax. And by comparison, the sentence “girl the short runs window to quickly the” does not make use of proper English syntax and so it’s not correct. In like manner, if programmers don’t follow the syntax rule of programming, there are bounds to be errors, and the code wouldn’t be acceptable to be compiled and executed. Some syntax-aware editors make use of special annotations and colors to alert the developer of a syntax error before it is compiled.
Compilers A computer compiler is a tool used to translate a source code to a target code, which may be a machine language for an embedded device or a particular platform. It could also be that the target code is another machine language. In the earliest versions, C++ compiled source codes into C. Then, a C compiler would process the C code to produce an executable program. But today, C++ compilers translate the source code into machine language. The C++ complete set of building tools includes a linker, preprocessor, and compiler. Preprocessor A preprocessor is a building tool used to modify or add contents to the source file before starting the compiler to process the code. Mainly, developers use the preprocessor to #include information. Compiler The compiler compiles the source code to machine language, as described above. Linker The linker acts as a link that combines the machine code the compiler generates with the compiled code or library code from another source to create a complete executable program. Most times, a C++ compiled code can’t run on its own; it often requires some additional kind of machine code for it to be an executable program. That missing machine code is often precompiled and stored in a library. Generally, don’t think of the preprocessor, compiler, and liner as separate programs, but as one process taking place to translate source codes to executable programs. Debuggers The debugger is another building tool programmers use to trace a program’s execution to locate and correct any errors made in the program’s implementation. With the help of the debuggers, programmers can run a
program and see the line in the source code, causing the current action simultaneously. A programmer can watch the variable's values and other programming elements in case their values change as they ought to. Debuggers are useful in locating errors or bugs and for fixing programs containing the errors. Profilers The profiler helps a programmer collect statistics of a program’s execution, which can help the programmer fine-tune appropriate parts of the code to improve the overall performance. The profiler helps to show how many times and how long a part portion of a program was executed in a particular run. Furthermore, profilers are useful in testing purposes so that all the codes have a use somewhere during the test, which is also known as coverage. The main use of the profiler is to locate those parts of a program that needs improvement so that the program can run faster. Variations of Writing Our Simple Program The example below shows an alternative way to write the simple example we created above. #include <iostream> using std::count; int main () { cout << “General structure of a simple C++ program!\n”; } The example above made use of a using directive, which allows us to make use of a shorter name for the std::cout printing object. In fact, we could even omit the prefix std:: and use the shorter name cout. However, this directive is optional, and if we’re to omit it, then we must make use of the longer name. The name std means “standard,” and in the code, the prefix std tells us that cout is a part of a collection of names known as the standard namespace. The standard namespace holds names for all functions and types of standard C++ that are needed for all standard-conforming C++ developing environment. Outside the standard library, components provided by thirds party programmers reside in their own separate namespaces. Below is another way to write a code using the shorter name for cout within the C++ program.
Comments 0
Loading comments...
Reply to Comment
Edit Comment