Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
25/09/2014
Course outline
Introduction to programming in C++: Keywords, identifiers, operators
(pre & post increment & decrement operators);
variables, objects and declarations; Data types;
Control structures; Pointers and references; arrays;
Functions (definition, declaration and calls);
Object-Oriented Programming (Classes, Objects, Inheritance,
Polymorphism)
...
05-10
...
30-2
...
E
...
the name of a person and his/her monthly salary belong to
different categories of data or data types
...
In practice, there may be the need to vary the
sequences and/or repeat the statements in order select from a variety of
circumstances
...
g
...
Data processing statements: Every program should be able to process data
...
E
...
the statements
needed to process a person's name is different from that needed for his age
...
4
25/09/2014
Introduction to C++
Explanation
C++ pronounced see-plus-plus is the object oriented extension of the C programming
language
...
• #include
The #include line is a preprocessor directive to the compiler to make
available to the following program unit what is defined in the
e
...
iostream
...
Preprocessor directives are interpreted by
the processor before the program is compiled
...
The input/output stream header
program that outputs data to the screen or inputs data from the keyboard
using C++’s stream output/input
...
It is made available by the inclusion of iostream in the
complete program
...
The purpose of a namespace is to localise the names of identifiers to
avoid name collision
...
E
...
the name cout which is brought into the program by the
preprocessor directive #include
by using namespace std; without which the stmt;
cout << " Welcome to see-plus-plus \n";
would have been; std::cout << " Welcome to seeplus-plus \n";
6
• int main()
This marks the start of the program itself
...
There may be several functions in a C++ program, but they will
always start execution at the main() function
...
• The left brace, {, indicates the start of the main() function’s body
and the corresponding right brace }indicates the end of the body
...
7
8
25/09/2014
• Output statement
cout << " Welcome to see-plus-plus \n";
The above statement instructs the computer to print the string of characters
contained between the double quotation marks to the screen
...
cout stands for console output
...
It prompts the
system to move the cursor to the beginning of the next line on the console
...
• The return(0); indicates that the end of the main function is reached
and therefore terminates the program or exits the function and return control
to the operating system
...
• A semicolon is required at the end of all C++ statements, its exclusion will
result to a syntax error
...
Comments are ignored by the
C++ compiler when the program is run
...
e
...
Comments that begin with // are called single-line comments
because they terminate at the end of the current line
...
This is a c++ style
comment
...
This is a C style comment
...
9
10
• String manipulation/String data type
In c++ access to the string data type is provide in the
include file
...
these
include, determining the length of strings, characters/strings can be
inserted or deleted from existing strings, two or more string literals
can be concatenated, among others
...
• Statements
The actual C++ program resides in the statements
...
All statements are terminated by a semicolon character ‘;’
...
g
...
A sequence of statements within braces ‘{’, ‘}’ is a block
...
string
Constructor
...
Assign with string s =“word”
int s
...
A sequence of characters between double quotation marks is simply
referred to as a string, character string, or string literal in C++
...
g
...
White-space characters in strings are not ignored by the compiler
...
string s
...
s
...
int s
...
s
...
12
25/09/2014
(2)
#include
#include
using namespace std;
int main() {
string s="My name is Floro ";
string s1="and this is Sam, my buddy ";
string s2="in the Lord of the Rings";
cout <
...
length()<<" "<
insert(17,s1);
return 0;
}
The statement, cout<
...
The next output statement; cout << s
...
length()<<" "<
nb white-spaced
characters are not ignored
...
insert(17,s1); inserts string
s1 at the end of string s, which has 17 characters
...
cout << s
...
cout << s+s1
...
cout << s
...
15
14
• Variables, objects and their declarations
A variable is a symbol that represents a storage location in the computer’s
memory
...
The following example declares a double variable and assigns 45
...
(4)
#include
using namespace std;
int main(){
double x; //variable declaration
x=45
...
45
Besides declaring variables and later assigning values to them, they can be initialize as they are
declared
...
(5)
#include
/*declaring var
...
and initializing
them*/
using namespace std;
int main(){
double x; //variable declaration
x=45
...
40; //declaring and initializing
double sum=x+y; //declaring and initializing with expression
cout <
}
The output is 45
...
45+20
...
85
17
19
• Keywords and identifiers
Every program is comprised of individual syntactic elements called tokens
...
Reserved words are words that are kept by the language for special purposes
and cannot be redefined for use as variables or any other purpose
...
E
...
int, double, char
...
Identifiers
A variable name (such as x, y, number1) is any valid identifier that is not a
keyword
...
C++ is case sensitive, so a1 and A1 are different identifiers
...
The syntax of
the input stream is cin>>val1>>val2>>…>>valn;
Below is a modified version of example 5 above that allows the user to input the values of x
and y from the keyboard at runtime
...
using namespace std;
int main(){
double x; //variable declaration
double y; //declaring and initializing
cout<<“Enter the values of x and y: ”;
cin>>x>>y;
double sum=x+y; //computes the sum of x and y
cout <
}
20
25/09/2014
The program in example 6 above reads in more than one input using the
same cin stream
...
(7)
#include
//this demonstrates how the cin object is used
...
g
...
Any fractional part in integer division
is discarded or truncated (i
...
no rounding occurs)
...
This
operator can only be used with integer operands
...
Expressions in C++ must be entered in straight-line form
...
g
...
precedence of arithmetic operators
Operator(s) Operations
Order of evaluation
()
Parentheses
Evaluated first
...
If there are several, they’re evaluated from left to right
...
Arithmetic operators are some of the
simplest operators in C++
...
Arithmetic operators are binary operators, i
...
, operators that take two operands
...
g
...
These operators are summarised below:
C++ operation
C++ arithmetic operator
C++ expression
Addition
+
f+8
Subtraction
-
p-c
Multiplication
*
b*m
Division
/
x/y
Modulus
%
r%s
22
(8) #include
//this demonstrates the use of arithmetic operators
using namespace std;
int main(){
int m=13, n=10;
cout<
}
Evaluated last
...
23
24
25/09/2014
• Compound Asignment Operators
C++ provides several assignment operators for abbreviating assignment expressions
...
g
...
*=, -=, /=, and %= are all compound assignment
operators
...
--
Preincrement
Decrement b by 1, then use the new value of b in the
expression in which b resides
--
Postincrement b--
--b
Use the current value of b in the expression in which b resides,
then decrement b by 1
...
26
• Equality and Relational operators
Standard algebraic C++ equality Sample C++
equality or
or relational condition
relational operator operator
Meaning of C++ condition
Relational operators
>
>
x>y
x is greater than y
<
<
x
x is less than y
≥
>=
x>=y
x is greater than or equal to y
≤
cout<
<=
x<=y
x is less than or equal to y
Equality operators
//demonstrate preincrement
c=5; //assign 5 to c
cout<
• Increment and Decrement operators
In addition to the assignment operators, C++ also provides two unary operators
for adding 1 to or subtracting 1 from the value of a numeric variable
...
These operators are summarised below
...
in some cases, writing != as =! will not be a syntax error but most definitely will
be a logical error that has an effect at execution time
...
• Control Structures/statements
Normally, statements in a C++ program execute one after the other in the order
in which they are written
...
All that we have learnt so far, each statement executes once, in a sequence or
the other in which they appear in the program
...
(11) #include
using namespace std;
/*this program prints “Passed” if student score is
greater than or equal to 60 */
int main(){
int grade;
cout <<“Enter student’s grade : “;
cin>>grade;
if(grade>=60)
cout<<“Passed”;
}
This program used the if statement to evaluate the exams score of a student and
prints Passed if the score is greater than or equal to 60 or exits if false
...
These include the conditional/ selection statements and repetition
statements
...
• The if statement
The if selection statement is a single-selection statement that allows conditional
execution by either selecting or ignoring a single action or a single group of
actions
...
30
• The program below compare two numbers using six if statements
...
(12) #include
using namespace std;
int main(){
int number1, number2;
cout <<“Enter two integers to compare: “;
cin>>number1>>number2;
if(number1==number2)
cout<
cout<
32
25/09/2014
if(number1
cout<
cout<
cout<
Number1=3, number2=7
Number1=22, number2=12
3!=7
22!=12
3<7
22>12
3<=7
Output
22>=12
33
The above program outputs “Passed” if grade is greater than or equal to 60 and
Failed otherwise
...
It takes three operands, the operands together with the condition operator, form a
conditional expression
...
E
...
the output statement
cout<<(grade>=60 ? “Passed” : “Failed);
contain a condition statement , (grade>=60 ? “Passed” : “Failed);
that evaluates to “Passed” if the condition grade>=60 is true and to “False” if
false
...
35
• The if … else selection statement
The if … else statement is called a double-selection statement because it selects
between two different actions (or group of actions) depending upon the value of a
condition (i
...
it performs on action if the condition is true and the other if the
condition if false)
...
Compound
statements in C++ are enclosed in curly braces
...
g
...
The following program uses compound statements to sort two integers in
increasing order