Document Details

GaloreEcstasy

Uploaded by GaloreEcstasy

Wellspring University

2024

Tags

C programming C++ programming computer science

Full Transcript

**WELL SPRING UNIVERSITY, BENIN CITY,** **EDOSTATE.** **DEPARTMENT OF COMPUTING** **SOFTWARE ENGINEERING,COMPUTER SCIENCE AND CYBER SECURITY PROGRAMME** **SECOND SEMESTER 2023/2024** **(3 UNITS)** **INTRODUCTION TO C/C++ PROGRAMMING** **CSC/SEN/CYB 224** **INTRODUCTION TO C AND C++ PROGRAMMI...

**WELL SPRING UNIVERSITY, BENIN CITY,** **EDOSTATE.** **DEPARTMENT OF COMPUTING** **SOFTWARE ENGINEERING,COMPUTER SCIENCE AND CYBER SECURITY PROGRAMME** **SECOND SEMESTER 2023/2024** **(3 UNITS)** **INTRODUCTION TO C/C++ PROGRAMMING** **CSC/SEN/CYB 224** **INTRODUCTION TO C AND C++ PROGRAMMING (3 UNITS) CORE** What is C++? ------------ C++ is a cross-platform Object Oriented Programming Language that can be used to create high-performance applications to solve problem for users. C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives programmers a high level of control over system resources and memory. The language was updated 4 major times in 2011, 2014, 2017, and 2020 to C++11, C++14, C++17, C++20. Why Do we Use C++ Now adays --------------------------- i. C++ is one of the world\'s most popular programming languages. ii. C++ can be found in today\'s operating systems, Graphical User Interfaces, and embedded systems. iii. C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. iv. C++ is portable and can be used to develop applications that can be adapted to multiple platforms. v. C++ is fun and easy to learn! vi. As C++ is close to [C](https://www.w3schools.com/c/index.php), [C\#](https://www.w3schools.com/cs/index.php) and [Java](https://www.w3schools.com/java/default.asp), it makes it easy for programmers to switch to C++ or vice versa. Difference between C and C++ ---------------------------- C++ was developed as an extension of [C](https://www.w3schools.com/c/index.php), and both languages have almost the same syntax. The main difference between C and C++ is that C++ support classes and objects, while C does not. The C++ Standard Library ======================== C++ comes with two standard libraries: the old C library (libc.lib), and the new C++ library (libcp.lib), which is logically divided into the stream library, and STL, the standard template library. Many implementations also include a pre-standard stream library for backward compatibility. Standard library header files and the std namespace --------------------------------------------------- The declarations of the functions, variables, and types in the standard libraries are distributed among some 51 header files. We must include these header files where we use the items they declare. For example, if we will be using lists, strings, and streams, we must include: \#include \\ \#include \\ \#include \ The angle brackets tell the pre-processor to look in the \"usual directory\" for the header file. **Structure of a C++ program** Probably the best way to start learning a programming language is with a program. So here is our first program: +-----------------------------------+-----------------------------------+ | *// my first program in C++* | **Hello World!** | | | | | \#include \ | | | | | | int main () | | | | | | { | | | | | | cout\ 3) && (b \> 5) evaluates to true (a \> 3) && (b \< 5) evaluates to false (a \> 3) \|\| (b \> 5) evaluates to true (a \> 3) \|\| (b \< 5) evaluates to true (a \< 3) \|\| (b \< 5) evaluates to false !(a \< 3) evaluates to true !(a \> 3) evaluates to false ### ### Example 5: Logical Operators \#include \ using namespace std; int main() { bool result; result = (3 != 5) && (3 \< 5); // true cout \ - **Initialize an array without specifying its size at declaration time.** - **Initialization by using the index of an element** **What if the elements initialized are less than the size of the array declared?** #### Example ### Accessing Array Elements in C++ To access the array elements, use the index number of the required element. The array index starts with 0. The index of the last element is n-1. **Two-Dimensional Array:** In this type of array, two indexes describe each element, the first index represents a row, and the second index represents a column. ![Two-dimensionalarray](media/image14.png)                                   Fig: Two-dimensional array  As you can see, the elements are arranged row-wise and column-wise; in a two-dimensional array, there are i number of rows and j number of columns. The above figure is a representation of a 3 x 3 matrix, which means there are three rows and three columns in the array. Now let's look at an example of a two-dimensional array. Two-dimensionalarray\_2.                                    Fig: Example of a two-dimensional array In this example, you are printing a two-dimensional array of three rows and three columns; you need to use two for loops. The first loop, i.e., i loop, runs for the row from 0 to 3, and the second loop, i.e., j loop, runs for the column from 0 to 3. Also Read: [Tutorial to C++ For Loop with Syntax and Examples](https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-for-loop) And below is the output of this example. ![wo-dimensionalarray\_3PNG](media/image16.png) Fig: Output **Multidimensional Array** The simplest example of a multidimensional array is a 2-d array; a two-dimensional array also falls under the category of a multidimensional array. This array can have any number of dimensions.The syntax for declaring a multidimensional array is: **Syntax:**                 Datatype array\_name \[size 1\]\[size 2\]..... \[size n\]; Here size1 size2 up to so on size n describes the number of dimensions; in the case of a 2-d array, there are only two dimensions, a multidimensional array can have any number of dimensions. **Example:  **                      int array\[5\]\[10\]\[4\]; Now, let's have a look at how to pass an array to a function. **How to Pass an Array to a Function** In C++, you can pass arrays to a function in the following ways: By passing a particular element of the array to the function **Syntax:**                void pass(int arr\[10\])               {                 body                  } Here arr\[10\] is an element that is at index 10 of the array, the pass is the name of the array, and void is the return type.  Example:  ArraytoFunction In this example, you must pass the element of the array at index 3, to the function pass, and inside the function, then you should print that particular element with the message: The element is. The output of the above example is: ![ArraytoFunction\_2](media/image18.png) By passing the whole array to the function: Syntax:                 void pass(int arr\[\])               {                body                } Here you are passing an unsized array arr\[\], i.e., it passed the whole array into the function. Also Read: [C++ Functions: Syntax, Types and Call Methods](https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-functions) **Example:** ArraytoFunction\_3                  In this example, an array 'arr' is passed to the function pass, and the array is getting printed with the help of for loop inside the function. What is an Array in C++ Programming? ------------------------------------ An array in C++ programming language is a powerful data structure that allows users to store and manipulate a collection of elements, all of the **same data type**in a single variable. Simply, it is a **collection of elements of the same data type**. Arrays are the derived data type in C++ that can store values of both - **fundamental data types** like int, and char; and **derived data types** like [pointers](https://www.scholarhat.com/tutorial/cpp/pointers-in-cpp), and structure. The values get stored at **contagious memory locations** that can be accessed with their index number. #### Syntax dataType arrayName\[arraySize\]; #### Example int a\[5\]; Here, an integer type array a is declared with the size 5. It can store 5 integer values. Types of Arrays in C++ ---------------------- ![](media/image20.png) **There are two types of **array** in C++, which are:** 1. **Single-dimensional array**: It is a collection of elements of the **same data type** that are stored in a contiguous block of memory. 2. **Multi-dimensional array**: It is an array that contains one or more arrays as its elements. #### Example \#**include** \

Use Quizgecko on...
Browser
Browser