Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Programming Notes: Variables, Data Types, and I/O, Study notes of Programming Languages

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

  • How do you declare and initialize a constant variable in C++?
  • What is the difference between global and local variables in C++?
  • What are the standard arithmetic rules for mathematical operations in C++?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

anarghya
anarghya 🇺🇸

4.2

(21)

255 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
FIT1048 Notes
2017 GUIDE ON C++ PROGRAMMING
C++ VERSION 11 USED!
Contents!
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
PAGE 1
pf3
pf4
pf5

Partial preview of the text

Download C++ Programming Notes: Variables, Data Types, and I/O and more Study notes Programming Languages in PDF only on Docsity!

FIT1048 Notes

2017 GUIDE ON C++ PROGRAMMING

C++ VERSION 11 USED

Contents

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

Introduction to C++

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.

  1. This is known as comment. A comment is some text written in the program that is never executed. It allows the programmer to provide information about the code they are writing. It is often visible in a different colour to the programmer, and it is useful to comment frequently throughout the code. A comment always follows two forward slashes ( // ).
  2. The # at the start of a line indicates that this line needs to be interpreted before the rest of the code can be. The command used here is the include directive. Includes allows the script to utilise functions and properties of external libraries or source code not usually compiled within the code. The name of the library is indicated within < and >. The iostream library allows the script to access input and output functions to the console.
  3. Blank lines are allowed in C++. There are ignored by the compiler and having empty lines is useful for keeping code clean and readable.
  4. The int main( ) defines the main function that executes when the program first starts. All code that lies within this function will run on startup.
  5. The braces are used to define the start and end of code blocks. Code blocks are used to indicate what lies within specific functions and classes. In this case, braces are used to start and end the main function. They also define the scope of the variables defined within.
  6. This line executes the command to print text to the console. std describes the namespace, where the function cout exists. This function outputs the result of the string following the function to the console window. Unlike usual functions, to input a string, two greater-than signs are used: << string. It is also important to terminate all executable code with a semi

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:

  • Global variables^ are located in the top of the document or in the header file and are able to be accessed by the entire class. - Local variables^ are located in code blocks, within the braces, and can only be referenced from code within the same set of braces. It is important to declare variables in the level of scope which is most lowest level possible for that particular variable. This prevents from unwanted classes accessing data. Here is an example to