



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to C++ programming, focusing on variables and data types, coding conventions, and input/output operations using the console. It covers the use of int main(), variables declaration, data types, constants, arithmetic operations, and input/output functions. The document also explains the concept of object-oriented programming and the use of objects and classes.
What you will learn
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!
INTRODUCTION TO C++ 2 VARIABLES AND DATA TYPES 3 CODING CONVENTIONS AND STYLE 8 SELECTION AND REPETITION 9 ARRAYS AND VECTORS 13 FUNCTIONS 15 CLASSES AND OBJECTS 18 REFERENCES AND POINTERS 23 INHERITANCE 27 POLYMORPHISM 29 OBJECT ORIENTED DESIGN 30 GLOSSARY 33
C++ is one of the major programming languages used in the world. In contrast to other popular languages such as Java, C# or Python, C++ allows for more specific memory management and executes code faster as it is compiled directly on the hardware. Similar to other languages, C++ runs from the top of the script to the bottom (or to a point where the program is ended). There is a main script that is executed first, and all other scripts can be called upon from the main script. To begin with the basics, the Hello World program displays some text to the user: In this particular case, we will output the text “Hello World” to the console , which is text-only interface used to connect the programmer with the application. It can be used to display simple debugging output, error messages or warnings. The console can also be configured to accept user input. In this unit, we will use the console as our primary input/output source and will create programs that utilise the console window. Looking at the code written above, C++ executes each code line sequentially, starting from line 1.
When declaring multiple variables, C++ provides a simple way of declaring them all on the same line, provided they all have the exact same data type. This helps to group variables visually, but does not affect the code. Here are some other examples of variable definitions using other data types. Note that the string data type is not in a different colour as the rest of them. This is because it is not a primitive C++ data type, but rather one that can be accessed using standard libraries and holds a collection of characters (rather than a single character that the char data type holds). Variables in C++ can also be declared as constants. This means that their value cannot be altered after their initialisation. They are useful for any values that are known to be the same throughout the code and can be of any data type. To define a constant, use the const keyword in front of the data type. Although it does not need to be followed, it is good coding practice to name constant variables using only capital letters and underscores. Variables can be used to perform operations on. For a numeric variable, such as an integer or a double, mathematics can be applied on them to produce a range of outputs. For example, two integers can be added together, and the result of that operation can be assigned to another variable. For mathematical operations, standard arithmetic rules apply (brackets first, multiplication and division next and addition and subtraction last). Brackets should be used more often to make calculations more readable. To increment a variable by a specific value, there are two ways to do the same piece of code. The first way retrieves the value from memory and then stores the value back added with 1. The second way is a shorthand version of the first, and simply adds one to the variable. The same can be done using the -=, *= and /= operators, for subtracting, multiplying and dividing. Another useful operator only applies to integers needing to increment (or decrement) by 1 and 1 only. The syntax is as follows: This is known as an auto-increment operator, which takes a value and adds one to it. They can be applied before or after the operation with the variable has occurred, depending where the ++ is located. Similarly, auto-decrements can subtract 1 from a variable and are defined as --.
C++ is an explicitly typed language, meaning different variable types cannot be simply equated or assigned to each other. To convert a data type to another data type, we must cast it to the new one. This can be done implicitly, which is handled by the C++ compiler but can sometimes produce unwanted errors, or it can be done explicitly. To cast a float to an integer we can use the static_cast operator. In the following example, we create a float variable and then cast it as an integer in a new variable. The casted data type lies within the < > of the operator. As C++ is an object oriented language, we can also declare objects in code. An object is an instance of a class , which can be thought of like a template script to describe what all instances will be like. Classes will describe what data members each instance will have and the functions that can be applied to them. Classes can be used to create an (theoretically) infinite number of instances or objects. Objects can alter the properties of their data types, but do not have a different set of code to their parent class. To declare an object of a class, we use the following format, where ClassName is the class that we are deriving the object from, and objectName is the reference name of the object (similar to a variable name). An example of a basic object is the string object. A string is a series of characters, such as text. However, a string is not a primitive data type in C++, but can be included from the string library and are used as objects in code. We can do this using the # include directive and then define the string in the same name as we do with common data type variables. As the string is a class, we can also call functions on the string to produce output data. To call a function on an instance, use dot notation followed by the function name. For example, the string function size( ) returns the number of characters contained within the string. For functions, a double parenthesis must terminate the function name. In this example, the variable size will have a value of 5, as the string contains 5 characters. We can then output the string to the console using the cout console command. Braces, { }, define the scope that variables exist within. Scope describes which regions of the code a variable can be accessed from. There are two types of variable scope: