0% found this document useful (0 votes)
11 views

C ++ Commands

1. A compiler is used to translate high-level languages like C++ into machine language. An IDE or integrated development environment provides tools like a text editor and compiler in one program. 2. Functions allow programmers to organize code into reusable units. A function contains a name, return type, parameters, and a body of code. Functions are defined with a signature, prototype, and definition. 3. Functions are declared with a prototype specifying the return type and parameters. The definition implements the body of code. Functions can be called from other parts of a program by passing arguments matching the parameters.

Uploaded by

burrellstef
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)
11 views

C ++ Commands

1. A compiler is used to translate high-level languages like C++ into machine language. An IDE or integrated development environment provides tools like a text editor and compiler in one program. 2. Functions allow programmers to organize code into reusable units. A function contains a name, return type, parameters, and a body of code. Functions are defined with a signature, prototype, and definition. 3. Functions are declared with a prototype specifying the return type and parameters. The definition implements the body of code. Functions can be called from other parts of a program by passing arguments matching the parameters.

Uploaded by

burrellstef
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/ 10

C++ Commands

Compiler Use to translate high level languages into


machine languages
IDE (Integrated Development Environment) Integrates development tools, including a text
editor, and tools to compile programs
- The easiest way to compile a program
Code Blocks To start a new code project:
- File → New → Empty File
endl causes the insertion point to move to the next
line
#include <iostream> allows us to use (the predefined object) cout to
generate output and (the manipulator) endl.
using namespace std allows you to use the cout and endl without the
prefix::
- if statement is not there you must write as:
std::cout
std::endl
<< stream insertion operator
main() header of the function main
- if a C++ program only has one function it must
be named “main”
variable a memory location whose contents can be
changed
Function - is a packaging of C++ statements into a unit
which is called as needed from elsewhere in the
program
- every C++ program has at least one function (it
is called main)
- functions can be called many times in the same
or different programs
- many commonly used functions are placed in
libraries and accessed through their header files
- a function has the following parts:
 name: any valid c++ identifier
 return value type: any data type or class
name. void is used to denote that this
function does not return a value
 parameter list: an ordered list giving the
types of the parameters
 body: a compound statement, inside \
{~\}, containing the program statements
that implement the function
- ex1:
void foo(); // declaration of foo
void bar(); // declaration of bar

void bar() // definition of bar.


{
cout << "I think I hear someone calling me." <<
endl;
}

void foo() // definition of foo.


{
cout << "Listen!" << endl;
bar(); // Notice that bar() has already been
declared
}
- ex2:
void bar() // declaration and definition of bar.
{
cout << "I think I hear someone calling me." <<
endl;
}

void foo() // declaration and definition of foo.


{
cout << "Listen!" << endl;
bar(); // Notice that bar() has already been
declared
}
- ex3:
void foo(); // declaration of foo
void bar(); // declaration of bar

void foo() // definition of foo.


{
cout << "Listen!" << endl;
bar(); // Notice that bar() has already been
declared
}

void bar() // definition of bar.


{
cout << "I think I hear someone calling me." <<
endl;
}
- ex4(is not legal):
void foo() // declaration and definition of foo.
{
cout << "Listen!" << endl;
bar(); // Oops! bar() has not yet been
declared.
}
void bar() // declaration and definition of bar.
{
cout << "I think I hear someone calling me." <<
endl;
}
Function prototype - consists of a return-type function-name and
parameter list
- We write the function’s prototype into our
code to declare the function
- Ex:
void foo(); // declaration of foo
int bar(int i); // declaration of bar
- must end with a semi-colon
- Every function prototype should have a
corresponding function definition elsewhere in
the program, possibly in a separate program file
or library.

Header file - allow functions to be defined separately & put


into libraries for common use
- consist mainly of function prototypes
(including member functions of classes)
function signature - used by the compiler for handling function
overload
- consists of the function-name & parameter-list
Overloading a function - means using the same function name for two
or more different function definitions
function definition - consists of all the elements of a function:
name, return type, parameter list, & body
- ex:
void foo(); // declaration of foo

// definition of foo:
void foo()
{
cout << "Listen!" << endl;
int i = bar(42);
}
- All definitions are also declarations
- A function definition must NOT contain a semi-
colon (‘;’) after the parameter-list
- function definition is followed by a compound
statement containing the function body. It is the
function body which defines the function.
Syntax for Functions // Function definition
return-value-type function-name( named-
parameter-list)
{
declarations-and-statements
}

// Function prototype
return-value-type function-name( parameter-
list);

// Function signature
function-name( parameter-list)

// parameter-list is a comma separated ordered


list of data types
// named-parameter-list is a comma separated
ordered list of data types,
// parameter is given a name
// The names of the parameters are only
meaningful within the parameter
// list and function definition.
// That is, you can use any name without
worrying if another program has
// also used that name
// the scope of a parameter name is limited to
the function definition.

Calling Functions parameter: an alias name for the actual


argument that will be used when the function is
called
 allows the same function to be used
anywhere in a program, at more than
one location of the program or In the
different programs without worrying
about naming conflicts
argument: the expression that is passed into the
function when you write the call
 the value of the argument is assigned to
the parameter at the time the function is
called
 For call by value, the value of the
argument is copied into the parameter.
 For call by reference, the address of the
argument is copied into the parameter.
ex:
void myFunction(int anInteger, float aFloat, char
aChar)
// anInteger, aFLoat, aChar are the aliases
(parameters) for the eventual
// arguments
{
//. . .
}
int main( )
{
int thisInt = 3; char thisChar = 'a'; float
thisFloat = 3.4;
myFunction(thisInt, thisFloat, thisChar);
// thisInt, thisFloat, thisChar are the
arguments
myFunction(3, 4.5, 'q'); // can pass in
constants as arguments too
}
Calling Functions Syntax // for parameters in a function definition
[type-modifier] type-descriptor [&] parameter-
name
// parameters in a function prototype can drop
the parameter-name

// no special syntax for argument- either use the


variable name or a constant

Parameter Passing (call by value or call by ref.) call by value:


- passing of arguments into functions whereby a
copy is made of the argument and assigned to
the parameter. (a copy parameter)
- Because it is a copy, any changes to the
parameter will not effect the value of the
original argument.
- In C++, the default method of passing
arguments is by value (except arrays).
- ex:
void myFunction(int someInt)
{
someInt++; // change value of
someInt
}
int main( )
{
int myInt = 3;
myFunction(myInt);
cout << "After function call: " <<
myInt << endl;
}

**Use call by value for int, float and char**


- Use call-by-value or copy parameters on small
data types (int, float, double, etc.) and on arrays,
which aren’t small but are actually passed as a
single address.
call by reference:
- the passing of arguments into functions
whereby the address of the argument is
assigned to the parameter.
- Any changes to the parameter will also change
the value of the original argument.
- Such parameters can be used as input
parameters or output parameters or combined
input-output parameters.
- Since in C++ the default method of passing
arguments is by value you need to tell the
compiler that you are passing by reference by
using “&” after the parameter type
- ex:
void addOne (int& counter)
{
counter++;
}
- A reference parameter is efficient because only
the address of the argument has to passed to
the function
- Adding the const modifier to a reference
parameter modifies the call by reference to call
by const reference. This tells the compiler not to
allow the called function change the value.
- ex:
void print (const int& value)
{
cout << value << endl;
}
- Use const reference parameters for some of
the larger data types that you will be creating
once we being to study structured data.
- Output parameters must be passed by (non-
const) reference.

Pre-defined function already written and is provided as part of the


system
Programming language a set of rules, symbols, and special words
comments (///) used to give a clear understanding of your code
to readers and other people who examine your
code
token the smallest individual unit of a program written
in any language. Divided into special symbols,
word symbols, and identifiers.
-Some examples:
+ - * / (1st row for mathematical symbols)
. ; ? , (2nd row punctuation marks from English
grammar)
<= != = = >= (3rd row is tokens made up of 2
characters)
Reserved words (Keywords) - tokens that are reserved word symbols
- reserved word symbols include:
 Int
 float
 double
 char
 const
 void
 return
Identifiers name of things that appear in programs, such as
variables, constants, and functions. All
identifiers must obey C++’S rules for identifiers.
Identifiers:
- consists of letters, digits, and the underscore
character (_) and must begin with a letter or
underscore
- also there can be no space between words
ex: cout and cin are both predefined identifiers.
-you can make user-defined identifiers as well
Whitespaces - used to separate special symbols, reserved
words, and identifiers
- include blank, tabs, and newline characters.
Data Types a set of values together with a set of allowed
operations
- C++ had three data types:
 simple data type
 structured data type
 pointers
Simple Data Types is the fundamental data type in C++ because it
becomes a building block for the structured data
types.
- 3 categories of simple data types:
 Integral: deals with integers, or numbers
without a decimal point
 Floating point: deals with decimal points
 Enumeration: user-defined data types
“int” data type are numbers such as: -6828, -67, 0, 78, 36782,
+782
- Rules to follow:
 Positive integers do t need a + sign in
front of them.
 No commas are used within an integer.
“bool” data type has only two values: true and false
- this is called the logical (Boolean) values
-the main purpose of the data type is to
manipulate logical (Boolean) expressions
“char” data type mainly used to represent single characters,
which are letters, digits, and special symbols.
- can enclose each character within single
quotation marks
- ex: ‘A’, ‘a’, ‘0’, ‘*’, ‘+’, ‘$’, ‘&’, ‘ ‘
Floating-Point data types allows C++ to deal with decimal points.
- Floating-point notation: used to represent
decimal numbers in scientific notation
- the letter ‘E’ stands for exponent
 Ex: 7.592400E2
*Three data types to manipulate decimal
numbers: float, double, & long double
float used to represent any decimal number between
-3.4 * 10^38 & 3.4 * 10^38 (4 bytes of memory)
- Max # of significant figures is 6 or 7
- shows numbers in less accuracy than double.
double used to represent any decimal number between
-1.7 * 10^308 & 1.7 * 10^308 (8 bytes of
memory)
- Max # of significant figures is 15
**precision: the max # of significant digits**
- Offers more precision than float which is
important
Declare a variable we specify the name of the variable & specify
what type of data a variable can store.
-ex:
 int counter;
 double interestRate;
 char grade;
Assignment Statement Destination = expression;
‘=’ is the assignment operator
- means the expression should be evaluated, and
the resulting value stored at the destination
named on the left
- Ex:
Cat = 3.52;
Pi = 3.14;
Areaofcircle = pi * r * r;
**When using variables on either side of an
assignment, we need to declare variable’s first**
Ex:
double Cat;
double pi;
double AreaOfCircle;
**We can combine the assignments and
variables & create initialization statements**
Ex:
double Cat = 3.522;
double pi = 3.14;
double Areaofcircle = pi * r * r;
Input/Output - The process of moving info into & out of a
program
- Input: can come from the keyboard, mouse, pr
network
- Output: can do the computer screen, file, or
network
Operators - Relational, equality & logical operators are
used to form logic expressions
Relational operators Symbols are:
<, <=, >, >=
Equality operators Symbols are:
==, !=
Given two #’s, a & b, the table to the right lists Example Meaning
ex. of logic expressions formed using the a == b a is equal to b
relational and equality operators a<b a is less than b
a>b a is greater than b
a <= b a is less than or equal
to b
a >= b a is greater than or
equal to b
a != b a is not equal to b
Logical operators Symbols are:
&&(AND) and ||(OR)
Given #’s a, b, c and d. the table to the right lists Example Meaning
ex. of combining relational and logical operators (a = = b) && (c < d) true if a is equal b AND
c is less than d
a = = b && c < d same as the above (see
precedence rules)
(a > b) || (c != d) true if a is greater than
b OR if c is not equal to
d
a > b || c != d same as the above (see
precedence)
Precedence Rules - precedence rules let the compiler decide which
interpretation to take.
Rules:
- do relational operators first
- then equality
- then the logical AND (&&)
- then the logical OR (||)
Ex:
a > b && b == c || c < d
Increment operator (++) - commonly used in loops or iterators
- ex.Increment:
int a = 10;
a = a+1; (is the same as ++a or a++)
++ tells compiler to add 1.
- ex.Decrement:
int a =10;
a = a – 1; (is the same as –a or a--)
-- tells compiler to subtract 1.
- Prefix/Postfix:
 Prefix: ++a or –a
 Postfix: a++ or a--

Decrement operator (--)

You might also like