Professional Documents
Culture Documents
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Applications of C++:
C++ finds varied usage in applications such as:
• Operating Systems & Systems Programming. e.g. Linux-based OS
(Ubuntu etc.)
• Browsers (Chrome & Firefox)
• Graphics & Game engines (Photoshop, Blender, Unreal-Engine)
• Database Engines (MySQL, MongoDB, Redis etc.)
• Cloud/Distributed Systems
Here are some key points to keep in mind while working with C++:
1. Object-Oriented Programming: C++ is an object-oriented
programming language, which means that it allows you to define
classes and objects to model real-world entities and their
behavior.
2. Strong Type System: C++ has a strong type system, which means
that variables have a specific type and that type must be
respected in all operations performed on that variable.
3. Low-level Access: C++ provides low-level access to system
resources, which makes it a suitable choice for system
programming and writing efficient code.
2 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Advantages of C++:
1. Performance: C++ is a compiled language, which means that its
code is compiled into machine-readable code, making it one of the
fastest programming languages.
2. Object-Oriented Programming: C++ supports object-oriented
programming, which makes it easier to write and maintain large,
complex applications.
3. Standard Template Library (STL): The STL provides a wide range of
algorithms and data structures for working with data, making it
easier to write efficient and effective code.
4. Machine Independent: C++ is not tied to any hardware or
processor. If the compiler compiles the program in the system, it
will be able to run no matter what the hardware is.
5. Large Community: C++ has a large, active community of developers
and users, providing a wealth of resources and support for
learning and using the language.
Disadvantages of C++:
1. Steep Learning Curve: C++ can be challenging to learn, especially
for beginners, due to its complexity and the number of concepts
that need to be understood.
2. Verbose Syntax: C++ has a verbose syntax, which can make code
longer and more difficult to read and maintain.
3. Error-Prone: C++ provides low-level access to system resources,
which can lead to subtle errors that are difficult to detect and
fix.
3 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return 0;
}
Output:
#include
• This is a preprocessor directive. The #include directive tells
the compiler to include the content of a file in the source code.
• For example, #include<iostream> tells the compiler to include the
standard iostream file which contains declarations of all the
standard input/output library functions.
using namespace std
• This is used to import the entity of the std namespace into the
current namespace of the program. The statement using namespace
std is generally considered a bad practice. When we import a
namespace we are essentially pulling all type definitions into
the current scope.
• The std namespace is huge. The alternative to this statement is
to specify the namespace to which the identifier belongs using
4 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cout<<“Hello World”;
• std::cout is an instance of the std::ostream class, that is used
to display output on the screen. Everything followed by the
character << in double quotes ” ” is displayed on the output
device. The semi-colon character at the end of the statement is
used to indicate that the statement is ending there.
return 0
• This statement is used to return a value from a function and
indicates the finishing of a function. This statement is
basically used in functions to return the results of the
operations performed by a function.
Indentation
• As you can see the cout and the return statement have been
indented or moved to the right side. This is done to make the
code more readable. We must always use indentations and comments
to make the code more readable.
Important Points
1. Always include the necessary header files for the smooth
execution of functions. For example, <iostream> must be included
to use std::cin and std::cout.
2. The execution of code begins from the main() function.
3. It is a good practice to use Indentation and comments in programs
for easy understanding.
4. cout is used to print statements and cin is used to take inputs.
Omitting Namespace
• You might see some C++ programs that runs without the standard
namespace library. The using namespace std line can be omitted
and replaced with the std keyword, followed by the :: operator
for some objects.
• Syntax:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
5 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int main() {
cout << "Hello World!";
return 0;
}
• You can add as many cout objects as you want. However, note that
it does not insert a new line at the end of the output.
• Syntax
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
New Lines
• To insert a new line, you can use the \n character
• Syntax
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
• Tip: Two \n characters after each other will create a blank line.
• Another way to insert a new line, is with the endl manipulator
• Syntax:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
• Both \n and endl are used to break lines. However, \n is most used.
6 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C++ Comments
• Comments can be used to explain C++ code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code. Comments can be singled-lined or multi-lined.
Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the
compiler (will not be executed).
• Syntax:
// This is a comment
cout << "Hello World!";
C++ Variables
• Variables are containers for storing data values.
• In C++, there are different types of variables (defined with
different keywords), for example:
➢ int - stores integers (whole numbers), without decimals, such
as 123 or -123
➢ double - stores floating point numbers, with decimals, such as
19.99 or -19.99
➢ char - stores single characters, such as 'a' or 'B'. Char
values are surrounded by single quotes
➢ string - stores text, such as "Hello World". String values are
surrounded by double quotes
➢ bool - stores values with two states: true or false
7 | Page
COMPUTER PROGRAMMING PART 3
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
int x = 5, y = 6, z = 50;
cout << x + y + z;
int x, y, z;
x = y = z = 50;
cout << x + y + z;
8 | Page