C++ (Daniel Bell) (Z Library)

Author: Daniel Bell

教育

C++ is a general purpose language language. This short book is written for beginners so even if you have no prior knowledge in C++, you won't face any difficulty understanding these tutorials.You will learn to code in C++ in just a few hours.Get your copy NowBook Objectives: The book objectives include the following: To help you understand the origin of C++ as a computer programming language. To help you understand the features of the C++ programming language. To equip you with C++ programming skills. To help you know the kind of applications that you can develop with C++. To equip you with general knowledge about computer programming and how/where you can apply it in real life. Who this Book is for? Anybody who is a complete beginner to C++. Anybody in need of advancing their C++ skills. Professionals in computer programming. Professors, lecturers or tutors who are looking to find better ways to explain C++ programming to their students in the simplest and easiest way. Students and academicians, especially those focusing on C, C++ and C# programming, computer science, and Databases development. What do you need for this Book? You only need a computer installed with an operating system, Linux, Windows or Mac OS X. The author has given a guide on how to setup your C++ coding environment. What is inside the book? GETTING STARTED WITH C++. C++ PROGRAM STRUCTURE. C++ DATA TYPES. C++ VARIABLES. C++ CONSTANTS. C++ OPERATORS. C++ LOOPS. DECISION MAKING. C++ FUNCTIONS. ARRAYS. STRINGS. POINTERS. CLASSES AND OBJECTS. INHERITANCE. OVERLOADING. POLYMORPHISM. DATA ABSTRACTION. C++ language is a direct descendant of C programming language with additional features such as type checking and object oriented programming. It is based on C but is an object-oriented programming language. It is widely used in graphics libraries, games, network devices, etc. This guide is written for beginners so even if you have no prior knowledge in C++, you won't face any problem understanding this

📄 File Format: PDF
💾 File Size: 4.1 MB
262
Views
109
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
(This page has no text content)
📄 Page 3
C++ for beginners Your comprehensive step-by-step guide to learn everything about C++ Daniel Harder Introduction C++ is a high-performance programming language that is widely used in a variety of applications, such as operating systems, web browsers, and video
📄 Page 4
games. It was developed in 1979 by Bjarne Stroustrup as an extension of the C programming language, adding object-oriented features and other enhancements. C++ is a statically-typed, compiled language, which means that it is more efficient than dynamically-typed languages like Python or JavaScript. It is also a very expressive language, allowing programmers to write code that is both efficient and easy to read. One of the key features of C++ is its support for object-oriented programming (OOP). In OOP, data and behavior are encapsulated in "objects," which can be used to model real-world concepts and interact with each other through methods. C++ also supports procedural programming, which is a more traditional style of programming that focuses on writing functions to perform specific tasks. C++ is a powerful and flexible language, but it can also be complex and difficult to learn for beginners. It requires a good understanding of computer science concepts and a solid foundation in programming concepts such as variables, data types, loops, and control structures. However, once you have a firm understanding of these concepts, C++ can be a very rewarding language to learn and use.
📄 Page 5
Chapter one basic concepts C++ type system In C++, data is stored and manipulated using various data types. These data types determine the size and layout of memory used by the variables, as well as the set of operations that can be performed on them. C++ has a rich type system that includes both built-in types and user-defined types. Built-in types include: Integer types: These represent whole numbers and include char , short , int , long , and long long . Each of these types has a different size, with char being the smallest and long long being the largest. Floating-point types: These represent numbers with fractional parts and include float , double , and long double . These types also have different sizes, with float being the smallest and long double being the largest. Boolean type: This represents a true/false value and is represented by the bool type. Character types: These represent individual characters and include char and wchar_t . char is used for ASCII characters and wchar_t is used for wide characters, which can represent characters from a variety of different alphabets. User-defined types in C++ include: Classes: A class is a user-defined data type that allows you to define your own data fields and methods. Structures: A structure is similar to a class, but the data fields and methods are public by default. Enumerations: An enumeration is a user-defined type that consists of a set of named constants. Typedef: The typedef keyword allows you to define a new name for an existing data type. This can be used to create more readable or expressive names for complex types. C++ also supports type casting, which allows you to convert a value from one data type to another. This can be useful when working with different data types that need to be used together, but care must be taken when using type
📄 Page 6
casting, as it can lead to loss of precision or other unexpected behavior if not used correctly. Scope In C++, the scope of a variable refers to the part of the program in which the variable is visible or can be accessed. A variable's scope is determined by the location of its declaration in the program. There are two main types of scope in C++: local scope and global scope. A local variable is one that is declared within a function or block of code. Local variables are only visible within the function or block in which they are declared, and they are only accessible from the point of their declaration to the end of the block. When the function or block ends, the local variable is destroyed and is no longer accessible. A global variable is one that is declared outside of any function or block of code. Global variables are visible throughout the entire program and can be accessed from any function or block of code. However, it is generally considered good programming practice to minimize the use of global variables, as they can make the program more difficult to understand and maintain. C++ also supports nested scopes, where a block of code is defined within another block of code. In this case, variables declared in the inner block are only visible within that block, but they may also be accessed from the outer block. The scope of a variable is an important concept in C++, as it determines the visibility and accessibility of the variable within the program. Understanding the scope of variables is essential for writing efficient and maintainable code. Header files In C++, a header file is a file that contains declarations of functions, variables, and other constructs that can be used in a C++ program. Header files are typically denoted by the .h suffix and are included in a C++ source file using the #include directive.
📄 Page 7
Header files are used for a variety of purposes in C++. They can be used to declare functions and variables that are defined in another source file, allowing them to be used in multiple files without duplication of code. They can also be used to declare functions and variables that are implemented in a library, allowing them to be used in a program without the need to link the library into the program. Header files can also be used to define macros, which are short pieces of code that are replaced by the preprocessor with their corresponding expanded form. Macros are often used to define constants and other values that need to be used in multiple places in a program. One of the main advantages of using header files is that they allow code to be organized and reused more easily. By separating declarations from definitions and placing them in a separate header file, it is possible to use the same code in multiple programs without having to copy and paste it. This makes it easier to maintain and update the code, as changes only need to be made in a single location. Some examples of standard C++ header files include <iostream> , which provides input and output streams for reading and writing data, and <string> , which provides functions and classes for working with strings. There are many other header files available in the C++ standard library, as well as in third-party libraries, that provide a wide range of functionality for various tasks. Translation units and linkage In C++, a translation unit is a source file and all the header files that it includes, along with any header files included by those header files, and so on. The process of turning a translation unit into an executable program is called compilation. During compilation, the preprocessor processes the #include directives in the translation unit, inserting the contents of the included header files into the source file. The resulting expanded source file is then passed to the compiler, which translates it into object code. Object code is a machine-readable representation of the program that can be executed by the computer. However, object code is not in a form that can be directly executed by the operating system. Instead, it must be combined with
📄 Page 8
other object code files and libraries to create an executable program. This process is called linkage. There are two types of linkage in C++: internal linkage and external linkage. Internal linkage refers to symbols (variables and functions) that are visible only within a single translation unit. These symbols are typically used for implementation details that are not intended to be accessed from other translation units. Internal linkage is achieved by declaring symbols with the static keyword. External linkage refers to symbols that are visible to multiple translation units. These symbols are typically used to define functions and variables that are intended to be shared between multiple translation units. External linkage is the default for symbols that are not declared with the static keyword. Linkage is an important concept in C++, as it determines the visibility and accessibility of symbols within a program. Understanding linkage is essential for writing efficient and maintainable code, particularly when working with large programs that consist of multiple source files and libraries. main function and command-line arguments In C++, the main function is the entry point of a program. It is the function that is called by the operating system when the program is started, and it determines the flow of control for the rest of the program. The main function has a specific syntax and return type. In C++, it can be declared in one of the following ways: The first version of main takes no arguments and returns an int value. The second version takes two arguments: argc and argv . argc is an int value that represents the number of command-line arguments passed to the program,
📄 Page 9
and argv is an array of char* values that holds the actual arguments. Command-line arguments are values that are passed to the program when it is started from the command line. They are typically used to pass configuration options or other input to the program. For example, consider the following program that prints the command-line arguments it receives: #include <iostream> To run this program and pass it some command-line arguments, you would use a command like this: This would print the following output: As you can see, the first argument ( argv[0] ) is the name of the program itself, and the remaining arguments are the ones passed on the command line. Command-line arguments can be useful for providing input to a program or for configuring its behavior. They are a common feature of many command- line programs and are easy to use in C++. Program termination In C++, a program terminates when the main function returns or when the exit function is called.
📄 Page 10
The main function is the entry point of a C++ program and determines the flow of control for the rest of the program. When the main function returns, the program terminates. The return value of main is the exit status of the program, which can be used by the operating system or other programs to determine the outcome of the program. A return value of 0 typically indicates that the program ran successfully, while a non-zero value typically indicates an error. For example, the following main function returns 0 to indicate that the program ran successfully: The exit function is a function defined in the cstdlib library that allows a program to terminate immediately. It takes an int value as an argument, which is the exit status of the program. The exit function can be called from anywhere in the program and will cause the program to terminate immediately, even if there are other functions or blocks of code that have not yet completed. For example, the following code calls the exit function to terminate the program with an exit status of 1: It is generally considered good programming practice to use return to terminate the main function and to reserve the exit function for emergency situations or exceptional circumstances. Understanding how a program terminates is important for writing efficient and maintainable code, as it allows you to ensure that all necessary cleanup tasks are performed before the program exits. Lvalues and rvalues In C++, an lvalue (short for "left value") is an expression that refers to a
📄 Page 11
memory location and can appear on the left side of an assignment. An rvalue (short for "right value") is an expression that does not refer to a memory location and cannot appear on the left side of an assignment. Here are some examples of lvalues and rvalues: In C++, lvalues have a specific type and can be used to refer to objects that are stored in memory. Rvalues, on the other hand, do not have a specific type and cannot be used to refer to objects stored in memory. Lvalues and rvalues are an important concept in C++, as they determine the set of operations that can be performed on expressions. For example, lvalues can be assigned to, while rvalues cannot. Lvalues can also be dereferenced, while rvalues cannot. Understanding the difference between lvalues and rvalues is essential for writing correct and efficient C++ code. Temporary objects In C++, a temporary object is an object that is created to hold the result of an expression and is destroyed immediately after the expression has been evaluated. Temporary objects are also known as "rvalues," as they are typically created from rvalue expressions (expressions that do not refer to a memory location and cannot appear on the left side of an assignment). Temporary objects are created in a variety of situations in C++. For example, they can be created when an rvalue is passed to a function or when an rvalue is used as the right operand of an assignment.
📄 Page 12
Here are some examples of temporary objects being created in C++: Point p = Point(1, 2); // a temporary object is created to hold the result of the Point constructor Temporary objects are often used to avoid unnecessary copies or to take advantage of move semantics, which allows the contents of an object to be "moved" rather than copied. It is important to be aware of temporary objects in C++, as they can have different behavior than normal objects and can lead to unexpected results if not used correctly. For example, it is not generally allowed to bind a non- const reference to a temporary object, as the temporary object will be destroyed as soon as the reference goes out of scope. Understanding the behavior of temporary objects is essential for writing correct and efficient C++ code. Alignment In C++, alignment refers to the way data is arranged in memory. Alignment can have an impact on the performance and efficiency of a program, as it can
📄 Page 13
affect the speed at which data is accessed and the amount of memory used. C++ provides a number of language features and library functions for controlling alignment. One way to control alignment in C++ is through the use of the alignas and alignof keywords. The alignas keyword can be used to specify the alignment of a variable or data type, and the alignof keyword can be used to determine the alignment of a variable or data type. For example, the following code defines a variable x with an alignment of 16 bytes: The alignof keyword can be used to determine the alignment of a variable or data type, as shown in the following example: Another way to control alignment in C++ is through the use of the std::aligned_storage and std::aligned_union types from the <type_traits> header. These types provide a way to store data with a specified alignment in a way that is portable and efficient. For example, the following code defines a variable storage that can be used to store an object of type T with an alignment of 16 bytes: Understanding alignment is important for writing efficient and performant C++ code, particularly when working with large data structures or when interacting with low-level hardware or systems. Proper alignment can significantly improve the performance of a program by reducing the number of memory accesses and Trivial, standard-layout, and POD types In C++, a type is considered trivial if it is a type that has a trivial default constructor, a trivial destructor, and no virtual functions or virtual base
📄 Page 14
classes. A type is considered standard-layout if it is a type that satisfies the following conditions: It is a trivial type. It has no non-static data members of type non-standard-layout class (or array of such types). It has no virtual functions and no virtual base classes. A type is considered a POD (Plain Old Data) type if it is a type that satisfies the following conditions: It is a standard-layout type. It has no non-static data members of type non-POD class (or array of such types). It has no user-defined copy assignment operator and no user-defined destructor. Trivial, standard-layout, and POD types are important concepts in C++, as they have specific behavior and properties that can be relied upon by the programmer. For example, trivial types can be memset to zero and copied using memcpy, while standard-layout types can be passed to C functions and can be used to implement union types. POD types can be used to implement C-style structs and can be initialized with a brace-enclosed initializer list. Understanding the behavior and properties of trivial, standard-layout, and POD types is important for writing efficient and correct C++ code, particularly when working with low-level hardware or systems, or when interfacing with C code. what is Value types In C++, a value type is a type that holds a value and is typically stored in memory on the stack. Value types are also known as "fundamental types," as they are the most basic types provided by the language. The standard C++ library provides several value types, including the following: bool : a boolean type that represents true or false. char : a character type that represents a single character. int , short , long , and long long : integer types of varying sizes.
📄 Page 15
float and double : floating-point types of varying precision. Value types are typically used to hold simple data, such as numbers, characters, and booleans. They are efficient to use, as they do not require any additional memory management and are stored directly in memory on the stack. Here is an example of using value types in C++: Value types are an important concept in C++, as they form the basis for many other types in the language. Understanding how value types work and how they can be used is essential for writing efficient and correct C++ code. Type conversions and type safety In C++, type conversions are the process of converting a value from one type to another. There are several ways to perform type conversions in C++, including explicit type casting, function overloading, and type conversion operators. Explicit type casting is the process of explicitly converting a value from one type to another using the static_cast operator. This is typically used to convert a value from a base type to a derived type, or from a derived type to a base type. For example, the following code uses explicit type casting to convert a double value to an int value: Function overloading is the process of defining multiple functions with the same name but with different parameter types. This allows the same function to be used to perform different operations depending on the type of its arguments.
📄 Page 16
For example, the following code defines an abs function that can be used to calculate the absolute value of an int , a float , or a double : Standard conversions In C++, a standard conversion is a predefined implicit type conversion that is performed by the compiler. Standard conversions are a set of predefined rules that dictate how values of different types can be converted to one another. There are three types of standard conversions in C++: 1. Lvalue-to-rvalue conversion: This conversion is applied to lvalues (expressions that refer to a memory location) to produce rvalues (expressions that do not refer to a memory location). This conversion is typically applied to objects to allow them to be used as rvalues, such as when they are passed to functions or used as the right operand of an assignment. 2. Array-to-pointer conversion: This conversion is applied to arrays to convert them to pointers to their first element. This conversion is typically applied when an array is passed to a function or when it is used in an expression. 3. Function-to-pointer conversion: This conversion is applied to function names to convert them to pointers to the function. This conversion is typically applied when a function name is passed to a function or when it is used in an expression. Standard conversions are applied automatically by the compiler, and the programmer does not need to explicitly specify them. They are an important
📄 Page 17
part of the C++ type system, as they allow values of different types to be used interchangeably in many contexts. Understanding how standard conversions work is essential for writing correct and efficient C++ code.
📄 Page 18
Chapter II built-in types Built-in types In C++, built-in types are a set of predefined types that are provided by the language and are implemented directly by the compiler. Built-in types are also known as "fundamental types" or "value types," as they are the most basic types provided by the language and are typically stored in memory on the stack. The C++ standard library provides several built-in types, including the following: bool : a boolean type that represents true or false. char : a character type that represents a single character. int , short , long , and long long : integer types of varying sizes. float and double : floating-point types of varying precision. void : a special type that represents the absence of a value. Built-in types are an important concept in C++, as they form the basis for many other types in the language. Understanding how built-in types work and how they can be used is essential for writing efficient and correct C++ code. Here is an example of using built-in types in C++: Data type ranges
📄 Page 19
In C++, the range of a data type is the set of values that it can represent. The range of a data type is determined by its size and the way it is encoded. The C++ standard library provides several built-in types, each with a specific range of values it can represent. The following table shows the range of some common built-in types in C++: Type Size (bytes) Minimum value Maximum value bool 1 false true char 1 -128 127 wchar_t 2 or 4 implementation-defined implementation-defined char16_t 2 0 65535 char32_t 4 0 4294967295 short 2 -32768 32767 int 4 -2147483648 2147483647 long 4 or 8 implementation-defined implementation-defined long long 8 -9223372036854775808 9223372036854775807 float 4 1.17549e-38 3.40282e+38 double 8 2.22507e-308 1.79769e+308 long double 8 or 10 or 16 implementation-defined implementation-defined It is important to be aware of the range of a data type when working with C++, as using a value outside of its range can lead to undefined behavior. Understanding the range of a data type is also important for choosing the appropriate type for a given task, as using a type with a larger range may result in better performance or more accurate results. nullptr In C++, nullptr is a special keyword that represents a null pointer value. It was introduced in C++11 as a safer and more explicit way to represent a null pointer than the use of the integer constant 0 or the macro NULL . nullptr has type std::nullptr_t , which is a special type that can be implicitly converted to any pointer type. This allows nullptr to be used in a variety of contexts where a pointer is expected, such as when initializing a pointer or when passing a pointer to a function.
📄 Page 20
Here is an example of using nullptr in C++: Using nullptr instead of 0 or NULL has several advantages. It is more explicit and easier to read, as it clearly indicates that a null pointer is being used. It is also safer, as it prevents accidental conversions between pointers and integers, which can lead to undefined behavior. nullptr is an important concept in C++, particularly when working with pointers and nullable values. Understanding how to use nullptr and how it differs from 0 and NULL is essential for writing correct and safe C++ code. nullptr In C++, nullptr is a special keyword that represents a null pointer value. It was introduced in C++11 as a safer and more explicit way to represent a null pointer than the use of the integer constant 0 or the macro NULL . nullptr has type std::nullptr_t , which is a special type that can be implicitly converted to any pointer type. This allows nullptr to be used in a variety of contexts where a pointer is expected, such as when initializing a pointer or when passing a pointer to a function. Here is an example of using nullptr in C++: Using nullptr instead of 0 or NULL has several advantages. It is more explicit and easier to read, as it clearly indicates that a null pointer is being used. It is also safer, as it prevents accidental conversions between pointers and integers, which can lead to undefined behavior.
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List