0% found this document useful (0 votes)
27 views14 pages

Copro 1 Week 6

Uploaded by

olimpoarianekaye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

Copro 1 Week 6

Uploaded by

olimpoarianekaye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

6 Basic Concept

C++ Programming

What should I expect?

 Know the basic concept of C++ programming;


 Understand the Assignment and Increment Operators; and
 Identify variables and know its corresponding data type.

What do I know?

Directions: Read the questions carefully and choose the correct option.

1. Which two statements are true for variables in C++?

a. Variables do not have names

b. Variables must have a data type

c. Variables are pre-processor directives

d. Variables must be declared before their use

2. Which of the following is true?

a. Comments are ignored by the compiler

b. Single line comment starts with an * (asterisk)

c. Comments are used to confuse programmers

3. C++ is a:

a. General purpose programming

b. Client-side scripting language

c. Movie making program

4. Defines the standard stream objects (input and output data).

a. I/O

b. <iostream>

c. <conio.h>

Global Success through Academic Excellence


COPRO 6 1
What do I remember?

Computers are some of the most versatile tools that we have available. They are capable
of performing stunning feats of computation, they allow information to be exchanged easily
regardless of their physical location, they simplify many every-day tasks, and they allow us to
automate many processes that would be tedious or boring to perform otherwise.
However, computers are not "intelligent" as we are. They have to be told in no uncertain
terms exactly what they're supposed to do, and their native languages are quite unlike anything
we speak. Thus, there's a formidable language barrier between a person who wishes a
computer to do something, and the computer that typically requires instructions in its native
language, machine code, to do anything.
So far, computers cannot figure out what they are supposed to do on their own, and thus they
rely on programs which we create, which are sets of instructions that the computer can
understand and follow.

What do I need to know?

What is C++?
• C++ is a general-purpose programming language.
• C++ is used to create computer programs. Anything from art applications, music players
and even video games!
• C++ is an object-oriented programming language which gives a clear structure to
programs and allows code to be reused, lowering development costs.
• C++ is a cross-platform language that can be used to create high-performance
applications.
• C++ was developed by Bjarne Stroustrup, as an extension to the C language.

Your First C++ Program


A C++ program is a collection of commands or statements.

Below is a simple code that has "Hello world!" as its output.

Global Success through Academic Excellence


COPRO 6 2
Let's break down the parts of the code.

C++ offers various headers, each of which contains information needed for programs to work
properly. This particular program calls for the header <iostream>.
The number sign (#) at the beginning of a line targets the compiler's pre-processor. In this
case, #include tells the pre-processor to include the <iostream> header.

The C++ compiler ignores blank lines.


In general, blank lines serve to improve the code's readability and structure.

In our code, the line using namespace std; tells the compiler to use
the std (standard) namespace.

Main
Program execution begins with the main
function, int main().

Global Success through Academic Excellence


COPRO 6 3
Curly brackets { } indicate the beginning and end of a function, which can also be called the
function's body. The information inside the brackets indicates what the function does when
executed.

The next line, cout << "Hello world!"; results in the display of "Hello world!" to the
screen.#include<iostream>

In C++, streams are used to perform input and output


operations.

In most program environments, the standard default output destination is the screen. In C+
+, cout is the stream object used to access it.
cout is used in combination with the insertion operator. Write the insertion operator as << to
insert the data that comes after it into the stream that comes before.

Statements

A block is a set of logically connected statements, surrounded by opening and closing curly
braces.
For example:

Return .

Global Success through Academic Excellence


COPRO 6 4
The last instruction in the program is the return statement. The line return 0; terminates
the main() function and causes it to return the value 0 to the calling process. A non-zero value
(usually of 1) signals abnormal termination

Getting the Tools


You need both of the following components to build C++ programs.
1. Integrated Development Environment (IDE) : Provides tools for writing source code. Any text
editor can be used as an IDE.
2. Compiler: Compiles source code into the final executable program. There are a number of C+
+ compilers available. The most frequently used and free available compiler is the GNU C/C+
+ compiler.
Desktop Application
Various C++ IDEs and compilers are available. We'll use a free tool called BloodshedSoftware ,
which includes both an IDE and a compiler, and is available for Windows.
To download Bloodshed Dev C++, go to https://www.bloodshed.net/devcpp.html , Click
the Downloads link, and choose " Dev-C++ 5.0 beta 9.2 (4.9.9.2) ( 9.0 MB) with
Mingw/GCC 3.4.2".
Choose your OS and download the setup file, which includes the C++ compiler (For Windows,
it's the one with mingw in the name).
Mobile Application
cppDroid is simple C/C++ IDE focused on learning programming languages and libraries.

To download cppDroid on your mobile phone, visit Google Play Store and search for CppDroid –
C/C++ IDE

To create a project, open DevC++ and click " Create a new source file " (or File->New->Source
File).

Global Success through Academic Excellence


COPRO 6 5
To run the program, click on the “ Execute" in the menu bar to compile and run the program or
press F9 on the keyboard.

A console window will open and display program output.

conio. h header used in c programming contains functions


for console input/output. ... h file is provided by Borland
turbo c compiler and GCC compiler doesn't support it.

Global Success through Academic Excellence


COPRO 6 6
Beginner c/c++ programmers and some books use this file but it is not recommended to use it
in your software/application.

The getch() Function in C and C++ Return Value: This function returns the character read from
the keyboard.

You can add multiple insertion operators after cout.

Result:

Printing text, Comments and Variables

New Line

The cout operator does not insert a line


break at the end of the output.One way
to print two lines is to use
the endl manipulator, which will put in a
line break.

The endl manipulator moves down to a new line to print the second text.

Global Success through Academic Excellence


COPRO 6 7
New Lines
The new line character \n can be used as an
alternative to endl.
The backslash (\) is called an escape character,
and indicates a "special" character.

Comments
Comments are explanatory statements that you can include in the C++ code to explain
what the code is doing.
The compiler ignores everything that appears in the comment, so none of that information
shows in the result.

A comment beginning with two slashes (//) is called a single-line comment. The slashes tell the
compiler to ignore everything that follows, until the end of the line.

When the code is compiled, it will ignore the //


prints "Hello world" statement and will produce
the following result:

Using Comments
Comments can be written anywhere, and can be
repeated any number of times throughout the
code.

Global Success through Academic Excellence


COPRO 6 8
Within a comment marked with /* and */, // characters have no special meaning, and vice
versa. This allows you to "nest" one comment type within the other.

Variables
Creating a variable reserves a memory location, or a space in memory for storing values. The
compiler requires that you provide a data type for each variable you declare.
C++ offer a rich assortment of built-in as well as user defined data types.

Working with Variables and Datatypes

Integer, a built-in type, represents a whole number value. Define integer using the
keyword int.C++ requires that you specify the type and the identifier for each variable
defined.An identifier is a name for a variable, function, class, module, or any other user-
defined item. An identifier starts with a letter (A-Z or a-z) or an underscore (_), followed by
additional letters, underscores, and digits (0 to 9).

Now, let's assign a value to the variable and print it.

Printing text, Comments and Variables

Let's create a program to calculate and print the sum of two integers.

Global Success through Academic Excellence


COPRO 6 9
Working with Variables and Datatypes

User Input
To enable the user to input a value, use cin in combination with the extraction operator ( >>).
The variable containing the extracted data follows the operator.
The following example shows how to accept user input and store it in the num variable:

Let's create a program that accepts the input of two numbers and prints their sum.

Global Success through Academic Excellence


COPRO 6 10
Basic Arithmetic, Assignment and Operators

Arithmetic Operators

C++ supports these arithmetic operators.

Addition
The addition operator adds its operands together.

Subtraction
The subtraction operator subtracts one operand from the other.

Multiplication
The multiplication operator multiplies its operands.

Division
The division operator divides the first operand by the second. Any remainder is dropped in
order to return an integer value.

Global Success through Academic Excellence


COPRO 6 11
If one or both of the operands are floating point values, the division operator performs floating
point division.

Modulus
The modulus operator (%) is informally known as the remainder operator because it returns the
remainder after an integer division.

Operator Precedence
Operator precedence determines the grouping of terms in an expression, which affects
how an expression is evaluated. Certain operators take higher precedence over others; for
example, the multiplication operator has higher precedence over the addition operator.

The program above evaluates 2*2 first, and then adds the result to 5.
As in mathematics, using parentheses alters operator precedence.

Assignment Operators
The simple assignment operator (=) assigns the right side to the left side.C++ provides
shorthand operators that have the capability of performing an operation and an assignment at
the same time.

The same shorthand syntax


applies to the multiplication, division, and modulus operators.

Global Success through Academic Excellence


COPRO 6 12
Increment Operator
The increment operator is used to increase an integer's value by one, and is a commonly
used C++ operator.

Decrement Operator
The decrement operator (--) works in much the same way as the increment operator, but
instead of increasing the value, it decreases it by one.

The increment and decrement operator has two forms, prefix and postfix.
Prefix increments the value, and then proceeds with the expression.
Postfix evaluates the expression and then performs the incrementing.

Global Success through Academic Excellence


COPRO 6 13
What should I remember?
C++ is a powerful general-purpose programming language. It can be used to develop
operating systems, browsers, games, and so on. C++ supports different ways of programming
like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as
flexible.
In programming, a variable is a container (storage area) to hold data. To indicate the
storage area, each variable should be given a unique name (identifier). Literals are data used
for representing fixed values. They can be used directly in the code. In C++ also we learned,
data types are declarations for variables. This determines the type and size of data associated
with variables. We discussed also the purpose of comments. Comments are hints that a
programmer can add to make their code easier to read and understand. They are completely
ignored by C++ compilers.

References:

Singh, (2017) Learn C++ Programming with examples


Retrieved from https://beginnersbook.com/2017/08/c-plus-plus-tutorial-for-beginners

Bjarne Stroustrup , (2008) Programming: Pri nc iples and Practice Using C++

Global Success through Academic Excellence


COPRO 6 14

You might also like