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

Topic 01 Review of Basic C++ Concepts For Those Who Know C or Have Taken COEN243

1. The document discusses programming concepts for COEN 244 including object-oriented programming, C++ as an OOP language, and examples of basic C++ programs such as outputting text, taking user input, and performing calculations. 2. Key concepts covered include classes, objects, namespaces, streams for input/output, variables, data types, functions, if/else statements, loops, and arrays. 3. Examples demonstrate basic syntax for declaring variables, taking user input, writing output, performing calculations, and using control structures like if/else and loops.

Uploaded by

Mohamed sarris
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Topic 01 Review of Basic C++ Concepts For Those Who Know C or Have Taken COEN243

1. The document discusses programming concepts for COEN 244 including object-oriented programming, C++ as an OOP language, and examples of basic C++ programs such as outputting text, taking user input, and performing calculations. 2. Key concepts covered include classes, objects, namespaces, streams for input/output, variables, data types, functions, if/else statements, loops, and arrays. 3. Examples demonstrate basic syntax for declaring variables, taking user input, writing output, performing calculations, and using control structures like if/else and loops.

Uploaded by

Mohamed sarris
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

COEN 243

COEN 244 – PROGRAMMING METHODOLOGY II


WINTER 2007

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

1 // A first program in C++.


3 #include <iostream>
4
5 // function main begins program execution
6 int main()
7 {
8 std::cout << "Welcome to C++!\n";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Welcome to C++!

4
std:cout (similar to printf en C)
 Standard output stream object
 std::cout

 “Connected” to screen

 <<

 Stream insertion operator


 Value to right (right operand) inserted into output
stream
 Namespace
 std:: specifies using name that belongs to

“namespace” std

5
Input Stream (Similar to scanf en C)

 Input stream
 >> (stream extraction operator)

 Used with std::cin


 Waits for user to input value, then press Enter (Return)
key
 Stores value in variable to right of 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

Enter first integer


45
Enter second integer
72
Sum is 117
7
Selection Structure: if-else

 Statement to express conditions


 Example 1
if ( grade >= 60 )
cout << "Passed";

 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

 A subscripted array name is an that


this
all elements of
array have the
same name, c)
lvalue
 Used on the left side of an assignment c[0] -45
c[1] 6
 Array elements are like other

Array of 12 elements
c[2] 0
variables c[3] 72

 Assignment and printing of an c[4]


c[5]
1543
-89
integer array c c[6] 0

c[0] = - 45; c[7] 62


c[8] -3
cout << c[0] + c[10]; c[9] 1
c[10] 6453
 Can perform operations inside c[11] 78
subscript
c[5 – 2] same as c[3] Position number of the
element within array c

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];

for (int i = 0; i < n; i++)

c[i] = 0; // set element at location i to 0

 Initializer list
 Specify each element when array declared

int c[5] = {1, 2, 3, 4, 5};

 If not enough initializers, rightmost elements set to 0

 If too many  syntax error

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)

array<type, arraySize> arrayName;

 E.g.
array<int, 10> c;

15
Initializing Arrays
 Standard C++11-style array
 Using a for loop
array<int, n> c;

for (size_t i{0}; i < c.size(); ++i)

c[i] = 0; // set element at location i to 0

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

// initialize elements of array n to 0


for (size_t i{0}; i < n.size(); ++i) {
n[i] = 0; // set element at location i to 0
}

cout << "Element" << setw(10) << "Value" << endl;

// output each array element's value


for (size_t j{0}; j < n.size(); ++j) {
cout << setw(7) << j << setw(10) << n[j] << endl;
}
}
Element Value
0 0
1 0
2 0
3 0
4 0
18
// Example 2
// Initializing an array in a declaration.
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;

int main() {
array<int, 5> n{32, 27, 64, 18, 95}; // list initializer

cout << "Element" << setw(10) << "Value" << endl;

// output each array element's value


for (size_t i{0}; i < n.size(); ++i) {
cout << setw(7) << i << setw(10) << n[i] << endl;
}
Element Value
} 0 32
1 27
2 64
3 18
4 95

19
Functions
 Programs can be quite large, we need to
break them down into smaller functions.

 Functions call other functions to complete


specific tasks.

 Divide and Conquer principle: Divide the


problem into smaller pieces, you conquer the
complexity of the problem.

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

 If no arguments, use void or leave blank


 Return-value-type
 Data type of result returned (use void if nothing
returned)

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

 Changes to copy do not change original

 Prevent unwanted side effects

 Call by reference
 Function can directly access data

 Changes affect original variables using during the call

 Use & after data type in prototype

 void myFunction( int & )


 Read “data is a reference to an int”

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;

 Makes a separate copy


 s2.assign(s1);

 Same as s2 = s1;
 myString.assign(s, start, N);

 Copies N characters from s, beginning at index


start
 Individual characters: s2[0] = s3[2];

25
Review of Pointers
 The pointer is one of the most important features of
C++

 Pointers allow manipulating directly the memory


locations where the data is saved

 Pointers enable dynamic allocation of memory


 Memory is allocated on demand

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:

int *p; // Declaring a pointer to integer

 This is read from right to left as: “p is a pointer to an integer”

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;

 Pointers can also be initialized at the time of declaration

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

 It is the opposite of the address operator


 Also called the dereferencing operator

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

p = &x; // assigns the address of x to p

cout << "x is stored at " << p <<" and its value is: "
cout << *p << endl;

p = &y; // assigns the address of y to p

cout << "y is stored at " << p <<" and its value is: "
cout << *p << endl;

return 0;

} // end function main


x is stored at 0012FF7C and its value is: 5
y is stored at 0012FF78 and its value is: 7

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;

int *xPtr = &x; // declares & initialize pointers to integers


int *yPtr = &y;
int *zPtr = &z;

*zPtr = *xPtr + *yPtr; // Addition of integers at xPtr and


// yPtr, then store it where zPtr points

cout << x << " + " << y << " = " << z << endl;

return 0;

} // end function main

5 + 3 = 8

31

You might also like