Topic 01 Review of Basic C++ Concepts For Those Who Know C or Have Taken COEN243
Topic 01 Review of Basic C++ Concepts For Those Who Know C or Have Taken COEN243
Topic 01
Review of basic C++ concepts
for those who know C or have
taken COEN243
Object-Oriented Programming (OOP)
The procedural approach divides problems into
tasks to be performed
Procedural programmers think primarily in terms
of procedures, and secondarily the data that
those procedures operate on
The object oriented takes a different approach
The programmers think of programs primarily in terms of
the data types (classes),
and secondarily of the operations specific to those data
types
2
OOP Languages
There are many OOP languages, the most
important ones are: Smalltalk, C++, Java, C#
C++ is a superset of C
C++ programs are fast and efficient.
Java is a mixture of C++ and Smalltalk. It offers a
better development environment for web-based
applications
C# is Microsoft’s version of Java
3
First C++ Program
Welcome to C++!
4
std:cout (similar to printf en C)
Standard output stream object
std::cout
“Connected” to screen
<<
“namespace” std
5
Input Stream (Similar to scanf en C)
Input stream
>> (stream extraction operator)
6
1
2
// Addition program.
Adding Two
3 #include <iostream>
4
5 // function main begins program execution
Integers
6 int main()
7 {
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum; // variable in which sum will be stored
11
12 std::cout << "Enter first integer\n"; // prompt
13 std::cin >> integer1; // read an integer
14
15 std::cout << "Enter second integer\n"; // prompt
16 std::cin >> integer2; // read an integer
17
18 sum = integer1 + integer2; // assign result to sum
19
20 std::cout << "Sum is " << sum << “\n”; // print sum
21
22 return 0; // indicate that program ended successfully
23
24 } // end function main
Example 2
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
8
Complex Conditions
Logical Operators:
Logical AND: &&
Logical OR: ||
Logical Negation: !
Assume a = 10 and b= 5
Evaluate the following expressions:
(a >= 6) && (b<10)
(a >= 6) !! (b<10)
!(a >= 6) && (b<10)
9
The Repetition Structures
While loop
int product = 2;
while ( product <= 1000 )
product = 2 * product;
You need to put braces if you have more than one statement in the while loop
For loop
for( int counter = 1; counter <= 10; counter++ )
cout << counter << endl;
10
Introduction to Arrays
11
Using Arrays
Name of array (Note
Array of 12 elements
c[2] 0
variables c[3] 72
12
Declaring Arrays
Standard C-style array
Cannot grow or shrink at run time
When declaring arrays, the following should be specified
Name
Type of array (e.g., int, char, bool, etc.)
Size of the array (i.e., number of elements)
type arrayName[arraySize];
E.g.
int c[10]; // array of 10 integers
float d[3284]; // array of 3284 floats
13
Declaring Arrays (cont.)
Standard C-style array (cont.)
Using a for loop
int c[n];
Initializer list
Specify each element when array declared
14
Declaring Arrays (cont.)
Standard C++11-style array
The std::array
Replacement for the standard C-style array
Cannot grow or shrink at run time
When declaring arrays, the following should be specified
Name
Type of array (e.g., int, char, bool, etc.)
Size of the array (i.e., number of elements)
E.g.
array<int, 10> c;
15
Initializing Arrays
Standard C++11-style array
Using a for loop
array<int, n> c;
16
Initializing Arrays (cont.)
According to C++ standard
size_t
Represents an unsigned integral type
Recommended for any variable representing array’s size or array’s
subscripts
Defined in header file <cstddef> and included by various other
headers
arrayName.size()
Returns the number of elements in the array
To use it, include the following preprocessor directive:
#include <array>
17
// Example 1
// Initializing an array's elements to zeros and printing the array.
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
int main() {
array<int, 5> n; // n is an array of 5 int values
int main() {
array<int, 5> n{32, 27, 64, 18, 95}; // list initializer
19
Functions
Programs can be quite large, we need to
break them down into smaller functions.
20
User-Defined Functions
Format for function definition
return-value-type function-name( parameter-list )
{
declarations and statements
}
Parameter list
Comma separated list of arguments
Data type needed for each argument
21
22
1 // Example 1
2 // Creating and using a programmer-defined function.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 int square( int ); // function prototype
9
10 int main()
11 {
12 // loop 10 times and calculate and output
13 // square of x each time
14 for ( int x = 1; x <= 10; x++ )
15 cout << square( x ) << " "; // function call
16
17 cout << endl;
18
19 return 0; // indicates successful termination
20
21 } // end main
22
23 // square function definition returns square of an integer
24 int square( int y ) // y is a copy of argument to function
25 {
26 return y * y; // returns square of y as an int
27
28 } // end function square
1 4 9 16 25 36 49 64 81 100
Parameter Passing
Call by value
Copy of data passed to function
Call by reference
Function can directly access data
23
Using Strings: The Class string
C++ class that represents strings
string declaration and initialization
string s1( "Hello" );
string s2( 8, 'x' );
8 'x' characters
string month = "March"
s1 and s2 are objects of the class
string
You need to include <string>
24
Using Strings
length member function: s1.length()
Use [] to access individual characters: s1[0]
0 to length-1
Stream extraction
cin >> stringObject;
Assignment
s2 = s1;
Same as s2 = s1;
myString.assign(s, start, N);
25
Review of Pointers
The pointer is one of the most important features of
C++
26
Declaring Pointers
A pointer to a variable x is a variable that stores the
memory address of the variable x
For example, a pointer to an integer variable is declared as
follows:
27
The Address Operator (&)
The address operator & is used to assign an address of
a variable to a pointer
int x;
int *p; p x
0011F
x = 5; 0011F 5
int p = &x;
28
The indirection operator (*)
The * operator is used to return the content of the
memory location a pointer points to
int x = 5;
p = &x;
y = *p; // 5 is now assigned to y
29
// Example 1: Pointers Declaration
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x = 5;
int y = 7;
int *p; // declares a pointer to an integer
cout << "x is stored at " << p <<" and its value is: "
cout << *p << endl;
cout << "y is stored at " << p <<" and its value is: "
cout << *p << endl;
return 0;
30
// Example 2: Pointers handling
// This program shows how to use the dereferencing operator
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x = 5;
int y = 3;
int z;
cout << x << " + " << y << " = " << z << endl;
return 0;
5 + 3 = 8
31