C++: 3 in 1- Beginner's Guide+ Simple and Effective Tips and Tricks+ Advanced Guide to Learn C++ Programming Effectively

In this book, you will learn blocks of codes, code fragments, and statements in each section and you can try it out on y

1,589 397 5MB

English Pages [418] Year 2021

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

C++: 3 in 1- Beginner's Guide+ Simple and Effective Tips and Tricks+ Advanced Guide to Learn C++ Programming Effectively

Table of contents :
C++A Comprehensive Beginner’s Guide to Learn About the Realms of C++ From A-Z
C++A Comprehensive Beginner’s Guide to Learn About the Realms of C++ From A-Z
Introduction
Chapter 1: Writing a C++ Program
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
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
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
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
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
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
Chapter 7: Sorting and Searching
Sorting
Flexible Sorting
Search
Binary Search
Vector Permutations
Randomly Permuting a Vector
Chapter 8: Standard C++ Classes
Chapter 8: Standard C++ Classes
String Objects
Input/Output Streams
Complex Numbers
Better Pseudorandom Number Generation
Chapter 9: Memory Management
Chapter 9: Memory Management
Memory Available to C++ Programs
Manual Memory Management
Linked Lists
Resource Management
Smart Pointers
Chapter 10: Generic Programming
Chapter 10: Generic Programming
Function Templates
Class Templates
Conclusion
C++Simple and Effective Tips and Tricks to learn C++ Programming Effectively
C++Simple and Effective Tips and Tricks to learn C++ Programming Effectively
Introduction
Chapter 1: The Fundamentals of C++
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++
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++
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
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
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
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++
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++
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
C++Advanced Guide to Learn C++ Programming Effectively
Introduction
Chapter 1: Using Pointers in C++
Chapter 1: Using Pointers in C++
Introduction to Pointers
How to Use Pointers in C++
Types of Pointers
Chapter 2: References in C++
Chapter 2: References in C++
Difference Between References and Pointers
How to Create References
Chapter 3: Introduction to Data Structures in C++
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++
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 5: Differences Between Classes and Structures
Chapter 6: Encapsulation in C++
Chapter 6: Encapsulation in C++
Chapter 7: Understanding Inheritance
Chapter 7: Understanding Inheritance
Introduction to Base and Derived Classes
Inheritance and Access
Inheritance Types
Multiple Inheritance
Chapter 8: Overloading in C++
Chapter 8: Overloading in C++
Introduction to Function Overloading
Introduction to Operator Overloading
Chapter 9: Polymorphism in C++
Chapter 9: Polymorphism in C++
Understanding Virtual Functions
Chapter 10: Abstraction in C++
Chapter 10: Abstraction in C++
Benefits
How to Enforce Abstraction
Example
Why Use Abstraction?
Chapter 11: Abstract Classes or Interfaces
Chapter 11: Abstract Classes or Interfaces
Chapter 12: Constructors in C++
Chapter 12: Constructors in C++
Constructor Types
Chapter 13: Copy Constructors in C++
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++
Chapter 14: Destructors in C++
Properties
When Do You Call a Destructor?
Difference Between Destructors and Member Functions
Chapter 15: Virtual Destructors in C++
Chapter 15: Virtual Destructors in C++
Pure Virtual Destructors
Chapter 16: Introduction to Private Destructors
Chapter 16: Introduction to Private Destructors
Chapter 17: Exception Handling in C++
Chapter 17: Exception Handling in C++
Importance of Exception Handling
Exception Handling Examples
Chapter 18: Stack Unwinding
Chapter 18: Stack Unwinding
Chapter 19: Identifying Exceptions in Base and Derived Classes
Chapter 19: Identifying Exceptions in Base and Derived Classes
Differentiating Between Block and Type Conversions
Chapter 20: Object Destruction and Error Handling
Chapter 20: Object Destruction and Error Handling
Chapter 21: Searching Algorithms
Chapter 21: Searching Algorithms
Linear Search
Binary Search
Jump Search
Chapter 22: Sorting Algorithms
Chapter 22: Sorting Algorithms
Bubble Sort
Selection Sort
Insertion Sort
Quicksort
Chapter 23: Tips to Optimize Code in C++
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
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

Citation preview

© 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 int main() { std::cout