teraexe · 2019-11-05 · the easiest way to learn c/c++/c# alvaro j. gene 1 table of content...

301
Author: Alvaro J. Gene Alias: Socket_0x03 Website: www.teraexe.com Email: [email protected] © Copyright 2015. Alvaro J. Gene. All rights reserved The Easiest Way to Learn C/C++/C# Version 1.2

Upload: others

Post on 06-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

Author: Alvaro J. Gene

Alias: Socket_0x03

Website: www.teraexe.com

Email: [email protected]

© Copyright 2015. Alvaro J. Gene. All rights reserved

The Easiest Way to Learn C/C++/C#

Version 1.2

Page 2: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

1

TABLE OF CONTENT

Introduction .................................................................................................................................................................................................... 3

Essential Concepts .......................................................................................................................................................................................... 4

Running an Application ................................................................................................................................................................................ 8

The C Programming Language .............................................................................................................................................................. 9

Printing Data..................................................................................................................................................................................................... 9

Data-Types & Variables.............................................................................................................................................................................. 11

Constants ......................................................................................................................................................................................................... 17

Storage Classes .............................................................................................................................................................................................. 19

Basic Operators – Arithmetic Operators ................................................................................................................................................ 21

Basic Operators – Relational Operators ................................................................................................................................................. 23

Basic Operators – Logical Operators ...................................................................................................................................................... 25

Basic Operators – Bitwise Operators ...................................................................................................................................................... 27

Basic Operators – Assignment Operators .............................................................................................................................................. 31

Conditional Statements ............................................................................................................................................................................... 37

Conditional Loops ........................................................................................................................................................................................ 45

Functions ......................................................................................................................................................................................................... 53

Variable Scope ............................................................................................................................................................................................... 56

Arrays ............................................................................................................................................................................................................... 57

Pointers ............................................................................................................................................................................................................ 62

Strings ............................................................................................................................................................................................................... 64

Structures ......................................................................................................................................................................................................... 68

Unions .............................................................................................................................................................................................................. 77

Input & Output ............................................................................................................................................................................................... 80

Input & Output: Text Files ......................................................................................................................................................................... 84

The C++ Programming Language ....................................................................................................................................................... 89

Printing Data................................................................................................................................................................................................... 89

Data-Types & Variables.............................................................................................................................................................................. 91

Constants ......................................................................................................................................................................................................... 93

Basic Operators ............................................................................................................................................................................................. 95

Conditional Statements ............................................................................................................................................................................. 102

Loop Statements .......................................................................................................................................................................................... 107

Functions ....................................................................................................................................................................................................... 112

Variable Scope ............................................................................................................................................................................................. 114

Arrays ............................................................................................................................................................................................................. 115

Pointers .......................................................................................................................................................................................................... 118

Page 3: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

2

Strings ............................................................................................................................................................................................................. 120

Structures ....................................................................................................................................................................................................... 123

Unions ............................................................................................................................................................................................................ 131

Input & Output ............................................................................................................................................................................................. 134

Input & Output: Text Files ....................................................................................................................................................................... 137

Date and Time .............................................................................................................................................................................................. 140

Reference Variables ................................................................................................................................................................................... 147

Exception Handling .................................................................................................................................................................................... 149

Dynamic Memory Allocation .................................................................................................................................................................. 156

Namespace .................................................................................................................................................................................................... 160

Object Oriented: Classes and Objects ................................................................................................................................................... 170

Object Oriented: Inheritance .................................................................................................................................................................... 180

Object Oriented: Function Overloading ............................................................................................................................................... 187

Object Oriented: Operator Overloading ............................................................................................................................................... 190

Object Oriented: Polymorphism ............................................................................................................................................................. 197

Object Oriented: Data Abstraction & Data Encapsulation ............................................................................................................. 204

The C# Programming Language ........................................................................................................................................................ 209

Printing Data................................................................................................................................................................................................. 209

Data-Types & Variables............................................................................................................................................................................ 211

Data-Type Conversion............................................................................................................................................................................... 212

Constants ....................................................................................................................................................................................................... 215

Basic Operators ........................................................................................................................................................................................... 216

Conditional Statements ............................................................................................................................................................................. 223

Loop Statements .......................................................................................................................................................................................... 228

Strings ............................................................................................................................................................................................................. 234

Arrays ............................................................................................................................................................................................................. 236

Structures ....................................................................................................................................................................................................... 241

Namespace .................................................................................................................................................................................................... 245

Object Oriented: Classes and Objects ................................................................................................................................................... 248

Object Oriented: Inheritance .................................................................................................................................................................... 261

Object Oriented: Function Overloading ............................................................................................................................................... 264

Object Oriented: Operator Overloading ............................................................................................................................................... 266

Object Oriented: Data Abstraction & Data Encapsulation ............................................................................................................. 269

Projects ......................................................................................................................................................................................................... 272

Server and Client ......................................................................................................................................................................................... 272

Port Scanner .................................................................................................................................................................................................. 292

HTTP Checker ............................................................................................................................................................................................. 295

Conclusion ................................................................................................................................................................................................... 300

Page 4: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

3

INTRODUCTION

Welcome to the Easiest Way to Learn C/C++/C#! In this tutorial, an individual can start learning C from

zero; in other words, a beginner can start learning the C programming language without any previous knowledge or

experience on other computer languages. Then, after learning C, an individual or coder can start learning C++, which

is the successor of C with Classes. Finally, after learning C and C++, if a programmer wants to expand his/her

knowledge and enhance his/her programming skills, he/she can start learning C#, which is one of the most

professional programming languages that a coder can use to build sophisticated computer applications.

C, a computer programming language developed around ninety-seventies, is considered by many scientists

and engineers as the mother of all modern programming languages. Why? C is considered as the mother of all

modern programming languages because it was used to build some of the most popular programming languages that

we can see today; for instance, the C programming language was used to build some languages like Java, JavaScript,

C++, C#, F#, Python, PHP, Perl, and many other languages. Furthermore, usually, when some of the most important

corporations and organizations start reporting the most popular programming languages on the world, those entities

provide a list of five or ten languages, which are derived from C.

In addition of being the mother of all modern languages, C is a computer programming language that has a

promising future of prosperity and success. Not only does C has a promising future, but also the C successors, which

are known as C++ and C#. Those languages have a promising future because they are supported by Microsoft

Corporation, which is one of the most distinctive and professional corporations on the computer world.

What kind of content can you find in the Easiest Way to Learn C/C++/C#? In this tutorial, you are going to

find some chapters or sections related to those topics:

• First, you will see some sections related to basic operators, conditional/loop statements, and similar topics.

• Second, you will learn how to build networking applications, such as a client and a server.

• Third, you will see some chapters related to hacking or computer security; in other words, you will see some

sections that will teach you how to build some security tools, including a port scanner and an HTTP checker.

Page 5: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

4

ESSENTIAL CONCEPTS

When an individual wants to start building his/her first applications, he/she should learn those computer

programming concepts that can give him/her that kind of vision and knowledge that a coder needs to start

understanding/building a great variety of source codes; therefore, in this section, a beginner who wants to start

learning about software development can see a summary related to the most important computer programming

concepts:

• High-Level Programming Language: A high-level programming language is closer to a human logic rather than a

computer logic; therefore, a human being is going to be more comfortable if he/she uses a high-level

programming language to build his/her computer applications. However, an important point that a human being

has to keep in mind is that a high-level programming language is more limited than a low-level programming

language; hence, there are some applications that a coder cannot build when he/she is using a high-level

programming language, including some virus, worms, shellcodes, and exploits. As you can see, those are some of

the most common high-level programming languages: Visual Basic, PHP, and Python.

• Low-Level Programming Language: A low-level programming language is more complex than a high-level

programming language because an individual has to type those kinds of codes that are closer to a computer logic

rather than a human logic; in addition, usually, a programmer has to type more codes when he/she is using a low-

level programming language instead of a high-level programming language. As previously stated, sometimes, a

coder has to use a low-level programming language to build those computer applications that he/she cannot

develop in a high-level language. As you can see, those are some of the most common low-level programming

languages: ASM and C.

• C: C, a computer programming language developed by Dennis Ritchie and Ken Thompson at the beginning of the

seventies, is one of the most popular computer programming languages that an individual can use to build a lot of

desktop applications; for example, an individual or coder can use the C programming language to build network

Page 6: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

5

traffic monitors, port scanners, remote administration tools, keyloggers, servers, clients, databases, and many

other tools.

Not only does a coder can use the C programming language to build computer applications, but also an individual

can use C and assembler to build an operating system if he/she is working with a professional team for many

years. For instance, at the beginning of the ninety seventies, a professional team was able to use C and assembler

to build an operating system, which is known as the UNIX operating system. In addition to the UNIX operating

system, there are other operating systems written in C and assembler, including Windows, Linux, and Mac. To

summarize, C is a powerful programming language that can be used by a professional team of programmers to

build a great variety of computer applications and operating systems, and a single coder can use C to build basic

applications like clients/servers/port-scanners.

In addition to the desktop applications and operating systems, one of the most important points that an individual

or coder should know about the C programming language is that some of the most popular compilers—a compiler

is an application that can convert a source code into a computer application; in other words, a compiler is a tool

that can be used by an individual to transform some programming codes into a desktop/web application—that a

lot of coders are using today are derived from C. To put it another way, thanks to the development of the C

computer programming language, there are others compilers/interpreters like Java, Perl, Python, and others.

What is the origin of the C computer programming language? In nineteen sixty-six, a computer scientist was able

to build a programming language that is known as the Basic Combined Programming Language (BCPL). Then, as

a new version of the BCPL, a new language was born: The B language. The B language, a computer

programming language developed by Ken Thompson at the end of the nineteen sixties, is the predecessor of the C

language; to put it another way, thanks to the development of the B language, the C language was born. Then,

after the development of the C programming language, this language has been used for decades to build some of

the most important operating systems and computer applications that we can see today.

Page 7: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

6

• C++: C++, a computer programming language developed by Bjarne Stroustrup in 1979, is the successor of a

computer programming language that is called C with classes, which is a superset of the C language. Thanks to

the C++ development, an individual can start building some computer applications while he/she is using high-

level/low-level programming features. Even though C++ was developed at the end of the seventies, it was not

until nineteen eighty-three that C++ received its name because this programming language was known as C with

classes until nineteen eighty-three. One of the most interesting facts that a coder should know about the C++

name is that it includes an increment operator (++), which is an operator that can be used by an individual to add

a value to his/her operands or variables, in other words, an increment operator increases the value of an operand

or variable. Although C++ was developed in nineteen seventy-nine and received its name in nineteen eighty-

three, it was not until nineteen eighty-five that the first edition of C++ was released. Then, after a long period of

time, there are many people around the world who are using this computer programming language to build a great

variety of applications, and there are a lot of coders who are building new versions and adding new features to

C++; for example, in 2014, a group of individuals or coder developed a new version of C++ that is called C++14.

• Source Code: In computer science, when an individual wants to create a computer application, he/she has to type

some codes that are known as the source code of an application. Usually, a source code is composed of a group of

commands, functions, and instructions; furthermore, a source code can be composed of comments because there

are some individuals who like to type some comments when they are creating or developing a source code.

After developing a source code, an individual has to use a compiler to transform or convert his/her source code

into a computer program, also known as an executable file. In addition to the compilers, a coder can also use an

interpreter to run a source code and execute its commands and instructions.

• IDE: IDE is an abbreviation for integrated development environment. An IDE is a computer application that can

be used by an individual to start building his/her own computer programs because an IDE provides a graphical

user interface composed of several tools that a coder can use to build his/her own applications; for example, there

Page 8: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

7

are some IDEs with a graphical user interface that are composed of a compiler/interpreter, a source code editor, a

network library, an input/output library, and other tools.

In this tutorial, we are going to use an IDE that is known as Microsoft Visual Studio 2008. Thanks to the Visual

Studio IDE, we are going to be able to compile and run our computer applications; in other words, because of the

Visual Studio IDE, we will be able to compile and run those applications developed on C, C++, and C#.

• Software Framework: A software framework is a computer application that can be used by an individual to build

his/her own applications because a software framework is a platform or environment that is composed of

compilers, source code editors, APIs, debuggers, and many other tools to build computer programs.

• .NET Framework: In 2000, Microsoft Corporation was able to build a software framework that is known as .NET

framework. Then, after a few years, Microsoft Corporation was able to include the third edition of its .NET

framework inside of an integrated development environment (IDE) that is called Visual Studio 2008.

• C#: In 1999, a group of computer scientists developed a computer programming language that was known as

Cool. Then, while Microsoft Corporation was working on its .NET framework, this organization was able to

include the Cool programming language inside of its .NET framework; however, instead of including the Cool

programming language as Cool, Microsoft Corporation included the Cool language as C#, which is also known as

C sharp.

One of the most interesting points that a coder should know about the C# programming language is that the C#

name comes from “a musical note” that is called C-Sharp. As previously stated, C++, the successor of C, has an

increment operator (++) on its name; similarly, C#, the successor of C++, has a sharp symbol on its name. Using

the sharp symbol is a way of saying that C# is an increment of C++ because the sharp symbol is composed of four

+ symbols (C# = C++++).

Page 9: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

8

Running an Application

In this section, you will learn how to use Microsoft Visual Studio 2008 to compile and run a computer

application. As you can see, those are the steps that an individual has to follow to compile and run an application

developed on C/C++:

• First, after installing Microsoft Visual Studio 2008 on a computer, an individual has to execute an executable file

that is located on a directory similar to this:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ devenv.exe

Sometimes, a shortcut of the devenv file is created on a computer desktop; therefore, an additional alternative is

to execute the devenv shortcut that is located on a computer desktop. Usually, the devenv shortcut file has this

name: Microsoft Visual Studio 2008.

• Second, after executing Microsoft Visual Studio 2008, a coder has to click on File, New, Project, Visual C++, and

choose Win32 Console Application. Then, a programmer has to type a project name on a field that is located on

the right-side of the Name keyword. In this tutorial, we are going to type File on the Name field. After typing a

project name, a coder has to click on OK.

• Third, a coder has to click on Next. Then, an individual has to choose Console Application below Application

Type; moreover, an individual has to choose Empty Project below Additional Options. After choosing Console

Application and Empty Project, a programmer has to click on Finish.

• Fourth, an individual has to click on View; then, he/she has to click on Solution Explorer. After clicking on

Solution Explorer, a coder has to make a right-click on Source Files, select Add, and click on New Item.

• Sixth, a programmer has to click on C++ File (.cpp) and type a name on the right-side of the Name field. In this

tutorial, we are going to type File on the Name field.

• Finally, an individual has to place or put a source code inside of the File.cpp file; then, he/she has to type Ctrl+F5

to compile and run a computer application.

Page 10: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

9

The C Programming Language

Printing Data

In this section, an individual can start learning some of the most basic programming codes that a coder can use to

start building a computer application that is going to display information inside of a Terminal, also known as a

Console or Command-Line. As you can see, those are some of the most basic programming codes that an individual

can use to display data on a Terminal:

• The include Statement: An individual can use the include statement when he/she wants to include a header-file.

What is a header-file? It is a file that is composed of several statements or functions that a coder can use to build a

computer application. If a C developer wants to include a header-file, he/she has to type a line similar to this:

#include <header-name.h>. One of the most common header-files that a coder can use to develop a computer

program is known as the stdio header.

• The <stdio.h> Library Header File: The stdio header is one of the C standard libraries that can be used by an

individual to perform input or output operations; for example, a coder can use the fread function to read the

content of a file, or a programmer can use the fwrite function to write or put information inside of a file. In

addition to those input/output operations, an individual can also use the stdio header when he/she wants to work

with some statements, such as the printf statement.

• The printf Statement: While a coder is using the stdio header, he/she can use the printf function to print or display

information inside of a terminal/computer-program. An important point that a coder should know about the printf

statement is that the information that he/she wants to display on a terminal/computer-program should be inside of

two quotation marks, which are going to be inside of two parentheses. Furthermore, if an individual wants to

show or display information on a different line, he/she can use the \n keyword.

• The Semicolon: Usually, at the end of a function or statement, an individual has to type a semicolon (;); for

example, at the end of the printf statement, a coder has to type a semicolon. As you can see, this is an example:

printf (“information”); .

Page 11: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

10

• The Main Function: The main function is also called the main method. When a programmer wants to use the C

programming language to build a computer application, he/she has to include the main method inside of his/her

source code; to put it another way, if an individual wants to use C to develop an application, he/she has to type

this line: in main(). An important point that a coder should know about the main function is that a coder has to

type the main function after including some headers like stdio.h.

• The return Keyword: After opening the main method and typing some codes inside of the main method, an

individual has to type the return keyword to finish the main method.

• Braces: The braces are also known as the curly brackets ({}). Usually, after typing a function name, an individual

has to use the opening brace to start a function; then, at the end of a function, a coder has to use the closing brace

to finish a function. As you can see, this is an example of the main function and its braces:

Int main() {

In this section, an individual has to type some programming codes.

}

• Comments: When an individual wants to leave a comment related to a block of code, he/she has to type a line

similar to this: /* Comments. */. As you can see in the previous line, a programmer can use the /* symbol to start

typing a comment; then, at the end of a comment, a coder can use the */ symbol to end or finish a comment.

Source Code:

#include <stdio.h> int main() { printf( "The Easiest Way to Learn C/C++/C# by Alvaro J. Gen e (Socket_0x03).\n" ); printf( "Welcome to this tutorial!\n" ); /*This is a comment.*/ return 0;

}

Result:

Page 12: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

11

Data-Types & Variables

One of the best ways that an individual can use to store and retrieve a value is using some variables and data-

types; more specifically, when an individual wants to store and retrieve a value inside of a random-access memory

(RAM) of a computer, he/she can use some variables and data-types.

What is a variable? In computer programming, a variable is one of the names that an individual has to build or

create to store and/or retrieve different kinds of values. For example, if a coder wants to build a variable name to store

a numerical value, he/she has to type a line similar to this: int Variable-Name = 3;. As you can see in the previous

line, I am building or declaring a variable that is holding a numerical value, which is the number three; furthermore, if

you pay close attention to the previous code, you will notice that there is an int, which is a data-type.

What is a data-type? In computer science, a data-type is the type of data that a variable will store inside of the

random-access memory (RAM) of a computer. Before typing a variable-name, an individual has to type a data-type,

which is related to the type of data that a coder is storing on a computer memory. For instance, a programmer may

specify or declare an integer data-type if he/she wants to store an integer or whole number on a computer memory, or

a C developer may type a character data-type if he/she wants to store a character or letter inside of the random-access

memory (RAM) of a system. In the C programming language, some of the most basic data-types are categorized in

two different groups, which are known as integer data-types and floating-point data-types.

Integer Data-Types

In the C programming language, when an individual or coder wants to store and/or retrieve an integer value,

he/she can use four different kinds of data-types: char, int, short, and long.

The Char Data-Type: a C developer can use the char data-type to store/retrieve an integer digit that is not going to

use a lot of random-access memory (RAM) because a char data-type is one of the smallest data-types available on the

C programming language that is composed of one byte or eight bits; therefore, if an individual wants to save memory

or space while he/she is declaring some variables, he/she can use the char data-type. Even though the char data-type

Page 13: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

12

is one of the best data-types that an individual can use to save memory, this data-type has a limited range: the

minimum value of a char data-type is -128, and the maximum value of a char data-type is 127. As you can see, this is

an example of how to use a char data-type and a printf statement to build and print an integer value:

char Variable_Name = 127;

printf("The Variable_Name Value is: %i", Variable_Name);

In the previous lines, you can see that I am using the char keyword to declare or build a char data-type;

furthermore, you can notice that I am using the %i symbol to print or display an integer value, which is the

Variable_Name value. One of the most important points that a coder should know about the printf statement and the

integer values is that an individual can use the %i symbol when he/she wants to print or show an integer number.

The int Data-Type: In addition to the char data-type, an individual can also use the int data-type when he/she

wants to store or retrieve different kinds of values or operands. On the one hand, a char data-type is going to use only

one byte of random-access memory (RAM) to store a value; on the other hand, an int data-type will use two bytes of

memory to store an integer number. Although an int data-type uses more random-access memory than a char data-

type, an individual or coder can use the int data-type to store or retrieve lower/higher values than a char data-type;

more specifically, a coder can use the int data-type to store/retrieve those kinds of values that are lower than -128, or

a programmer can use the int data-type to store/retrieve those kinds of operands that are higher than 127. The

minimum value of an int data-type that is storing two bytes of data is -32768, and the maximum value of an int data-

type that is storing two bytes of data is 32767. As you can see, this is an example of how to use an int data-type and a

printf statement to build and print a whole number:

int Variable_Name = 32767;

printf("The Variable_Name Value is: %i", Variable_Name);

In the previous lines, you can see that I am using the int keyword to declare an int data-type, and you can also see

that I am using the %i symbol to display a whole number.

Page 14: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

13

The short Data-Type: Similar to the int data-type, an individual can use the short data-type when he/she wants to

store/retrieve those kinds of values that are going to use only two bytes of random-access memory. As previously

stated, the minimum value of an int data-type that is storing two bytes of data is -32768, and the maximum value of

an int data-type that is storing two bytes of data is 32767; in the same way, the minimum value of a short data-type

that is storing two bytes of data is -32768, and the maximum value of a short data-type that is storing two bytes of

data is 32767. As you can see in the previous block of code, an individual has to type the int keyword before typing a

variable name; likewise, a C developer has to type the short keyword before typing a variable name. Furthermore,

after using the int keyword to build an int data-type, a coder has to use the printf statement and its %i symbol to

display an integer number; similarly, after using the short keyword to declare a short data-type, a programmer has to

use the printf statement and its %i symbol to show a whole number. As you can see, this is an example:

short Variable_Name = 32767;

printf("The Variable_Name Value is: %i", Variable_Name);

The long Data-type: An individual or coder can use a long data-type when he/she has to build a computer

application that is going to use long values, such as millions or billions. Even though a C developer can use a long

data-type to declare long values, this kind of data-type uses more random-access memory (RAM) than other data-

types; to clarify, the long data-type uses 4 bytes or 32 bits of random-access memory while there are other data-types

that are using 8 or 16 bits of memory, including the chart, int, and short data-type. The minimum value of a long data-

type that is using 32 bits of random-access memory is -2147483648, and the maximum value of a long data-type that

is using 4 bytes of memory is 2147483647. Like the other integer data-types, an individual has to use the printf

statement and its %i symbol to display or show a long data-type. As you can see, this is an example of how to build

and display a long data-type:

long Variable_Name = 2147483647;

printf("The Variable_Name Value is: %i", Variable_Name);

Page 15: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

14

Floating-Point Data-Types

In addition to the integer data-types, an individual can use the C programming language to store and/or retrieve

floating-point values; in that case, a coder can use two different kinds of data-types, which are known as float and

double.

The float Data-type: An individual can use a float data-type when he/she wants to declare floating-point numbers

instead of integer numbers, also known as whole numbers. What is a floating-point number? It is the opposite of an

integer number because a floating-point number is a number with a decimal point, so a floating-point number is

composed of one or more decimals because the value a floating-point number is close to a real number value. On the

one hand, a whole number can be 5, 20, or 86; on the other hand, a floating-point number can be 3.14 or 2.25. When

an individual wants to build and print a floating-point number, he/she has to type some codes similar to this:

float Variable_Name = 3.14;

printf("The Variable_Name Value is: %f", Variable_Name);

As you can see in the previous lines, I am using the float keyword before a variable name to build a floating-point

number; moreover, I am using the printf statement and its %f symbol to display or print a floating-point value. An

individual can use the printf statement and its %i symbol when he/she wants to display an integer number; however,

when an individual wants to display or print a floating-point number, he/she has to use the %f symbol instead of the

%i symbol.

One of the most important points that an individual should know about the float data-type is that this kind of data-

type uses four bytes or 32 bits of random-access memory. Furthermore, the minimum value of a float data-type is

1.2E-38, and the maximum value of a float data-type is 3.4E+38.

The double Data-Type: In addition to the float data-type, a coder can also use the double data-type to declare or

build floating-point numbers. However, a double data-type uses eight bytes of random-access memory instead of four

bytes, which means that a double data-type uses more random-access memory than a float data-type. Although a

double data-type uses more memory than a float data-type, an individual can use a double data-type to declare higher

Page 16: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

15

values than a float data-type. The minimum value of a double data-type is 2.3E-308, and the maximum value of a

double data-type is 1.7E+308. When an individual or coder wants to build and print a double data-type, he/she has to

type a block of code similar to this:

double Variable_Name = 3.14;

printf("The Variable_Name Value is: %f", Variable_Name);

As you can see in the previous lines, a C developer can use the double keyword to build a floating-point number,

and a coder can use the printf statement and its %f symbol when he/she wants to print a floating-point number.

Source Code – Integer Types:

#include <stdio.h>

int main() {

char AGene_A = 127;

int AGene_B = 32767;

short AGene_C = 32767;

long AGene_D = 2147483647;

printf( "The AGene_A Value is: %i \n" , AGene_A);

printf( "The AGene_B Value is: %i \n" , AGene_B);

printf( "The AGene_C Value is: %i \n" , AGene_C);

printf( "The AGene_D Value is: %i \n" , AGene_D);

return 0;

}

Result – Integer Types:

Page 17: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

16

Source Code – Floating-Point Types:

#include <stdio.h> int main() { float AGene_A = 3.14; double AGene_B = 20.5; printf( "The AGene_A Value is: %f \n" , AGene_A); printf( "The AGene_B Value is: %f \n" , AGene_B); return 0; }

Result – Floating-Point Types:

Source Code – Printing a Character:

#include <stdio.h> int main() { char AGene_A = 'A' ; signed int AGene_B = 127; unsigned int AGene_C = 255; printf( "The AGene_A Value is: %i \n" , AGene_A); printf( "The AGene_A Value is: %c \n\n" , AGene_A);

printf( "The AGene_B Value is: %i \n" , AGene_B); printf( "The AGene_C Value is: %i \n" , AGene_C); return 0;

}

Result – Printing a Character:

Page 18: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

17

Constants

When an individual is building a computer application, he/she can define or create a constant. What is a constant?

In computer science, a constant is a value that cannot be changed; therefore, if an individual or coder wants to build a

word with a value that cannot be modified, he/she has to create a constant instead of creating a variable because a

variable value can be altered. In the C programming language, when a C developer wants to declare or build a

constant, he/she can use two different kinds of keywords, which are known as const and #define.

The const Keyword: As previously stated, an individual can use the const keyword to build some constants; in

that case, after typing the const keyword, a coder has to type a data-type; then, after typing a data-type, a programmer

has to type a constant name and its value. As you can see, this is an example:

const int CONSTANT_NAME = 5;

In the previous line, you can see that I am using uppercase letters to type a constant name because it is a good

programming practice to use uppercase letters when an individual is declaring or creating some constants.

The #define Keyword: In addition to the const keyword, a C developer can also use the #define keyword when

he/she wants to declare or create some constants. In that case, after typing the #define keyword, a coder has to type a

constant name without typing a data-type; then, after typing a constant name, a coder has to type a constant value

without typing an equality sign or a semicolon. As you can see, this is an example:

#define CONSTANT_NAME 86

As explained above, while a coder is defining or creating some constants, it is a good programming practice to

use uppercase letters when an individual is typing a constant name, so I am typing some uppercase letters in the

previous line of code.

One of the best ways to learn about constants is seeing an example of a source code; hence, I am going to provide

three source codes about constants at the end of this section. In the first source code, you will learn how to use the

const keyword. Then, in the second source code, you will learn how to use the #define keyword. Finally, you will see

a source code where I am using the const and #define keyword to display some constants.

Page 19: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

18

Source Code – The const Keyword:

#include <stdio.h> int main() { const int AG_CONST = 86; printf( "The constant value is: %d \ n" , AG_CONST); return 0; }

Result – The const Keyword:

Source Code – The #define Keyword:

#include <stdio.h> #define AG_CONST 5 int main() { printf( "The constant value is: %d \n" , AG_CONST); return 0; }

Result – The #define Keyword:

Source Code:

#include <stdio.h> #define AG_CONST_A 5 int main() { const int AG_CONST_B = 86; printf( "The first constant value is: %d \n" , AG_CONST_A); printf( "The second constant value is: %d \n" , AG_CONST_B); return 0; }

Result:

Page 20: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

19

Storage Classes

When an individual is creating a variable, he/she can specify or define a storage class. What is a storage class? In

computer science, a storage class is one of the variable attributes that a coder can use to specify or define a variable

scope/lifetime; furthermore, an individual can use a storage class to specify the memory location of a variable. In the

C programming language, there are four different kinds of storage classes, which are known as automatic, register,

static, and external.

• The Automatic Storage-Class: A C developer can use the auto keyword when he/she wants to specify an

automatic storage-class to one of his/her variables. Usually, a coder is going to use an automatic specifier when

he/she wants to build a variable with default storage, which is a variable that is going to be stored inside of a

random-access memory. In addition to the storage location, an individual can use the auto specifier to build a

variable of local scope. What is a local scope? A local scope is an area that is inside of a block of code or

function, so a variable of local scope is a variable that is going to work inside of a block of code or function. To

put it another way, a variable of local scope is a variable that a coder can use inside of a function or block of code;

for example, if an individual defines a local scope variable inside of the main function, he/she can use that

variable only inside of the main function.

• The Register Storage-Class: A coder can use the register keyword to specify a register storage-class to one of

his/her variables. Usually, an individual is going to use the register specifier when he/she is building a computer

application with a local variable that can be accessed in a short period of time because a variable with a register

storage class is going to be stored inside of a register instead of a random-access memory.

• The Static Storage-Class: An individual can use the static keyword when he/she wants to build a static variable.

What is a static variable? In computer programming, a static variable is a variable with a longer lifetime than a

regular variable because a static variable is going to remain inside of a random-access memory as long as a

computer application or a particular block of code is running on a computer.

Page 21: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

20

• The External Storage-Class: While a C developer is building a global variable, he/she can use the extern keyword

to use his/her variable on different kinds of files. For example, if a coder builds an external variable inside of a

file that is called first-file.c, he/she can also use his/her variable on another file that is called second-file.c.

Source Code:

#include <stdio.h>

int main() {

auto int AGene_A = 5;

register int AGene_B = 20;

static int AGene_C = 86;

printf( "The AGene_A value is: %i \n" , AGene_A);

printf( "The AGene_B value is: %i \n" , AGene_B);

printf( "The AGene_C value is: %i \n" , AGene_C);

return 0;

}

Result:

Page 22: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

21

Basic Operators – Arithmetic Operators

In the C programming language, an individual or coder can use different kinds of arithmetic operators to change

the value of his/her operands or variables. As you can see, those are the arithmetic operators that a programmer can

use to manipulate the value of some operands or variables:

• The Addition Operator (+): When an individual wants to add the values of some operands or variables, he/she has

to use the addition operator. For example, in the C programming language, if a coder wants to add two values,

he/she has to declare three variables and type a line similar to this: int A = 5; int B = 20; int C;. In the previous

code, you will notice that a variable is not holding any kind of value because the third variable is going to receive

a value after using the addition operator to add the values of the first two variables; in that case, if a C developer

wants to use the two previous variables (A = 5 and B = 20) to add their values to the third variable (C), he/she has

to type a line similar to this: C = A + B;. Then, after using the addition operator to add two operands, a coder has

to type a line like this to print the value of the third variable (C): printf ("The C value is: %d", C);. An important

point that an individual should know about the previous steps is that a coder can use those steps to work with

other arithmetic operators, including the subtraction operator, the multiplication operator, and other operators.

• The Subtraction Operator (-): A coder can use the subtraction operator when he/she wants to substrate some

operands; in other words, a C developer can use the subtraction operator to subtract the values of some variables.

• The Multiplication Operator (*): The multiplication operator can be used by a programmer to multiply some

operands or variables.

• The Division Operator (/): An individual can use the division operator to divide some values.

• The Modulus Operator (%): A coder can use the modulus operator to return the division remainder.

• The Increment Operator (++): When an individual or coder wants to increase the value of an operand or variable,

he/she can use the increment operator.

Page 23: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

22

• The Decrement Operator (--): In the C programming language, the decrement operator can be used to decrement

the values of an operand or variable.

Source Code:

#include <stdio.h>

int main() { int AGene_A = 5; int AGene_B = 20; int AGene_C; //The Addition Operator: AGene_C = AGene_A + AGene_B; printf( "The AGene_C value is: %d \n" , AGene_C); //The Subtraction Operator: AGene_C = AGene_A - AGene_B; printf( "The AGene_C value is: %d \n" , AGene_C); //The Multiplication Operator: AGene_C = AGene_A * AGene_B; printf( "The AGene_C value is: %d \n" , AGene_C); //The Division Operator: AGene_C = AGene_A / AGene_B; printf( "The AGene_C value is: %d \n" , AGene_C); //The Modulus Operator: AGene_C = AGene_A % AGene_B; printf( "The AGene_C value is: %d \n" , AGene_C); //The Increment Operator: AGene_C = AGene_A++; printf( "The AGene_C value is: %d \n" , AGene_C); //The Decrement Operator: AGene_C = AGene_A--; printf( "The AGene_C value is: %d \n" , AGene_C); return 0; }

Result:

Page 24: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

23

Basic Operators – Relational Operators

In the C programming language, a programmer can use six different kinds of relational operators to test the value

of some operands or variables. As you can see, those are the relational operators that a coder can use test or check the

value of some operands or variables:

• The Equal Operator (==): A coder can use the equal operator (==) to test if the value of an operand is equal to the

value of another operand. If the value of an operand is not equal to the value of another operand, the computer

application is going to print zero, which means false; on the other hand, if the value of an operand is equal to the

value of another operand, the computer application is going to print one, which means true. In the C programming

language, one of the most important points that a coder should know about the relational operators is that the rule

of zero (false) and one (true) is not only applied to the equal operator, but also to the other relational operators,

including the greater-than operator, the less-than operator, and other operators.

• The Not-Equal Operator (!=): In addition to the equal operator, an individual can also use the not-equal operator

to test the value of a variable or operand. In this case, the computer program will print one (true) if the value of a

variable is not equal to the value of another variable; in contrast, the application is going to show zero (false) if

the value of an operand is equal to the value of another operand.

• The Greater-Than Operator (>): A C developer can use the greater-than operator to test if the value of an operand

is greater than the value of another operand. If the value of a variable is greater than the value of another variable,

the application is going to display one, which means true; however, a computer application is going to display the

number zero (false) if the value of an operand is not greater than the value of another operand.

• The Less-Than Operator (<): A programmer can use the less-than operator to test if the value of an operand or

number is less than the value of another operand or number. In this case, a computer application is going to

display the number one (true) if the value of an operand is less than the value of another operand, but it is going to

show the number zero (0) if the value of an operand is greater than the value of another operand or variable.

Page 25: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

24

• The Greater-Than or Equal Operator (>=): An individual or computer scientist can use the greater-than or equal

operator to test or check if the value of a variable is greater-than or equal to the value of another variable.

• The Less-Than or Equal Operator (<=): A C developer can use the less-than or equal operator to test if the value

of an operand or number is less than or equal to the value of another operand or number.

Source Code:

#include <stdio.h>

int main() {

int AG_A = 5;

int AG_B = 20;

//Note: 0 = false and 1 = true.

printf( "The Equal Operator: %d \n" , AG_A == AG_B); //False = 0.

printf( "The Not-Equal Operator: %d \n" , AG_A != AG_B); //True = 1.

printf( "The Greater-Than Operator: %d \n" , AG_A > AG_B); //False = 0.

printf( "The Less-Than Operator: %d \n" , AG_A < AG_B); //True = 1.

printf( "The Greater-Than or Equal Operator: %d \n" , AG_A >= AG_B); //False = 0.

printf( "The Less-Than or Equal Operator: %d \n" , AG_A <= AG_B); //True = 1.

return 0;

}

Result:

Page 26: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

25

Basic Operators – Logical Operators

In the C programming language, a programmer can use three different kinds of logical operators to test the value

of some operands or variables. As you can see, those are the logical operators that a coder can use to test or check the

value of some operands or variables:

• The Logical AND Operator (&&): When a C developer wants to know if two variables have a positive value

(true), he/she can use the logical AND operator. If both variables are positive, the logical AND operator will

display one, which means true; however, the computer program is going to display zero (false) if one of the two

operands is false. In the next application, the first logical AND operator will print one (true) because AG_A and

AG_C have a positive value; to put it another way, the first logical AND operator is going to display one (true)

because there are two operands (AG_A and AG_C) with the value of true. Then, the second logical AND operator

will display zero (false) because the AG_B variable has a false value. Finally, the third logical AND operator will

show the number zero (false) because there are two negative operands, which are known as AG_B and AG_D.

• The Logical OR Operator (||): When an individual or coder wants to know if at least one of the two variables is

positive (true), he/she can use the logical OR operator. The logical OR operator is going to display the number

one (true) if at least one of the two variables is positive (true), but the logical OR operator will print zero (false) if

both variables are negative (false). In the next computer application, the first logical OR operator is going to

display the number one (true) because the AG_A variable has a positive value (true). Then, the second logical OR

operator will show the number zero (false) because AG_B and AG_D have a negative value (false). Finally, the

third logical OR operator is going to print one (true) because AG_A and AG_C have a positive value (true).

• The Logical NOT Operator (!): A programmer can use the logical NOT operator when he/she wants to know if a

variable is not positive (false). On the one hand, the logical NOT operator is going to print the number one (true)

if at least one of the two operands is not true; on the other hand, the logical NOT operator will display zero (false)

if both operands are positive or true. In the next application, the first logical NOT operator will print one (true)

Page 27: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

26

because the AG_B variable is negative or false. Then, the second logical NOT operator is going to display zero

(false) because there are not negative or false variables. Finally, the third logical NOT operator will show one

(true) because AG_B and AG_D have a negative value (false).

Source Code:

#include <stdio.h> int main() { int AG_A = true ; int AG_B = false ; int AG_C = true ; int AG_D = false ; int AGene; //The Logical AND Operator: If both variables are t rue; then, it will print true. AGene = AG_A && AG_C; printf( "The Logical AND Operator: %d \ n" , AGene); //Both True: True = 1. AGene = AG_A && AG_B; printf( "The Logical AND Operator: %d \n" , AGene); //True & False: False = 0. AGene = AG_B && AG_D; printf( "The Logical AND Operator: %d \n" , AGene); //False & False: False = 0. //The Logical OR Operator: If a variable is true; t hen, it will print true. AGene = AG_A || AG_B; printf( "The Logical OR Operator: %d \ n" , AGene); //True & False: True = 1. AGene = AG_B || AG_D; printf( "The Logical OR Operator: %d \ n" , AGene); //Both False: False = 0. AGene = AG_A || AG_C; printf( "The Logical OR Operator: %d \n" , AGene); //Both True: True = 1. //The Logical NOT Operator: If a variable is not tr ue(false); then, it will print true. AGene = !(AG_A && AG_B); printf( "The Logical NOT Operator: %d \n" , AGene); //True & False: True = 1. AGene = !(AG_A && AG_C); printf( "The Logical NOT Operator: %d \n" , AGene); //Both True: False = 0. AGene = !(AG_B && AG_D); printf( "The Logical NOT Operator: %d \n" , AGene); //Both False: True = 1. return 0; }

Result:

Page 28: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

27

Basic Operators – Bitwise Operators

When an individual or coder wants to change some operand or variable values, he/she can use some bitwise

operators to perform bit operations. Do you know what a bit is? In computer programming and information

technology, a bit is the smallest unit of data or information; furthermore, a bit can be represented either zero (0) or

one (1). Do you know what a bit operation is? In computer science, a bit operation is when an individual or computer

application is using two bits to perform an operation and obtain a final result as a product of that operation; for

example, an individual or computer application may multiply two bits and obtain a final result as a product of a bit

multiplication. If a computer scientist wants to perform a lot of bit operations, he/she can build a computer

application instead of performing a lot of bit operations manually. In the C programming language, a coder can use

those bitwise operators when he/she wants to build a computer application that is going to perform bit operations:

• The Bitwise AND Operator (&): After declaring or building a variable that is going to be stored inside of the

random-access memory, a computer is going to store that variable using two different kinds of bits, which are

known as zero (0) and one (1). Then, after storing two variable values, an individual can use the bitwise AND

operator to build a computer application that is going to multiply the bits of two variable values and store a result

inside of the random-access memory. In that case, the bitwise AND operator will store the number one (1) if both

bits have the number one (1); on the other hand, the bitwise AND operator will store the number zero (0) if one of

the two bits is zero (0). As you can see, this is a manual example:

43 = 0010 1011 86 = 0101 0110 02 = 0000 0010

In the previous example, a coder can see that he/she can obtain the number two after storing two values and

perform a bit multiplication or bit operation. In the C programming language, a programmer can use the bitwise

AND operator to perform a bit multiplication; then, he/she can use the printf statement and its %d keyword to

display the result of a bit multiplication. An important point that a coder should know about the printf statement

Page 29: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

28

and its %d keyword is that a result is going to be displayed in ASCII numbers instead of binary numbers. As you

can see, this is an example of how to use the bitwise AND operator and the printf statement to display the result

of a bit operation:

int AG_A = 43; //43 = 0010 1011.

int AG_B = 86; //86 = 0101 0110.

AGene = AG_A & AG_B; // 2 = 0000 0010.

printf("1. The Bitwise AND Operator: %d \n\n", AGene);

In the first two lines, I am storing two values, which are known as 43 and 86. In the random-access memory of a

computer, those two numbers are stored as 00101011 and 01010110. Then, in the third line, I am using the

bitwise AND operator to perform a bit operation, which is going to multiply the bits of the first two operands and

store the value 00000010, which is 2 in ASCII numbers. Finally, in the fourth line, I am using the printf statement

and its %d keyword to display or print the number 2.

• The Bitwise OR Operator (|): The Bitwise OR operator will store the number one (1) if one of the two bits is one

(1), and it will store one (1) if both bits have the number one (1); however, the Bitwise OR operator is going to

store the number zero (0) if both bits have the number zero (0). As you can see, this is an example:

43 = 0010 1011

86 = 0101 0110

127 = 0111 1111

• The Bitwise XOR Operator (^): The Bitwise XOR operator will store the number one (1) if both bits have a

different value, such as zero (0) and one (1), but it is going to store the number zero (0) if both bits have the same

value; for instance, the Bitwise XOR operator will store the number zero (0) if both bits have the value zero (0) or

one (1). As you can see, this is an example:

43 = 0010 1011

86 = 0101 0110

125 = 0111 1101

Page 30: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

29

• The Binary Ones Complement Operator (~): An individual can use the binary ones complement operator (~)

when he/she wants to store the opposite value of an operand or variable. The binary ones complement operator

will store the number zero (0) if a bit is one (1); in contrast, the binary ones complement operator is going to store

the number one (1) if a bit is zero (0). As you can see, this is an example:

43 = 0010 1011 -44 = 1101 0100

• The Binary Left Shift Operator (<<): When a coder wants to build a computer application that is going to use a

variable value to move its bits to the left side, he/she can use the binary left shift operator. An important point that

a C developer should know about the binary left shift operator is that he/she has to specify the number of bits that

he/she wants to move to the left side; for example, in the next computer application, I am typing the number one

because I want to move only one bit to the left side. While the binary left shift operator is moving one or more

bits to the left side, this operator starts filling the empty places with a zero value. As you can see, this is an

example:

43 = 0010 1011

86 = 0101 0110

• The Binary Right Shift Operator (>>): When a C developer wants to build a computer application that is going to

use a variable value to move its bits to the right side, he/she can use the binary right shift operator. An important

point that a programmer should know about the binary right shift operator is that he/she has to specify the number

of bits that he/she wants to move to the right side; for instance, in the next computer application, I am typing the

number one because I want to move only one bit to the right side. As previously stated, the binary left shift

operator is going to start filling the empty places with zero while it is moving one or more bits to the left side, but

the binary right shift operator is different because it does not start filling the empty places with zero while it is

moving one or more bits to the right side. As you can see, this is an example:

43 = 0010 1011

21 = 1 0101

Page 31: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

30

Source Code:

#include <stdio.h> int main() { int AG_A = 43; //43 = 0010 1011. int AG_B = 86; //86 = 0101 0110. int AGene = 0; AGene = AG_A & AG_B; // 2 = 0000 0010. printf( "1. The Bitwise AND Operator: %d \n\n" , AGene); AGene = AG_A | AG_B; // 127 = 0111 1111. printf( "2. The Bitwise OR Operator: %d \n\n" , AGene); AGene = AG_A ^ AG_B; // 125 = 0111 1101. printf( "3. The Bitwise XOR Operator: %d \n\n" , AGene); AGene = ~AG_A; // -44 = 1101 0100. printf( "4. The Binary Ones Complement Operator: %d \n\n" , AGene); AGene = AG_A << 1; // 86 = 0101 0110. printf( "5. The Binary Left Shift Operator: %d \n\n" , AGene); AGene = AG_A >> 1; // 21 = 1 0101. printf( "6. The Binary Right Shift Operator: %d \n\n" , AGene); return 0;

}

Result:

Page 32: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

31

Basic Operators – Assignment Operators

In the C programming language, a programmer can use different kinds of assignment operators to manipulate and

change the value of some operands or variables. As you can see, those are the assignment operators that an individual

can use to manipulate and change some operand values:

• The Simple Assignment Operator (=): When a C developer is building a computer application, he/she can use the

simple assignment operator to assign the values of a right operand to the values of a left operand; to clarify, a

coder can use the simple assignment operator to transfer or allocate the values of a right operand to the values of a

left operand. If you want to understand more about the simple assignment operator, you can see this block of

code:

int AG_A = 100; int AG_B = 86; int AGene; AGene = AG_A + AG_B;

printf("1. The Assignment Operator: %d \n\n", AGene);

In the first line, I am declaring or building three variables, which are known as AG_A, AG_B, and AGene. Then,

in the second line, I am adding the values of the first two variables (AG_A and AG_B), so the computer is going to

store the 186 value inside of the random-access memory. After adding the values of the first two variables, the

186 value is going to be assigned or transferred to the value of the left operand thanks to the simple assignment

operator; in other words, the AGene variable is going to obtain the 186 value because the simple assignment

operator is assigning or transferring the values of the right operands, which are AG_A and AG_B. Finally, in the

third line, I am using the printf statement and its %d keyword to display or print the AGene value.

• The Addition Assignment Operator (+=): An individual can use the addition assignment operator when he/she

wants to add the values of the right operand to the values of the left operand. On the one hand, when a coder is

using the simple assignment operator, an operand value is going to be assigned or transferred to the value of

another operand; on the other hand, when a C developer is using the addition assignment operator, an operand

Page 33: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

32

value will be added to the value of another operand. At the end of this section, you can see a source code where

the simple assignment operator is compared with the addition assignment operator.

• The Subtraction Assignment Operator (-=): When a programmer wants to perform a subtraction, he/she can use

the subtraction assignment operator. In that case, the left operand will be subtracted by the right operand; then,

after performing that operation, the subtraction assignment operator will assign the subtraction result to the left

operand. As you can see, this is an example:

int AG_A = 100; int AGene = 200; AGene -= AG_A; printf("The Subtraction Operator: %d \n\n", AGene);

In the first line, there are two operands or variables, which are known as AG_A and AGene. Then, in the second

line, the subtraction assignment operator is performing this subtraction: AGene (200) – AG_A (100) = 100. After

performing the previous subtraction, the subtraction result (100) will be added to the left operand (AGene).

Finally, in the third line, the printf statement and its %d keyword will display the AGene value, which is 100.

• The Multiplication Assignment Operator (*=): When a C developer wants to perform a multiplication, he/she can

use the multiplication assignment operator. In that case, the left operand will be multiplied by the right operand;

then, after performing that operation, the multiplication assignment operator will assign the multiplication result

to the left operand.

• The Division Assignment Operator (/=): When a C developer wants to perform a division, he/she can use the

division assignment operator. In that case, the left operand will be divided by the right operand; then, after

performing that operation, the division assignment operator will assign the division result to the left operand.

• The Modulus Assignment Operator (%=): When an individual or coder wants to obtain a division remainder,

he/she can use the modulus assignment operator. In that case, the left operand will be divided by the right

operand, and the modulus assignment operator will assign the remainder result to the left operand.

Page 34: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

33

• The Left Shift Assignment Operator (<<=): Like the binary left shift operator (<<), a C developer can use the left

shift assignment operator when he/she wants to move the bit of an operand value to the left side. An important

point that a C developer should know about the left shift assignment operator is that he/she has to specify the

number of bits that he/she wants to move to the left side; for example, in the next computer application, I am

typing the number one because I want to move only one bit to the left side. While the left shift assignment

operator is moving one or more bits to the left side, this operator starts filling the empty places with a zero value.

As you can see, this is an example:

86 = 0101 0110

172 = 1010 1100

• The Right Shift Assignment Operator (>>=): Similar to the binary right shift operator (>>), a C developer can use

the right shift assignment operator when he/she wants to move the bit of an operand value to the right side. An

important point that a C developer should know about the right shift assignment operator is that he/she has to

specify the number of bits that he/she wants to move to the right side; for example, in the next computer

application, I am typing the number one because I want to move only one bit to the right side. As previously

stated, the left shift assignment operator is going to start filling the empty places with zero while it is moving one

or more bits to the left side, but the right shift assignment operator is different because it does not start filling the

empty places with zero while it is moving one or more bits to the right side. As you can see, this is an example:

172 = 1010 1100

86 = 101 0110

Page 35: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

34

Source Code – Assignment Operators: Simple vs. Addition:

#include <stdio.h>

int main() {

int AG_A = 50;

int AG_B = 100;

int AG_C = 50;

int AG_D = 100;

AG_A = AG_B;

printf( "1. The Simple Assignment Operator: %d \n\n" , AG_A); // AG_A = 100.

AG_C += AG_D;

printf( "2. The Addition Assignment Operator: %d \n\n" , AG_C); // AG_C = 150.

return 0;

}

Result – Assignment Operators: Simple vs. Addition:

Page 36: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

35

Source Code – The Assignment Operators:

#include <stdio.h> int main() { int AG_A = 100; int AG_B = 86; int AGene; AGene = AG_A + AG_B; printf( "1. The Assignment Operator: %d \n\n" , AGene); AGene += AG_A; printf( "2. The Addition Assignment Operator: %d \n\n" , AGene); AGene -= AG_A; printf( "3. The Subtraction Assignment Operator: %d \n\n" , AGene); AGene *= AG_A; printf( "4. The Multiplication Assignment Operator: %d \n\n " , AGene); AGene /= AG_A; printf( "5. The Division Assignment Operator: %d \n\n" , AGene); AGene %= AG_A; printf( "6. The Modulus Assignment Operator: %d \n\n" , AGene); AGene <<= 1;

printf( "7. The Left-Shift Assignment Operator: %d \n\n" , AGene); AGene >>= 1; printf( "8. The Right-Shift Assignment Operator: %d \n\n" , AGene); AGene &= AG_A; printf( "9. The Bitwise AND Assignment Operator: %d \n\n" , AGene); AGene ^= AG_A;

printf( "10. The Bitwise Exclusive OR Assignment Operator: %d \n\n" , AGene); AGene |= AG_A; printf( "11. The Bitwise Inclusive OR Assignment Operator: %d \n\n" , AGene); return 0;

}

Page 37: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

36

Result – The Assignment Operators:

Page 38: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

37

Conditional Statements

While an individual is using C to build a computer application, he/she can add some conditional statements. What

is a conditional statement? In computer science, a conditional statement is a statement that is going to evaluate some

events and execute a block of code only if a particular event is true/false; for instance, a conditional statement may

execute a block of code after evaluating a user-event, instruction, character, or code. In the C programming language,

there are three different kinds of conditional statements, which are known as if-statement, if-else statement, and

switch statement.

• The if-Statement: An individual can use the if-statement to evaluate a condition. After evaluating a condition, the

if-statement is going to execute a block of code only if a particular condition is true. For instance, an if-statement

may execute a block of code if an operand has a superior value than another operand. As you can see, this is the

if-statement syntax:

if (condition) {

//Codes.

}

o Condition: In this parameter, an individual has to type a condition; for instance, in this field, a coder may type a

condition similar to this: AG_A > AG_B.

o Codes: If the previous condition is positive or true, the if-statement is going to execute a block of code, which are

those kinds of codes that a coder is including inside of the if-statement.

• The if-else Statement: Not only does a coder can use the if-statement to evaluate a condition, but also a C

developer can use the if-else statement to test a condition. The if-else statement will execute a block of code if a

particular condition is positive; in other words, the if-else statement is going to execute some codes if a condition

is true. However, if a condition is false, the if-else statement is going to execute a different block of code. As you

can see, this is the if-else statement syntax:

Page 39: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

38

if (condition) {

//First-Codes.

}

else {

//Second-Codes.

}

o Condition: In this field, an individual has to type those kinds of codes that an if-else statement is going to

evaluate; for instance, in this field, a coder may type a condition similar to this: AG_A > AG_B.

o First-Codes: If the previous condition is positive (true), the if-else statement is going to execute those kinds of

codes that are inside of this field.

o Second-Codes: If the previous condition is negative (false), the if-else statement will execute a block of code,

which is a block of code that is located below of a keyword that is called the “else” keyword.

• The Nested if-Statement: In computer programming, when an individual is adding an if-statement inside of

another if-statement, he/she is performing a programming technique that is known as the nested if-statement. In

the C programming language, a coder has to follow those instructions to code a nested if-statement:

if (First-Condition) {

//First-Codes.

if (Second-Condition) {

//Second-Codes.

}

}

o First-Condition: the if-statement is going to evaluate those kinds of codes that are inside of this field.

o First-Codes: If the previous condition is positive, the if-statement will execute its block of code, including the

second condition of the second if-statement, which is inside of the first if-statement.

o Second-Condition: If the first condition is positive, the second condition is going to be evaluated.

o Second-Codes: If the second condition is positive, the second if-statement will execute its block of code; to

clarify, the codes that are inside of the second if-statement will be executed if the second condition is true.

Page 40: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

39

• The switch Statement: A programmer can use the switch statement when he/she wants to build a computer

application that is going to compare a variable-value or expression with different kinds of alternatives; then, at the

same time that a switch statement is comparing an expression with some alternatives or options, the switch

statement is going to execute a block of code only if there is a logical relationship between an expression and an

alternative. In the C programming language, the alternatives of a switch statement are also known as “cases”

because each alternative has the case keyword. As you can see, this is the switch statement syntax:

switch (expression) {

case First-Alternative:

//First-Codes.

break;

case Second-Alternative:

//Second-Codes.

break;

default:

//Default-Codes.

}

o Expression: In this parameter, an individual has to type a variable value, which is going to be the value that a

switch statement will compare with some options or alternatives, also known as cases.

o First-Alternative: In this field, an individual has to type the first value that a switch statement is going to evaluate.

o First-Codes: If the previous alternative is positive, the switch statement is going to execute this block of code.

o Second-Alternative: if the first alternative is negative, the switch statement is going to use the second case to

evaluate a second alternative.

o Second-Codes: If the second alternative is positive, the switch statement is going to execute this block of code.

o Default: At the end of a switch statement, if the previous alternatives are negative, the default case will execute a

block of code.

• The Nested switch Statement: Similar to the nested if-statement, when a coder is using the C programming

language to add a switch statement inside of another switch statement, he/she is performing a programming

Page 41: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

40

technique that is known as the nested switch statement. If a programmer wants to build a nested switch statement,

he/she can see this syntax:

switch (First-Expression) {

case First-Alternative-of-First-Expression:

//First-Codes-of-First-Expression.

switch (Second-Expression) {

case First-Alternative-of-Second-Expression:

//First-Codes-of-Second-Expression

break;

case Second-Alternative-of-Second-Expression:

//Second-Codes-of-Second-Expression.

}

break;

case Second-Alternative-of-First-Expression:

//Second-Codes-of-First-Expression.

}

Source Code – The if-Statement:

#include <stdio.h>

int main() {

int AG_A = 86;

int AG_B = 20;

printf( "The AG_A value is: %d.\n" , AG_A);

printf( "The AG_B value is: %d.\n" , AG_B);

if (AG_A > AG_B)

{

printf( "The AG_A value is superior to the AG_B value.\n" );

}

return 0;

}

Page 42: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

41

Result – The if-Statement:

Source Code – The if-else Statement:

#include <stdio.h>

int main() {

int AG_A = 5;

int AG_B = 5;

printf( "The AG_A value is: %d.\n" , AG_A);

printf( "The AG_B value is: %d.\n\n" , AG_B);

if (AG_A != AG_B) {

printf( "The AG_A value is not equal to the AG_B value.\n" );

}

else {

printf( "The AG_A value is equal to the AG_B value.\n" );

}

return 0;

}

Result – The if-else Statement:

Page 43: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

42

Source Code – The Nested if-Statement:

#include <stdio.h>

int main() {

int AG_A = 5;

int AG_B = 20;

int AG_C = 20;

printf( "The AG_A value is: %d.\n" , AG_A);

printf( "The AG_B value is: %d.\n" , AG_B);

printf( "The AG_C value is: %d.\n\n" , AG_C);

if (AG_A != AG_B) {

printf( "The AG_A value is not equal to the AG_B value.\n" );

if (AG_B = AG_C) {

printf( "The AG_B value is equal to the AG_C value.\n" );

}

}

return 0;

}

Result – The Nested if-Statement:

Page 44: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

43

Source Code – The Switch Statement:

#include <stdio.h>

int main() {

int AGene = 2;

switch (AGene) {

case 1 :

printf( "First Alternative.\n" );

break ;

case 2 :

printf( "Second Alternative.\n" );

break ;

default :

printf( "Default Alternative.\n" );

}

return 0;

}

Result – The Switch Statement:

Page 45: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

44

Source Code – The Nested Switch Statement:

#include <stdio.h>

int main() {

int AG_A = 1;

char AG_B = 'B' ;

switch (AG_A) {

case 1 :

printf( "First Alternative.\n" );

switch (AG_B) {

case 'A' :

printf( "The Second Switch Statement: First Alternative.\n" );

break ;

case 'B' :

printf( "The Second Switch Statement: Second Alternative.\n " );

break ;

}

break ;

case 2 :

printf( "Second Alternative.\n" );

break ;

default :

printf( "Default Alternative.\n" );

}

return 0;

}

Result – The Nested Switch Statement:

Page 46: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

45

Conditional Loops

In the C programming language, an individual or coder can use different kinds of conditional loop statements

when he/she wants to execute a block of code several times, such as five, ten, or more times; furthermore, a

programmer can use some loop statements to execute a block of code an infinite number of times. As you can see,

those are some of the most common loop statements that a programmer can use when he/she is using C to build a

computer application:

• The While-Loop Statement: A C developer can use the while-loop statement when he/she wants to build a

computer application that is going to execute a block of code as long as a condition is positive; shortly, the while-

loop statement will execute some codes as long as a condition is true. As you can see, this is the while-loop

statement syntax:

While (condition) {

//Codes;

}

o Condition: In this field, an individual has to type a condition, which is going to be evaluated by a while-loop

statement.

o Codes: If the previous condition is positive, the while-loop statement is going to execute a block of code; on the

other hand, if the previous condition is negative, the while-loop statement will not execute those kinds of codes

that are inside of the while-loop statement.

One of the most important points that an individual should know about the while-loop statement is that a coder

can use that statement to execute a block of code a number of times; for instance, a C developer can use the

while-loop statement to execute a block of code five, ten, twenty, or more times.

• The For-Loop Statement: Similar to the while-loop statement, an individual can use the for-loop statement when

he/she wants to perform a loop and execute some codes as long as a condition is positive; hence, a coder can also

use the for-loop statement to run a block of code more than one time. Even though both statements are associated

Page 47: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

46

with loops and can run a block of code several times, an individual has to use a while-loop statement and a for-

loop statement in a different way because those statements have a different syntax. When a C developer is using a

while-loop statement, he/she has to use only one field or expression to type a condition; on the other hand, when a

coder is using a for-loop statement, he/she can use three different kinds of fields, also known as expressions. One

of the best ways to learn more about the three expressions of a for-loop statement is seeing its syntax, so this is

the for-loop statement syntax:

for (First-Expression; Second-Expression; Third-Expression) {

//Codes

}

o First-Expression: In the first expression, a C developer has to type the initial value of an operand or variable. An

important point that a coder should know about the first expression of a for-loop statement is that a loop is going

to start with that kind of value that a coder is typing on the first expression. For example, if a coder is using a for-

loop statement to display more than ten numbers or values, he/she has to type the first number or value on the first

expression; then, the first number or value that a computer application is going to display is the value or number

that is on the first expression of a for-loop statement.

o Second-Expression: The second expression is also known as the conditional expression. In the second field, an

individual has to type those kinds of codes that a for-loop statement is going to evaluate. If a condition is positive,

the for-loop statement will execute its block of code; on the other hand, if a condition is negative or false, the loop

is not going to run its codes.

o Third-Expression: The third expression is also called the increment expression because it increases an operand

value. In the third field, a C developer has to type those codes that are going to increase a variable value.

o Codes: If a condition is positive, a for-loop statement will execute its codes.

• The Do-While Loop Statement: Similar to the two previous loop statements, an individual can use the do-while

loop statement to perform a loop and execute a block of code several times. However, one of the differences

Page 48: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

47

between the two previous loops and a do-while loop statement is that a do-while loop statement is going to

execute a block of code before testing a condition. In other words, first, a do-while loop statement is going to run

a block of code; then, after running a block of code, the do-while loop statement will test a condition. Finally, if a

condition is positive, the do-while loop will run its block of code. As you can see, this is the syntax of a do-while

loop statement:

do {

//Codes;

}

while (condition);

o Codes: A do-while loop statement is going to execute or run this block of code.

o Condition: After running the previous block of code, the do-while loop statement will test a condition; then, if the

condition is true, the do-while loop statement will execute the previous block of code, but the do-while loop

statement is going to stop if the condition is negative.

• The break Statement: An individual can use the break statement when he/she wants to finish or end a loop. For

example, a coder can use the break statement inside of a while loop statement to finish a loop if a condition is

positive.

• The continue Statement: A coder can use the continue statement inside of a loop statement when he/she wants to

end only an iteration or sequence of a loop.

• The goto Statement: A coder can use the goto statement when he/she wants to jump from one function or code to

another function or code.

Page 49: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

48

Source Code – The While-Loop Statement:

#include <stdio.h> int main () { int AGene = 80; while ( AGene <= 86 ) { printf( "The AGene Value is: %i \n" , AGene); AGene++; } return 0; }

Result – The While-Loop Statement:

Source Code – The For-Loop Statement:

#include <stdio.h> int main () { int AGene; for (AGene = 15; AGene <= 20; AGene++) { printf( "The AGene value is: %i \n" , AGene); } return 0;

}

Result – The For-Loop Statement:

Page 50: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

49

Source Code – The Do-While Loop Statement:

#include <stdio.h>

int main () {

int AGene = 0;

do {

printf( "The AGene value is: %i \n" , AGene);

AGene++;

}

while (AGene <= 5);

return 0;

}

Result – The Do-While Loop Statement:

Page 51: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

50

Source Code – The Break Statement:

#include <stdio.h>

int main () {

int AGene = 80;

while ( AGene <= 90 ) {

printf( "The AGene value is: %i \n" , AGene);

AGene++;

if ( AGene > 86) {

break ;

}

}

return 0;

}

Result – The Break Statement:

Page 52: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

51

Source Code – The Continue Statement:

#include <stdio.h>

int main () {

int AGene = 15;

do {

if ( AGene == 21) {

AGene = 80;

continue ;

}

printf( "The AGene value is: %i \n" , AGene);

AGene++;

}

while ( AGene <= 86 );

return 0;

}

Result – The Continue Statement:

Page 53: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

52

Source Code – The goto Statement:

#include <stdio.h>

int main () {

int AGene = 15;

TERAEXE:

do {

if ( AGene == 21) {

AGene = 80;

goto TERAEXE;

}

printf( "The AGene value is: %i \n" , AGene);

AGene++;

}

while ( AGene <= 86 );

return 0;

}

Result – The goto Statement:

Page 54: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

53

Functions

In this section, we will learn how to build a function. What is a function? It is a block of code that a computer

application is going to use to perform a particular task; for example, a computer application can use a function to add

some values to an operand, find the maximum value of a group of variables, find the minimum value of a group of

numbers, and many other tasks. When a coder wants to build a computer application that is going to perform a

particular task several times, he/she can build a function instead of typing the same block of code more than one time;

then, after building a function, an individual only has to call that function to perform a particular task.

In the first source code, we are going to build a function, which is known as the Teraexe function. What is the

purpose of the Teraexe function? It is to add some values to an operand; more specifically, the purpose of the Teraexe

function is to add the value eighty-six (86) to an operand or variable. At the beginning of the next source code, the

AG_A value is twenty-three (23); then, after calling the Teraexe function, the AG_A value is going to be one-

hundred nine (109) because twenty-three plus eighty-six is one-hundred nine.

In the second source code, we are going to build and call the Teraexe function to add some values to a new

operand or variable. In this case, instead of adding eighty-six (86) to the AG_A variable, we are going to add the

values of the AG_A variable and the Teraexe function to a new variable, which is known as the AG_B variable. At

the end, the AG_A value will be twenty-three (23), and the AG_B value will be one-hundred nine (109). Sometimes,

when an individual is building and calling a function, he/she has to perform a task and conserve the original values of

his/her operands; therefore, in the second source code, after building and calling the Teraexe function to add some

values, the AG_A value will conserve its original value, which is twenty-three (23).

Page 55: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

54

Source Code – First Example:

#include <stdio.h>

//Declaring the Teraexe function:

int Teraexe( int AG_Adding_86);

int main () { //The main function.

int AG_A = 23; //Declaring a variable.

//Calling the Teraexe function:

AG_A = Teraexe(AG_A);

//Displaying a variable value:

printf( "The AG_A value is: %i \n" , AG_A);

return 0;

}

//Building a function that is going to add 86:

int Teraexe( int AG_Adding_86) {

int AG_Variable; //Declaring a variable.

AG_Variable = AG_Adding_86 + 86; //Adding 86.

return AG_Variable; //Returning a variable value.

}

Result – First Example:

Page 56: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

55

Source Code – Second Example:

#include <stdio.h>

//Declaring the Teraexe function:

int Teraexe( int AG_Adding_86);

int main () { //The main function.

int AG_A = 23; //Declaring a variable.

int AG_B; //Declaring a variable to call a function.

//Calling the Teraexe function:

AG_B = Teraexe(AG_A);

//Displaying the AG_A value:

printf( "The AG_A value is: %i \n" , AG_A);

//Displaying the AG_B value:

printf( "The AG_B value is: %i \n" , AG_B);

return 0;

}

//Building a function that is going to add 86:

int Teraexe( int AG_Adding_86) {

int AG_Variable; //Declaring a variable.

AG_Variable = AG_Adding_86 + 86; //Adding 86.

return AG_Variable; //Returning a variable value.

}

Result – Second Example:

Page 57: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

56

Variable Scope

While an individual or coder is declaring a variable, he/she has to choose a variable scope. What is a variable

scope? It is the location of a variable, which can be local or global. On the one hand, a variable of local scope is a

variable that is declared inside of a function; on the other hand, a variable of global scope is a variable that is declared

outside of a function. In the next source code, I am declaring a variable of local scope that is called AG_B, and I am

declaring a variable of global scope that is known as AG_A.

One of the most important facts that a C developer should know about a variable scope is that a variable of local

scope can be used only inside of a function; to clarify, if an individual declares a variable inside of a function, he/she

can use that variable only inside of that function. However, if a coder declares a variable of global scope, he/she can

use that variable on different kinds of functions.

Source Code – Variable Scope:

#include <stdio.h>

int AG_A = 86; //Declaring a variable of global scope.

int main () { //The main function.

int AG_B = 20; //Declaring a variable of local scope.

//Displaying a global variable:

printf( "The AG_A value is: %i \n" , AG_A);

//Displaying a local variable:

printf( "The AG_B value is: %i \n" , AG_B);

return 0;

}

Result – Variable Scope:

Page 58: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

57

Arrays

In this chapter, you will learn how to build some arrays. What is an array? In computer science, an array is a

variable that can be used by a coder to store and retrieve a group of values, which are known as the elements of an

array. Therefore, instead of declaring a lot of variables that are going to hold some values, an individual may declare

only an array that is going to hold a group of values. As you can see, this is an example of how to declare an array

that is going to hold four elements or values: int AG_Array[] = {5, 20, 86, 23};. In the previous line, you will notice

that I am declaring an array with the name of AG_Array; furthermore, you are going to notice that I am including

those four elements inside of the AG_Array array: 5, 20, 86, 23. In that case, the first element of AG_Array is 5, the

second element of AG_Array is 20, the third element of AG_Array is 86, and so on.

When a C developer wants to use the printf statement to display the first element of an array, he/she has to type a

line similar to this: printf("Displaying the first element of an array: %i", AG_Array[0]);. As you can see in the

previous line, I am typing the 0 value to display the first element of an array. Why? In the C programming language,

arrays are zero-based, which means that an individual has to type 0 to display the first element of an array, 1 to

display the second element of an array, 2 to display the third element of an array, and so on.

In the second source code, you are going to see an example of how to use the for-loop statement to display all the

elements of an array; hence, instead of typing four lines of codes to display each single element of an array, I am

going to use only three lines to display all the elements of an array through the for-loop statement. If a programmer

wants to use the for-loop statement to display all the elements of an array, he/she has to type a block of code similar

to this:

for (AG_Loop = 0; AG_Loop < 4; AG_Loop++) {

printf("%i \n", AG_Array[AG_Loop]);

}

In the previous block of code, I am typing “AG_Loop < 4” because I want to display the first four elements of an

array. Therefore, in the second expression of a for-loop statement, if an individual wants to show the first three

Page 59: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

58

elements of an array, he/she may type a code similar to this: AG_Loop < 3. As previously stated, the second

expression of a for-loop statement is a conditional expression that is going to evaluate and execute a block of code

only if a condition is positive, but it is not going to evaluate and execute some codes if a condition is negative. In the

previous block of code, the loop statement is going to execute a block of code provided that an array is less than four.

In the third source code, you are going to see an example of how to build a two-dimensional array, also known as

a 2D-array. What is a two-dimensional array? It is an array of arrays that is composed of some rows and columns,

such as this:

int AG_Array[2][3] =

{

{55, 20, 86},

{11, 12, 23}

};

One of the most important facts that a C developer should know about the previous block of code is that I am

typing some digits related to the number of rows and columns. As you can see, I am typing 2 because there are two

rows, and I am typing 3 because there are three columns (AG_Array[2][3] ). After building or declaring a two-

dimensional array, a coder can use the printf statement to display some values; for example, if an individual wants to

display the number 12 that is located on the second row and second column, he/she has to type a line similar to this:

printf("Displaying an element of a two-dimensional array: %i", AG_Array[1][1]); .

Page 60: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

59

Source Code – Using printf to display elements:

#include <stdio.h>

int main () { //The main function.

//Declaring an array with three elements:

int AG_Array[] = {5, 20, 86};

//Using the 0 value to display the first element of an array:

printf( "The first element of an array: %i \n" , AG_Array[0]);

//Using the 1 value to display the second element o f an array:

printf( "The second element of an array: %i \n" , AG_Array[1]);

//Using the 2 value to display the third element of an array:

printf( "The third element of an array: %i \n" , AG_Array[2]);

return 0;

}

Result – Using printf to display elements:

Page 61: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

60

Source Code – Using a for-loop to display elements:

#include <stdio.h>

int main () { //The main function.

//Declaring a variable for the for-loop statement:

int AG_Loop;

//Declaring an array with four elements:

int AG_Array[] = {5, 20, 86, 23};

printf( "Displaying all the elements of an array: \n" );

/* Using the for-loop statement to display all the elements

of an array: */

for (AG_Loop = 0; AG_Loop < 4; AG_Loop++) {

printf( "%i \n" , AG_Array[AG_Loop]);

}

return 0;

}

Result – Using a for-loop to display elements:

Page 62: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

61

Source Code – Two-Dimensional Array:

#include <stdio.h>

int main () { //The main function.

int AG_Array[2][3] =

{

{55, 20, 86},

{11, 12, 23}

};

//First row, first column:

printf( "The value is: %i \n" , AG_Array[0][0]);

//First row, second column:

printf( "The value is: %i \n" , AG_Array[0][1]);

//First row, third column:

printf( "The value is: %i \n" , AG_Array[0][2]);

//Second row, first column:

printf( "The value is: %i \n" , AG_Array[1][0]);

//Second row, second column:

printf( "The value is: %i \n" , AG_Array[1][1]);

//Second row, third column:

printf( "The value is: %i \n" , AG_Array[1][2]);

return 0;

}

Result – Two-Dimensional Array:

Page 63: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

62

Pointers

Before providing some information about the pointers, I am going to provide a review related to the variables. As

previously stated, when an individual wants to store and retrieve information inside of the random-access memory of

a computer, he/she has to declare a variable. An important point that a coder should know about a variable is that its

value is going to be stored as a hexadecimal value on the random-access memory of a system; for example, the value

86 may be stored as e8feec. If a coder wants to display the memory address of a variable, he/she has to use the

ampersand operator (&) and type a line similar to this: printf ("Memory address of a variable: %x", &Variable);.

In the C programming language, when an individual wants to allocate a memory address, he/she has to build a

pointer variable. What is a pointer variable? It is a variable that is going to hold the memory address of a regular

variable. In a previous section, we learned how to build regular variables to store or hold different kinds of values; in

this chapter, we will learn how to build pointer variables to hold some memory addresses. If an individual or coder

wants to build a pointer variable, he/she has to use the unary operator (*) and type a line similar to this: Data-Type

*Pointer_Variable;. As you can see, this is an example: int *AG_Pointer;. Then, if an individual wants to store the

memory address of a regular variable inside of a pointer variable, he/she has to use the ampersand operator (&) and

type a line like this: Pointer_Variable = &Regular_Variable;. Finally, if a C developer wants to use a pointer

variable to display the value of a regular variable, he/she can use the unary operator (*) and type a line similar to this:

printf("Using a pointer to display the value of a regular variable: %i", *Pointer_Name);.

Page 64: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

63

Source Code – Pointers:

#include <stdio.h>

int main () { //The main function.

int AG_A = 86; //Declaring a regular variable.

int AG_B = 20; //Declaring a regular variable.

int *AG_Pointer; //Declaring a pointer variable.

//Storing the memory address of AG_A on AG_Pointer:

AG_Pointer = &AG_A;

//Displaying the memory address stored on AG_Pointe r:

printf( "Memory address stored on AG_Pointer: %x \n" , AG_Pointer);

//Displaying the memory address of AG_A:

printf( "The AG_A memory address: %x \n" , &AG_A);

/*Displaying the AG_B memory address. In this case, you are going to

notice that the result is different than the oth er two results.*/

printf( "The AG_B memory address: %x \n" , &AG_B);

//Using a pointer variable to display the value of a regular variable:

printf( "Using a pointer to display the AG_A value: %i \n" , *AG_Pointer);

return 0;

}

Result – Pointers:

Page 65: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

64

Strings

After learning how to use different kinds of arrays, a C developer should expand his/her knowledge and learn how

to build a string. What is a string? In computer programming, a string is an array that is composed of a group of

characters; furthermore, at the end of a string, there is a null character: \0. On the one hand, if an individual wants to

use the traditional way to build a string, he/she has to type a line similar to this: char array[5] = {'G', 'e', 'n', 'e', '\0'};.

On the other hand, if a C developer wants to use the double quote style to build a string, he/she has to type a line like

this: char string[] = "Gene";. As you can see in the previous code, I am not typing a null character (0) because a C

compiler is going to place a null character automatically. An important point that a coder should know about the C

programming language and its strings is that an individual can use different kinds of string commands to manipulate

his/her strings, including some commands like strcpy, strcat, strlen, and strcmp.

• The strcpy command: A C developer can use the strcpy command when he/she wants to copy the characters of a

string into another string. If a string has some characters, those characters are going to be deleted and replaced for

the characters of another string. For example, in the next source code, we are declaring those two strings:

char AG_String_A[] = "String One"; char AG_String_B[] = "String Two";

Then, after using the strcpy command, the AG_String_A string is able to obtain the value of the AG_String_B

string. As you can see, this is the syntax of the strcpy command:

strcpy (String_A, String_B);

o String_A: In this parameter, a coder has to type a string, which will be the string that will receive some characters.

o String_B: In this field, a programmer has to type a string, which will be a string that is going to copy some

characters into another string.

• The strcat command: An individual can use the strcat command when he/she wants to concatenate some strings;

to put it another way, a coder can use the strcat command to concatenate or put-together the characters of two

strings. As you can see, this is the syntax of the strcat command:

strcat (String_A, String_B);

Page 66: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

65

o String_A: In this parameter, a C developer has to type a string, which will be the string that is going to receive

some characters at the end of its characters.

o String_B: In this field, an individual has to type that kind of string that is going to place or put some characters at

the end of another string.

• The strlen command: After performing a string concatenation, a C developer can use the strlen command to

display the length of a string. If an individual wants to display the length of a string, he/she has to type a block of

code similar to this:

AG_Length_A = strlen(AG_String_A);

printf("Length of AG_String_A: %i \n", AG_Length_A);

As you can see, this is an example of how a computer organizes a string and its memory:

String: char array[5] = {'G', 'e', 'n', 'e', '\0'};

String: char string[] = "Gene";.

Index: G = 0, e = 1, n = 2, e = 3, 0 = 4. Total length: 4.

Memory: G = 0xCCFFC1, e = 0xCCFFC2, n = 0xCCFFC3, e = 0xCCFFC4, 0 = 0xCCFFC5.

• The strcmp command: When an individual wants to know if the value of a string is superior/inferior/equal to the

value of another string, he/she can use the strcmp command. In this case, the strcmp command is going to display

a positive number (1) if the value of a string is superior to the value of another string; on the other hand, the

strcmp command is going to display a negative number (-1) if the value of a string is less than the value of

another string. In addition to those results, the strcmp command will show the number zero (0) if both strings

have the same value. If an individual wants to use the strcmp command to compare two string values, he/she has

to type a block of code similar to this:

AG_strcmp = strcmp(String_A, String_B);

printf("Comparing two strings: %i \n", AG_strcmp);

Page 67: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

66

In the previous block of code, the strcmp command is going to display a positive number (1) if the value of

String_B is superior to the value of String_A; on the other hand, the strcmp command will show a negative

number (-1) if the value of String_B is less than the value of String_A.

Source Code – Strings:

#include <stdio.h>

#include <string.h>

int main () {

char AG_String_A[] = "String One" ;

char AG_String_B[] = "String Two" ;

char AG_String_C[] = " and String Three" ;

int AG_Length_A;

//Displaying a String:

printf( "Value of AG_String_A: %s \n" , AG_String_A);

/*Copying a string (AG_String_B to AG_String_A).

Now, AG_String_A has this value: String Two.*/

strcpy(AG_String_A, AG_String_B);

//Displaying AG_String_A:

printf( "Value of AG_String_A: %s \n" , AG_String_A);

/*Concatenating a string (AG_String_A & AG_String_C ):

Now, AG_String_A has this value: String Two and String Three*/

strcat(AG_String_A, AG_String_C);

//Displaying AG_String_A:

printf( "Value of AG_String_A: %s \n" , AG_String_A);

//Displaying the length of a string after its conca tenation:

AG_Length_A = strlen(AG_String_A);

printf( "Length of AG_String_A: %i \n" , AG_Length_A);

return 0;

}

Result – Strings:

Page 68: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

67

Source Code – Comparing Strings:

#include <stdio.h>

#include <string.h>

int main () {

char AG_A[] = "20" ;

char AG_B[] = "20" ;

char AG_C[] = "5" ;

int AG_strcmp;

printf( "The AG_A value is: %s \n" , AG_A);

printf( "The AG_B value is: %s \n" , AG_B);

printf( "The AG_C value is: %s \n\n" , AG_C);

//Knowing if a string is less/more than another str ing.

//It will return 0 because AG_A is equal to AG_B.

AG_strcmp = strcmp(AG_A, AG_B);

printf( "Comparing AG_B and AG_A: %i \n" , AG_strcmp);

//Comparing AG_C and AG_B.

//It will return -1 because AG_C is less than AG_B.

AG_strcmp = strcmp(AG_B, AG_C);

printf( "Comparing AG_C and AG_B: %i \n" , AG_strcmp);

//Comparing AG_A and AG_C.

//It will return 1 because AG_A is more than AG_C.

AG_strcmp = strcmp(AG_C, AG_A);

printf( "Comparing AG_A and AG_C: %i \n" , AG_strcmp);

return 0;

}

Result – Comparing Strings:

Page 69: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

68

Structures

In a previous exercise, we learned how to build an array, which is a “derived data-type” that is composed of some

elements of “a single” data-type. However, in this chapter, we are going to learn how to build a structure, which is a

“defined data-type” that can be composed of some element of “a group” of data-types; to put it another way, a

structure is a data-type that an individual can use when he/she wants to define some variables or items with different

kinds of data-types. When a C developer wants to build a structure to define a group of data-types, he/she can use the

struct statement and type a block of code similar to this:

struct Structure_Name {

First_Data-Type Variable_Name;

Second_Data-Type Variable_Name;

Third_Data-Type Variable_Name;

}

An important point that a coder should know about a structure is that a data-type and its variable name is known as a

member; in other words, in a structure, a data-type and its variable name is called a member. Following this logic, in

the previous block of code, you are going to notice that there are three members inside of a structure.

After building a structure, a coder has to define a structure inside of a function, such as the main function; in that

case, an individual has to type a line of code similar to this: Structure_Name Simple_Structure. Then, after defining a

structure inside of a function, an individual has to specify a value. On the one hand, if a C developer wants to add

some values to an integer data-type, he/she can use the member access operator (.) and type a line of code like this:

Simple_Structure.Variable_Name = Value. On the other hand, if a programmer wants to add some values to a char

data-type, he/she has to use the strcpy function and type a line similar to this:

strcpy(Simple_Structure.Variable_Name, "Value");. Finally, after following the previous steps, an individual can use

the printf statement to display some variable values. As you can see, this is an example:

printf("Displaying a value: %s", Simple_Structure.Variable_Name);

Page 70: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

69

• Structures and Functions: While an individual is working with structures, he/she can use some functions to

display a group of values without typing a lot of codes. At the end of this section, you are going to see an example

of how to build a structure and a function to display a group of values.

• Structures and Pointers: While an individual is building some structures and functions, he/she can work with

pointers. In that case, when an individual is declaring a function, he/she has to use an asterisk (*) to build a

pointer inside of a function. As you can see, this is an example:

void Function_Name(struct Structure_Name *Structure_Pointer);. Then, after building a function, an individual

has to store a variable on a function; more specifically, after building a function with a pointer, a coder has to

store a structure variable with a memory address inside of a function, in that case, a coder has to use the &

operator and type a line of code similar to this: function_Name(&Structure_Variable_Name);. Finally, after

following the previous steps, an individual can use a function and a printf statement to display that kind of

information that is stored on a variable; in that case, a C developer has to type a block of code similar to this:

void Function_Name(struct Structure_Name *Structure_Pointer) {

printf("Displaying a structure-variable value: %s", Structure_Pointer->First_Structure_Variable);

printf("Displaying a structure-variable value: %s", Structure_Pointer->Second_Structure_Variable);

printf("Displaying a structure-variable value: %s", Structure_Pointer->Third_Structure_Variable);

}

As you can see in the previous block of code, I am using a pointer and a -> operator to gain access to that kind of

information or data that is stored inside of a structure variable.

Page 71: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

70

Source Code – Structures:

#include <stdio.h>

#include <string.h>

//Declaring the first structure:

struct Hard_Commodities {

char Platinum[86];

char Diamond[86];

char Gold[86];

};

//Declaring the second structure:

struct Soft_Commodities {

char Coffee[120];

char Corn[120];

};

int main() { //The main function.

//Declaring hard commodities:

struct Hard_Commodities HC_Countries_A;

struct Hard_Commodities HC_Countries_B;

struct Hard_Commodities HC_Countries_C;

//Declaring soft commodities:

struct Soft_Commodities SC_Countries_A;

struct Soft_Commodities SC_Countries_B;

//Hard Commodities - First countries:

strcpy(HC_Countries_A.Platinum, "South Africa." );

strcpy(HC_Countries_A.Diamond, "Australia." );

strcpy(HC_Countries_A.Gold, "USA." );

Page 72: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

71

//Hard Commodities - Second countries:

strcpy(HC_Countries_B.Platinum, "Russia." );

strcpy(HC_Countries_B.Diamond, "South Africa." );

strcpy(HC_Countries_B.Gold, "Germany." );

//Hard Commodities - Third countries:

strcpy(HC_Countries_C.Platinum, "Canada." );

strcpy(HC_Countries_C.Diamond, "Russia." );

strcpy(HC_Countries_C.Gold, "Italy." );

//Soft Commodities - First Countries:

strcpy(SC_Countries_A.Coffee, "Brazil." );

strcpy(SC_Countries_A.Corn, "USA." );

//Soft Commodities - Second Countries:

strcpy(SC_Countries_B.Coffee, "Vietnam." );

strcpy(SC_Countries_B.Corn, "China." );

//Displaying the first countries with hard commodit y reserves:

printf( "First Major Countries with Hard Commodity Reserves :\n" );

printf( "Country with Platinum reserves: %s \n" , HC_Countries_A.Platinum);

printf( "Country with Diamond Reserves: %s \n" , HC_Countries_A.Diamond);

printf( "Country with Gold Reserves: %s \n\n" , HC_Countries_A.Gold);

//Displaying the second major countries with hard c ommodity reserves:

printf( "Second Major Countries with Hard Commodity Reserve s:\n" );

printf( "Country with Platinum reserves: %s \n" , HC_Countries_B.Platinum);

printf( "Country with Diamond Reserves: %s \n" , HC_Countries_B.Diamond);

printf( "Country with Gold Reserves: %s \n\n" , HC_Countries_B.Gold);

Page 73: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

72

//Displaying the third major countries with hard co mmodity reserves:

printf( "Third Major Countries with Hard Commodity Reserves :\n" );

printf( "Country with Platinum reserves: %s \n" , HC_Countries_C.Platinum);

printf( "Country with Diamond Reserves: %s \n" , HC_Countries_C.Diamond);

printf( "Country with Gold Reserves: %s \n\n" , HC_Countries_C.Gold);

//Displaying the major soft-commodity producing cou ntries:

printf( "First Major Countries Producing Soft-Commodities:\ n" );

printf( "Country with Coffee Production: %s \n" , SC_Countries_A.Coffee);

printf( "Country with Corn Production: %s \n\n" , SC_Countries_A.Corn);

//Displaying the second major soft-commodity produc ing countries:

printf( "Second Major Countries Producing Soft-Commodities: \n" );

printf( "Country with Coffee Production: %s \n" , SC_Countries_B.Coffee);

printf( "Country with Corn Production: %s \n\n" , SC_Countries_B.Corn);

return 0;

}

Result – Structures:

Page 74: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

73

Source Code – Structures & Functions:

#include <stdio.h>

#include <string.h>

//Declaring the first structure:

struct Teraexe_PC {

char Operating_System[86];

char Memory_RAM[86];

char Hard_Drive[86];

};

//Declaring a function:

void Display_PC( struct Teraexe_PC PC);

int main() { //The main function.

//Declaring some computers:

struct Teraexe_PC First_Teraexe_PC;

struct Teraexe_PC Second_Teraexe_PC;

struct Teraexe_PC Third_Teraexe_PC;

//First Computer:

strcpy(First_Teraexe_PC.Operating_System, "Linux." );

strcpy(First_Teraexe_PC.Memory_RAM, "4GB." );

strcpy(First_Teraexe_PC.Hard_Drive, "250GB." );

//Second Computer:

strcpy(Second_Teraexe_PC.Operating_System, "Windows." );

strcpy(Second_Teraexe_PC.Memory_RAM, "8GB." );

strcpy(Second_Teraexe_PC.Hard_Drive, "500GB." );

//Third Computer:

Page 75: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

74

strcpy(Third_Teraexe_PC.Operating_System, "MAC." );

strcpy(Third_Teraexe_PC.Memory_RAM, "16GB." );

strcpy(Third_Teraexe_PC.Hard_Drive, "1TB." );

//The next line is to display some data about the f irst PC:

Display_PC(First_Teraexe_PC);

//The next line is to display some data about the s econd PC:

Display_PC(Second_Teraexe_PC);

//The next line is to display some data about the t hird PC:

Display_PC(Third_Teraexe_PC);

return 0;

}

void Display_PC( struct Teraexe_PC PC) {

//Displaying Computers:

printf( "Operating System: %s \n" , PC.Operating_System);

printf( "Memory RAM: %s \n" , PC.Memory_RAM);

printf( "Hard Drive: %s \n\n" , PC.Hard_Drive);

}

Result – Structures & Functions:

Page 76: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

75

Source Code – Structures & Pointers:

#include <stdio.h>

#include <string.h>

//Declaring the first structure:

struct Teraexe_PC {

char Operating_System[86];

char Memory_RAM[86];

char Hard_Drive[86];

};

//Declaring a function:

void Display_PC( struct Teraexe_PC *PC);

int main() { //The main function.

//Declaring some computers:

struct Teraexe_PC First_Teraexe_PC;

struct Teraexe_PC Second_Teraexe_PC;

struct Teraexe_PC Third_Teraexe_PC;

//First Computer:

strcpy(First_Teraexe_PC.Operating_System, "Linux." );

strcpy(First_Teraexe_PC.Memory_RAM, "4GB." );

strcpy(First_Teraexe_PC.Hard_Drive, "250GB." );

//Second Computer:

strcpy(Second_Teraexe_PC.Operating_System, "Windows." );

strcpy(Second_Teraexe_PC.Memory_RAM, "8GB." );

strcpy(Second_Teraexe_PC.Hard_Drive, "500GB." );

//Third Computer:

Page 77: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

76

strcpy(Third_Teraexe_PC.Operating_System, "MAC." );

strcpy(Third_Teraexe_PC.Memory_RAM, "16GB." );

strcpy(Third_Teraexe_PC.Hard_Drive, "1TB." );

//The next line is to display some data about the f irst PC:

Display_PC(&First_Teraexe_PC);

//The next line is to display some data about the s econd PC:

Display_PC(&Second_Teraexe_PC);

//The next line is to display some data about the t hird PC:

Display_PC(&Third_Teraexe_PC);

return 0;

}

void Display_PC( struct Teraexe_PC *PC) {

//Displaying Computers:

printf( "Operating System: %s \n" , PC->Operating_System);

printf( "Memory RAM: %s \n" , PC->Memory_RAM);

printf( "Hard Drive: %s \n\n" , PC->Hard_Drive);

}

Result – Structures & Pointers:

Page 78: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

77

Unions

Similar to structures, a union is a defined data-type that an individual can use to store a group of members; as

previously stated, a member is composed of a data-type and its variable name, so a coder can use a union to store a

data-type and its variable name. Even though a C developer can use a structure or a union to store a group of

members, there is a difference related to their memory locations. On the one hand, a coder can use a structure to

define a group of members with different kinds of memory addresses; on the other hand, a programmer can use a

union to define a group of members with the same memory address, in other words, a C developer can use a union to

build a group of data-types with the same memory address. Because a union can store a group of members with the

same memory address, an individual cannot access to a group of union-members at the same time, which means that a

coder cannot display all the values of a group of union-members at the same time; in contrast, because a structure can

store a group of members with different kinds of memory addresses, an individual can access to a group of structure-

members at the same time, which means that a C developer can display all the values of a group of structure-members

at the same time. At the end of this section, an individual is going to see two source codes or computer applications.

The first computer application is going to display an error because a coder cannot access to a group of union-

members at the same time, but the second application is not going to display an error because it is not accessing to a

group of union-members simultaneously.

• Union Syntax: When a C developer wants to define a union that is going to store some members, he/she has to

type a block of code similar to this:

union Union_Name {

First_Data-Type Variable_Name;

Second_Data-Type Variable_Name;

}

• Displaying a Union: A C developer can use the printf statement and the member access operator (.) when he/she

wants to gain access to a union member and display its value. As you can see, this is an example:

printf("Displaying a union member: %s", Union_Variable.Union_Member);

Page 79: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

78

Source Code – Unions: The Wrong Way:

#include <stdio.h>

#include <string.h>

//Declaring a union:

union AG_Teraexe_Products {

int AG_Cost; //Defining an int data-type.

char AG_Name[23]; //Defining a char data-type.

};

int main() { //The main function.

union AG_Teraexe_Products AGTP;

//Using strcpy to add data to AG_Name:

strcpy(AGTP.AG_Name, "Laptop" );

//Adding data to AG_Cost:

AGTP.AG_Cost = 500;

//Trying to display a product name; however, it wil l show a worm with a happy face.

printf( "Product Name: %s \n" , AGTP.AG_Name);

//Displaying the product cost:

printf( "Product Cost: %i \n" , AGTP.AG_Cost);

return 0;

}

Result – Unions: The Wrong Way:

Page 80: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

79

Source Code – Unions: The Right Way:

#include <stdio.h>

#include <string.h>

//Declaring a union:

union AG_Teraexe_Products {

int AG_Cost; //Defining an int data-type.

char AG_Name[23]; //Defining a char data-type.

};

int main() { //The main function.

union AG_Teraexe_Products AGTP;

//Using strcpy to add data to AG_Name:

strcpy(AGTP.AG_Name, "Laptop" );

//Displaying the product name:

printf( "Product Name: %s \n" , AGTP.AG_Name);

//Adding data to AG_Cost:

AGTP.AG_Cost = 500;

//Displaying the product cost:

printf( "Product Cost: %i \n" , AGTP.AG_Cost);

return 0;

}

Result – Unions: The Right Way:

Page 81: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

80

Input & Output

In this section, an individual can start learning about input and output operations. What is an input? In computer

science, an input is a place where an individual/application/file is going to put some data or information. For

example, if an individual is able to build a computer application that is going to put some characters on a text file,

he/she can say that he/she is going to place his/her characters on an input, which is the text file. If an individual wants

to be more specific, he/she can say that the input is a place inside of a text file, which is the location where his/her

characters are going to be added. As you can see, this is another example of an input: if an individual is using an

integrated development environment (IDE) to develop a computer application, he/she has to type his/her codes inside

of a place, which is known as the input of an IDE. What is an output? In information technology, an output is the

result of a computer application; in other words, an output is what a computer program is going to display or show

inside of a computer screen or computer terminal. For example, if an individual is able to build a computer

application that is going to display or show some characters on a command-line, he/she can say that the output of

his/her program are those characters that he/she is going to see inside of his/her command-line.

In the C programming language, when an individual wants to perform input/output operations, he/she can use

those functions: getchar, putchar, gets, puts, scanf, and printf.

• The getchar Function: An individual or coder can use the getchar function when he/she wants to read a single

character and store its value inside of a variable. If an individual wants to use the getchar function to store a

character on a variable, he/she has to type a line of code similar to this: Variable_Name = getchar();.

• The putchar Function: After using the getchar function to read a single character and store its value on a variable,

a C developer can use the putchar function to put or place a single character on a computer program or computer

terminal. As you can see, this is the syntax of the putchar function: putchar(Variable_Name);. In the first

parameter of the putchar function, a coder has to type a variable, which is going to be a variable with that kind of

value or character that a coder wants to display on a computer program or computer terminal.

Page 82: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

81

• The gets Function: If an individual wants to store a group of characters instead of a single character, he/she can

use the gets function; to put it another way, a coder can use the gets function when he/she wants to store a group

of characters inside of a variable. As you can see, this is the syntax of the gets function: gets(Variable_Name);. In

the first parameter of the gets function, a C developer has to type that kind of variable that is going to store a

group of characters.

• The puts Function: After using the gets function to store a group of characters on a variable, a coder can use the

puts function to display or show a group of characters inside of a computer program or computer terminal. If an

individual wants to use the puts function, he/she has to type a line of code similar to this: puts(Variable_Name);.

In variable name, an individual has to type a variable with that kind of value that an individual wants to display

on a computer program or computer terminal.

• The scanf Function and the printf Statement: In addition to the previous functions, a coder can use the scanf

function and the printf statement to perform input and/or output operations. If an individual wants to store a group

of characters/digits inside of a variable, he/she can use the scanf function; on the other hand, if a coder wants to

display a group of characters/digits on a computer program or computer terminal, he/she can use the printf

statement. If a C developer wants to use the scanf function and the print statement to perform input and/or output

operations, he/she has to type a block of code similar to this:

scanf("%i %s", &Digit_Variable, Character_Variable);

printf("%i %s", Digit_Variable, Character_Variable);

o Scanf Function – Digit_Variable: In this parameter, a coder has to type that kind of variable that will store a digit

or number. As you can see in the previous code, I am using the & operator before typing a variable name. Each

time that a coder wants to use the scanf function to store a number, he/she has to use the & operator.

o Scanf Function – Character_Variable: In this field, an individual has to type a variable, which is going to be a

variable that will store a string or group of characters.

Page 83: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

82

Source Code – getchar and putchar:

#include <stdio.h> int main() { //Declaring a variable to get and put a character: char AG_Get_n_Put; printf( "Type a character: " ); //Using the getchar function to get a character: AG_Get_n_Put = getchar(); printf( "Character: " ); //Using the putchar function to put a character: putchar(AG_Get_n_Put); printf( " \ n" ); return 0; }

Result – getchar and putchar:

Source Code – gets and puts:

#include <stdio.h> int main() { //Declaring a variable to get and put some characte r: char AG_Get_n_Put[86]; printf( "Type a group of characters: " ); //Using the gets function to get some characters: gets(AG_Get_n_Put); printf( "Characters: " ); //Using the puts function to put or place some characters: puts(AG_Get_n_Put); return 0;

}

Result – gets and puts:

Page 84: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

83

Source Code – The scanf Function – First Example:

#include <stdio.h> int main() { //Declaring a variable to get and put a digit: int AG_Get_n_Put_Digit; //Declaring a va r iable to get and put some character: char AG_Get_n_Put_Characters[86]; //Using scanf to get a digit and some characters: printf( "Type a digit; then, type some characters: " ); scanf( "%i %s" , &AG_Get_n_Put_Digit, AG_Get_n_Put_Characters); //Displaying a digit and some characters: printf( "%i %s" , AG_Get_n_Put_Digit, AG_Get_n_Put_Characters); printf( " \ n" ); return 0; }

Result – The scanf Function – First Example:

Source Code – The scanf Function – Second Example:

#include <stdio.h> int main() { //Declaring a variable to get and put some digits: int AG_DA, AG_DB, AG_DC; //Declaring a variable to get and put some characte r: char AG_Get_n_Put_Characters[86]; //Using scanf to get three numbers and some charact ers: printf( "Type a group of three digits; then, type some char acters: " ); scanf( "%i %i %i %s" , &AG_DA, &AG_DB, &AG_DC, AG_Get_n_Put_Characters); //Displaying three numbers and some characters: printf( "%i %i %i %s" , AG_DA, AG_DB, AG_DC, AG_Get_n_Put_Characters); printf( "\n" ); return 0; }

Result – The scanf Function – Second Example:

Page 85: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

84

Input & Output: Text Files

In the previous chapter, we learned how to use different kinds of C functions to store some values on a variable

and display those values on a computer terminal. In this chapter, we will learn how to perform input and output

operations on text files; more specifically, we are going to learn how to store some characters on a text file, and we

will learn how to read a text file to display its characters inside of a computer terminal. When we want to perform

input and/or output operations on text files, we can use those functions: fopen, fclose, fputs, and fgets.

• The fopen Function: A C developer can use the fopen function when he/she wants to open a text file. If a

computer does not have a text file, the fopen function is going to create a text file, which means that a coder can

use the fopen function to open or create a text file. As you can see, this is the syntax of the fopen function:

fopen(File-Directory, File-Instruction);

o File-Directory: In the first parameter, an individual has to type a file-directory, which is the location of a text file.

o File-Instruction: In the second parameter, a coder has to type an instruction related to the way that a text file is

going to be open/reading/writing. As you can see, those are some of the most common instructions that a coder

can use on the second parameter of the fopen function:

� The r Keyword: A coder can use the r keyword to open a file and display its characters on a terminal/program.

� The w Instruction: A C developer can use the w instruction to open a text file. An important fact that a coder

should know about the w keyword is that it is going to create a new file if a computer does not have a text file on

a specific directory; furthermore, if the fopen function is able to find a text file, it is going to delete its content or

information before performing some operations.

� The a Instruction: While a coder is using the fopen function, he/she can use the a keyword to open a text file. In

this case, the fopen function is going to open a text file for writing purposes, and it is going to start adding some

characters at the end of its content without deleting some characters.

� The r+ Instruction: An individual can use the r+ keyword on the second parameter of the fopen function when

he/she wants to open a text file for reading and writing purposes.

Page 86: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

85

� The w+ Instruction: A coder can use the w+ keyword to open a text file for reading and writing purposes. In this

case, the fopen function is going to create a new file if a text file does not exist; furthermore, if the fopen function

is able to find a text file, it is going to delete its content or information before performing some operations.

� The a+ Instruction: A coder can use the a+ keyword to open a text file for reading and writing purposes. In this

case, the fopen function is going to create a new file if a text file does not exist. When an individual is using the

fopen function and its a+ keyword, the fopen function adds some information on a text file without deleting its

content or characters.

• The fclose Function: After using the fopen function to open a text file, a C developer can use the fclose function

to close a text file. If an individual wants to close a text file, he/she has to type a line of code similar to this:

fclose(File *Variable_Name);. On the one hand, the fclose function is going to return a value of zero (0) if it is

able to close a text file; on the other hand, the fclose function is going to return an end-of-file (EOF) if it is not

able to perform its task, which is to close a text file. Thanks to this function, after building a computer

application, a coder knows if a text file was closed successfully.

• The fputs Function: After using the fopen function to open a text file, an individual can use the fputs function to

place a string or group of characters inside of a text file. In that case, a coder has to type a line of code similar to

this: fputs(File *Variable_Name);. An important point that a C developer should know about the fputs function is

that this function is going to return a value of zero (0) if it is able to perform its operation, which is to put or place

some characters on a text file.

• The fgets Function: After opening a text file through the fopen function, an individual can use the fgets statement

to read a group of characters and display those characters inside of a computer program or computer terminal. As

you can see, this is the syntax of the fgets statement:

fgets(Character_Variable, Buffer_Size, (File*)Variable_Name);

o Character_Variable: After using the char data-type to declare a character variable, an individual has to place that

variable on the first parameter of the fgets function because it is going to store a group of characters. Then, after

Page 87: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

86

using the fgets statement to store a group of characters inside of a character variable, an individual can use the

printf statement to display those characters.

o Buffer_Size: In the second parameter, a C developer has to type a number related to the buffer-size of a variable

that is located on the first parameter of the fgets function.

o (File*)Variable_Name: In the third parameter, an individual has to type a variable, which is a variable that is

holding some data related to a text file that an individual was able to open through the fopen function.

Source Code – Writing a Text File:

#include <stdio.h> int main() { FILE *AG_File; int AG_P; //Using the fopen function to open a text file: AG_File = fopen( "C://test.txt" , "w+" ); //Using the fputs function to put or place data int o a text file: AG_P = fputs( "The Easiest Way to Learn C/C++/C# by Socket_0x03. \ n" , AG_File); /*On the one hand, the fputs function is going to r eturn zero (NULL) if it is able to perform its operation; on the other h and, the fputs function is going to return EOF (End - of - File) if it is not able to perform its task.*/ //Using the if statement to display information abo ut the fputs function: if (AG_P == 0) { printf( "The fputs function was able to add information int o a file. \n" ); //Using the fclose function to close a file: fclose(AG_File); return 0; } }

Result – Writing a Text File:

Page 88: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

87

Source Code – Reading a Text File – First Example:

#include <stdio.h>

int main() {

FILE *AG_File;

char AG_Buffer[86];

//Using the fopen function to open a file:

AG_File = fopen( "C://test.txt" , "r" );

//Using the fgets function to read data from a file :

fgets(AG_Buffer, 86, AG_File);

//Displaying a short message:

printf( "Reading data from a file: \n" );

//Using the printf function to display data:

printf( "%s \n" , AG_Buffer);

//Using the fclose function to close a file:

fclose(AG_File);

return 0;

}

Result – Reading a Text File – First Example:

Page 89: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

88

Source Code – Reading a Text File – Second Example:

#include <stdio.h>

int main() {

FILE *AG_File;

char AG_Buffer[86];

//Using the fopen function to open a file:

AG_File = fopen( "C://test.txt" , "r" );

//Using an if-else statement to display a message i f a file does not exist:

if (AG_File == NULL) {

printf( "The fopen function was not able to find a text fil e.\n" );

return 0;

}

else {

//Using the fgets function to read data from a file :

fgets(AG_Buffer, 86, AG_File);

//Displaying a short message:

printf( "Reading data from a file: \n" );

//Using the printf function to display data:

printf( "%s \n" , AG_Buffer);

//Using the fclose function to close a file:

fclose(AG_File);

return 0;

}

}

Results – Reading a Text File – Second Example:

Page 90: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

89

The C++ Programming Language

Printing Data

In this section, an individual can start learning about C++, which is known as the successor of “C with classes”

and provides high-level programming features. Because this is a continuation to the previous section that is about C, I

am not going to provide information related to those kinds of data-types/variables/statements that I explained on the

previous section. Therefore, if an individual wants to understand this section, he/she has to know about C, or he/she

has to read the previous section that is about C.

In this chapter, I am going to provide information related to the most basic programming codes that a coder can

use to display data on C++; in other words, I am going to provide information about the iostream header, the std

namespace, the cout statement, and the endl statement.

• The iostream Library Header File: This is the C++ standard library header that an individual can use when he/she

wants to use different kinds of C++ statements or commands, including those statements that a coder can use to

display information and work with variables/operands/loops.

• The std Namespace: While an individual is using the iostream header, he/she can use this line: using namespace

std. If a coder uses the previous line at the beginning of a source code, he/she does not have to type the std

keyword each time that he/she is typing an iostream command or statement. For example, after including the std

namespace, if a coder wants to display some information or text on a terminal, he/she only has to type a line

similar to this: cout << "Text.";. On the other hand, if a coder does not include the std namespace at the beginning

of a source code, he/she has to type a line similar to this each time that he/she wants to display information:

std::cout << "Text.";. As you can see, it is more comfortable for an individual or coder to use the std namespace

when he/she is using the iostream header.

• The cout Statement: After importing the iostream header and using the std namespace, an individual can use the

cout statement to display or show information inside of a terminal, also known as a command-line or console. As

Page 91: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

90

you can see, this is an example of how to use the cout statement: cout << "information that a coder wants to

display on a terminal.";.

• The endl Statement: Similar to the “n\” keyword, an individual can use the endl statement when he/she wants to

display information in another line. In other words, after importing the iostream header, a coder can use the endl

statement to end a line and print information in the next line. An important point that a coder should know about

the endl statement is that he/she has to type the “<<” keyword before typing the “endl” keyword. As you can see,

this is an example of how to use the cout statement and the endl statement:

cout << "First Line." << endl;

cout << "Second Line." << endl;

Source Code – Printing Data:

#include <iostream>

using namespace std;

int main(){

cout << "The Easiest Way to Learn C/C++/C# by Alvaro J. Gen e (Socket_0x03)." << endl;

cout << "Second Line." << endl;

return 0;

}

Source Code – Printing Data:

Page 92: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

91

Data-Types & Variables

In the C++ programming language, when an individual wants to store and retrieve data inside of the random-

access memory of a computer, he/she can use some data-types, such as the wide character data-type.

• The Wide Character Data-Type: A coder can use the wchar_t keyword to build a wide character data-type, which

will use two or four bytes of random-access memory. On the one hand, a wide character data-type is going to use

two bytes of random-access memory if it is encoded in 16 bits; on the other hand, a wide character data-type is

going to use four bytes of random-access memory if it is encoded in 32 bits.

Usually, an individual is going to declare or build a wide character data-type when he/she is developing a

computer application that is going to store international characters, such as Chinese or Japanese characters. As

you can see, this is an example of how to build a wide-character variable and display its value:

wchar_t A = 'C';

cout << ''The A value is: '' << A;

In the previous block of code, the cout statement is going to display the number 67 because the C character is

stored as 67; the C character is using the ASCII format.

• The sizeof Statement: After declaring or building a variable, a C++ developer can use the sizeof statement to

display the size of a variable; to clarify, an individual can use the sizeof statement when he/she wants to know

how many bytes of random-access memory a data-type or variable is using. For example, in the case of a wide

character, the sizeof statement will say if a wide character data-type is using two or four bytes of random-access

memory. If a programmer wants to use the sizeof statement, he/she has to type a line similar to this:

wchar_t A = 'C';

cout << ''The A value is: '' << sizeof(A);

Page 93: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

92

Source Code:

#include <iostream>

using namespace std;

int main(){

char AGene_A = 'A' ;

int AGene_B = 32767;

float AGene_C = 3.14;

double AGene_D = 20.5;

wchar_t AGene_E = 'C' ;

cout << "The AGene_A value is: " << AGene_A << endl;

cout << "The AGene_A size is: " << sizeof (AGene_A) << endl;

cout << "The AGene_B value is: " << AGene_B << endl;

cout << "The AGene_B size is: " << sizeof (AGene_B) << endl;

cout << "The AGene_C value is: " << AGene_C << endl;

cout << "The AGene_C size is: " << sizeof (AGene_C) << endl;

cout << "The AGene_D value is: " << AGene_D << endl;

cout << "The AGene_D size is: " << sizeof (AGene_D) << endl;

cout << "The AGene_E value is: " << AGene_E << endl;

cout << "The AGene_E size is: " << sizeof (AGene_E) << endl;

return 0;

}

Result:

Page 94: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

93

Constants

In a previous exercise, we learned how to use C to build constant variables. Similarly, in this section, we will

learn how to use the C++ programming language to define some constants. As previously stated, when an individual

wants to build some constants, he/she can use two different kinds of keywords, which are known as const and

#define. Then, after defining a constant, a coder can use the cout statement to display a constant or operand value.

Source Code – The const Keyword:

#include <iostream>

using namespace std;

int main(){

const int AG_CONST = 86;

cout << "The constant value is: " << AG_CONST << endl;

return 0;

}

Result – The const Keyword:

Page 95: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

94

Source Code – The define Keyword:

#include <iostream>

using namespace std;

#define AG_CONST 5

int main(){

cout << "The constant value is: " << AG_CONST << endl;

return 0;

}

Result – The define Keyword:

Source Code:

#include <iostream>

using namespace std;

#define AGene_A 5

int main(){

const int AGene_B = 86;

cout << "The AGene_A value is: " << AGene_A << endl;

cout << "The AGene_B value is: " << AGene_B << endl;

return 0;

}

Result:

Page 96: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

95

Basic Operators

In a previous exercise, we learned how to use the C programming language to manipulate some variable values

thanks to the C operators; similarly, in this chapter, we will learn how to use C++ to manipulate some operand values

through the C++ operators. As previously mentioned, after declaring or building some variables, an individual can

use those C/C++ operators to change or manipulate some operand values: the arithmetic operators, relational

operators, logical operators, bitwise operators, assignment operators, and misc operators.

Page 97: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

96

Source Code – Arithmetic Operators:

#include <iostream> using namespace std; int main() { int AGene_A = 5; int AGene_B = 20; int AGene_C; //The Addition Operator: AGene_C = AGene_A + AGene_B; cout << "The AGene_C value is: " << AGene_C << endl; //The Subtraction Operator: AGene_C = AGene_A - AGene_B; cout << "The AGene_C value is: " << AGene_C << endl; //The Multiplication Operator: AGene_C = AGene_A * AGene_B; cout << "The AGene_C value is: " << AGene_C << endl; //The Division Operator: AGene_C = AGene_A / AGene_B; cout << "The AGene_C value is: " << AGene_C << endl; //The Modulus Operator: AGene_C = AGene_A % AGene_B; cout << "The AGene_C value is: " << AGene_C << endl; //The Increment Operator: AGene_C = AGene_A++; cout << "The AGene_C value is: " << AGene_C << endl;

//The Decrement Operator: AGene_C = AGene_A--; cout << "The AGene_C value is: " << AGene_C << endl; return 0;

}

Result – Arithmetic Operators:

Page 98: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

97

Source Code – Relational Operators:

#include <iostream> using namespace std; int main() { int AG_A = 20; int AG_B = 5; int AG_C = 5; //The Equal Operator: if (AG_B == AG_C) { cout << "The AG_B value is equal to the AG_C value." << endl; } //The Not-Equal Operator: if (AG_A != AG_C) { cout << "The AG_A value is not equal to the AG_C value." << endl; } //The Greater-Than Operator: if (AG_A > AG_C) { cout << "AG_A is more than AG_C." << endl; } //The Less-Than Operator: if (AG_B < AG_A) { cout << "AG_B is less than AG_A." << endl; } //The Greater-Than or Equal to Operator if (AG_A >= AG_B) {

cout << "AG_A is more than or equal to AG_B." << endl; } //The Less-Than or Equal to Operator: if (AG_A <= AG_C) { cout << "AG_A is less than or equal to AG_C." << endl; } else { cout << "AG_A is more than or equal to AG_C." << endl; }

return 0; }

Result – Relational Operators:

Page 99: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

98

Source Code – Logical Operators:

#include <iostream> using namespace std; int main() { int AG_A = true ; int AG_B = false ; int AG_C = true ; int AG_D = false ; int AGene; //The Logical AND Operator: If both variables are t rue; then, it will print true. AGene = AG_A && AG_C; cout << "The Logical AND Operator: " << AGene << endl; //Both True: True = 1. AGene = AG_A && AG_B; cout << "The Logical AND Operator: " << AGene << endl; //True & False: False = 0. AGene = AG_B && AG_D; cout << "The Logical AND Operator: " << AGene << endl; //False & False: False = 0. //The Logical OR Operator: If a variable is true; t hen, it will print true. AGene = AG_A || AG_B; cout << "The Logical OR Operator: " << AGene << endl; //True & False: True = 1. AGene = AG_B || AG_D; cout << "The Logical OR Operator: " << AGene << endl; //Both False: False = 0. AGene = AG_A || AG_C; cout << "The Logical OR Operator: " << AGene << endl; //Both True: True = 1.

//The Logical NOT Operator: If a variable is not tr ue(false); then, it will print true. AGene = !(AG_A && AG_B); cout << "The Logical NOT Operator: " << AGene << endl; //True & False: True = 1. AGene = !(AG_A && AG_C); cout << "The Logical NOT Operator: " << AGene << endl; //Both True: False = 0. AGene = !(AG_B && AG_D); cout << "The Logical NOT Operator: " << AGene << endl; //Both False: True = 1. return 0;

}

Result – Logical Operators:

Page 100: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

99

Source Code – Bitwise Operators:

#include <iostream> using namespace std; int main() { int AG_A = 43; //43 = 0010 1011. int AG_B = 86; //86 = 0101 0110. int AGene = 0; AGene = AG_A & AG_B; // 2 = 0000 0010. cout << "1. The Bitwise AND Operator: " << AGene << endl << endl; AGene = AG_A | AG_B; // 127 = 0111 1111. cout << "2. The Bitwise OR Operator: " << AGene << endl << endl; AGene = AG_A ^ AG_B; // 125 = 0111 1101. cout << "3. The Bitwise XOR Operator: " << AGene << endl << endl; AGene = ~AG_A; // -44 = 1101 0100. cout << "4. The Binary Ones Complement Operator: " << AGene << endl << endl; AGene = AG_A << 1; // 86 = 0101 0110. cout << "5. The Binary Left Shift Operator: " << AGene << endl << endl; AGene = AG_A >> 1; // 21 = 1 0101. cout << "6. The Binary Right Shift Operator: " << AGene << endl << endl;

return 0; }

Result – Bitwise Operators:

Page 101: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

100

Source Code – Assignment Operators:

#include <iostream> using namespace std; int main() { int AG_A = 100; int AG_B = 86; int AGene; AGene = AG_A + AG_B; cout << "1. The Assignment Operator: " << AGene << endl << endl; AGene += AG_A; cout << "2. The Addition Assignment Operator: " << AGene << endl << endl; AGene -= AG_A; cout << "3. The Subtraction Assignment Operator: " << AGene << endl << endl; AGene *= AG_A; cout << "4. The Multiplication Assignment Operator: " << AGene << endl << endl; AGene /= AG_A; cout << "5. The Division Assignment Operator: " << AGene << endl << endl; AGene %= AG_A; cout << "6. The Modulus Assignment Operator: " << AGene << endl << endl;

AGene <<= 1; cout << "7. The Left-Shift Assignment Operator: " << AGene << endl << endl; AGene >>= 1; cout << "8. The Right-Shift Assignment Operator: " << AGene << endl << endl; AGene &= AG_A; cout << "9. The Bitwise AND Assignment Operator: " << AGene << endl << endl;

AGene ^= AG_A; cout << "10. The Bitwise Exclusive OR Assignment Operator: " << AGene << endl << endl; AGene |= AG_A; cout << "11. The Bitwise Inclusive OR Assignment Operator: " << AGene << endl << endl; return 0; }

Page 102: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

101

Result – Assignment Operators:

Page 103: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

102

Conditional Statements

In a previous exercise, we learned how to use the C programming language to define some conditional

statements; in a like manner, in this section, we will learn how to use C++ to build different kinds of conditional

statements, including the if-statement, the if-else statement, the switch statement, and some nested statements.

Source Code – The if-Statement:

#include <iostream>

using namespace std;

int main() {

int AG_A = 86;

int AG_B = 20;

cout << "The AG_A value is: " << AG_A << "." << endl;

cout << "The AG_B value is: " << AG_B << "." << endl;

if (AG_A > AG_B) {

cout << "The AG_A value is superior to the AG_B value." << endl;

}

return 0;

}

Result – The if-Statement:

Page 104: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

103

Source Code – The if-else Statement:

#include <iostream>

using namespace std;

int main() {

int AG_A = 5;

int AG_B = 5;

cout << "The AG_A value is: " << AG_A << endl;

cout << "The AG_B value is: " << AG_B << endl;

if (AG_A != AG_B) {

cout << "The AG_A value is not equal to the AG_B value." << endl;

}

else {

cout << "The AG_A value is equal to the AG_B value." << endl;

}

return 0;

}

Result – The if-else Statement:

Page 105: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

104

Source Code – The Nested if-Statement:

#include <iostream>

using namespace std;

int main() {

int AG_A = 5;

int AG_B = 20;

int AG_C = 20;

cout << "The AG_A value is: " << AG_A << endl;

cout << "The AG_B value is: " << AG_B << endl;

cout << "The AG_C value is: " << AG_C << endl << endl;

if (AG_A != AG_B) {

cout << "The AG_A value is not equal to the AG_B value." << endl;

if (AG_B = AG_C) {

cout << "The AG_B value is equal to the AG_C value." << endl;

}

}

return 0;

}

Result – The Nested if-Statement:

Page 106: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

105

Source Code – The Switch Statement:

#include <iostream>

using namespace std;

int main() {

int AGene = 2;

switch (AGene) {

case 1 :

cout << "First Alternative." << endl;

break ;

case 2 :

cout << "Second Alternative." << endl;

break ;

default :

cout << "Default Alternative." << endl;

}

return 0;

}

Result – The Switch Statement:

Page 107: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

106

Source Code – The Nested Switch Statement:

#include <iostream>

using namespace std;

int main() {

int AG_A = 1;

char AG_B = 'B' ;

switch (AG_A) {

case 1 :

cout << "First Alternative." << endl;

switch (AG_B) {

case 'A' :

cout << "The Second Switch Statement: First Alternative." << endl;

break ;

case 'B' :

cout << "The Second Switch Statement: Second Alternative." << endl;

break ;

}

break ;

case 2 :

cout << "Second Alternative." << endl;

break ;

default :

cout << "Default Alternative." << endl;

}

return 0;

}

Result – The Nested Switch Statement:

Page 108: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

107

Loop Statements

In a previous exercise, we learned how to use the C programming language to build different kinds of loop

statements; likewise, in this chapter, we will learn how to use C++ to define or build some loop statements. As

previously stated, an individual or coder can use some loop statements when he/she wants to execute a block of code

several times as long as a condition is positive. Like the C programming language, a programmer can use C++ to

define or build those loop statements: while loop statement, for loop statement, and do-while loop statement.

Source Code – The While-Loop Statement:

#include <iostream>

using namespace std;

int main() {

int AGene = 80;

while ( AGene <= 86 ) {

cout << "The AGene Value is: " << AGene << endl;

AGene++;

}

return 0;

}

Result – The While-Loop Statement:

Page 109: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

108

Source Code – The For-Loop Statement:

#include <iostream> using namespace std; int main() { int AGene; for (AGene = 15; AGene <= 20; AGene++) { cout << "The AGene value is: " << AGene << endl; } return 0; }

Result – The For-Loop Statement:

Source Code – The Do-While Loop Statement:

#include <iostream> using namespace std; int main() { int AGene = 0; do { cout << "The AGene value is: " << AGene << endl; AGene++; } while (AGene <= 5); return 0; }

Result – The Do-While Loop Statement:

Page 110: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

109

Source Code – The Break Statement:

#include <iostream>

using namespace std;

int main() {

int AGene = 80;

while ( AGene <= 90 ) {

cout << "The AGene value is: " << AGene << endl;

AGene++;

if ( AGene > 86) {

break ;

}

}

return 0;

}

Result – The Break Statement:

Page 111: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

110

Source Code – The Continue Statement:

#include <iostream>

using namespace std;

int main() {

int AGene = 15;

do {

if ( AGene == 21) {

AGene = 80;

continue ;

}

cout << "The AGene value is: " << AGene << endl;

AGene++;

}

while ( AGene <= 86 );

return 0;

}

Result – The Continue Statement:

Page 112: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

111

Source Code – The goto Statement:

#include <iostream>

using namespace std;

int main() {

int AGene = 15;

TERAEXE:

do {

if ( AGene == 21) {

AGene = 80;

goto TERAEXE;

}

cout << "The AGene value is: " << AGene << endl;

AGene++;

}

while ( AGene <= 86 );

return 0;

}

Result – The goto Statement:

Page 113: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

112

Functions

In a previous exercise, we learned how to use the C programming language to build different kinds of functions;

likewise, in this chapter, we will learn how to use C++ to define or build some functions. As previously stated, a

function is a block of code that an individual can build and/or call when he/she wants to perform some operations,

such as adding some values to an operand or variable.

Source Code – First Example of Functions:

#include <iostream> using namespace std; //Declaring the Teraexe function: int Teraexe( int AG_Adding_86); int main () { //The main function. int AG_A = 23; //Declaring a variable. //Calling the Teraexe function: AG_A = Teraexe(AG_A); //Displaying a variable value: cout << "The AG_A value is: " << AG_A << endl; return 0;

} //Building a function that is going to add 86: int Teraexe( int AG_Adding_86) { int AG_Variable; //Declaring a variable. AG_Variable = AG_Adding_86 + 86; //Adding 86. return AG_Variable; //Returning a variable value.

}

Result – First Example of Functions:

Page 114: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

113

Source Code – Second Example of Functions:

#include <iostream>

using namespace std;

//Declaring the Teraexe function:

int Teraexe( int AG_Adding_86);

int main () { //The main function.

int AG_A = 23; //Declaring a variable.

int AG_B; //Declaring a variable to call a function.

//Calling the Teraexe function:

AG_B = Teraexe(AG_A);

//Displaying the AG_A value:

cout << "The AG_A value is: " << AG_A << endl;

//Displaying the AG_B value:

cout << "The AG_B value is: " << AG_B << endl;

return 0;

}

//Building a function that is going to add 86:

int Teraexe( int AG_Adding_86) {

int AG_Variable; //Declaring a variable.

AG_Variable = AG_Adding_86 + 86; //Adding 86.

return AG_Variable; //Returning a variable value.

}

Result – Second Example of Functions:

Page 115: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

114

Variable Scope

In a previous exercise, we learned how to use the C programming language to build some variables with a

local/global scope; similarly, in this chapter, we will learn how to use C++ to define some variables of local and

global scope. As explained above, a variable of local scope is a variable that an individual was able to build inside of

a function, such as the main function; in contrast, a variable of global scope is a variable that a programmer was able

to build outside of a function.

Source Code – Variable Scope:

#include <iostream>

using namespace std;

int AG_A = 86; //Declaring a variable of global scope.

int main () { //The main function.

int AG_B = 20; //Declaring a variable of local scope.

//Displaying a global variable:

cout << "The AG_A value is: " << AG_A <<endl;

//Displaying a local variable:

cout << "The AG_B value is: " << AG_B <<endl;

return 0;

}

Result – Variable Scope:

Page 116: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

115

Arrays

In a previous exercise, we learned how to use the C programming language to build some arrays; in the same

way, in this chapter, we will learn how to use C++ to define or build different kinds of arrays. As previously stated,

an array is a special variable that an individual or coder can build when he/she wants to store and retrieve a group of

values, which are known as the elements of an array.

Source Code – Using cout to display elements:

#include <iostream>

using namespace std;

int main () { //The main function.

//Declaring an array with three elements:

int AG_Array[] = {5, 20, 86};

//Using the 0 value to display the first element of an array:

cout << "The first element of an array: " << AG_Array[0] <<endl;

//Using the 1 value to display the second element o f an array:

cout << "The second element of an array: " << AG_Array[1] <<endl;

//Using the 2 value to display the third element of an array:

cout << "The third element of an array: " << AG_Array[2] <<endl;

return 0;

}

Result – Using cout to display elements:

Page 117: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

116

Source Code – Using a for-loop to display elements:

#include <iostream>

using namespace std;

int main () { //The main function.

//Declaring a variable for the for-loop statement:

int AG_Loop;

//Declaring an array with four elements:

int AG_Array[] = {5, 20, 86, 23};

cout << "Displaying all the elements of an array: " << endl;

/* Using the for-loop statement to display all the elements

of an array: */

for (AG_Loop = 0; AG_Loop < 4; AG_Loop++) {

cout << "" << AG_Array[AG_Loop] << endl;

}

return 0;

}

Result – Using a for-loop to display elements:

Page 118: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

117

Source Code – Two-Dimensional Array:

#include <iostream>

using namespace std;

int main () { //The main function.

int AG_Array[2][3] =

{

{55, 20, 86},

{11, 12, 23}

};

//First row, first column:

cout << "The value is: " << AG_Array[0][0] << endl;

//First row, second column:

cout << "The value is: " << AG_Array[0][1] << endl;

//First row, third column:

cout << "The value is: " << AG_Array[0][2] << endl;

//Second row, first column:

cout << "The value is: " << AG_Array[1][0] << endl;

//Second row, second column:

cout << "The value is: " << AG_Array[1][1] << endl;

//Second row, third column:

cout << "The value is: " << AG_Array[1][2] << endl;

return 0;

}

Result – Two-Dimensional Array:

Page 119: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

118

Pointers

In a previous exercise, we learned how to use the C programming language to build some pointers; likewise, in

this chapter, we will learn how to use C++ to define or build different kinds of pointer variables. As previously stated,

an individual can define some pointer variables when he/she wants to build a variable that is going to hold the

memory address of a regular variable; then, after building a pointer variable that is going to hold the memory address

of a regular variable, a coder can use a pointer to display or show the value of a regular variable.

Page 120: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

119

Source Code – Pointers:

#include <iostream>

using namespace std;

int main () { //The main function.

int AG_A = 86; //Declaring a regular variable.

int AG_B = 20; //Declaring a regular variable.

int *AG_Pointer; //Declaring a pointer variable.

//Storing the memory address of AG_A on AG_Pointer:

AG_Pointer = &AG_A;

//Displaying the memory address stored on AG_Pointe r:

cout << "Memory address stored on AG_Pointer: " << AG_Pointer << endl << endl;

//Displaying the memory address of AG_A:

cout << "The AG_A memory address: " << &AG_A << endl << endl;

/*Displaying the AG_B memory address. In this case, you are going to

notice that the result is different than the oth er two results.*/

cout << "The AG_B memory address: " << &AG_B << endl << endl;

//Using a pointer variable to display the value of a regular variable:

cout << "Using a pointer to display the AG_A value: " << *AG_Pointer << endl << endl;

return 0;

}

Result – Pointers:

Page 121: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

120

Strings

In a previous exercise, we learned how to use the C programming language to define and display a group of

characters, also known as a string; likewise, in this chapter, we will learn how to use C++ to define and display a

string or group of characters. On the one hand, when an individual or coder is using the C programming language to

work with strings, he/she should use the string header; on the other hand, when a C developer is using the C++

programming language to work with strings, he/she should use a string header that is known as the cstring header file.

In the next source code, we will use the cstring header to display a group of characters, also called a string.

Page 122: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

121

Source Code – Strings:

#include <iostream>

#include <cstring>

using namespace std;

int main () {

char AG_String_A[] = "String One" ;

char AG_String_B[] = "String Two" ;

char AG_String_C[] = " and String Three" ;

int AG_Length_A;

//Displaying a String:

cout << "Value of AG_String_A: " << AG_String_A <<endl;

/*Copying a string (AG_String_B to AG_String_A).

Now, AG_String_A has this value: String Two.*/

strcpy(AG_String_A, AG_String_B);

//Displaying AG_String_A:

cout << "Value of AG_String_A: " << AG_String_A <<endl;

/*Concatenating a string (AG_String_A & AG_String_C ):

Now, AG_String_A has this value: String Two and String Three*/

strcat(AG_String_A, AG_String_C);

//Displaying AG_String_A:

cout << "Value of AG_String_A: " << AG_String_A <<endl;

//Displaying the length of a string after its conca tenation:

AG_Length_A = strlen(AG_String_A);

cout << "Length of AG_String_A: " << AG_Length_A <<endl;

return 0;

}

Result – Strings:

Page 123: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

122

Source Code – Comparing Strings:

#include <iostream>

#include <cstring>

using namespace std;

int main () {

char AG_A[] = "20" ;

char AG_B[] = "20" ;

char AG_C[] = "5" ;

int AG_strcmp;

cout << "The AG_A value is: " << AG_A << endl;

cout << "The AG_B value is: " << AG_B << endl;

cout << "The AG_C value is: " << AG_C << endl << endl;

//Knowing if a string is less/more than another str ing.

//It will return 0 because AG_A is equal to AG_B.

AG_strcmp = strcmp(AG_A, AG_B);

cout << "Comparing AG_B and AG_A: " << AG_strcmp << endl;

//Comparing AG_C and AG_B.

//It will return -1 because AG_C is less than AG_B.

AG_strcmp = strcmp(AG_B, AG_C);

cout << "Comparing AG_C and AG_B: " << AG_strcmp << endl;

//Comparing AG_A and AG_C.

//It will return 1 because AG_A is more than AG_C.

AG_strcmp = strcmp(AG_C, AG_A);

cout << "Comparing AG_A and AG_C: " << AG_strcmp << endl;

return 0;

}

Result – Comparing Strings:

Page 124: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

123

Structures

In a previous exercise, we learned how to use the C programming language to build some structures; in a similar

way, in this chapter, we will learn how to use C++ to define different kinds of structures. As previously stated, there

is a difference between an array and a structure: on the one hand, an array is a “derived data-type” that is composed

of some elements of “a single” data-type; on the other hand, a structure is a “defined data-type” that can be composed

of some elements of “a group” of data-types. In other words, a structure is a data-type that an individual can use when

he/she wants to define some variables or items with different kinds of data-types.

Source Code – Structures:

#include <iostream>

#include <cstring>

using namespace std;

//Declaring the first structure:

struct Hard_Commodities {

char Platinum[86];

char Diamond[86];

char Gold[86];

};

//Declaring the second structure:

struct Soft_Commodities {

char Coffee[120];

char Corn[120];

};

Page 125: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

124

int main() { //The main function.

//Declaring hard commodities:

struct Hard_Commodities HC_Countries_A;

struct Hard_Commodities HC_Countries_B;

struct Hard_Commodities HC_Countries_C;

//Declaring soft commodities:

struct Soft_Commodities SC_Countries_A;

struct Soft_Commodities SC_Countries_B;

//Hard Commodities - First countries:

strcpy(HC_Countries_A.Platinum, "South Africa." );

strcpy(HC_Countries_A.Diamond, "Australia." );

strcpy(HC_Countries_A.Gold, "USA." );

//Hard Commodities - Second countries:

strcpy(HC_Countries_B.Platinum, "Russia." );

strcpy(HC_Countries_B.Diamond, "South Africa." );

strcpy(HC_Countries_B.Gold, "Germany." );

//Hard Commodities - Third countries:

strcpy(HC_Countries_C.Platinum, "Canada." );

strcpy(HC_Countries_C.Diamond, "Russia." );

strcpy(HC_Countries_C.Gold, "Italy." );

//Soft Commodities - First countries:

strcpy(SC_Countries_A.Coffee, "Brazil." );

strcpy(SC_Countries_A.Corn, "USA." );

Page 126: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

125

//Soft Commodities - Second countries:

strcpy(SC_Countries_B.Coffee, "Vietnam." );

strcpy(SC_Countries_B.Corn, "China." );

//Displaying the first countries with hard commodit y reserves:

cout << "First Major Countries with Hard Commodity Reserves : " << endl;

cout << "Country with Platinum reserves: " << HC_Countries_A.Platinum << endl;

cout << "Country with Diamond Reserves: " << HC_Countries_A.Diamond << endl;

cout << "Country with Gold Reserves: " << HC_Countries_A.Gold << endl << endl;

//Displaying the second major countries with hard c ommodity reserves:

cout << "Second Major Countries with Hard Commodity Reserve s: " << endl;

cout << "Country with Platinum reserves: " << HC_Countries_B.Platinum << endl;

cout << "Country with Diamond Reserves: " << HC_Countries_B.Diamond << endl;

cout << "Country with Gold Reserves: " << HC_Countries_B.Gold << endl << endl;

//Displaying the third major countries with hard co mmodity reserves:

cout << "Third Major Countries with Hard Commodity Reserves : " << endl;

cout << "Country with Platinum reserves: " << HC_Countries_C.Platinum << endl;

cout << "Country with Diamond Reserves: " << HC_Countries_C.Diamond << endl;

cout << "Country with Gold Reserves: " << HC_Countries_C.Gold << endl << endl;

//Displaying the major soft-commodity producing cou ntries:

cout << "First Major Countries Producing Soft-Commodities: " << endl;

cout << "Country with Coffee Production: " << SC_Countries_A.Coffee << endl;

cout << "Country with Corn Production: " << SC_Countries_A.Corn << endl << endl;

Page 127: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

126

//Displaying the second major soft-commodity produc ing countries:

cout << "Second Major Countries Producing Soft-Commodities: " << endl;

cout << "Country with Coffee Production: " << SC_Countries_B.Coffee << endl;

cout << "Country with Corn Production: " << SC_Countries_B.Corn << endl << endl;

return 0;

}

Result – Structures:

Page 128: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

127

Source Code – Structures & Functions:

#include <iostream>

#include <cstring>

using namespace std;

//Declaring the first structure:

struct Teraexe_PC {

char Operating_System[86];

char Memory_RAM[86];

char Hard_Drive[86];

};

//Declaring a function:

void Display_PC( struct Teraexe_PC PC);

int main() { //The main function.

//Declaring some computers:

struct Teraexe_PC First_Teraexe_PC;

struct Teraexe_PC Second_Teraexe_PC;

struct Teraexe_PC Third_Teraexe_PC;

//First Computer:

strcpy(First_Teraexe_PC.Operating_System, "Linux." );

strcpy(First_Teraexe_PC.Memory_RAM, "4GB." );

strcpy(First_Teraexe_PC.Hard_Drive, "250GB." );

//Second Computer:

strcpy(Second_Teraexe_PC.Operating_System, "Windows." );

strcpy(Second_Teraexe_PC.Memory_RAM, "8GB." );

Page 129: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

128

strcpy(Second_Teraexe_PC.Hard_Drive, "500GB." );

//Third Computer:

strcpy(Third_Teraexe_PC.Operating_System, "MAC." );

strcpy(Third_Teraexe_PC.Memory_RAM, "16GB." );

strcpy(Third_Teraexe_PC.Hard_Drive, "1TB." );

//The next line is to display some data about the f irst PC:

Display_PC(First_Teraexe_PC);

//The next line is to display some data about the s econd PC:

Display_PC(Second_Teraexe_PC);

//The next line is to display some data about the t hird PC:

Display_PC(Third_Teraexe_PC);

return 0;

}

void Display_PC( struct Teraexe_PC PC) {

//Displaying Computers:

cout << "Operating System: " << PC.Operating_System << endl;

cout << "Memory RAM: " << PC.Memory_RAM << endl;

cout << "Hard Drive: " << PC.Hard_Drive << endl << endl;

}

Result – Structures & Functions:

Page 130: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

129

Source Code – Structures & Pointers:

#include <iostream>

#include <cstring>

using namespace std;

//Declaring the first structure:

struct Teraexe_PC {

char Operating_System[86];

char Memory_RAM[86];

char Hard_Drive[86];

};

//Declaring a function:

void Display_PC( struct Teraexe_PC *PC);

int main() { //The main function.

//Declaring some computers:

struct Teraexe_PC First_Teraexe_PC;

struct Teraexe_PC Second_Teraexe_PC;

struct Teraexe_PC Third_Teraexe_PC;

//First Computer:

strcpy(First_Teraexe_PC.Operating_System, "Linux." );

strcpy(First_Teraexe_PC.Memory_RAM, "4GB." );

strcpy(First_Teraexe_PC.Hard_Drive, "250GB." );

//Second Computer:

strcpy(Second_Teraexe_PC.Operating_System, "Windows." );

strcpy(Second_Teraexe_PC.Memory_RAM, "8GB." );

Page 131: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

130

strcpy(Second_Teraexe_PC.Hard_Drive, "500GB." );

//Third Computer:

strcpy(Third_Teraexe_PC.Operating_System, "MAC." );

strcpy(Third_Teraexe_PC.Memory_RAM, "16GB." );

strcpy(Third_Teraexe_PC.Hard_Drive, "1TB." );

//The next line is to display some data about the f irst PC:

Display_PC(&First_Teraexe_PC);

//The next line is to display some data about the s econd PC:

Display_PC(&Second_Teraexe_PC);

//The next line is to display some data about the t hird PC:

Display_PC(&Third_Teraexe_PC);

return 0;

}

void Display_PC( struct Teraexe_PC *PC) {

//Displaying Computers:

cout << "Operating System: " << PC->Operating_System << endl;

cout << "Memory RAM: " << PC->Memory_RAM << endl;

cout << "Hard Drive: " << PC->Hard_Drive << endl << endl;

}

Result – Structures & Pointers:

Page 132: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

131

Unions

In a previous exercise, we learned how to use the C programming language to build some unions; likewise, in this

chapter, we will learn how to use C++ to define some unions. As previously stated, a union is a defined data-type that

an individual can use when he/she wants to store a group of members; to put it another way, a union is a defined data-

type that a programmer can use when he/she wants to store a group of data-types with their variable names.

Page 133: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

132

Source Code – Unions: The Wrong Way:

#include <iostream>

#include <cstring>

using namespace std;

//Declaring a union:

union AG_Teraexe_Products {

int AG_Cost; //Defining an int data-type.

char AG_Name[23]; //Defining a char data-type.

};

int main() { //The main function.

union AG_Teraexe_Products AGTP;

//Using strcpy to add data to AG_Name:

strcpy(AGTP.AG_Name, "Laptop" );

//Adding data to AG_Cost:

AGTP.AG_Cost = 500;

//Trying to display the product name; however, it w ill show a worm with a happy face.

cout << "Product Name: " << AGTP.AG_Name << endl;

//Displaying the product cost:

cout << "Product Cost: " << AGTP.AG_Cost << endl;

return 0;

}

Result – Unions: The Wrong Way:

Page 134: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

133

Source Code – Unions: The Right Way:

#include <iostream>

#include <cstring>

using namespace std;

//Declaring a union:

union AG_Teraexe_Products {

int AG_Cost; //Defining an int data-type.

char AG_Name[23]; //Defining a char data-type.

};

int main() { //The main function.

union AG_Teraexe_Products AGTP;

//Using strcpy to add data to AG_Name:

strcpy(AGTP.AG_Name, "Laptop" );

//Displaying the product name:

cout << "Product Name: " << AGTP.AG_Name << endl;

//Adding data to AG_Cost:

AGTP.AG_Cost = 500;

//Displaying the product cost:

cout << "Product Cost: " << AGTP.AG_Cost << endl;

return 0;

}

Result – Unions: The Right Way:

Page 135: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

134

Input & Output

In a previous exercise, we learned how to use the C programming language to perform input and output

operations; similarly, in this chapter, we will learn how to use C++ to perform input and output operations. In the

C++ programming language, when an individual wants to perform input and output operations, he/she can use those

functions or statements: cin, cout, cerr, and clog.

• The cin Statement: A C++ developer can use the cin statement to perform input operations and read a group of

characters; in other words, a coder can use the cin statement to store those kinds of characters that a user is typing

on a computer program or computer terminal. While a programmer is using the cin statement for input operations,

he/she has to use the stream extraction operator (>>) and type a line of code like this: cin >> Variable-Name;.

Then, after using the cin statement to store some characters, a coder can use some output operators like

cout/cerr/clog to display those characters on a computer terminal.

• The cout Statement: After using the cin statement to store some characters, an individual can use the cout

statement when he/she wants to display a variable value, which is an output operation. As previously stated, when

a coder wants to use the cout statement to display a variable value, he/she has to use the stream insertion operator

(<<) and type a block of code similar to this:

Data-type Variable-Name; //Defining a variable.

cout << "Variable value: " << Variable-Name; //Performing an output operation to display a variable.

• The cerr Statement: In addition to the cout statement, a coder can use the cerr statement to perform output

operations; in that case, an individual has to use the stream insertion operator (<<) and type a line of code similar

to this: cerr << "Variable value: " << Variable-Name; //Performing an output operation to display a variable.

• The clog Statement: Not only does a coder can use the cout/cerr statement to perform output operations, but also

he/she can use the clog statement to perform an output operation and display a variable value. Similar to the

Page 136: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

135

cout/cerr statement, if a coder wants to use the clog statement to display some characters on a computer program

or computer console, he/she has to type a block of code like this:

Data-type Variable-Name; //Defining a variable.

clog << "Variable value: " << Variable-Name; //Performing an output operation to display a variable.

Source Code – First Example of Input & Output:

#include <iostream>

using namespace std;

int main() {

char AG_cin[86];

cout << "Type a word: " ;

//Using cin for input operations:

cin >> AG_cin;

//Using cout for output operations:

cout << "The cout Statement: " << AG_cin << endl;

//Using cerr for output operations:

cerr << "The cerr Statement: " << AG_cin << endl;

//Using clog for output operations:

clog << "The clog Statement: " << AG_cin << endl;

return 0;

}

Result – First Example of Input & Output:

Page 137: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

136

Source Code – Second Example of Input & Output:

#include <iostream>

using namespace std;

int main() {

//Building a variable with a group of characters:

char AG_A[] = "The Easiest Way to Learn C/C++/C# by Socket_0x03" ;

//Using cout for output operations:

cout << "The cout Statement: " << AG_A << endl;

//Using cerr for output operations:

cerr << "The cerr Statement: " << AG_A << endl;

//Using clog for output operations:

clog << "The clog Statement: " << AG_A << endl;

return 0;

}

Result – Second Example of Input & Output:

Page 138: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

137

Input and Output: Text Files

In the previous section, we learned how to use the iostream library header file to perform basic input and output

operations thanks to those statements: cin, cout, cerr, and clog. Similarly, in this section, we are going to learn how to

perform input and output operations on files thanks to the fstream library header file.

• The fstream Library Header File: As explained above, a C++ developer can use the fstream header when he/she

wants to perform input/output operations on different kinds of files. For example, a coder can use the fstream

header and its ofstream constructor to add some characters on a text file, and he/she can use the fstream header

and its ifstream constructor to display the characters of a text file inside of a computer terminal or computer

program. As you can see, those are some of the most important constructors that an individual can use when

he/she is using the fstream library header file to perform input/output operations: ofstream, ifstream, and stream.

• The ofstream constructor: A programmer can use the ofstream constructor when he/she wants to build an object,

which is going to be used with the open function to open a file for writing purposes. In that case, a coder has to

type a block of code similar to this:

ofstream Object_Name;

Object_Name.open("File_Directory");

Then, after using an ofstream object to open a file and perform some operations, an individual should close the

file; in that case, a coder can use the ofstream object and the close function to type a line of code like this:

Object_Name.close();

• The ifstream constructor: A C++ developer can use the ifstream constructor when he/she wants to build an object,

which is going to be used with the open function to open a file for reading purposes. In that case, a coder has to

type a block of code similar to this:

ifstream Object_Name;

Object_Name.open("File_Directory");

Similar to the ofstream constructor, after using the ifstream object to open a file and perform some operations, a

coder should close that file through the close function; in that case, he/she has to type a line like this:

Page 139: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

138

Object_Name.close();

• The stream constructor: A coder can use the stream constructor when he/she wants to build an object, which is

going to be used with the open function to open a file for writing/reading purposes, which means that an

individual can use the stream constructor as an ofstream/ifstream constructor.

Source Code – Input & Output on Text Files – Adding Data:

#include <iostream> /*Including the fstream header library file to perf orm input and output operations on text files:*/ #include <fstream> using namespace std; int main () { //Declaring a variable to store some characters: char AG_Data[86]; /*Using the ofstream constructor to build an object without opeaning a file. In the future, we will use this object to open a file.*/ ofstream AG_Object; //Using the ofstream object and the open function t o open a file: AG_Object.open( "C://test.txt" ); //Displaying a text message: cout << "Type a word: " ; //Adding some characters to the AG_Data variable: cin.getline(AG_Data, 86); //Adding some characters to a text file: AG_Object << AG_Data << endl; //Using the close function to close a text file: AG_Object.close(); return 0;

}

Result – Input & Output on Text Files – Adding Data:

Page 140: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

139

Source Code – Input & Output on Text Files – Displaying Data:

#include <iostream>

/*Including the fstream header library file to perf orm

input and output operations on text files:*/

#include <fstream>

using namespace std;

int main () {

//Declaring a variable to store some characters:

char AG_Data[86];

/*Using the ifstream constructor to build an object without

opeaning a file. In the future, we will use this object to

open a file.*/

ifstream AG_Object;

//Using the ifstream object and the open function t o open a file:

AG_Object.open( "C://test.txt" );

//Displaying a text message:

cout << "Reading data from a text File: " ;

/*Using the AG_Object to Add or store some characte rs on

the AG_Data variable:*/

AG_Object >> AG_Data;

//Displaying the AG_Data data on a computer termina l:

cout << AG_Data << endl;

//Using the close function to close a text file:

AG_Object.close();

return 0;

}

Result – Input & Output on Text Files – Displaying Data:

Page 141: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

140

Date and Time

In this chapter, a coder can expand his/her knowledge and start learning about date/time programming. When a

C++ developer wants to perform time operations, he/she can use the ctime library header file.

The ctime Header File: As previously stated, a C++ developer can use the ctime header file when he/she wants to

perform time operations. For example, an individual can use the ctime header file to display the current or local time

of a computer; in addition, a coder can use the ctime header file to show the coordinated universal time (UTC) of a

computer. As you can see, those are some of the most important commands or functions that an individual can use to

perform time operations through the ctime header file: time, ctime, and localtime.

• The time Function: An individual can use the time function when he/she wants to display the current time of a

computer; to put it another way, a coder can use the time function to return the local time of a system. As you can

see, this is the syntax of the time function: time_t time(time_t *timer);. If a coder wants to use the time function to

store the local time of a computer inside of a variable, he/she can use the time_t data-type and type a line of code

like this: time_t Variable_Name = time(NULL);.

An important fact that a C++ developer should know about the time function is that this function is going to

return -1 if a system does not have time, which means that a coder can use an if-statement to display a text

message if there is an error or problem. Another important fact about the time function is that it is going to

display the local calendar time of a computer in seconds; therefore, if a coder wants to show the local time of a

computer in a more comfortable way, he/she can use the ctime function.

• The ctime Function: After using the time function to store a time value on a variable name, an individual can use

the ctime function to convert a time value into a string or group of characters that a human being can read easily.

After using the ctime function to convert a time value into a string, a coder can use the cout statement to display a

time value in a format similar to this: Day Month Year Hour:Minutes:Seconds Year \n. As you can see, this is the

syntax of the ctime function: char *ctime(const time_t *time);. If a C++ developer wants to use the ctime function

Page 142: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

141

to convert some digits or seconds into a comfortable format that a coder can read easily, he/she has to type a line

of code like this:

char *Pointer_Variable = ctime(&Time-Value_Variable);

o Pointer_Variable: In this field, an individual has to type a pointer variable that is going to hold a time value in a

format that an individual can read easily.

o Time-Value_Variable: In this parameter, a C++ developer has to type a variable name, which is holding a time

value in a format that a human being cannot read easily. In other words, in this field, a coder has to type a variable

that is holding a time value, which is composed of a group of digits or seconds.

After using the ctime function to convert some digits or seconds into a group of characters, a coder can use the

cout statement to display those characters on a computer program or computer terminal; in that case, an individual

has to type a line of code similar to this:

cout << "Displaying the local time of a computer" << Pointer_Variable;.

• The localtime Function: After using the ctime function to store a time value on a variable of time_t data-type, an

individual can use the localtime function to convert a time value and store its value on a pointer variable of tm

date-type, also known as the tm structure. What is the tm structure? It is a data-type that is composed of some

elements that an individual can use to store and display the local time of a computer. First, a time value is going

to be stored inside of the tm structure; then, a coder can use the cout statement and the tm elements to display the

local time of a system. As you can see, those are some of the most important elements of the tm structure that a

C++ developer can use to display the local time of a computer on a computer program or computer terminal:

1. The tm_sec Element: A coder can use this element to display the seconds of a minute. The minimum value of the

tm_sec element is 0, and the maximum value of the tm_sec element is 59.

2. The tm_min Element: An individual can use the tm_min element to show the minutes of an hour. The minimum

value of the tm_min element is 0, and the maximum value of the tm_min element is 59.

Page 143: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

142

3. The tm_hour Element: A C++ developer can use the tm_hour element to print the hours of a day. The minimum

value of the tm_hour element is 0, and the maximum value of the tm_hour is 24.

4. The tm_wday Element: An individual can use the tm_wday element when he/she wants to display a digit that is

going to represent the day of a week. In this case, the minimum value of the tm_wday element is 0, and the

maximum value of the tm_wday element is 6; therefore, if a coder wants to display some values that go from 1

(Sunday) to 7 (Saturday), he/she has to add the number one to his/her values and type a line of code similar to

this: cout << "Day of Week: " 1 + Pointer_Variable->tm_wday;

5. The tm_mday Element: When a programmer wants to display a number that is going to represent the day of a

month, he/she has to use the tm_mday element; in this case, the minimum value of the tm_mday element is going

to be 1, and the maximum value of the tm_mday element is going to be 31. As you should know, 1 is going to be

the first day of a month, and 31 will be the last day of a month if a month has 31 days.

6. The tm_yday: A C++ developer can use the tm_yday when he/she wants to build a computer application that is

going to display a number that will represent the day of a year. In this case, the minimum value of the tm_yday

element is 0, and the maximum value of the tm_yday is 365 or 366; therefore, if a coder wants to display some

numbers that go from 1 (January 1) to 365 (a non-leap year has 365 days) or 366 (a leap year has 366 days),

he/she has to add the number one to his/her cout statement.

7. The tm_mon Element: A computer scientist can use the tm_mon element when he/she wants to show a number

that will represent the month of a year. In this case, the minimum value is 0, and the maximum value is 11; hence,

an individual should use the number one to display some numbers that go from 1 (January) to 12 (December).

8. The tm_year Element: An individual can use the tm_year element to display the current year of a computer. An

important point that a coder should know about this element is that the tm_year element is going to display the

current year minus 1900, so a coder has to add the number 1900 inside of his/her cout statement to display the

current time of a system. As you can see, this is an example of how to show the current year of a computer

through the tm_year element: cout << "Year: " 1900 + Pointe_Variabler->tm_year;.

Page 144: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

143

• The gmtime Function: After using the ctime function to store a time value on a regular variable, a C++ developer

can use the gmtime function to convert a time value into a pointer variable that is going to hold the Coordinated

Universal Time (UTC) of a computer. In this process, a regular variable of time_t data-type is converted into a

pointer variable of tm data-type, also known as a tm structure. As you can see, this is the syntax of the gmtime

function: struct tm *gmtime(const time_t *timer);. Hence, if a coder wants to use the previous syntax to convert a

time value into a UTC time value, he/she has to type a line of code similar to this:

tm *Pointer_Variable = gmtime(&Regular_Variable);

o Pointer_Variable: In this field, an individual has to type a pointer variable that is going to hold a UTC time value.

o Regular_Variable: In this parameter, a C++ developer has to type a variable name that is holding a time value that

a coder was able to obtain after using the ctime function.

An important fact that an individual should know about the gmtime function is that this function is going to obtain

a UTC time value in a way that a human being cannot read easily; therefore, after using the gmtime function, an

individual should use the asctime function to convert a UTC time value into a group of characters that he/she can

read easily.

• The asctime Function: After using the gmtime function to store a UTC time value into a pointer variable of tm

structure, a coder can use the asctime function to convert a UTC time value into a string or group of characters

that a human being can read easily because the asctime function is going to store a UTC time value in this kind of

format: Day Month Date Hours:Minutes:Seconds Years; then, after using the asctime function to store a UTC

value, a coder can use the cout statement to display a UTC time value in a comfortable format. As you can see,

this is the syntax of the asctime function: char *asctime(const struct tm *timeptr);. Therefore, if a coder wants to

use the asctime function, he/she has to type a line of code like this:

char *Pointer_Variable asctime(GMTime_Variable);

o Pointer_Variable: In this field, an individual has to type a pointer variable that is going to hold a UTC time value

in this format: Day Month Date Hours:Minutes:Seconds Years \n\0.

Page 145: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

144

o GMTime_Variable: In the first parameter of the asctime function, an individual has to type a variable name,

which is the same variable that he/she used to store a UTC time value through the gmtime function.

Source Code – Date and Time:

#include <iostream> //Including the ctime library header file to work w ith date and time: #include <ctime> using namespace std; int main() { //The main function. /*The ctime header file has this data-type: time_t. We will use the time_t data-type and its time func tion to hold the current time of a computer*/ //Using the time_t data-type to build a variable: time_t AG_PCTime; //Using the time function for the AG_PC-Time variab le: AG_PCTime = time(0); //Displaying the number of seconds since 01/01/1970 : cout << "Number of seconds since 01/01/1970:" << endl << AG_PCTime << endl;

/*The ctime header file has this function: ctime. After using the time function to hold a time-value , we will use the ctime function to convert a time-value into a grou p of characters, also known as a string.*/ //Using the ctime function to convert a time-value to characters: char *AG_Date = ctime(&AG_PCTime);

//Using the cout statement to display the previous characters of a time-value: cout << "Displaying the local time of a computer: " << endl << AG_Date; return 0;

}

Result – Date and Time:

Page 146: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

145

Source Code – Date and Time – The localtime Function:

#include <iostream> //Including the ctime library header file to work w ith date and time: #include <ctime> using namespace std; int main() { //The main function. /*The ctime header file has this data-type: time_t. We will use the time_t data-type and its time func tion to hold the current time of a computer*/ //Using the time_ t data - type to build a variable: time_t AG_PCTime; //Using the time function for the AG_PC - Time variable: AG_PCTime = time(NULL); //Displaying the number of seconds since 01/01/1970 : cout << "Number of seconds since 01/01/1970:" << endl << AG_PCTime << endl; /*The ctime header file has this function: localtim e. After using the time function to hold a time - value, we will use the localtime function to convert a time-value into a group of characters, also known as a string.*/ //Using the localtime function to convert a time-va lue: tm *AG_Pointer = localtime(&AG_PCTime); //Using the cout statement to display the previous characters of a time-value: cout << "Time (Hours:Minutes:Seconds): " << AG_Pointer - >tm_hour; cout << ":" << AG_Pointer->tm_min; cout << ":" << AG_Pointer - >tm_sec << endl; cout << "Day of Week: " << 1 + AG_Pointer->tm_wday << endl; cout << "Day of Month: " << AG_Pointer->tm_mday << endl; cout << "Day of Year: " << 1 + AG_Pointer->tm_yday << endl; cout << "Current Month: " << 1 + AG_Pointer->tm_mon << endl; cout << "Current Year: " << 1900 + AG_Pointer->tm_year << endl; return 0;

}

Result – Date and Time – The localtime Function:

Page 147: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

146

Source Code – Date and Time – UTC Time:

#include <iostream>

//Including the ctime library header file to work w ith date and time:

#include <ctime>

using namespace std;

int main() { //The main function.

char AG;

/*The ctime header file has this data-type: time_t.

We will use the time_t data-type and its time func tion to hold the

current time of a computer*/

//Using the time_t data-type to build a variable:

time_t AG_PCTime;

//Using the time function for the AG_PC-Time variab le:

AG_PCTime = time(0);

//Displaying the number of seconds since 01/01/1970 :

cout << "Number of seconds since 01/01/1970:" << endl << AG_PCTime << endl;

/*The ctime header file has those two functions: gm time and asctime.

We will use the two previous functions to display the UTC time of a

computer.*/

//The gmtime function will convert a time value to a tm structure:

tm *AG_Date = gmtime(&AG_PCTime);

//The asctime function will convert a tm time struc ture into a group

//of characters, also known as a string.

char *AG_StringTime = asctime(AG_Date);

//The cout statement will display the Coordinated U niversal Time:

cout << "Displaying the UTC of a computer: " << endl << AG_StringTime;

return 0;

}

Result – Date and Time – UTC Time:

Page 148: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

147

Reference Variable

In this chapter, a coder can expand his/her knowledge and start learning about reference variables. What is a

reference variable? It is a variable that is going to hold the same values of a regular variable. In the first example, you

are going to notice that a reference variable is going to display the same value of a regular variable. Then, in the

second example, you will see those stages:

I. After building a simple variable, I am going to build a reference variable.

II. Second, I am going to display the value of a reference variable, which will be 86.

III. Third, I am going to add a new value to a simple variable; at the same time, a reference variable is going to

change its value because the value of a reference variable is the same of a regular variable.

IV. Finally, I am going to show the new value of a reference variable, which will be 20.

Source Code – First Example of a Reference Variable:

#include <iostream> using namespace std;

int main () { //Declaring a regular variable: int AG_Regular = 86; //Declaring a reference variable: int & AG_Reference = AG_Regular; //Displaying a regular variable: cout << "Regular Variable: " << AG_Regular << endl; //Displaying a reference variable: cout << "Reference Variable: " << AG_Reference << endl; return 0; }

Result – First Example of a Reference Variable:

Page 149: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

148

Source Code – Second Example of a Reference Variable:

#include <iostream>

using namespace std;

int main () {

//Declaring a regular variable:

int AG_Regular = 86;

//Declaring a reference variable:

int & AG_Reference = AG_Regular;

//Displaying the first value of a reference variabl e:

cout << "First Value of a Reference Variable: " << AG_Reference << endl;

/*Adding a new value to a regular variable. At the same

time, the reference variable is going to get that value*/

AG_Regular = 20;

//Displaying the new value of a reference variable:

cout << "New Value of a Reference Variable: " << AG_Reference << endl;

return 0;

}

Result – Second Example of a Reference Variable:

Page 150: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

149

Exception Handling

An individual can use the exception handling technique when he/she wants to build a computer application that is

going to perform an exception if a coder/user does something that is not allowed, such as including a specific digit or

character. At the end of this section, you will see an example of exception handling; in that example, the computer

application is going to perform an exception if a GPA is more than 4.0 because the GPA limit is four. Some of the

most important commands or statements that a coder can use to perform an exception handling technique are known

as try, throw, and catch.

• The try Statement: An individual can use a try statement when he/she wants to build a computer application that

is going to deal with one or more exceptions. Then, after using a try statement, a coder may use a throw statement

to perform some tasks; finally, a coder “has to use” one or more catch statements to perform other operations. An

important point that a coder should know about a try statement is that he/she has to use a try statement first than a

throw/catch statement; furthermore, a coder should not use a try statement without using a catch statement.

• The throw Statement: An individual can use the throw keyword when he/she wants to launch or throw an

exception. While a coder is using a throw statement, he/she can also use an if statement to build a block of code

similar to this:

If (condition) {

throw "Text_Message";

}

o Condition: In this parameter, an individual has to type a condition related to an exception. For example, if a digit

is more than four, the computer application is going to throw or launch an exception; in that case, a coder has to

type a line of code like this: variable_name >= 4.

o Text_Message: In this field, a programmer has to type a text message, which is going to be displayed on a

computer screen if the previous condition is affirmative; on the other hand, if the previous condition is negative,

the throw statement will not display a text message.

Page 151: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

150

An important point that a coder should know about a throw statement is that he/she has to use a throw statement

inside of a try statement.

• The catch Statement: A coder can use the catch statement when he/she wants to catch an exception and display a

text message related to an exception. As you can see, this is the syntax of a catch statement:

catch (Exception_Name Variable) {

//Codes to handle an exception.

}

o Exception_Name: In this field, an individual has to type an exception name. If a coder is able to throw or launch

an object of type const char*, he/she has to type const char* inside of the catch statement; in that case, an

individual has to type a line of code like this: catch (const char *Pointer_Variable) { }. In addition to the const

char, a programmer can also use an ellipsis (…) when he/she wants to build a catch statement that is going to

handle any type of exception; in that case, a coder has to type an ellipsis inside of the parenthesis of a catch

statement. As you can see, this is an example of how to use a catch statement and an ellipsis: catch (…) { }. If an

individual wants to understand/learn more about the programming techniques that a coder can use through a catch

statement, he/she has to see the source codes that are located at the end of this section.

• The Try-Throw-Catch Statement: When an individual wants to use the three previous statements to perform the

exception handling technique, he/she has to type a block of code similar to this:

try {

if(condition) {

throw "Text message related to an exception";

}

}

catch (Exception_Name Variable) {

//Codes to handle an exception.

}

Page 152: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

151

Source Code – First Example – Ellipsis:

#include <iostream>

using namespace std;

int main() {

int AG_A;

for (AG_A = 80; AG_A <= 90; AG_A++) {

try {

if (AG_A == 86) {

throw "Exception!" ;

}

else {

cout << "Number: " << AG_A << endl;

}

}

catch (...) {

}

}

return 0;

}

Result – First Example – Ellipsis:

Page 153: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

152

Source Code – Second Example – Const Char:

#include <iostream>

using namespace std;

int main() {

int AG_A;

for (AG_A = 80; AG_A <= 90; AG_A++) {

try {

if (AG_A == 86) {

throw "Exception: AG_A is equal to 86" ;

}

else {

cout << "Number: " << AG_A << endl;

}

}

catch ( char const *AG_X) {

cerr << AG_X << endl;

}

}

return 0;

}

Result – Second Example – Const Char:

Page 154: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

153

Source Code – Third Example – Const Char & Function:

#include <iostream>

using namespace std;

double AG_Exception( int AG_A) {

if (AG_A == 86) {

throw "Exception: AG_A is equal to 86" ;

}

return AG_A;

}

int main() {

int AG_A;

for (AG_A = 80; AG_A <= 90; AG_A++) {

try {

AG_A = AG_Exception(AG_A);

cout << "Number: " << AG_A << endl;

}

catch ( char const *AG_X) {

cerr << AG_X << endl;

}

}

return 0;

}

Result – Third Example – Const Char & Function:

Page 155: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

154

Source Code – Fourth Example – Part 1:

#include <iostream>

using namespace std;

double AG_Exception( int AG_A) {

if (AG_A >= 4) {

throw "Exception: GPA is more than four." ;

}

return AG_A;

}

int main() {

int AG_GPA = 3; //GPA in a 4.0 scale.

try {

AG_GPA = AG_Exception(AG_GPA);

cout << "GPA: " << AG_GPA << endl;

}

catch ( char const *AG_X) {

cerr << AG_X << endl;

}

return 0;

}

Result – Fourth Example – Part 1:

Page 156: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

155

Source Code – Fourth Example – Part 2:

#include <iostream>

using namespace std;

double AG_Exception( int AG_A) {

if (AG_A >= 4) {

throw "Exception: GPA is more than four." ;

}

return AG_A;

}

int main() {

int AG_GPA = 5; //GPA in a 4.0 scale.

try {

AG_GPA = AG_Exception(AG_GPA);

cout << "GPA: " << AG_GPA << endl;

}

catch ( char const *AG_X) {

cerr << AG_X << endl;

}

return 0;

}

Result – Fourth Example – Part 2:

Page 157: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

156

Dynamic Memory Allocation

In this section, an individual can start learning about a computer programming technique that is called dynamic

memory allocation. When a programmer wants to allocate or store a variable value on the random-access memory

(RAM) of a computer, he/she can store his/her variable value inside of two different places, which are known as stack

and heap—in this section, the heap is a location on the RAM, and it should not be confuse with another heap concept

that states that a heap is a data structure that deals with nodes and max-heap/min-heap on a family tree. On the one

hand, a variable value is going to be stored within the stack if a coder declares a local variable without using the new

operator, in other words, a variable value will be stored on the stack if an individual is able to build a variable inside

of a function without using the new operator; on the other hand, a variable value is going to be stored within the heap

if a programmer is able to build or declare a variable through a pointer and a new operator.

• The new Operator: As explained above, an individual can use the new operator to store a variable value within the

heap; in that case, if a coder wants to use the new operator to store a variable value on the heap, he/she has to use

this syntax: pointer_variable = new data-type;. Evidently, in the previous line of code, a programmer has to type

a pointer variable before using the new operator, which means that a coder has to build a pointer variable before

using the new operator. As you can see, this is an example of how to declare a pointer and use the new operator:

int *Pointer_Variable; //Declaring a pointer variable.

Pointer_Variable = new int; //Using the new operator to allocate memory on the heap.

Then, after using the new operator, an individual can use the equal operator to store a value inside of a pointer

variable; in that case, a coder has to type a line of code like this: *Pointer_Variable = Value;. As you can see,

this is an example of how to declare a pointer variable, use the new operator, and store a value:

int *Pointer_Variable; //Declaring a pointer variable.

Pointer_Variable = new int; //Using the new operator to allocate memory on the heap.

*Pointer_Variable = 52086; //Adding the 52086 value to a pointer variable thanks to the equal operator.

Page 158: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

157

An important point that a C++ developer should know about storing dynamic memory on the heap is that he/she

has to clean his/her garbage. To put it another way, after using the new operator to allocate a block of memory

within the heap, a coder has to clean or de-allocate his/her block of memory; in that case, an individual can use

the delete operator to erase those variable values that are stored on the heap.

• The delete Operator: As previously stated, a C++ developer can use the delete operator when he/she wants to de-

allocate those variable values that are stored on the heap; in that case, a coder can use this syntax to de-allocate

dynamic memory: delete Pointer_Variable;.

• Memory Allocation and Arrays: In addition to the data-types, an individual can also use the new operator to

allocate different kinds of arrays inside of the heap; in that case, a coder has to type a block of code like this:

data-type *Pointer_Variable; //Declaring a pointer variable.

Pointer_Variable = new data-type[size]; //Using the new operator to allocate an array on the heap.

Then, after using the new operator to allocate or store an array on the heap, a C++ developer can use the delete

operator to clean or de-allocate that kind of memory that is stored inside of the heap; in that case, an individual

can use this syntax to de-allocate an array: delete [] Pointer_Variable;. As you can see, this is an example of how

to declare a pointer variable, use the new operator to allocate an array, and use the delete operator to de-allocate

an array:

data-type *Pointer_Variable; //Declaring a pointer variable.

Pointer_Variable = new data-type[size]; //Using the new operator to allocate an array on the heap.

Delete [] Pointer_Variable; //Using the delete operator to de-allocate a block of memory.

In the second source code, you will see an example of how to build a structure or object that is going to work with

an array of characters. To put it another way, you are going to see an example of how to use the new operator to

allocate an array of objects inside of the heap; then, we are going to de-allocate or clean that array of objects

thanks to the delete operator.

Page 159: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

158

Source Code – First Example:

#include <iostream>

using namespace std;

int main() { //The main function.

//Declaring a pointer variable:

int *AG_Pointer_Variable;

//Using the new operator to allocate memory within the heap:

AG_Pointer_Variable = new int ;

//Adding a value to a pointer variable:

*AG_Pointer_Variable = 52086;

//Displaying a pointer value thanks to the cout sta tement:

cout << *AG_Pointer_Variable << endl;

//Using the delete operator to de-allocate memory:

delete AG_Pointer_Variable;

return 0;

}

Result – First Example:

Page 160: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

159

Source Code – Second Example:

#include <iostream>

using namespace std;

//Declaring a structure:

struct AG_Struct {

//Building a public function to display a text mess age:

public :

AG_Struct() {

cout << "The Easiest Way to Learn C/C++/C# by Socket_0x03." << endl;

}

};

//The main Function:

int main() {

//Declaring a pointer variable:

AG_Struct *AG_Array;

//Using the new operator to allocate memory on the heap:

AG_Array = new AG_Struct[5];

//Using the delete operator to de-allocate memory:

delete [] AG_Array;

return 0;

}

Result – Second Example:

Page 161: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

160

Namespace

Sometimes, while an individual is using the C++ programming language to build a computer application, he/she

has to declare a function that is using exactly the same name than another function; then, a programmer has to call

one of those functions that are using the same name. What computer programming technique can a coder apply to call

a specific function if both functions are using the same name? In this situation, before calling a specific function, an

individual has to use the namespace keyword while he/she is declaring or building a group of identifiers, which are

those identifiers that are using some functions of the same name; in this case, a coder has to use a syntax like this:

namespace First_Identifier_Name {

void Function_Name() {

//Codes of a function.

}

}

namespace Second_Identifier_Name {

void Function_Name() {

//Codes of a function.

}

}

If we use the previous syntax to build a block of code, we can obtain this result:

namespace AG_Stock_Market {

void AG_Income() {

cout << "Net Income: $1500" << endl;

}

}

namespace AG_Bond_Market {

void AG_Income() {

cout << "Net Income: $2000" << endl;

}

}

Page 162: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

161

As you can see in the previous block of code, I am typing the namespace keyword before typing an identifier

name, which is also called a namespace name. Furthermore, you are going to notice that both identifiers or

namespaces are using two functions of the same name; to put it another way, in the previous block of code, you will

notice that there are two identifiers or namespaces (AG_Stock_Market and AG_Bond_Market) that are using a

function of the same name, which is known as the AG_Income function.

After using the namespace keyword to declare a group of identifiers, an individual can use two different kinds of

programming techniques to call a function that is using exactly the same name of another function. As you can see,

those are the two programming techniques that a coder can apply to call a specific function: the double-colon (::) and

the using keyword.

• The double-colon Technique: Once an individual has used the namespace keyword, he/she can use this syntax to

call a function: Identifier_Name::Function_Name();.

o Identifier_Name: In this field, a coder has to type an identifier name, which is also known as a namespace name.

o Function_Name: Here, an individual has to type a function name, which is the function that is going to perform

some operations or tasks.

In the first source code, you will see an example of how to call a function thanks to the double-colon technique.

• The using Keyword: After using the namespace keyword to build some identifiers or namespaces, an individual

can use this syntax to call a function:

using namespace Identifier_Name;

Function_Name();

In the second source code, you will see an example of this computer programming technique.

• The Nested Namespace: An important point that a coder should know about the namespace keyword is that he/she

can use a namespace identifier inside of another namespace identifier, which is a programming technique that is

known as the nested namespace. If a C++ developer wants to perform the nested namespace technique, he/she can

use this syntax:

Page 163: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

162

namespace First_Identifier_Name {

void Function_Name() {

//Codes of a function.

}

namespace Second_Identifier_Name {

void Function_Name() {

//Codes of a function.

}

}

}

If we use the previous syntax to build a block of code, we can obtain a result like this:

namespace AG_Commodity_Market {

void AG_Income() {

cout << "Net Income: $2000" << endl;

}

//Declaring the first nested namespace:

namespace AG_Hard_Commodity {

void AG_Income() {

cout << "Net Income: $1500" << endl;

}

//Declaring the second nested namespace:

namespace AG_Soft_Commodity {

void AG_Income() {

cout << "Net Income: $500" << endl;

}

}

}

In the last source code of this section, you are going to see an example of how to perform the nested namespace

technique. In that source code, the nested namespace technique is performed inside of the third namespace, which

is called the AG_Commodity_Market.

Page 164: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

163

• The using Keyword for a Nested-Namespace: After building a nested-namespace, an individual can use this

syntax to call a function that is inside of a nested-namespace:

using namespace NameSpace_Name::Nested-NameSpace_Name;

Function_Name();

As you can see in the previous block of code, there is a double-colon (::) between a regular namespace and a

nested namespace; therefore, each time that a coder wants to call a function that is on a nested-namespace, he/she

has to use the double-colon (::).

Example: In the third source code of this section, you will see an example of how to call a nested-namespace

through the using keyword. In that example, I am typing a block of code like this:

using namespace AG_Commodity_Market::AG_Soft_Commodity;

AG_Income();

Page 165: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

164

Source Code – First Example:

#include <iostream>

using namespace std;

//Declaring the first namespace:

namespace AG_Stock_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $2000" << endl;

cout << "NET Income: $1500" << endl << endl;

}

}

//Declaring the second namespace:

namespace AG_Bond_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $20000" << endl;

cout << "NET Income: $2000" << endl << endl;

}

}

//Declaring the third namespace:

namespace AG_Commodity_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $10000" << endl;

cout << "NET Income: $2000" << endl << endl;

Page 166: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

165

}

}

//The main Function:

int main () {

cout << "Stock Market: " << endl;

//Calling the AG_Income function from the first nam espace:

AG_Stock_Market::AG_Income();

cout << "Bond Market: " << endl;

//Calling the AG_Income function from the second na mespace:

AG_Bond_Market::AG_Income();

cout << "Commodity Market: " << endl;

//Calling the AG_Income function from the third nam espace:

AG_Commodity_Market::AG_Income();

return 0;

}

Result – First Example:

Page 167: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

166

Source Code – Second Example:

#include <iostream>

using namespace std;

//Declaring the first namespace:

namespace AG_Stock_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $2000" << endl;

cout << "NET Income: $1500" << endl << endl;

}

}

//Declaring the second namespace:

namespace AG_Bond_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $20000" << endl;

cout << "NET Income: $2000" << endl << endl;

}

}

//Declaring the third namespace:

namespace AG_Commodity_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $10000" << endl;

cout << "NET Income: $2000" << endl << endl;

Page 168: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

167

}

}

//The main Function:

int main () {

cout << "Bond Market: " << endl;

//Calling the AG_Income function from the second na mespace:

using namespace AG_Bond_Market;

AG_Income();

return 0;

}

Result – Second Example:

Page 169: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

168

Source Code – Nested Namespace:

#include <iostream>

using namespace std;

//Declaring the first namespace:

namespace AG_Stock_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $2000" << endl;

cout << "NET Income: $1500" << endl << endl;

}

}

//Declaring the second namespace:

namespace AG_Bond_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $20000" << endl;

cout << "NET Income: $2000" << endl << endl;

}

}

//Declaring the third namespace:

namespace AG_Commodity_Market {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $10000" << endl;

cout << "NET Income: $2000" << endl << endl;

}

//Declaring the first nested namespace:

namespace AG_Hard_Commodity {

//Declaring the AG_Income Function:

Page 170: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

169

void AG_Income() {

cout << "Gross Income: $9000" << endl;

cout << "NET Income: $1500" << endl << endl;

}

}

//Declaring the second nested namespace:

namespace AG_Soft_Commodity {

//Declaring the AG_Income Function:

void AG_Income() {

cout << "Gross Income: $11000" << endl;

cout << "NET Income: $500" << endl << endl;

}

}

}

//The main Function:

int main () {

cout << "Soft-Commodity Market: " << endl;

/*Calling the AG_Income function, which is located on

the second nested namespace:*/

using namespace AG_Commodity_Market::AG_Soft_Commodity;

AG_Income();

return 0;

}

Result – Nested Namespace:

Page 171: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

170

Object Oriented: Classes and Objects

In this chapter, an individual can start learning about object-oriented programming (OOP), which is a computer

programming model or paradigm that a coder can use to build those kinds of computer applications that are working

with objects because an object-oriented programming language is designed to work with objects. An important fact

that a C/C++ developer should know about those languages is that the C programming language is not an object-

oriented language; on the other hand, C++ is an object-oriented language, which means that a coder can use the C++

programming language to work with different kinds of classes and objects. In this section, a C++ developer can

expand his/her knowledge and start learning how to build different kinds of classes and objects.

• Classes: Similar to the struct statement, an individual can use the class statement when he/she wants to define a

block of code that is going to hold a group of members; as previously stated, a member is composed of a data-

type and a variable name. When a coder is building a class, he/she has to type a block of code similar to this:

class Class_Name {

Access-to-Members:

First_Data-Type Variable_Name;

Second_Data-Type Variable_name;

Third_Data-Type Variable_Name;

};

Access-to-Members: In this field, an individual has to specify an access method, which can be private or public.

On the one hand, a coder is going to use the private keyword when he/she wants to define a class with private

members, which are those members that a programmer can use only inside of a class. On the other hand, a coder

is going to use the public keyword when he/she wants to define a class with public members, which are those

members that an individual can use outside of a class; to put it another way, an individual has to type the public

keyword when he/she wants to build a class that is going to hold a group of members that are public to other

functions/classes, which means that a class member can be accessed from other classes or functions, such as the

main function. In the next source code, we are going to define a class with public members because we want to

Page 172: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

171

use a group of public members inside of the main function; in other words, we will define a public class because

we want to use the main function to access to some public members.

• Objects: After building a class that is holding a group of public members, an individual has to declare some

objects inside of a function to start using the public members of a class on his/her function. In that case, a C++

developer has to type a line of code similar to this:

Class_Name Object_Name;

o Class_Name: In this field, a C++ developer has to type a class name, which is the name of a class that a coder was

able to build through the class statement.

o Object_Name: In this field, a coder has to type an object name. Then, if an individual wants to access to a public

member because he/she wants to assign a value to his/her member, he/she has to type a line of code similar to

this: Object_Name.Member_Name = Value;.

An important point that a coder should remember is that he/she has to declare an object in a similar way that

he/she defines a data-type and its variable name. As you can see, those are some examples of how to declare an

object and a variable name:

Class_Name Object_Name; //Example of how to define an object.

Data-Type Variable_Name; //Example of how to define a variable.

Page 173: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

172

Source Code – Classes & Objects:

#include <iostream>

using namespace std;

//Building a class, which is called Earnings:

class Earnings {

public :

int Platinum;

int Diamond;

int Silver;

};

int main() {

//Declaring an object (Earnings January):

Earnings January; //Declaring January of Earnings type.

//Declaring another object (Earnings February):

Earnings February; //Declaring February of Earnings type.

int Total; //Declaring a variable to add the total amount of c ash.

//Earning 8000 dollars for a platinum investment:

January.Platinum = 8000;

//Earning 5000 dollars for a diamond investment:

January.Diamond = 5000;

//Earning 2500 dollars for a silver investment:

January.Silver = 2500;

Page 174: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

173

//Earning 9000 dollars for a platinum investment:

February.Platinum = 9000;

//Earning 7000 dollars for a diamond investment:

February.Diamond = 7000;

//Earning 3000 dollars for a silver investment:

February.Silver = 3000;

cout << "Total Gross per Month:" << endl;

/*Adding the total amount of cash that a

corporation was able to earn on January:*/

Total = January.Platinum + January.Diamond + Janua ry.Silver;

//Displaying the total amount of cash:

cout << "January: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on February:*/

Total = February.Platinum + February.Diamond + Feb ruary.Silver;

//Displaying the total amount of cash:

cout << "February: " << Total << endl;

return 0;

}

Result – Classes & Objects:

Page 175: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

174

Source Code – Classes & Objects – Private Members & Errors:

/*Note: This application will display an error beca use there is a

private member, which is called Silver. As previous ly stated, a

coder cannot access to a private member through a f unction, such

as the main function.*/

#include <iostream>

using namespace std;

//Building a class, which is called Earnings:

class Earnings {

public :

int Platinum;

int Diamond;

//Declaring a function:

int Total_F( void );

void TPlatinum ( int TP);

void TDiamond ( int TD);

void TSilver ( int TS);

//Declaring a private member:

private :

int Silver;

};

//Defining functions:

int Earnings::Total_F( void ) {

//Adding the total amount of cash:

return Platinum + Diamond + Silver;

}

void Earnings::TPlatinum ( int TP) {

Page 176: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

175

Platinum = TP;

}

void Earnings::TDiamond ( int TD) {

Diamond = TD;

}

void Earnings::TSilver ( int TS) {

Silver = TS;

}

//The Main Function:

int main() {

//Declaring an object (Earnings January):

Earnings January; //Declaring January of Earnings type.

//Declaring another object (Earnings February):

Earnings February; //Declaring February of Earnings type.

//Declaring another object (Earnings March):

Earnings March; //Declaring March of Earnings type.

int Total; //Declaring a variable to add the total amount of c ash.

//Earning 8000 dollars for a platinum investment:

January.Platinum = 8000;

//Earning 5000 dollars for a diamond investment:

January.Diamond = 5000;

//Earning 2500 dollars for a silver investment:

January.Silver = 2500;

//Earning 9000 dollars for a platinum investment:

February.Platinum = 9000;

//Earning 7000 dollars for a diamond investment:

February.Diamond = 7000;

//Earning 3000 dollars for a silver investment:

February.Silver = 3000;

Page 177: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

176

//Earning 6000 dollars for a platinum investment:

March.Platinum = 6000;

//Earning 4000 dollars for a diamond investment:

March.Diamond = 4000;

//Earning 1000 dollars for a silver investment:

March.Silver = 1000;

cout << "Total Gross per Month:" << endl;

/*Adding the total amount of cash that a

corporation was able to earn on January:*/

Total = January.Total_F();

//Trying to display the total amount of cash:

cout << "January: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on February:*/

Total = February.Total_F();

//Trying to display the total amount of cash:

cout << "February: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on March:*/

Total = March.Total_F();

//Trying to display the total amount of cash:

cout << "March: " << Total << endl;

return 0;

}

Result – Classes & Objects – Private Members & Errors:

Page 178: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

177

Source Code – Classes & Objects – Using Functions:

#include <iostream>

using namespace std;

//Building a class, which is called Earnings:

class Earnings {

public :

int Platinum;

int Diamond;

int Silver;

//Declaring a function:

int Total_F( void );

void TPlatinum ( int TP);

void TDiamond ( int TD);

void TSilver ( int TS);

};

//Defining functions:

int Earnings::Total_F( void ) {

//Adding the total amount of cash:

return Platinum + Diamond + Silver;

}

void Earnings::TPlatinum ( int TP) {

Platinum = TP;

}

void Earnings::TDiamond ( int TD) {

Diamond = TD;

}

Page 179: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

178

void Earnings::TSilver ( int TS) {

Silver = TS;

}

//The Main Function:

int main() {

//Declaring an object (Earnings January):

Earnings January; //Declaring January of Earnings type.

//Declaring another object (Earnings February):

Earnings February; //Declaring February of Earnings type.

//Declaring another object (Earnings March):

Earnings March; //Declaring March of Earnings type.

int Total; //Declaring a variable to add the total amount of c ash.

//Earning 8000 dollars for a platinum investment:

January.Platinum = 8000;

//Earning 5000 dollars for a diamond investment:

January.Diamond = 5000;

//Earning 2500 dollars for a silver investment:

January.Silver = 2500;

//Earning 9000 dollars for a platinum investment:

February.Platinum = 9000;

//Earning 7000 dollars for a diamond investment:

February.Diamond = 7000;

//Earning 3000 dollars for a silver investment:

February.Silver = 3000;

//Earning 6000 dollars for a platinum investment:

March.Platinum = 6000;

//Earning 4000 dollars for a diamond investment:

Page 180: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

179

March.Diamond = 4000;

//Earning 1000 dollars for a silver investment:

March.Silver = 1000;

cout << "Total Gross per Month:" << endl;

/*Adding the total amount of cash that a

corporation was able to earn on January:*/

Total = January.Total_F();

//Displaying the total amount of cash:

cout << "January: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on February:*/

Total = February.Total_F();

//Displaying the total amount of cash:

cout << "February: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on March:*/

Total = March.Total_F();

//Displaying the total amount of cash:

cout << "March: " << Total << endl;

return 0;

}

Result – Classes & Objects – Using Functions:

Page 181: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

180

Object Oriented: Inheritance

Inheritance, an object-oriented programming technique developed by two computer scientists in 1967, is an OOP

technique that an individual can use to perform an IS-A relationship among two or more classes/objects. What is an

IS-A relationship? It is the relationship between a subclass or child-class and its superclass or parent-class. To put it

another way, an IS-A is an inheritance relationship between a derived class and its base class, where they are sharing

the same properties and behaviours, which means that a member declared on a superclass can be used on a subclass.

Thanks to the inheritance technique, if a coder was able to declare a group of members/functions on a class, he/she

does not have to build the same members/functions on another class. As you can see, this is the syntax that a C++

developer can use when he/she wants to perform an inheritance technique:

class subclass: access-method superclass

o Subclass: In this field, a coder has to specify a subclass, also known as a derived-class.

o Access-Method: In this field, an individual has to specify the way that he/she wants to gain access to the members

or functions of a superclass, also called a base class. As you can see, those are some of the keywords that an

individual can use when he/she is specifying an access method: public, private, or protected. If a coder does not

specify an access method, the subclass is going to be private by default.

o Superclass: In this field, a C++ developer has to type a superclass, also known as a base class or main class.

An important point that a coder should know about the inheritance technique is that he/she can perform two

different kinds of inheritances, which are known as simple and multiple. On the one hand, a simple inheritance is

composed of a base-class and a derived-class. On the other hand, a multiple inheritance is composed of two or more

base-classes and one or more derived-classes; for example, in the second source code of this section, you are going to

notice a computer application that is composed of two base-classes and a derived-class. When a C++ developer wants

to perform a multiple inheritance technique, he/she has to type on his/her derived-class a line of code like this:

class subclass: access-method first-superclass, access-method second-superclass

Page 182: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

181

Source Code – Simple Inheritance:

#include <iostream>

using namespace std;

//Building the Earnings class, which is a base clas s:

class Earnings {

public :

void TPlatinum( int P) {

Platinum = P;

}

void TDiamond( int D) {

Diamond = D;

}

void TSilver( int S) {

Silver = S;

}

protected :

int Platinum;

int Diamond;

int Silver;

};

//Building the TotalEarnings class, which is a deri ved class:

class TotalEarnings: public Earnings {

public :

//Adding the total amount of cash:

int Total_F() {

return (Platinum + Diamond + Silver);

Page 183: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

182

}

};

//The Main Function:

int main() {

//Declaring an object (Earnings January):

TotalEarnings January; //Declaring January of Earnings type.

//Declaring another object (Earnings February):

TotalEarnings February; //Declaring February of Earnings type.

//Declaring another object (Earnings March):

TotalEarnings March; //Declaring March of Earnings type.

int Total; //Declaring a variable to add some values.

//Earning 8000 dollars for a platinum investm ent:

January.TPlatinum(8000);

//Earning 5000 dollars for a diamond investment:

January.TDiamond(5000);

//Earning 2500 dollars for a silver investment:

January.TSilver(2500);

//Earning 9000 dollars for a platinum investment:

February.TPlatinum(9000);

//Earning 7000 dollars for a diamond investment:

February.TDiamond(7000);

//Earning 3000 dollars for a silver investment:

February.TSilver(3000);

//Earning 6000 dollars for a platinum investment:

March.TPlatinum(6000);

//Earning 4000 dollars for a diamond investment:

Page 184: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

183

March.TDiamond(4000);

//Earning 1000 dollars for a silver investment:

March.TSilver(1000);

cout << "Total Gross per Month:" << endl;

/*Adding the total amount of cash that a

corporation was able to earn on January:*/

Total = January.Total_F();

//Displaying the total amount of cash:

cout << "January: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on February:*/

Total = February.Total_F();

//Displaying the total amount of cash:

cout << "February: " << Total << endl;

/*Adding the total amount of cash that a

corporation was able to earn on March:*/

Total = March.Total_F();

//Displaying the total amount of cash:

cout << "March: " << Total << endl;

return 0;

}

Result – Simple Inheritance:

Page 185: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

184

Source Code – Multiple Inheritances:

#include <iostream>

using namespace std;

//Building the Earnings class, which is a base clas s:

class AG_Earnings {

public :

void AG_TPlatinum( int AG_P) {

AG_Platinum = AG_P;

}

void AG_TDiamond( int AG_D) {

AG_Diamond = AG_D;

}

void AG_TSilver( int AG_S) {

AG_Silver = AG_S;

}

protected :

int AG_Platinum;

int AG_Diamond;

int AG_Silver;

};

class AG_Investment {

public :

//Subtracting cash because of investment:

int AG_Investment_F( int AG_inv) {

return AG_inv - 5000;

}

};

//Building the TotalEarnings class, which is a deri ved class:

class AG_TotalEarnings: public AG_Earnings, public AG_Investment {

public :

Page 186: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

185

//Adding the total amount of cash:

int AG_Total_F() {

return (AG_Platinum + AG_Diamond + AG_Silver);

}

};

//The Main Function:

int main() {

//Declaring an object (Earnings January):

AG_TotalEarnings AG_January; //Declaring January of Earnings type.

//Declaring another object (Earnings February):

AG_TotalEarnings AG_February; //Declaring February of Earnings type.

//Declaring another object (Earnings March):

AG_TotalEarnings AG_March; //Declaring March of Earnings type.

int AG_Total_Gross; //Declaring a variable to add some values.

int AG_Total_Net; //Declaring a variable that will reduce $5000.

//Earning 8000 dollars for a platinum investm ent:

AG_January.AG_TPlatinum(8000);

//Earning 5000 dollars for a diamond investment:

AG_January.AG_TDiamond(5000);

//Earning 2500 dollars for a silver investment:

AG_January.AG_TSilver(2500);

//Earning 9000 dollars for a platinum investment:

AG_February.AG_TPlatinum(9000);

//Earning 7000 dollars for a diamond investment:

AG_February.AG_TDiamond(7000);

//Earning 3000 dollars for a silver investment:

AG_February.AG_TSilver(3000);

//Earning 6000 dollars for a platinum investment:

AG_March.AG_TPlatinum(6000);

//Earning 4000 dollars for a diamond investment:

AG_March.AG_TDiamond(4000);

//Earning 1000 dollars for a silver investment:

Page 187: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

186

AG_March.AG_TSilver(1000);

cout << "Total per Month:" << endl;

/*Adding the total amount of cash that a

corporation was able to earn on January:*/

AG_Total_Gross = AG_January.AG_Total_F();

//Subtracting cash because of investment:

AG_Total_Net = AG_January.AG_Investment_F(AG_Total _Gross);

//Displaying the total amount of cash:

cout << "January: " << AG_Total_Net << endl;

/*Adding the total amount of cash that a

corporation was able to earn on February:*/

AG_Total_Gross = AG_February.AG_Total_F();

//Subtracting cash because of investment:

AG_Total_Net = AG_February.AG_Investment_F(AG_Tota l_Gross);

//Displaying the total amount of cash:

cout << "February: " << AG_Total_Net << endl;

/*Adding the total amount of cash that a

corporation was able to earn on March:*/

AG_Total_Gross = AG_March.AG_Total_F();

//Subtracting cash because of investment:

AG_Total_Net = AG_March.AG_Investment_F(AG_Total_G ross);

//Displaying the total amount of cash:

cout << "March: " << AG_Total_Net << endl;

return 0;

}

Result – Multiple Inheritances:

Page 188: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

187

Object Oriented: Function Overloading

One of the object-oriented programming techniques that a coder can use is called function overloading. What is a

function overloading? It is when an individual is able to build a block of code that is composed of one or more

functions of the same name; for instance, a coder is performing a function overloading when he/she is building a

class/function that is composed of those two functions: AG_Function() and AG_Function(). As you can see in the

previous line, both functions have the same name; to put it another way, the two previous functions have the same

identifier. Then, after building a class that is composed of two functions of the same identifier, a coder can use the

main function to call those functions; in that case, the compiler is going to choose the most appropriate function,

which depends on the way that a coder/user is calling those functions.

In the first source code, I am building those two functions: AG_F(int AG_Integer) and AG_F(char* AG_C). First,

the first function is going to be executed because I am using the main function to call an integer number through a

line of code like this: AGC.AG_F(23);. Then, the second function will be executed because I am using the main

function to call a character instead of an integer number; in that case, I am typing this line: AGC_AG_F(“G”);.

In the second source code, I am building those two functions: AG_Function (int AG_Integer) and AG_Function

(char AG_F). First, the first function is going to be executed if a user is able to type an integer number on a computer

terminal; then, the second function will be executed if a user is able to type a character on a computer console.

An important point that a C++ developer should know about the function overloading is that he/she can perform

this technique if he/she is building two or more functions that are performing similar operations or tasks. Then, after

performing the function overloading technique, an individual or coder is going to be able to find some functions

easily because he/she is building an organized source code.

Page 189: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

188

Source Code – Overloading – First Example:

#include <iostream>

using namespace std;

//Declaring a class:

class AG_Class {

public :

//First Function:

void AG_F( int AG_Integer) {

cout << "Displaying an Integer Number: " << AG_Integer << endl;

}

//Second Function:

void AG_F( char * AG_C) {

cout << "Displaying a Character: " << AG_C << endl;

}

};

//The main function:

int main( void ) {

AG_Class AGC;

/*The next line will use the first function to

display an integer number*/

AGC.AG_F(23);

/*The next line will use the second function to

display a character*/

AGC.AG_F( "G" );

return 0;

}

Result – Overloading – First Example:

Page 190: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

189

Source Code – Overloading – Second Example:

#include <iostream> using namespace std; //Declaring a class: class AG_Class { public : //First Function: void AG_Function( int AG_Integer) { cout << "Number: " << AG_Integer << endl; } //Second Function: void AG_Function( char AG_F) { cout << "Character: " << AG_F << endl; } }; //The main function: int main( void ) { AG_Class AGC; int AG_A; //Declaring a variable to store an integer number. char AG_B; //Declaring a variable to store a character. //Displaying a text message: cout << "Type an integer number: " ; //The next line will store an integer cin >> AG_A; //The next line will display an integer: AGC.AG_Function(AG_A); //Displaying a text message: cout << "Type a character: " ; //The next line will store an integer cin >> AG_B; //The next line will display an integer: AGC.AG_Function(AG_B); return 0; }

Result – Overloading – Second Example:

Page 191: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

190

Object Oriented: Operator Overloading

In this section, an individual can start learning an object-oriented programming technique that is known as

operator overloading. What is an operator overloading? It is a programming technique that allows a coder to perform

some operations or tasks on an object or a user-defined data-type; for example, in the next source code, we are going

to overload the addition operator (+), which is going to perform this operation: adding the values of two objects and

store the final value inside of a third object. Thanks to this technique, instead of adding some values directly

(Variable_Name = 2320 + 2386), we can use a group of objects to add their values in a way similar to this:

Object_C = Object_B + Object_A

When a C++ developer wants to perform the operator overloading technique, he/she has to use two keywords,

which are called operator and this. In addition, an individual has to use a syntax similar to this:

Class_Name Operator_Keyword Operator_Symbol (const Class_Name &Object_Name) {

//Codes

}

o Operator Symbol: In this field, a C++ developer has to type that kind of operator that he/she wants to overload,

such as the addition operator (+), the subtraction operator (-), the multiplication operator (*), or other operators.

o Codes: In this field, an individual can use “the this keyword” when he/she wants to access to the members of a

class; for example, in the next source code, we are going to use “the this operator” to gain access to those three

members: AG_Platinum, AG_Gold, and AG_Silver.

One of the best ways to understand how to perform the operator overloading technique is seeing an example;

therefore, at the end of this section, I am going to provide two examples related to the operator overloading

technique. In those examples, you are going to see two different ways of using an operator that is called the this

operator.

Page 192: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

191

Source Code – Operator Overloading – First Example:

#include <iostream>

using namespace std;

//Declaring a class:

class Earnings {

//Declaring three public members:

public :

int AG_Platinum;

int AG_Gold;

int AG_Silver;

//Using the operator keyword:

Earnings operator +( const Earnings& AG_Obj) {

Earnings AG_Object;

AG_Object = * this ; //Using the this pointer.

AG_Object.AG_Platinum = AG_Object.AG_Platinum + A G_Obj.AG_Platinum;

AG_Object.AG_Gold = AG_Object.AG_Gold + AG_Obj.AG _Gold;

AG_Object.AG_Silver = AG_Object.AG_Silver + AG_Ob j.AG_Silver;

return AG_Object;

}

};

Page 193: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

192

//The main function:

int main( void ) {

//Building three objects:

Earnings January; //Declaring January of Earnings data-type.

Earnings February; //Declaring February of Earnings data-type.

Earnings AG_Total; //Declaring AG_Total of Earnings data-type.

//January Earnings:

January.AG_Platinum = 3000;

January.AG_Gold = 2000;

January.AG_Silver = 1000;

//Displaying the total earnings of January:

AG_Total = January;

cout << "Displaying the Earnings of January: " << endl;

cout << "Platinum: " << AG_Total.AG_Platinum << endl;

cout << "Gold: " << AG_Total.AG_Gold << endl;

cout << "Silver: " << AG_Total.AG_Silver << endl << endl;

//February Earnings:

February.AG_Platinum = 3500;

February.AG_Gold = 5000;

February.AG_Silver = 1500;

//Displaying the total earnings of February:

Page 194: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

193

AG_Total = February;

cout << "Displaying the Earnings of February: " << endl;

cout << "Platinum: " << AG_Total.AG_Platinum << endl;

cout << "Gold: " << AG_Total.AG_Gold << endl;

cout << "Silver: " << AG_Total.AG_Silver << endl << endl;

/*Adding the earnings of January and February. In o the words,

adding the values of two objects inside of a third object.*/

AG_Total = January + February;

//Displaying the earnings of January and February:

cout << "Displaying the Earnings of January and February: " << endl;

cout << "Platinum: " << AG_Total.AG_Platinum << endl;

cout << "Gold: " << AG_Total.AG_Gold << endl;

cout << "Silver: " << AG_Total.AG_Silver << endl << endl;

return 0;

}

Result – Operator Overloading – First Example:

Page 195: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

194

Source Code – Operator Overloading – Second Example:

#include <iostream>

using namespace std;

//Declaring a class:

class Earnings {

//Declaring three public members:

public :

int AG_Platinum;

int AG_Gold;

int AG_Silver;

//Using the operator keyword:

Earnings operator +( const Earnings& AG_Obj) {

Earnings AG_Object;

AG_Object.AG_Platinum = this ->AG_Platinum + AG_Obj.AG_Platinum;

AG_Object.AG_Gold = this ->AG_Gold + AG_Obj.AG_Gold;

AG_Object.AG_Silver = this ->AG_Silver + AG_Obj.AG_Silver;

return AG_Object;

}

};

//The main function:

int main( void ) {

Page 196: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

195

//Building three objects:

Earnings January; //Declaring January of Earnings data-type.

Earnings February; //Declaring February of Earnings data-type.

Earnings AG_Total; //Declaring AG_Total of Earnings data-type.

//January Earnings:

January.AG_Platinum = 3000;

January.AG_Gold = 2000;

January.AG_Silver = 1000;

//Displaying the total earnings of January:

AG_Total = January;

cout << "Displaying the Earnings of January: " << endl;

cout << "Platinum: " << AG_Total.AG_Platinum << endl;

cout << "Gold: " << AG_Total.AG_Gold << endl;

cout << "Silver: " << AG_Total.AG_Silver << endl << endl;

//February Earnings:

February.AG_Platinum = 3500;

February.AG_Gold = 5000;

February.AG_Silver = 1500;

//Displaying the total earnings of February:

AG_Total = February;

cout << "Displaying the Earnings of February: " << endl;

cout << "Platinum: " << AG_Total.AG_Platinum << endl;

Page 197: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

196

cout << "Gold: " << AG_Total.AG_Gold << endl;

cout << "Silver: " << AG_Total.AG_Silver << endl << endl;

/*Adding the earnings of January and February. In o the words,

adding the values of two objects inside of a third object.*/

AG_Total = January + February;

//Displaying the earnings of January and February:

cout << "Displaying the Earnings of January and February: " << endl;

cout << "Platinum: " << AG_Total.AG_Platinum << endl;

cout << "Gold: " << AG_Total.AG_Gold << endl;

cout << "Silver: " << AG_Total.AG_Silver << endl << endl;

return 0;

}

Result – Operator Overloading – Second Example:

Page 198: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

197

Object Oriented: Polymorphism

One of the object-oriented programming techniques that a coder can use is called polymorphism. What is

polymorphism? The word polymorphism comes from an ancient Greek term that is composed of two words, which

are known as many (poly) forms (morphe); shortly, polymorphism is a Greek word, which means many (poly) forms

(morphe). In computer programming or computer science, polymorphism is the capability of something to behave in

different kinds of forms; more specifically, in object-oriented programming, polymorphism is the power and ability

of a function or method to behave in multiple ways thanks to an object and a pointer variable. One of the best ways to

understand the previous lines is seeing some illustrations; therefore, before providing a source code of polymorphism,

I am going to provide two illustrations of polymorphism that will give you an idea of how to perform this technique:

class Base_Class { public: Polymorphism_Function() { } } class First_Derived_Class { public: Polymorphism_Function() { } } class Second_Derived_Class { public: Polymorphism_Function() { } } //The main function: Int main() { Base_Class *Pointer_Variable; First_Derived_Class Object_of_First_Derived_Class; Second_Derived_Class Object_of_Second_Derived_Class; Pointer_Variable = &Object_of_First_Derived_Class; Cout << "First Form: "Pointer_Variable->Polymorphism_Function; Pointer_Variable = &Object_of_Second_Derived_Class; Cout << "Second Form: "Pointer_Variable->Polymorphism_Function;

Page 199: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

198

As you can see in the previous illustration, there are three classes that are using a function of the same name; in

other words, there are three classes that are using the Polymorphism_Function. Then, in the main function, there are

two objects that are storing their memory addresses inside of a pointer variable. Finally, a pointer variable is using the

Polymorphism_Function to display or show two results. Even though the name of the Pointer_Variable and the

Polymorphism_Function are exactly the same, the result is not going to be the same because there are two derived

classes that are using a function with different implementations that are performing different operations or tasks.

After providing a short illustration of polymorphism, I will show an extended illustration of this technique:

class Base_Class {

public:

void Regular_Function(){ //Declaring a Regular Function.

}

virtual int Polymorphism_Function() { //Using the virtual keyword for this function.

return 0;

}

}

class First_Derived_Class {

public:

Polymorphism_Function() {

Return(Member + Member + Member); //Performing Operation A.

}

}

class Second_Derived_Class {

public:

Polymorphism_Function() {

Return(Member + Member + Member - 5000); //Performing Operation B.

Page 200: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

199

}

}

//The main function:

Int main() {

Base_Class *Pointer_Variable;

First_Derived_Class Object_of_First_Derived_Class;

Second_Derived_Class Object_of_Second_Derived_Class;

Pointer_Variable = &Object_of_First_Derived_Class;

Cout << "First Form: "Pointer_Variable->Polymorphism_Function;

Pointer_Variable = &Object_of_Second_Derived_Class;

Cout << "Second Form: "Pointer_Variable->Polymorphism_Function;

As you can see in the previous example, before typing the Polymorphism_Function that is located on a base class,

I am using the virtual keyword, also called the virtual function. What is the virtual function? It is a function that a

C++ developer can use to perform the polymorphism technique because the virtual function is going to tell to a

compiler that a coder wants to build a function with dynamic linkage instead of static linkage. Thanks to the dynamic

linkage, a function that is located on a base class can be overridden by those functions that are located on their

derived classes, which allow a pointer variable of a base class to work properly with those objects and functions that

are located on different kinds of derived classes.

In the previous illustration, in addition to the virtual function, you are going to notice that there are two derived

classes that are using a function with the same name, which is called the Polymorphism_Function. As you can see,

those functions are going to display a different result because the second derived class is using an additional value,

which is -5000.

Page 201: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

200

Source Code – Polymorphism:

#include <iostream>

using namespace std;

//Declaring a base class:

class AG_Earnings {

/*Declaring three protected members, which are the

hard-commodities that a corporation is using to ea rn money:*/

protected :

int AG_Platinum;

int AG_Diamond;

int AG_Silver;

//Declaring two functions (a regular and a virtual function):

public :

/*Declaring a regular function:*/

void Hard_Commodities ( int AG_P, int AG_D, int AG_S) {

AG_Platinum = AG_P;

AG_Diamond = AG_D;

AG_Silver = AG_S;

}

/*Declaring a virtual function to perform

polymorphism operations:*/

virtual int AG_Adding_Cash() {

return 0;

Page 202: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

201

}

};

//Declaring the first derived class:

class AG_January: public AG_Earnings {

/*Declaring a public function to add some values: * /

public :

int AG_Adding_Cash( void ) {

return (AG_Platinum + AG_Diamond + AG_Silver);

}

};

//Declaring the second derived class:

class AG_February: public AG_Earnings {

/*Declaring a public function to add some values.

Adding 3000 dollars because of bonus: */

public :

int AG_Adding_Cash( void ) {

return (AG_Platinum + AG_Diamond + AG_Silver + 3000);

}

};

//Declaring the third derived class:

class AG_March: public AG_Earnings {

/*Declaring a public function to add some values.

Subtracting 1500 dollars because of investment:*/

Page 203: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

202

public :

int AG_Adding_Cash( void ) {

return (AG_Platinum + AG_Diamond + AG_Silver - 1500);

}

};

//Declaring the main function:

int main() {

/*Declaring a pointer variable (*AG_Pointer) of AG_ Earnings type.

Notice that AG_Earnings is the name of a base clas s.*/

AG_Earnings *AG_Pointer;

/*Declaring an object, which is called AG_January_O bject.

Notice that AG_January is the name of a derived cl ass*/

AG_January AG_January_Object;

/*Declaring an object, which is called AG_February_ Object.

Notice that AG_February is the name of a derived c lass*/

AG_February AG_February_Object;

/*Declaring an object, which is called AG_March_Obj ect.

Notice that AG_March is the name of a derived clas s*/

AG_March AG_March_Object;

//Displaying a text message:

cout << "Teraexe - Total per Month:" << endl << endl;

//**********************January******************** **

//Storing the address of the first derived class:

AG_Pointer = &AG_January_Object;

//Adding some values thanks to the Hard_Commodities function:

Page 204: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

203

AG_Pointer->Hard_Commodities(8000,5000,2500);

//Calling the AG_Adding_Cash function of the Januar y class:

cout << "January: " << AG_Pointer->AG_Adding_Cash() << endl;

//**********************February******************* ***

//Storing the address of the second derived class:

AG_Pointer = &AG_February_Object;

//Adding some values thanks to the Hard_Commodities function:

AG_Pointer->Hard_Commodities(9000,7000,3000);

//Calling the AG_Adding_Cash function of the Februa ry class:

cout << "February: " << AG_Pointer->AG_Adding_Cash() << endl;

//***********************March********************* ***

//Storing the address of the third derived class:

AG_Pointer = &AG_March_Object;

//Adding some values thanks to the Hard_Commodities function:

AG_Pointer->Hard_Commodities(6000,4000,1000);

//Calling the AG_Adding_Cash function of the March class:

cout << "March: " << AG_Pointer->AG_Adding_Cash() << endl << endl;

return 0;

}

Result – Polymorphism:

Page 205: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

204

Object Oriented: Data Abstraction & Data Encapsulation

One of the object-oriented programming techniques that an individual can use is known as data abstraction. What

is data abstraction? It is a programming technique that an individual can use when he/she wants to build a class that is

going to hide some data-types, members, or codes. Even though a data-type/function will not be totally hidden, it is

going to be hidden from its internal implementation, which means that a coder can see a data-type/function name, but

he/she cannot see its internal codes easily.

There are two important concepts that a C++ developer should study if he/she is learning about data abstraction.

As you can see, those are the two essential concepts: abstract properties and concrete implementation. On the one

hand, the abstract properties are those kinds of codes that an individual can see easily because he/she has to use those

codes to perform some operations or tasks; for example, the abstract properties or external interface of a data-type can

be an object/function name that a programmer can use to perform a subtraction/addition/multiplication. One the other

hand, a concrete implementation or internal implementation are those kinds of codes that an individual cannot see

easily, such as those block of codes that are inside of a function.

The idea of data abstraction is to build a code/object/function where an individual has knowledge about its use

and abstract properties, but he/she does not need knowledge about its internal implementation. For example, when a

pilot is flying an airplane, he/she has knowledge about those abstract properties: (a) an airplane will turn on/off its

lights if a pilot is able to perform X movement (b) an airplane will move to the left/right side if a pilot perform Y

movement. However, a pilot does not need knowledge about some internal implementations; for instance, a pilot does

not need knowledge about an aircraft engine, including all its complex artefacts and mechanical functions. Another

example of this idea is when a programmer is using the cout statement to display a text message on a computer

console or computer program. In this case, a coder has knowledge about the abstract properties of a cout statement

when he/she knows that a cout statement can be used to display or show data on a computer terminal or computer

program; however, although a coder knows that he/she can use the cout statement to display data, he/she does not

Page 206: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

205

need to know the internal implementation of a cout statement, which is how the cout statement is going to work to be

able to display information on a computer terminal.

In addition to the data abstraction, there is a similar programming technique that is known as data encapsulation.

In this case, while an individual is performing the data abstraction technique, he/she has to build a class that is

composed of private and public members. If a coder is able to perform the two previous techniques at the same time,

he/she is performing a data encapsulation technique, which means that a coder can perform the data abstraction

technique and the encapsulation technique at the same time.

Page 207: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

206

Source Code – Data Abstraction & Data Encapsulation:

#include <iostream>

using namespace std;

class AG_Class {

//Declaring some private/hidden members:

private :

//Building a member to perform a subtraction:

int AG_Result_A;

//Building a member to perform an addition:

int AG_Result_B;

//Declaring public functions/constructors/members:

public :

//Declaring the AG_Class constructor:

AG_Class() {

//Building a member to perform a subtraction:

int AG_Integer_A = AG_Result_A;

//Building a member to perform an addition:

int AG_Integer_B = AG_Result_B;

}

/*Building a function to subtract 14000 dollars

because of investment:*/

void AG_Sub_Invest( int AG_Invest, int AG_Bonus) {

//Investment on Platinum:

Page 208: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

207

int AG_Platinum = 5000;

//Investment on Diamond:

int AG_Diamond = 5000;

//Investment on Silver:

int AG_Silver = 4000;

//Total:

int AG_Total;

/*The AG_Total variable will hold the total amount

of investment on platinum, diamond, and silver: */

AG_Total = AG_Platinum + AG_Diamond + AG_Silver;

//Adding some bonus:

AG_Bonus = 2500;

//Subtracting and adding some values:

AG_Result_A = AG_Invest - AG_Total;

AG_Result_B = AG_Bonus;

}

//Building a function to display/return a result:

int AG_Display() {

return AG_Result_A + AG_Result_B;

};

};

//The main function:

int main() {

//Building the AG_Class_Investment object of AG_Cla ss data-type:

AG_Class AG_Class_Investment;

Page 209: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

208

//Declaring the AG_Cash variable, which are the ear nings of a corporation:

int AG_Cash = 70000;

//Bonus:

int AG_Bonus;

//Declaring a variable to add the NET:

int AG_NET;

//Displaying the earnings of a corporation:

cout << "Teraexe - Total Gross Earnings: " << AG_Cash << endl;

/*Using the AG_Sub_Invest function to work with a v alue, which

is 70000. The function will subtract 15000 dollars to a value:*/

AG_Class_Investment.AG_Sub_Invest(AG_Cash, AG_Bonu s);

/*Using the AG_Display function to return or displa y a result;

the AG_NET variable will hold the result:*/

AG_NET = AG_Class_Investment.AG_Display();

//Displaying or Returning a result through the AG_N ET variable:

cout << "Teraexe - Total NET Earnings: " << AG_NET << endl;

return 0;

}

Result – Data Abstraction & Data Encapsulation:

Page 210: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

209

The C# Programming Language

Printing Data

In this section, an individual can start learning about C#, which is known as the successor of C++ and provides a

great variety of libraries that a coder can use to build sophisticated computer applications. Because this is a

continuation to the previous sections that are about C and C++, I am not going to provide information related to those

kinds of data-types/variables/statements that I explained on the previous sections. Therefore, if an individual wants to

understand this section, he/she has to know about C and C++, or he/she has to read the previous sections, which are

about C and C++.

In this chapter, you can start learning some of the most basic codes that a coder has to learn to display data inside

of a computer application or terminal. In other words, in this section, you can learn how to use the C# programming

language to display information through a class and a method, which are known as the System.Console class and the

WriteLine method.

• The System.Console Class: An individual or coder can use the System.Console class when he/she wants to use

the command-line or console for different kinds of purposes, such as reading or showing data.

• The WriteLine Method: While a programmer is using the System.Console class, he/she can use the WriteLine

method to display information on a console, also known as command-line.

Running a C# Application

In this section, you will learn how to use Microsoft Visual Studio 2008 to compile and run a computer

application developed on C#. As you can see, those are the steps that an individual has to follow to compile and run

an application developed on C#:

• First, after installing Microsoft Visual Studio 2008 on a computer, an individual has to execute an executable file

that is located on a directory similar to this:

Page 211: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

210

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ devenv.exe

Sometimes, a shortcut of the devenv file is created on a computer desktop; therefore, an additional alternative is

to execute the devenv shortcut that is located on a computer desktop. Usually, the devenv shortcut file has this

name: Microsoft Visual Studio 2008.

• Second, after executing Microsoft Visual Studio 2008, a coder has to click on File, New, Project, Visual C#, and

choose Console Application. Then, a programmer has to type a project name on a field that is located on the right-

side of the Name keyword. In this tutorial, we are going to type Teraexe on the Name field. After typing a project

name, a coder has to click on OK.

Source Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

Console .WriteLine( "The Easiest Way to Learn C/C++/C# by Alvaro J. Gen e (Socket_0x03)." );

}

}

}

Result:

Page 212: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

211

Data-Types & Variables

In a previous exercise, we learned how to use C and C++ to store and retrieve information through some data-

types and variables; likewise, in this chapter, we will learn how to use the C# programming language to store and

retrieve data on the random-access memory of a computer.

Source Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace File8 { class Program { static void Main( string [] args) { char AG_A = 'A' ; int AG_B = 32767; float AG_C = 3.14F; double AG_D = 20.5; Console .WriteLine( "The AG_A value is: {0}" , AG_A); Console .WriteLine( "The AG_A size is: {0}" , sizeof ( char )); Console .WriteLine( "The AG_B value is: {0}" , AG_B); Console .WriteLine( "The AG_B size is: {0}" , sizeof ( int )); Console .WriteLine( "The AG_C value is: {0}" , AG_C); Console .WriteLine( "The AG_C size is: {0}" , sizeof ( float )); Console .WriteLine( "The AG_D value is: {0}" , AG_D); Console .WriteLine( "The AG_D size is: {0}" , sizeof ( double )); Console .WriteLine( "AG_A: {0}. AG_B: {1}. AG_C: {2}." , AG_A, AG_B, AG_C); } }

}

Result:

Page 213: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

212

Data-Type Conversion

While an individual is working with data-types, he/she can define a group of data-types and convert a data-type to

another kind of data-type; for example, a coder can define an int data-type and a char data-type, then, he/she can

convert an int data-type to a char data-type. In the C# programming language, an individual can perform two different

kinds of data-type conversions, which are known as explicit and implicit.

• Explicit Type Conversion: An explicit data-type conversion is when a coder is converting a data-type manually,

and he/she is using pre-defined functions and typing a line of code similar to this:

Variable_A = (Data-Type) Variable_B;

o Variable_A: In this field, a coder has to type a variable name, which is going to use a data-type that will convert

the data-type of another variable (Variable_B).

o Data-Type: In this field, a C# developer has to type a data-type that is going to convert the data-type of another

variable, which is the variable that is located on its right-side (Variable_B).

o Variable_B: In this field, an individual has to type a variable name, which is going to hold a data-type that is

going to be converted into another data-type.

• Implicit Type Conversion: An implicit data-type conversion is when a compiler is converting a data-type

automatically, and a coder has to type a group of C# methods like ToString/ToChar/ToDouble to convert his/her

data-types. For example, when an individual wants to perform an implicit data-type conversion to convert an int

data-type to a string data-type, he/she has to type a line of code similar to this:

Console.WriteLine(Variable_Name.ToString());

o Variable_Name: In this field, an individual has to type a variable name, which is a variable that is holding a data-

type that is going to be converted into a string data-type.

An important point that a coder should know the C# programming language is that it has more than fifteen

methods to convert data-types; however, in this tutorial, we will use only the ToString method to convert a data-type.

Page 214: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

213

Source Code – Explicit Data-Type Conversion:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

public static void Main() {

//Declaring a float data-type:

float AG_A = 3.14F;

//Declaring an int data-type:

int AG_B;

//Converting a float data-type into an int type:

AG_B = ( int )AG_A;

//Displaying the value of an int data-type:

Console .WriteLine( "Value: {0}" , AG_B);

//Declaring a char data-type:

char AG_C = 'A' ;

//Declaring an int data-type:

int AG_D;

/*Converting a char data-type into a int data-type.

Because an int data-type only stores ingeters and

A is 65 in ASCII, the int data-type is going to

store the 65 value.*/

AG_D = ( int )AG_C;

//Displaying an int data-type value, which is 65.

Console .WriteLine( "Value: {0}" , AG_D);

}

}

}

Result – Explicit Data-Type Conversion:

Page 215: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

214

Source Code – Implicit Data-Type Conversion:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

public static void Main( string [] args){

//Declaring an int data-type:

int AG_A = 75;

//Declaring a char data-type:

char AG_B = 'A' ;

//Using the ToString function to convert an int dat a-type

//and display its value:

Console .WriteLine( "Value: {0}" , AG_A.ToString());

//Converting and displaying a char data-type:

Console .WriteLine( "Value: {0}" , AG_B.ToString());

}

}

}

Result – Implicit Data-Type Conversion:

Page 216: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

215

Constants

In a previous exercise, we learned how to use C and C++ to build constant variables. Similarly, in this section, we

will learn how to use the C# programming language to define some constants. As previously stated, when an

individual wants to build some constants, he/she can use the const keyword. Then, after defining a constant, a coder

can use the WriteLine method to display a constant or operand value.

Source Code – The const Keyword:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

const int AG_CONST = 86;

Console .WriteLine( "The constant value is: {0}" , AG_CONST);

}

}

}

Result – The const Keyword:

Page 217: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

216

Basic Operators

In a previous exercise, we learned how to use C and C++ to manipulate some variable values thanks to the C and

C++ operators; similarly, in this chapter, we will learn how to use the C# programming language to manipulate some

operand values through the C# operators. As previously mentioned, after declaring or building some variables, an

individual can use those C/C++/C# operators to change or manipulate some operand values: the arithmetic operator,

relational operators, logical operators, bitwise operators, assignment operators, and misc operators.

Page 218: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

217

Source Code – Arithmetic Operators:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace File8 { class Program { static void Main( string [] args) { int AGene_A = 5; int AGene_B = 20; int AGene_C; //The Addition Operator AGene_C = AGene_A + AGene_B; Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); //The Subtraction Operator: AGene_C = AGene_A - AGene_B; Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); //The Multiplication Operator: AGene_C = AGene_A * AGene_B; Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); //The Division Operator: AGene_C = AGene_A / AGene_B; Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); //The Modulus Operator: AGene_C = AGene_A % AGene_B;

Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); //The Increment Operator: AGene_C = AGene_A++; Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); //The Decrement Operator: AGene_C = AGene_A--; Console .WriteLine( "The AGene_C value is: {0}" , AGene_C); }

} }

Result – Arithmetic Operators:

Page 219: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

218

Source Code – Relational Operators:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace File8 { class Program { static void Main( string [] args) { int AG_A = 20; int AG_B = 5; int AG_C = 5; //The Equal Operator: if (AG_B == AG_C) { Console .WriteLine( "The AG_B value is equal to the AG_C value." , AG_C); } //The Not - Equal Operator: if (AG_A != AG_C) { Console .WriteLine( "The AG_A value is not equal to the AG_C value." , AG_C); } //The Greater-Than Operator: if (AG_A > AG_C) { Console .WriteLine( "AG_A is more than AG_C." , AG_C); } //The Less-Than Operator: if (AG_B < AG_A) { Console .WriteLine( "AG_B is less than AG_A." , AG_C); } //The Greater-Than or Equal to Operator if (AG_A >= AG_B) { Console .WriteLine( "AG_A is more than or equal to AG_B." , AG_C); } //The Less-Than or Equal to Operator: if (AG_A <= AG_C) { Console .WriteLine( "AG_A is less than or equal to AG_C." , AG_C); } else { Console .WriteLine( "AG_A is more than or equal to AG_C." , AG_C); } } } }

Result – Relational Operators:

Page 220: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

219

Source Code – Logical Operators:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace File8 { class Program { static void Main( string [] args) { bool AG_A = true ; bool AG_B = false ; bool AG_C = true ; bool AG_D = false ; bool AGene; //The Logical AND Operator: If both variables are t rue; then, it will print true. AGene = AG_A && AG_C; Console .WriteLine( "The Logical AND Operator: {0}" , AGene); //Both True: True = 1. AGene = AG_A && AG_B; Console .WriteLine( "The Logical AND Operator: {0}" , AGene); //True & False: False = 0. AGene = AG_B && AG_D; Console .WriteLine( "The Logical AND Operator: {0}" , AGene); //False & False: False = 0. //The Logical OR Operator: If a variable is true; t hen, it will print true. AGene = AG_A || AG_B; Console .WriteLine( "The Logical OR Operator: {0}" , AGene); //True & False: True = 1. AGene = AG_B || AG_D; Console .WriteLine( "The Logical OR Operator: {0}" , AGene); //Both False: False = 0. AGene = AG_A || AG_C; Console .WriteLine( "The Logical OR Operator: {0}" , AGene); //Both True: True = 1. //The Logical NOT Operator: If a variable is not tr ue(false); then, it will print true. AGene = !(AG_A && AG_B); Console .WriteLine( "The Logical NOT Operator: {0}" , AGene); //True & False: True = 1. AGene = !(AG_A && AG_C); Console .WriteLine( "The Logical NOT Operator: {0}" , AGene); //Both True: False = 0. AGene = !(AG_B && AG_D); Console .WriteLine( "The Logical NOT Operator: {0}" , AGene); //Both False: True = 1. } } }

Result – Logical Operators:

Page 221: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

220

Source Code – Bitwise Operators:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace File8 { class Program { static void Main( string [] args) { int AG_A = 43; //43 = 0010 1011. int AG_B = 86; //86 = 0101 0110. int AGene = 0; AGene = AG_A & AG_B; // 2 = 0000 0010. Console .WriteLine( "1. The Bitwise AND Operator: {0}" , AGene); AGene = AG_A | AG_B; // 127 = 0111 1111. Console .WriteLine( "2. The Bitwise OR Operator: {0}" , AGene); AGene = AG_A ^ AG_B; // 125 = 0111 1101. Console .WriteLine( "3. The Bitwise XOR Operator: {0}" , AGene); AGene = ~AG_A; // -44 = 1101 0100. Console .WriteLine( "4. The Binary Ones Complement Operator: {0}" , AGene); AGene = AG_A << 1; // 86 = 0101 0110.

Console .WriteLine( "5. The Binary Left Shift Operator: {0}" , AGene); AGene = AG_A >> 1; // 21 = 1 0101. Console .WriteLine( "6. The Binary Right Shift Operator: {0}" , AGene); } } }

Result – Bitwise Operators:

Page 222: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

221

Source Code – Assignment Operators:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace File8 { class Program { static void Main( string [] args) { int AG_A = 100; int AG_B = 86; int AGene; AGene = AG_A + AG_B; Console .WriteLine( "1. The Assignment Operator: {0}" , AGene); AGene += AG_A; Console .WriteLine( "2. The Addition Assignment Operator: {0}" , AGene); AGene -= AG_A; Console .WriteLine( "3. The Subtraction Assignment Operator: {0}" , AGene); AGene *= AG_A; Console .WriteLine( "4. The Multiplication Assignment Operator: {0}" , AGene); AGene /= AG_A; Console .WriteLine( "5. The Division Assignment Operator: {0}" , AGene); AGene %= AG_A; Console .WriteLine( "6. The Modulus Assignment Operator: {0}" , AGene); AGene <<= 1; Console .WriteLine( "7. The Left-Shift Assignment Operator: {0}" , AGene); AGene >>= 1; Console .WriteLine( "8. The Right-Shift Assignment Operator: {0}" , AGene); AGene &= AG_A; Console .WriteLine( "9. The Bitwise AND Assignment Operator: {0}" , AGene); AGene ^= AG_A; Console .WriteLine( "10. The Bitwise Exclusive OR Assignment Operator: {0}" , AGene); AGene |= AG_A; Console .WriteLine( "11. The Bitwise Inclusive OR Assignment Operator: {0}" , AGene); } } }

Page 223: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

222

Result – Assignment Operators:

Page 224: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

223

Conditional Statements

In a previous exercise, we learned how to use the C/C++ programming language to define some conditional

statements; in a like manner, in this section, we will learn how to use C# to build different kinds of conditional

statements, including the if-statement, the if-else statement, the switch statement, and some nested statements.

Source Code – The if-Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AG_A = 86;

int AG_B = 20;

Console .WriteLine( "The AG_A value is: {0}." , AG_A);

Console .WriteLine( "The AG_B value is: {0}." , AG_B);

if (AG_A > AG_B) {

Console .WriteLine( "The AG_A value is superior to the AG_B value." );

}

}

}

}

Result – The if-Statement:

Page 225: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

224

Source Code – The if-else Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AG_A = 5;

int AG_B = 5;

Console .WriteLine( "The AG_A value is: {0}." , AG_A);

Console .WriteLine( "The AG_B value is: {0}." , AG_B);

if (AG_A != AG_B) {

Console .WriteLine( "The AG_A value is not equal to the AG_B value." );

}

else {

Console .WriteLine( "The AG_A value is equal to the AG_B value." );

}

}

}

}

Result – The if-else Statement:

Page 226: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

225

Source Code – The Nested if-Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AG_A = 5;

int AG_B = 20;

int AG_C = 20;

Console .WriteLine( "The AG_A value is: {0}." , AG_A);

Console .WriteLine( "The AG_B value is: {0}." , AG_B);

Console .WriteLine( "The AG_C value is: {0}." , AG_C);

if (AG_A != AG_B) {

Console .WriteLine( "The AG_A value is not equal to the AG_B value." );

if (AG_B == AG_C) {

Console .WriteLine( "The AG_B value is equal to the AG_C value." );

}

}

}

}

}

Result – The Nested if-Statement:

Page 227: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

226

Source Code – The Switch Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene = 2;

switch (AGene) {

case 1:

Console .WriteLine( "First Alternative." );

break ;

case 2:

Console .WriteLine( "Second Alternative." );

break ;

default :

Console .WriteLine( "Default Alternative." );

break ;

}

}

}

}

Result – The Switch Statement:

Page 228: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

227

Source Code – The Nested Switch Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AG_A = 1;

char AG_B = 'B' ;

switch (AG_A) {

case 1:

Console .WriteLine( "First Alternative." );

switch (AG_B) {

case 'A' :

Console .WriteLine( "The Second Switch Statement: First Alternative." );

break ;

case 'B' :

Console .WriteLine( "The Second Switch Statement: Second Alternative." );

break ;

}

break ;

case 2:

Console .WriteLine( "Second Alternative." );

break ;

default :

Console .WriteLine( "Default Alternative." );

break ;

}

}

}

}

Result – The Nested Switch Statement:

Page 229: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

228

Loop Statements

In a previous exercise, we learned how to use the C/C++ programming language to build different kinds of loop

statements; likewise, in this chapter, we will learn how to use C# to define or build some loop statements. As

previously stated, an individual or coder can use some loop statements when he/she wants to execute a block of code

several times as long as a condition is positive. Like the C/C++ programming language, a programmer can use C# to

define or build those loop statements: while loop statement, for loop statement, and do-while loop statement.

Source Code – The While-Loop Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene = 80;

while ( AGene <= 86 ) {

Console .WriteLine( "The AGene Value is: {0}" , AGene);

AGene++;

}

}

}

}

Result – The While-Loop Statement:

Page 230: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

229

Source Code – The For-Loop Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene;

for (AGene = 15; AGene <= 20; AGene++) {

Console .WriteLine( "The AGene value is: {0}" , AGene);

}

}

}

}

Result – The For-Loop Statement:

Page 231: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

230

Source Code – The Do-While Loop Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene = 0;

do {

Console .WriteLine( "The AGene value is: {0}" , AGene);

AGene++;

}

while (AGene <= 5);

}

}

}

Result – The Do-While Loop Statement:

Page 232: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

231

Source Code – The Break Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene = 80;

while (AGene <= 90) {

Console .WriteLine( "The AGene value is: {0}" , AGene);

AGene++;

if (AGene > 86) {

break ;

}

}

}

}

}

Result – The Break Statement:

Page 233: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

232

Source Code – The Continue Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene = 15;

do {

if (AGene == 21) {

AGene = 80;

continue ;

}

Console .WriteLine( "The AGene value is: {0}" , AGene);

AGene++;

}

while (AGene <= 86);

}

}

}

Result – The Continue Statement:

Page 234: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

233

Source Code – The goto Statement:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int AGene = 15;

TERAEXE:

do {

if (AGene == 21) {

AGene = 80;

goto TERAEXE;

}

Console .WriteLine( "The AGene value is: {0}" , AGene);

AGene++;

}

while (AGene <= 86);

}

}

}

Result – The goto Statement:

Page 235: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

234

Strings

In a previous exercise, we learned how to use the C/C++ programming language to define a string, which is a

group of characters; likewise, in this chapter, we will learn how to use C# to define different kinds of strings. As

previously stated, when an individual wants to build a variable that is going to hold a group of characters, he/she can

use the string operator.

Source Code – Strings:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

string AG_String_Copy = "String One" ;

string AG_String_A = "Value 86" ;

string AG_String_B = " String Two" ;

string AG_String_C = " and String Three" ;

//Displaying a String:

Console .WriteLine( "First value of AG_String_A: {0}" , AG_String_A);

//Copying a String value:

AG_String_A = string .Copy(AG_String_Copy);

Page 236: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

235

//Displaying the new value of AG_String_A:

Console .WriteLine( "New value of AG_String_A: {0}" , AG_String_A);

//Using the string concatenation operator (+) to jo in two strings:

string AG_C = AG_String_B + AG_String_C;

//Displaying the AG_C value after performing a conc atenation:

Console .WriteLine( "Value of AG_C:{0}" , AG_C);

//Using concat to concatenate three strings:

string AG_Concat = string .Concat(AG_String_A, AG_String_B, AG_String_C);

//Displaying the AG_Concat value after performing a concatenation:

Console .WriteLine( "Value of AG_Concat: {0}" , AG_Concat);

}

}

}

Result – Strings:

Page 237: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

236

Arrays

In a previous exercise, we learned how to use the C/C++ programming language to build some arrays; in the same

way, in this chapter, we will learn how to use C# to define or build different kinds of arrays. As previously stated, an

array is a special variable that an individual or coder can build when he/she wants to store and retrieve a group of

values, which are known as the elements of an array.

Source Code – Array and Integers:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int [] AG_Array = new int [3] {5, 20, 86};

//Using the value 0 to display the first element of an array:

Console .WriteLine( "First element: {0}" , AG_Array[0]);

//Using the value 1 to display the second element o f an array:

Console .WriteLine( "Second element: {0}" , AG_Array[1]);

//Using the value 2 to display the third element of an array:

Console .WriteLine( "Third element: {0}" , AG_Array[2]);

}

}

}

Result – Array and Integers:

Page 238: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

237

Source Code – Array and Strings:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

String [] AG_Array = new String [3] { "Desktop" , "Laptop" , "iPhone" };

//Using the value 0 to display the first element of an array:

Console .WriteLine( "First element: {0}." , AG_Array[0]);

//Using the value 1 to display the second element o f an array:

Console .WriteLine( "Second element: {0}." , AG_Array[1]);

//Using the value 2 to display the third element of an array:

Console .WriteLine( "Third element: {0}." , AG_Array[2]);

}

}

}

Result – Array and Strings:

Page 239: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

238

Source Code – Array and For-loop:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

String [] AG_Array = new String [5]

{

"Desktop" ,

"Laptop" ,

"iPhone" ,

"iPad" ,

"iPod" ,

};

int AG_Loop;

for (AG_Loop = 0; AG_Loop < 5; AG_Loop++){

Console .WriteLine( "Displaying Elements: {0}." , AG_Array[AG_Loop]);

}

}

}

}

Result – Array and For-loop:

Page 240: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

239

Source Code – Two-Dimensional Array with Integers:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

int [,] AG_Array = new int [2, 3]

{

{55, 20, 86},

{11, 12, 23},

};

//First row, first column:

Console .WriteLine( "The value is: {0}" , AG_Array[0, 0]);

//First row, second column:

Console .WriteLine( "The value is: {0}" , AG_Array[0, 1]);

//First row, third column:

Console .WriteLine( "The value is: {0}" , AG_Array[0, 2]);

//Second row, first column:

Console .WriteLine( "The value is: {0}" , AG_Array[1, 0]);

//Second row, second column:

Console .WriteLine( "The value is: {0}" , AG_Array[1, 1]);

//Second row, third column:

Console .WriteLine( "The value is: {0}" , AG_Array[1, 2]);

}

}

}

Result – Two-Dimensional Array with Integers:

Page 241: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

240

Source Code – Two-Dimensional Array with Strings:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

static void Main( string [] args) {

String [,] AG_Array = new String [2, 3]

{

{ "Laptop" , "Desktop" , "Computer" },

{ "iPhone" , "iPod" , "iPad" },

};

Console .WriteLine( "First Category: " );

//First row, first column:

Console .WriteLine( "A. First Product: {0}" , AG_Array[0, 0]);

//First row, second column:

Console .WriteLine( "A. Second Product: {0}" , AG_Array[0, 1]);

//First row, third column:

Console .WriteLine( "A. Third Product: {0}" , AG_Array[0, 2]);

Console .WriteLine( "Second Category: " );

//Second row, first column:

Console .WriteLine( "B. First Product: {0}" , AG_Array[1, 0]);

//Second row, second column:

Console .WriteLine( "B. Second Product: {0}" , AG_Array[1, 1]);

//Second row, third column:

Console .WriteLine( "B. Third Product: {0}" , AG_Array[1, 2]);

}

}

}

Result – Two-Dimensional Array with Strings:

Page 242: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

241

Structures

In a previous exercise, we learned how to use the C/C++ programming language to build some structures;

similarly, in this chapter, we will learn how to use C# to define different kinds of structures. As previously stated,

there is a difference between an array and a structure: on the one hand, an array is a “derived data-type” that is

composed of some elements of “a single” data-type; on the other hand, a structure is a “defined data-type” that can be

composed of some elements of “a group” of data-types. In other words, a structure is a data-type that an individual

can use when he/she wants to define some variables or items with different kinds of data-types.

Source Code – Structures:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class Program {

//Declaring the first structure:

struct Hard_Commodities {

public string Platinum;

public string Diamond;

public string Gold;

};

//Declaring the second structure:

struct Soft_Commodities {

public string Coffee;

Page 243: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

242

public string Corn;

};

//The Main Method:

static void Main( string [] args) {

//Declaring hard commodities:

Hard_Commodities HC_Countries_A;

Hard_Commodities HC_Countries_B;

Hard_Commodities HC_Countries_C;

//Declaring soft commodities:

Soft_Commodities SC_Countries_A;

Soft_Commodities SC_Countries_B;

//Hard Commodities - First countries:

HC_Countries_A.Platinum = "South Africa." ;

HC_Countries_A.Diamond = "Australia." ;

HC_Countries_A.Gold = "USA." ;

//Hard Commodities - Second countries:

HC_Countries_B.Platinum = "Russia." ;

HC_Countries_B.Diamond = "South Africa." ;

HC_Countries_B.Gold = "Germany." ;

//Hard Commodities - Third countries:

HC_Countries_C.Platinum = "Canada." ;

HC_Countries_C.Diamond = "Russia." ;

HC_Countries_C.Gold = "Italy." ;

Page 244: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

243

//Soft Commodities - First countries:

SC_Countries_A.Coffee = "Brazil." ;

SC_Countries_A.Corn = "USA." ;

//Soft Commodities - Second countries:

SC_Countries_B.Coffee = "Vietnam." ;

SC_Countries_B.Corn = "China." ;

//Displaying the first countries with hard commodit y reserves:

Console .WriteLine( "First Major Countries with Hard Commodity Reserves :" );

Console .WriteLine( "Country with Platinum reserves: {0}" , HC_Countries_A.Platinum);

Console .WriteLine( "Country with Diamond Reserves: {0}" , HC_Countries_A.Diamond);

Console .WriteLine( "Country with Gold Reserves: {0}" , HC_Countries_A.Gold);

Console .WriteLine( "" );

//Displaying the second major countries with hard c ommodity reserves:

Console .WriteLine( "Second Major Countries with Hard Commodity Reserve s:" );

Console .WriteLine( "Country with Platinum reserves: {0}" , HC_Countries_B.Platinum);

Console .WriteLine( "Country with Diamond Reserves: {0}" , HC_Countries_B.Diamond);

Console .WriteLine( "Country with Gold Reserves: {0}" , HC_Countries_B.Gold);

Console .WriteLine( "" );

//Displaying the third major countries with hard co mmodity reserves:

Console .WriteLine( "Third Major Countries with Hard Commodity Reserves :" );

Console .WriteLine( "Country with Platinum reserves: {0}" , HC_Countries_C.Platinum);

Console .WriteLine( "Country with Diamond Reserves: {0}" , HC_Countries_C.Diamond);

Console .WriteLine( "Country with Gold Reserves: {0}" , HC_Countries_C.Gold);

Page 245: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

244

Console .WriteLine( "" );

//Displaying the major soft-commodity producing cou ntries:

Console .WriteLine( "First Major Countries Producing Soft-Commodities:" );

Console .WriteLine( "Country with Coffee Production: {0}" , SC_Countries_A.Coffee);

Console .WriteLine( "Country with Corn Production: {0}" , SC_Countries_A.Corn);

Console .WriteLine( "" );

//Displaying the second major soft-commodity produc ing countries:

Console .WriteLine( "Second Major Countries Producing Soft-Commodities: " );

Console .WriteLine( "Country with Coffee Production: {0}" , SC_Countries_B.Coffee);

Console .WriteLine( "Country with Corn Production: {0}" , SC_Countries_B.Corn);

Console .WriteLine( "" );

}

}

}

Result – Structures:

Page 246: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

245

Namespace

In a previous exercise, we learned how to use the C++ programming language to declare different kinds of

namespaces; likewise, in this section, we will learn how to use C# to build a group of namespaces. As explained

above, an individual can use the namespace keyword when he/she wants to build/call a function that is using exactly

the same name than another function. Thanks to the namespace keyword, while an individual is building an

application that is using two functions of the same name, the compiler or computer knows what kind of function it

has to call, in other words, thanks to the namespace keyword, a compiler or computer is going to know the function

that is going to perform its operations or tasks.

Page 247: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

246

Source Code – Namespace:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

//Declaring the first namespace:

namespace AG_Stock_Market {

class namespace_c1 {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $2000" );

Console .WriteLine( "NET Income: $1500" );

Console .WriteLine();

}

}

}

//Declaring the second namespace:

namespace AG_Bond_Market {

class namespace_c1 {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $20000" );

Console .WriteLine( "NET Income: $2000" );

Console .WriteLine();

}

}

}

//Declaring the third namespace:

namespace AG_Commodity_Market {

class namespace_c1 {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $10000" );

Console .WriteLine( "NET Income: $2000" );

Page 248: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

247

Console .WriteLine();

}

}

}

class AG_Program {

//Declaring Main Function:

static void Main( string [] args) {

//Building three objects:

AG_Stock_Market. namespace_c1 Obj_A = new AG_Stock_Market. namespace_c1 ();

AG_Bond_Market. namespace_c1 Obj_B = new AG_Bond_Market. namespace_c1 ();

AG_Commodity_Market. namespace_c1 Obj_C = new AG_Commodity_Market. namespace_c1 ();

Console .WriteLine( "Stock Market: " );

//Calling the AG_Income function from the first nam espace:

Obj_A.AG_Income();

Console .WriteLine( "Bond Market: " );

//Calling the AG_Income function from the second na mespace:

Obj_B.AG_Income();

Console .WriteLine( "Commodity Market: " );

//Calling the AG_Income function from the third nam espace:

Obj_C.AG_Income();

}

}

}

Result – Namespace:

Page 249: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

248

Source Code – The using Keyword:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

using AG_Stock_Market;

using AG_Bond_Market;

using AG_Commodity_Market;

//Declaring the first namespace:

namespace AG_Stock_Market {

class AG_CA {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $2000" );

Console .WriteLine( "NET Income: $1500" );

Console .WriteLine();

}

}

}

//Declaring the second namespace:

namespace AG_Bond_Market {

Page 250: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

249

class AG_CB {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $20000" );

Console .WriteLine( "NET Income: $2000" );

Console .WriteLine();

}

}

}

//Declaring the third namespace:

namespace AG_Commodity_Market {

class AG_CC {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $10000" );

Console .WriteLine( "NET Income: $2000" );

Console .WriteLine();

}

}

}

class AG_Program {

//Declaring Main Function:

static void Main( string [] args) {

//Building three objects:

Page 251: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

250

AG_CA Obj_A = new AG_CA();

AG_CB Obj_B = new AG_CB();

AG_CC Obj_C = new AG_CC();

Console .WriteLine( "Stock Market: " );

//Calling the AG_Income function from the first nam espace:

Obj_A.AG_Income();

Console .WriteLine( "Bond Market: " );

//Calling the AG_Income function from the second na mespace:

Obj_B.AG_Income();

Console .WriteLine( "Commodity Market: " );

//Calling the AG_Income function from the third nam espace:

Obj_C.AG_Income();

}

}

}

Result – The using Keyword:

Page 252: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

251

Source Code – Nested-Namespace:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

using AG_Stock_Market;

using AG_Bond_Market;

using AG_Commodity_Market;

using AG_Commodity_Market.AG_Hard_Commodity;

using AG_Commodity_Market.AG_Soft_Commodity;

//Declaring the first namespace:

namespace AG_Stock_Market {

class AG_CA {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $2000" );

Console .WriteLine( "NET Income: $1500" );

Console .WriteLine();

}

}

}

//Declaring the second namespace:

namespace AG_Bond_Market {

class AG_CB {

//Declaring the AG_Income Function:

public void AG_Income() {

Page 253: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

252

Console .WriteLine( "Gross Income: $10000" );

Console .WriteLine( "NET Income: $2500" );

Console .WriteLine();

}

}

}

//Declaring the third namespace:

namespace AG_Commodity_Market {

class AG_CC {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $20000" );

Console .WriteLine( "NET Income: $2000" );

Console .WriteLine();

}

}

//Declaring the first nested namespace:

namespace AG_Hard_Commodity {

class AG_CC_A {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $9000" );

Console .WriteLine( "NET Income: $1500" );

Console .WriteLine();

}

}

}

//Declaring the second nested namespace:

Page 254: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

253

namespace AG_Soft_Commodity {

class AG_CC_B {

//Declaring the AG_Income Function:

public void AG_Income() {

Console .WriteLine( "Gross Income: $11000" );

Console .WriteLine( "NET Income: $500" );

Console .WriteLine();

}

}

}

}

class AG_Program {

//Declaring Main Function:

static void Main( string [] args) {

//Building five objects:

AG_CA Obj_A = new AG_CA();

AG_CB Obj_B = new AG_CB();

AG_CC Obj_C = new AG_CC();

AG_CC_A Obj_D = new AG_CC_A();

AG_CC_B Obj_E = new AG_CC_B();

Console .WriteLine( "Stock Market: " );

//Calling the AG_Income function from the first nam espace:

Obj_A.AG_Income();

Console .WriteLine( "Bond Market: " );

//Calling the AG_Income function from the second na mespace:

Obj_B.AG_Income();

Console .WriteLine( "Commodity Market: " );

Page 255: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

254

//Calling the AG_Income function from the third nam espace:

Obj_C.AG_Income();

Console .WriteLine( "Hard-Commodity Market: " );

/*Calling the AG_Income function, which is located on

the second nested namespace:*/

Obj_D.AG_Income();

Console .WriteLine( "Soft-Commodity Market: " );

/*Calling the AG_Income function, which is located on

the second nested namespace:*/

Obj_E.AG_Income();

}

}

}

Result – Nested-Namespace:

Page 256: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

255

Object Oriented: Classes and Objects

In a previous exercise, we learned how to use the C++ programming language to build a group of classes and

objects; similarly, in this chapter, we will learn how to use C# to define different kinds of classes and objects. In the

C# programming language, when an individual wants to define a class, he/she has to use the class keyword. In

addition, if a coder wants to add some members to his/her class, he/she has to use this syntax:

class Class_Name {

Access_Member Data_Type Variable_Name;

}

o Access_Member: In this field, a C# developer has to type an access member, which can be private or public. As

previously stated, when an individual is building a class, he/she has to define public members if he/she wants to

use his/her members on other functions, such as the main function.

o Data_Type: In this field, a programmer has to specify a data-type, such as int, char, or other.

o Variable_Name: As you should know, after specifying a data-type, a coder has to type a variable name.

• Objects: As soon as an individual is able to build a class that is composed of some members, he/she can start

building some objects inside of a function, such as the main function. In that case, a C# developer can use this

syntax: Class_Name Object_Name = new Class_Name;. As you can see in the previous line of code, a coder has

to use the new operator when he/she is creating an object.

• Access to a Class Member: After building an object, an individual can use the dot operator (.) to gain access to the

members of a class; to put it another way, as soon as a coder is able to define an object, he/she can use the dot

operator (.) to link an object with a member. If a C# developer wants to use the dot operator (.) to link an object

with a member and add some values, he/she can use this syntax:

Object_Name.Member_Name = Value;

Page 257: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

256

Source Code – Classes & Objects:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

//Building a class, which is called Earnings:

class Earnings {

//Adding three members to a class:

public int AG_Platinum;

public int AG_Diamond;

public int AG_Silver;

}

class AG_Program {

//The Main Function:

static void Main( string [] args) {

//Declaring an object (Earnings January):

Earnings January = new Earnings (); //Declaring January of Earnings type.

Earnings February = new Earnings (); //Declaring February of Earnings type.

int AG_Total; //Declaring a variable to add the total amount of c ash.

//Earning 8000 dollars for a platinum investment:

January.AG_Platinum = 8000;

//Earning 5000 dollars for a diamond investment:

January.AG_Diamond = 5000;

//Earning 2500 dollars for a silver investment:

Page 258: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

257

January.AG_Silver = 2500;

//Earning 9000 dollars for a platinum investment:

February.AG_Platinum = 9000;

//Earning 7000 dollars for a diamond investment:

February.AG_Diamond = 7000;

//Earning 3000 dollars for a silver investment:

February.AG_Silver = 3000;

Console .WriteLine( "Total Gross per Month:" );

/*Adding the total amount of cash that a

corporation was able to earn on January :*/

AG_Total = January.AG_Platinum + Januar y.AG_Diamond + January.AG_Silver;

//Displaying the total amount of cash:

Console .WriteLine( "January: {0}" , AG_Total);

/*Adding the total amount of cash that a

corporation was able to earn on Februar y:*/

AG_Total = February.AG_Platinum + Febru ary.AG_Diamond + February.AG_Silver;

//Displaying the total amount of cash:

Console .WriteLine( "February: {0}" , AG_Total);

}

}

}

Result – Classes & Objects:

Page 259: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

258

Source Code – Classes & Objects – Using Functions:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

//Building a class, which is called Earnings:

class Earnings {

//Adding a three members to a class:

public int AG_Platinum;

public int AG_Diamond;

public int AG_Silver;

//Declaring the Total_F Function:

public int Total_F() {

//Adding the total amount of cash:

return AG_Platinum + AG_Diamond + AG_Silver;

}

public void TPlatinum( int TP) {

AG_Platinum = TP;

}

public void TDiamond( int TD) {

AG_Diamond = TD;

}

public void TSilver( int TS) {

AG_Silver = TS;

Page 260: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

259

}

}

class AG_Program {

//The Main Function:

static void Main( string [] args) {

//Declaring an object (Earnings January):

Earnings January = new Earnings (); //Declaring January of Earnings type.

Earnings February = new Earnings (); //Declaring February of Earnings type.

Earnings March = new Earnings (); //Declaring March of Earnings type.

int AG_Total; //Declaring a variable to add the total amount of c ash.

//Earning 8000 dollars for a platinum investment:

January.AG_Platinum = 8000;

//Earning 5000 dollars for a diamond investment:

January.AG_Diamond = 5000;

//Earning 2500 dollars for a silver investment:

January.AG_Silver = 2500;

//Earning 9000 dollars for a platinum investment:

February.AG_Platinum = 9000;

//Earning 7000 dollars for a diamond investment:

February.AG_Diamond = 7000;

//Earning 3000 dollars for a silver investment:

February.AG_Silver = 3000;

//Earning 6000 dollars for a platinum investment:

March.AG_Platinum = 6000;

//Earning 4000 dollars for a diamond investment:

March.AG_Diamond = 4000;

Page 261: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

260

//Earning 1000 dollars for a silver investment:

March.AG_Silver = 1000;

Console .WriteLine( "Total Gross per Month:" );

/*Adding the total amount of cash that a

corporation was able to earn on January :*/

AG_Total = January.Total_F();

//Displaying the total amount of cash:

Console .WriteLine( "January: {0}" , AG_Total);

/*Adding the total amount of cash that a

corporation was able to earn on Februar y:*/

AG_Total = February.Total_F();

//Displaying the total amount of cash:

Console .WriteLine( "February: {0}" , AG_Total);

/*Adding the total amount of cash that a

corporation was able to earn on March:* /

AG_Total = March.Total_F();

//Displaying the total amount of cash:

Console .WriteLine( "March: {0}" , AG_Total);

}

}

}

Result – Classes & Objects – Using Functions:

Page 262: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

261

Object Oriented: Inheritance

In a previous exercise, we learned how to use the C++ programming language to perform an object-oriented

programming technique that is known as inheritance; likewise, in this chapter, we will learn how to use C-sharp to

perform the inheritance programming technique. As previously stated, a coder can use the inheritance technique to

perform an IS-A relationship, which is a relationship between a base-class and a derived class; in other words, an IS-

A relationship is a relationship between a parent-class and a child-class. Thanks to inheritance, two C# classes can

share their properties and behaviours. What does it mean? It means that if an individual is able to declare a

public/protected member on a base-class, he/she can use it on a derived-class without building or declaring the same

members inside of his/her child-class.

Source Code – Inheritance:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

//Building the Earnings class, which is a base clas s:

class Earnings {

//Declaring three protected members:

protected int AG_Platinum;

protected int AG_Diamond;

protected int AG_Silver;

public void TPlatinum( int TP) {

AG_Platinum = TP;

}

public void TDiamond( int TD) {

AG_Diamond = TD;

}

Page 263: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

262

public void TSilver( int TS) {

AG_Silver = TS;

}

}

//Building the TotalEarnings class, which is a deri ved class:

class TotalEarnings : Earnings {

//Declaring a function to add the total amount of c ash:

public int Total_F() {

return (AG_Platinum + AG_Diamond + AG_Silver);

}

};

class AG_Program {

//The Main Function:

static void Main( string [] args) {

/*Declaring three objects:*/

//Declaring January of Earnings type:

TotalEarnings January = new TotalEarnings ();

//Declaring February of Earnings type:

TotalEarnings February = new TotalEarnings ();

//Declaring March of Earnings type:

TotalEarnings March = new TotalEarnings ();

//Declaring a variable to add the total amount of c ash:

int AG_Total;

January.TPlatinum(8000);

//Earning 5000 dollars for a diamond investment:

January.TDiamond(5000);

//Earning 2500 dollars for a silver investment:

January.TSilver(2500);

//Earning 9000 dollars for a platinum investment:

February.TPlatinum(9000);

//Earning 7000 dollars for a diamond investment:

February.TDiamond(7000);

Page 264: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

263

//Earning 3000 dollars for a silver investment:

February.TSilver(3000);

//Earning 6000 dollars for a platinum investment:

March.TPlatinum(6000);

//Earning 4000 dollars for a diamond investment:

March.TDiamond(4000);

//Earning 1000 dollars for a silver investment:

March.TSilver(1000);

Console .WriteLine( "Total Gross per Month:" );

/*Adding the total amount of cash that a

corporation was able to earn on January :*/

AG_Total = January.Total_F();

//Displaying the total amount of cash:

Console .WriteLine( "January: {0}" , AG_Total);

/*Adding the total amount of cash that a

corporation was able to earn on Februar y:*/

AG_Total = February.Total_F();

//Displaying the total amount of cash:

Console .WriteLine( "February: {0}" , AG_Total);

/*Adding the total amount of cash that a

corporation was able to earn on March:* /

AG_Total = March.Total_F();

//Displaying the total amount of cash:

Console .WriteLine( "March: {0}" , AG_Total);

}

}

}

Result – Inheritance:

Page 265: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

264

Object Oriented: Function Overloading

In a previous exercise, we learned how to use the C++ programming language to perform an object-oriented

programming technique that is known as function overloading; likewise, in this chapter, we will learn how to use C-

sharp to perform the function overloading technique.

Source Code – Function Overloading:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

//Building the AG_Class class:

class AG_Class {

//First Function:

public void AG_F( int AG_Integer) {

Console .WriteLine( "Displaying an Integer Number: {0}" , AG_Integer);

}

//Second Function:

public void AG_F( string AG_C) {

Console .WriteLine( "Displaying an Character: {0}" , AG_C);

}

}

class AG_Program {

Page 266: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

265

//The Main Function:

static void Main( string [] args) {

//Declaring or building an object:

AG_Class AGC = new AG_Class ();

/*The next line will use the first function to

display an integer number*/

AGC.AG_F(23);

/*The next line will use the second function to

display a character*/

AGC.AG_F( "G" );

}

}

}

Result – Function Overloading:

Page 267: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

266

Object Oriented: Operator Overloading

In a previous exercise, we learned how to use the C++ programming language to perform an object-oriented

programming technique that is known as operator overloading; similarly, in this chapter, we will learn how to use C-

sharp to perform the operator overloading technique. As previously stated, when an individual wants to overload an

operator, he/she has to use the operator keyword. Even though an individual has to use “the this keyword” to perform

the operator overloading technique on C++, he/she does not have to use “the this operator” to perform this technique

on C-sharp.

Source Code – Operator Overloading:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

//Building the AG_Earnings class:

class AG_Earnings {

//Declaring three public members:

public int AG_Platinum;

public int AG_Gold;

public int AG_Silver;

//Using the operator keyword:

public static AG_Earnings operator +( AG_Earnings ObjA, AG_Earnings ObjB) {

AG_Earnings AG_Object = new AG_Earnings ();

AG_Object.AG_Platinum = ObjA.AG_Platinum + ObjB.AG_Platinum;

AG_Object.AG_Gold = ObjA.AG_Gold + ObjB.AG_ Gold;

Page 268: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

267

AG_Object.AG_Silver = ObjA.AG_Silver + ObjB .AG_Silver;

return AG_Object;

}

}

class AG_Program {

//The Main Function:

static void Main( string [] args) {

//Building three objects.

//Declaring January of Earnings data-type:

AG_Earnings January = new AG_Earnings ();

//Declaring February of Earnings data-type:

AG_Earnings February = new AG_Earnings ();

//Declaring AG_Total of Earnings data-type:

AG_Earnings AG_Total = new AG_Earnings ();

//January Earnings:

January.AG_Platinum = 3000;

January.AG_Gold = 2000;

January.AG_Silver = 1000;

//Displaying the total earnings of January:

AG_Total = January;

Console .WriteLine( "Displaying the Earnings of January:" );

Console .WriteLine( "Platinum: {0}" , AG_Total.AG_Platinum);

Console .WriteLine( "Gold: {0}" , AG_Total.AG_Gold);

Console .WriteLine( "Silver: {0}" , AG_Total.AG_Silver);

Console .WriteLine();

//February Earnings:

February.AG_Platinum = 3500;

February.AG_Gold = 5000;

February.AG_Silver = 1500;

Page 269: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

268

//Displaying the total earnings of February:

AG_Total = February;

Console .WriteLine( "Displaying the Earnings of February:" );

Console .WriteLine( "Platinum: {0}" , AG_Total.AG_Platinum);

Console .WriteLine( "Gold: {0}" , AG_Total.AG_Gold);

Console .WriteLine( "Silver: {0}" , AG_Total.AG_Silver);

Console .WriteLine();

/*Adding the earnings of January and February. In o the words,

adding the values of two objects inside of a third object.*/

AG_Total = January + February;

//Displaying the earnings of January and February:

Console .WriteLine( "Displaying the Earnings of January and February:" );

Console .WriteLine( "Platinum: {0}" , AG_Total.AG_Platinum);

Console .WriteLine( "Gold: {0}" , AG_Total.AG_Gold);

Console .WriteLine( "Silver: {0}" , AG_Total.AG_Silver);

Console .WriteLine();

}

}

}

Result – Operator Overloading:

Page 270: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

269

Object Oriented: Data Abstraction & Data Encapsulation

In a previous exercise, we learned how to use the C++ programming language to perform two object-oriented

programming techniques that are known as data abstraction and data encapsulation; in a similar way, in this section,

we will learn how to use C# to perform the data abstraction and data encapsulation.

Source Code – Data Abstraction & Data Encapsulation:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace File8 {

class AG_Class {

/*Declaring some private/hidden members.*/

//Building a member to perform a subtraction:

private int AG_Result_A;

//Building a member to perform an addition:

private int AG_Result_B;

/*Declaring public functions/constructors/members*/

public AG_Class() {

//Building a member to perform a subtraction:

int AG_Integer_A = AG_Result_A;

//Building a member to perform an addition:

int AG_Integer_B = AG_Result_B;

}

/*Building a function to subtract 14000 dollars

because of investment:*/

public void AG_Sub_Invest( int AG_Invest, int AG_Bonus) {

Page 271: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

270

//Investment on Platinum:

int AG_Platinum = 5000;

//Investment on Diamond:

int AG_Diamond = 5000;

//Investment on Silver:

int AG_Silver = 4000;

//Total:

int AG_Total;

/*The AG_Total variable will hold the total amount

of investment on platinum, diamond, and silver: */

AG_Total = AG_Platinum + AG_Diamond + A G_Silver;

//Adding some bonus:

AG_Bonus = 2500;

//Subtracting and adding some values:

AG_Result_A = AG_Invest - AG_Total;

AG_Result_B = AG_Bonus;

}

//Building a function to display/return a result:

public int AG_Display() {

return AG_Result_A + AG_Result_B;

}

}

class AG_Program {

//Declaring the Main Function:

static void Main( string [] args) {

//Building the AG_Class_Investment object of AG_Cla ss data-type:

AG_Class AG_Class_Investment = new AG_Class ();

Page 272: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

271

//Declaring the AG_Cash variable, which are the ear nings of a corporation:

int AG_Cash = 70000;

//Bonus:

int AG_Bonus = 0;

//Declaring a variable to add the NET:

int AG_NET;

//Displaying the earnings of a corporation:

Console .WriteLine( "Teraexe - Total Gross Earnings: {0}" , AG_Cash);

/*Using the AG_Sub_Invest function to work with a v alue, which

is 70000. The function will subtract 15 000 dollars to a value:*/

AG_Class_Investment.AG_Sub_Invest(AG_Ca sh, AG_Bonus);

/*Using the AG_Display function to return or displa y a result;

the AG_NET variable will hold the resul t:*/

AG_NET = AG_Class_Investment.AG_Display ();

//Displaying or Returning a result through the AG_N ET variable:

Console .WriteLine( "Teraexe - Total NET Earnings: {0}" , AG_NET);

}

}

}

Result – Data Abstraction & Data Encapsulation:

Page 273: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

272

Projects

Server & Client

In this section, instead of learning how to build basic applications related to functions/operators/statements, an

individual can expand his/her knowledge and enhance his/her programming skills to start building networking

applications, including a client and a server. One of the best ways to start building different kinds of networking

applications is studying the most basic networking concepts; therefore, in this chapter, I am going to provide a

summary related to the most basic networking concepts that a coder should learn to start developing a client and a

server. As you can see, this is a brief summary about the most basic networking concepts:

• Network: In computer science, a network is also known as a computer-network or data-network. A data network,

a group of devices or computers that can use different kinds of nodes and protocols to exchange data, can be used

by an individual to communicate with those internauts who are inside of the same network; to put it another way,

an individual can use a computer network to send and/or receive some messages, files, sounds, and more. One of

the most important points that an individual should know about a computer network is that a computer network

has two or more devices that are exchanging data or information through an IP address and a port.

• IP Address: IP address is an abbreviation for Internet Protocol Address. What is an IP address? It is a numerical

identification for those devices that are inside of a network. More specifically, in a computer network, an IP

address is a numerical identification for different kinds of devices, such as computers, printers, cell-phones, iPads,

iPods, and other electronic devices; furthermore, each single device connected to a computer network has its own

IP address, which is used to identify a particular device from another device. In a network, when a coder is

building a computer application that is going to send data to another program that is located on a different

computer, he/she has to specify an IP address, which is going to be the IP address of the computer that will

receive some data or information. As you can see, an IP address is similar to a house address. If an individual

wants to send an envelope to a different house, he/she has to write a house address on his/her envelope; similarly,

Page 274: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

273

if a coder wants to send data or information to a different computer, he/she has to specify an IP address on his/her

computer application.

What are some of the most common IP addresses? In the twenty and twenty-one century, some of the most

common types of IP addresses are known as IPv4 and IPv6.

o IPv4: IPv4 is an abbreviation for Internet Protocol version four. The IPv4 can be used by an entity or

organization to connect more than four billion devices on a network; more specifically, an entity or organization

can use the IPv4 to connect 4,294,967,296 (2^32) devices inside of a computer network because the IPv4 has a

capacity of 4,294,967,296 (2^32) IP addresses. One of the reasons why the IPv4 has a capacity of 2^32 IP

addresses is because it uses 32 bits for each IP address; to put it another way, each single IP address of an IPv4 is

composed of 32 bits, so the IPv4 cannot assign more than 2^32 IP addresses.

Usually, the IPv4 is expressed in dotted-decimal notation. As you can see, this is an example of an IPv4 in dotted-

decimal notation and binary code:

Dotted-Decimal Notation: 160.17.232.100

Binary Code: 10100000 . 00010001 . 11101000 . 01100100

In the previous example, you will notice that 160 is equivalent to 10100000 in its binary form; furthermore, you

are going to notice that 10100000 is a binary number composed of 8 bits because each single zero (0) or one (1)

represents a bit. If you multiply 8 bits times 4 fields, you are going to obtain 32 bits, which is the number of bits

of an IP address of an IPv4.

o IPv6: Because of the IPv4 capacity or limit, some scientists and engineers were able to build the Internet Protocol

version six, which is composed of 128 bits. Because the IPv6 is composed of 128 bits, an entity or organization

can use the IPv6 to connect 3.4x10^38 devices or computers inside of a network; in other words, an entity can use

the IPv6 to assign 3.4x10^38 IP addresses.

Usually, an IPv6 is expressed in eight fields of hexadecimal digits. As you can see, this is an example of an IPv6:

0202:CD57:3252:80B3:CD57:3252:0202:FF52.

Page 275: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

274

• Protocol: In telecommunications, a protocol is also known as a network protocol or communication protocol. In a

computer network, a communication protocol is a set or group of digital rules that a device can use to

communicate with other devices; in other words, in a network, a protocol is a digital rule that a device or

computer can use for different kinds of purposes, such as sending messages, receiving messages, exchange

sounds, transfer data, and other purposes. Usually, when an individual is using a computer application to

exchange/transfer data with another application that is located on a different computer, he/she is going to use two

network protocols that are called the Transmission Control Protocol (TCP) and the Internet Protocol (IP), which

are the two major protocols of the Internet Protocol Suite.

• The Internet Protocol Suite: The Internet Protocol Suite, a group of digital rules and communication protocols

developed at the end of the 1960s, is also known as the TCP/IP because the two major protocols are the

Transmission Control Protocol (TCP) and the Internet Protocol (IP). Thanks to the TCP/IP, a device or computer

can use different kinds of digital rules and communication protocols to exchange data with those devices or

computers that are inside of the same computer network.

In the Internet Protocol Suite, while an individual is using the two major protocols, he/she can also use other

network protocols to perform specific tasks. For example, if an individual wants to send/receive emails, he/she

can use the Simple Mail Transfer Protocol; on the other hand, if an internaut wants to send/receive files, he/she

can use the Simple File Transfer Protocol. As you can see, those are some of the most common protocols that an

individual can use to perform specific tasks:

Port 21: File Transfer Protocol (FTP)

Port 22: Secure Shell (SSH)

Port 23: Telnet

Port 25: Simple Mail Transfer Protocol (SMTP)

Port 53: Domain Name System (DNS)

Port 80: Hypertext Transfer Protocol (HTTP)

Port 110: Port Office Protocol (POP3)

Page 276: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

275

In the previous list, in addition to the protocol names, you are going to notice that I am including different kinds

of port numbers. What is a port? In computer science, a network port is a communication channel that a computer

application is going to use to exchange data with another program. When a coder is building a computer

application that is going to send/receive data, he/she has to specify a port number; usually, a coder is going to

specify a particular port depending of the task that he/she wants to perform; in other words, an individual is going

to specify a particular port depending of the application that he/she wants to build. For instance, if a C developer

wants to create a computer application that is going to send and receive emails, he/she can use the port 25, which

is going to use a network protocol that is called the Simple Mail Transfer Protocol.

• Server: In computer science, a server is a computer application that an individual can use when he/she wants to

provide or share some kind of information or data to his/her clients. An example of a server is an HTTP server,

which is a computer application that an individual can use to share web pages with different kinds of clients.

Another example of a server is a FTP server, which is a computer program that a user can use when he/she wants

to share a great variety of files to his/her clients.

In a computer network, a server is composed of an IP address, a port, and a protocol; in addition, a server is going

to be waiting for incoming connections, in other words, a server is going to be waiting for a client connection.

Then, after receiving a client connection, a server is going to send/receive some kind of data through a

communication channel that is between a server and a client.

• Client: In information technology, a client is a computer application that an individual can use when he/she wants

to perform a connection to a server. Then, after performing a connection to a server, an individual can obtain that

kind of information or data that a server is sharing with its clients. In the next source code, we will learn how to

build a client that is going to perform a connection to a server; then, this client is going to obtain some data from a

server. After performing a connection and obtaining some information, the client is going to display that

information inside of a terminal, also known as a command-line or console.

Page 277: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

276

• Endpoint: In a computer network, a communication endpoint is a final point on a communication channel;

furthermore, an endpoint is composed of an IP address, a port, and a protocol. One of the best ways to understand

what is a communication endpoint is thinking on a server and a client. When a client is connecting and

transferring data to a server, there is a communication channel; the final point of a communication channel

between a client and a server is known as a communication endpoint.

• Network Socket: In a computer network, a socket is a communication endpoint; more specifically, a network

socket is a final point on a communication channel that is composed of an IP address, a port number, and a

network protocol. When an individual is using a computer programming language to create a server/client, he/she

has to build a network socket. While a coder is building a network socket, he/she has to specify an IP address and

a port number, which will be used by a client/server to establish a connection and send/receive data.

In the C/C++ programming language, when a programmer wants to build a socket, he/she has to specify a

network socket. As you can see, those are some of the most common network sockets that a coder can use when

he/she is building a network socket: stream sockets, datagram sockets, and raw sockets.

o Stream Sockets: One of the most reliable ways to send and receive data is using stream sockets because this kind

of network socket can use a protocol that is called the Transmission Control Protocol (TCP), which is one of the

main protocols of the Internet Protocol Suite, also known as TCP/IP. In the internet, some of the most common

computer applications that are working as servers are using stream sockets, including FTP servers, HTTP servers,

SMTP servers, and many other servers.

o Datagram Sockets: A datagram socket is faster than a stream socket, but this kind of socket is not as reliable as a

stream socket because a datagram socket is developed to use the User Datagram Protocol (UDP) instead of the

Transmission Control Protocol (TCP). Some of the most common computer applications that are using datagram

sockets are known as TFTP servers, DNS servers, and SNMP servers.

o Raw Sockets: When a coder wants to send and/or receive data through a new communication protocol, he/she can

build a computer application that is going to use raw sockets. In addition to the new communication protocols,

Page 278: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

277

this kind of network socket can be used by those programmers who want to build a computer application that is

going to send and/or receive data through those protocols: the Internet Message Protocol (ICMP) and the Internet

Group Management Protocol (IGMP).

• The winsock2 Library Header File: winsock2 is an abbreviation for Windows Socket II; hence, winsock2 is also

called the Windows Socket II. In a computer network, an individual can use the winsock2 header file to build

networking applications; more specifically, a coder can use the Windows Socket II to develop those kinds of

computer applications that can send and receive data over a computer network because the winsock2 is a header

file that a programmer can use to work with IP addresses, port numbers, network sockets, network protocols, and

other internet features. As you can see, those are some of the most important structures/functions that an

individual can use when he/she is working with the winsock2 header file:

o WSADATA Structure: WSA is an abbreviation for Windows Sockets API; therefore, WSADATA is an

abbreviation for data of Windows Socket API. When a coder wants to start building a networking application,

he/she has to use the WSADATA structure because that structure has data or information about the windows

sockets, including a Windows Socket version and other details. After declaring or creating a variable that is going

to hold some data about the windows sockets, a coder can use that variable with other functions to perform some

operations; for example, a programmer can use a WSADATA variable with the WSAStartup function to start

some windows sockets.

o The WSAStartup Function: An individual can use the WSAStartup function when he/she wants to start using the

winsock.dll file. What is the winsock.dll file? It is a file that an individual or coder can use to work with the

protocols of the Internet Protocol Suite, including the two main protocols that are known as the Transmission

Control Protocol (TCP) and the Internet Protocol (IP); as previously stated, an internaut can use the TCP/IP to

send and receive data over internet. If a programmer wants to use the WSAStartup function, he/she has to know

its syntax. As you can see, this is the WSAStartup function syntax:

WSAStartup (Socket-Version, WSAData);

Page 279: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

278

� Socket-Version: In the first parameter, an individual has to type a socket version.

� WSAData: In the second field, a coder has to type a WSAData variable, which is going to hold data about the

windows sockets. The WSAStartup function is going to use the data or information of the WSAData variable to

be able to initiate some window sockets.

As you can see, this is an example of how to use the WSAStartup function:

WSAStartup (MAKEWORD(2,0) &AG_WSA);

In the previous code, you are going to notice that I am typing the MAKEWORD keyword before typing the

Windows Socket version; furthermore, you can see that I am typing 2,0 inside of some parenthesis because I want

to use the Windows Socket version 2.0. Then, after typing the Windows Socket version, I am using the symbol &

and typing a WSAData variable.

o The Socket Function: An individual or coder can use the socket function when he/she wants to create a network

socket. As you can see, this is the socket function syntax that a coder can use to build a network socket:

Socket(Address-Family, Socket-Type, Protocol-Type);

� Address-Family: In the first parameter, an individual has to specify an address family, also known as an IP

address family. In the next source code, we will use the AF_INET value because we want to use the Internet

Protocol version four (IPv4) address family. If an individual wants to use the Internet Protocol version six (IPv6)

address family, he/she can use the AF_INET6 value instead of the AF_INET value. In addition to the two

previous values, a programmer can also use other values. As you can see, this is a list of the other values that a

programmer can use on the first parameter of the socket function:

AF_UNSPEC: Unspecified Address Family.

AF_IPX: The IPX/SPX Address Family.

AF_APPLETALK: The AppleTalk Address Family.

AF_NETBIOS: The NetBIOS Address Family.

AF_IRDA: The Infrared Data Association (IrDA) Address Family.

AF_BTH: The Bluetooth Address Family.

Page 280: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

279

� Socket-Type: In the second parameter, an individual has to specify a socket type. In the next computer

application, we are going to use the SOCK_STREAM value because we want to build a stream socket. As

previously stated, using stream sockets is one of the most reliable ways to exchange data or information because

this kind of sockets can use a protocol that is called the Transmission Control Protocol (TCP). In addition to the

stream sockets, a coder can also use other values to build other sockets. For example, a coder can use the

SOCK_DGRAM value to build datagram sockets, or a programmer can use the SOCK_RAW value to create raw

sockets.

� Protocol-Type: In the third field, an individual has to specify a protocol type. In the next source code, because we

want to use the Transmission Control Protocol (TCP) to send and receive information, we are going to specify

IPPROTO_TCP, which is an abbreviation for Internet Address Protocol – Transmission Control Protocol.

Usually, when a coder specifies AF_INET/AF_INET6 on the first parameter and SOCK_STREAM on the second

parameter, he/she is going to specify IPPROTO_TCP on the third parameter because the TCP protocol is related

with the values of the two previous parameters. In addition to the IPPROTO_TCP value, there are other values

that an individual can use on the third parameter of the socket function, such as:

IPPROTO_ICMP: The Internet Control Message Protocol (ICMP).

IPPROTO_IGMP: The Internet Group Management Protocol (IGMP).

BTHPROTO_RFCOMM: The Bluetooth Radio Frequency Communication (Bluetooth RFCOMM).

IPPROTO_UDP: The User Datagram Protocol (UDP).

IPPROTO_ICMPV6: The Internet Control Message Protocol version 6 (ICMPv6).

IPPROTO_RM: The Pragmatic General Multicast (PGM).

o The SOCKADDR_IN Structure: SOCKADDR_IN is an abbreviation for Socket Address - Internet. After using

the socket function to create a network socket, a coder can use the SOCKADDR_IN structure to specify a socket

address; in other words, an individual can use the SOCKADDR_IN structure to specify a local/remote endpoint.

As previously stated, an endpoint is composed of an address family, an IP address, and a port number; therefore,

while a coder is using the SOCKADDR_IN structure to specify a local/remote endpoint, he/she has to type those

Page 281: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

280

values: an address family, an IP address, and a port number. Then, after specifying the previous values, a network

socket is going to establish a connection to those values. As you can see, those are the parameters of the

SOCKADDR_IN structure that a coder can use to specify an endpoint address:

� The sin_family Parameter: A coder can use the sin_family keyword to specify an address family. In the next

computer application, we will use the AF_INET value because we are working with IPv4.

� The sin_port Parameter: An individual can use the sin_port parameter to specify a port number.

� The sin_addr Parameter: A programmer can use the sin_addr keyword to specify an IP address.

In the next block of code, I am going to provide an example of how to use the SOCKADDR_IN structure to

specify an endpoint address:

SOCKADDR_IN AG_Socket_Address; //Declaring a variable.

AG_Socket_Address.sin_family = AF_INET; //Specifying an address family.

AG_Socket_Address.sin_port = htons(2323); //Specifying a port number.

AG_Socket_Address.sin_addr.s_addr = inet_addr("127.0.0.1"); //Specifying an IP address.

o The bind Function: After building a network socket and specifying an endpoint address, an individual can use the

bind function to bind a network socket to an endpoint address; in other words, a coder can use the bind function to

bind or tie an unbound socket to an address family, IP address, and a port number. As you can see, this is the

syntax of the bind function:

Bind(Network-Socket,(SOCKADDR *)(&Endpoint-Address, Endpoint-Length);

� Network Socket: In the first parameter, a C/C++ developer has to specify an unbound socket. First, a coder has to

type a line like this to define or create a socket variable: int AGS = NULL;. Then, an individual has to type a line

similar to this to create a network socket: AGS = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);. Finally, a

programmer can use the AGS variable on the first parameter of the bind function.

Page 282: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

281

� Endpoint-Address: In the second parameter, an individual has to type an endpoint address or socket address. First,

a coder has to use the SOCKADDR_IN structure to build an endpoint variable and specify an endpoint address,

which is going to hold an address family, an IP address, and a port number. Then, after building an endpoint

variable and specifying an endpoint address, a programmer can use the endpoint variable inside of the second

parameter of the bind function.

� Endpoint-Length: In the third parameter, a coder has to specify the length of the endpoint address; to clarify, in

the third field, an individual has to specify the length of the endpoint variable, and he/she has to specify the length

value in bytes. One of the best ways to specify the length value of the endpoint address is using a function, which

is known as the sizeof function; in that case, an individual or coder has to type a line similar to this:

sizeof(Endpoint-Address);. To clarify, an individual has to type a line similar to this to specify the length value of

an endpoint address: sizeof(Endpoint-Variable);.

As you can see, this is an example of how to use the find function:

bind(Network-Socket,(SOCKADDR *)(&Endpoint-Variable),sizeof(Endpoint-Variable));

o The listen Function: After building a network socket and using the bind function, a C/C++ developer can use the

listen function to add some digital-rules or network-functions to his/her socket; in this case, a network socket is

going to start listening for incoming connections thanks to the listen function. As you can see, this is the listen

function syntax:

listen(Network-Socket, Connection-Length);

� Network-Socket: In the first field, a programmer has to type a network socket, which is a network socket that is

bound to an endpoint address through the bind function.

� Connection-Length: In the second parameter, a coder has to specify a value related to the limit of pending or

incoming connections.

As you can see, this is an example of how to use the listen function: listen(Network-Socket, 1);.

Page 283: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

282

o The accept Function: After building a network socket, an individual can use the accept function to start accepting

incoming connections. As you can see, this is the syntax of the accept function:

accept(Network-Socket, Endpoint-Address, Endpoint-Length);

� Network-Socket: In the first parameter, an individual has to specify a network socket, which is going to start

accepting incoming connections.

� Endpoint-Address: This parameter is optional. In this field, a coder has to specify an endpoint address.

� Endpoint-Length: This parameter is optional. In this field, a programmer has to specify an endpoint-length.

As you can see, this is an example of how to use the accept function to start accepting incoming connections:

accept(Network-Socket, NULL, NULL);

As you can see in the previous code, we are only specifying a network socket and typing the NULL keyword on

the other two parameters because the other two parameters are optional.

o The send Function: After accepting a socket connection, a coder can use the send function to start sending data or

information to another computer application. As you can see, this is the send function syntax that an individual

can use to send information:

send(Socket, Data, Length, Flags);

� Socket: In the first parameter, an individual has to specify a network socket, which is the socket that is going to

send data or information through a computer network.

� Data: In the second parameter, a C/C++ developer has to specify the data or information that a network socket is

going to deliver to another system; to put it another way, in the second field, a coder has to specify the buffer that

a network socket will send to a client or server.

� Length: In this parameter, a coder has to specify the length of the data or buffer that he/she wants to send to a

client/server, and he/she has to specify the length in bytes. One of the best ways to specify the length value of a

buffer is using a function, which is called the strlen function; in this case, a coder has to type a line similar to this:

Page 284: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

283

strlen(Data);. To Clarify, an individual has to type a line like this to specify the length value of a buffer or data:

strlen(Buffer-Variable);.

� Flags: This parameter is optional. A coder can use this field when he/she wants to specify how a call is going to

be performed. If a coder does not want to use this parameter, he/she can type the 0 value inside of the fourth

parameter of the send function.

As you can see, this is an example of how to use the send function: send(Socket, Data,strlen(Data),0);.

o The memset Function: mem is an abbreviation for memory; hence, memset stands for memory-set. Before using

the recv function to send data to a client/server, an individual can use the memset function of the memory header

file (memory.h) when he/she wants to set all the characters or bytes of a memory to zero or NULL, and it can also

be used to set a buffer to a specific number of characters. As you can see, this is the syntax of the memset

function:

memset(Data, Set, Number);

� Data: In the first parameter, an individual has to specify a variable, which will be the variable that is holding that

kind of data that we want to send to a client/server.

� Set: In the second field, a coder has to specify a value related to the number of characters that he/she wants to set.

� Number: In the third parameter, a programmer has to specify the number of characters.

o The recv Function: After accepting a socket connection, a coder can use the recv function to start receiving data

or information from a client or a server. As you can see, this is the syntax of the recv function that an individual

can use to receive information:

recv(Socket, Data, Length, Flags);

� Socket: In the first parameter, an individual has to specify a network socket, which is the socket that is going to

receive data or information through a computer network.

� Data: In the second parameter, a C/C++ developer has to specify the data or information that a network socket is

going to receive from a server/client.

Page 285: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

284

� Length: In this parameter, a coder has to specify the length of the data or buffer that he/she wants to receive from

a client or a server; moreover, an individual has to use byte numbers when he/she is specifying a length value.

� Flags: The flag field is optional. A coder can use this parameter when he/she wants to specify a group of flags that

are going to change a socket performance.

As you can see, this is an example of how to use the send function: recv(Socket, Data,512,0);.

o The WSACleanup Function: After using the Windows Socket II and its library files (.lib/.dll), an individual can

use the WSACleanup function to clean or stop using the windows socket files. In this case, when a coder is using

the WSACleanup function, he/she does not have to type a parameter; hence, if a coder wants to use the

WSACleanup function, he/she only has to type this line: WSACleanup();.

o The closesocket Function: After opening a network socket, a coder can use the closesocket function to close a

socket. As you can see, this is the syntax of the closesocket function: socket(Network-Socket);.

o The shutdown Function: When a coder wants to stop sending/receiving data through the send/recv function,

he/she can use the shutdown function. As you can see, this is the syntax of the shutdown function:

Shutdown(Socket, Operation);

� Socket: In the first field, an individual has to specify a network socket, which is a socket that is sending/receiving

different kinds of messages.

� Operation: In the second parameter, a coder has to specify a value related to the operation that he/she wants to

perform. In this field, an individual can use three different types of values:

SD_RECEIVE: Stop Receiving Messages.

SD_SEND: Stop Sending Messages.

SD_BOTH: Stop Receiving and Sending Messages.

Page 286: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

285

Source Code – Server Developed on C++:

#include <iostream> #include <winsock2.h> using namespace std; #pragma comment( lib , "wsock32.lib" ) int main() { /************************************************** ****************** ************************The WSASTartup Function*** ******************* ************************************************** ******************/ //Declaring the AG_WSA variable, which has data abo ut sockets: WSADATA AG_WSA; //The WSASTarup function to start using some socket files (.dll). //The MAKEWORD keyword to specify a socket version (2.0). int result = WSAStartup(MAKEWORD(2,0),&AG_WSA); /*The WSASTartup function will return 0 if it is ab le to perform its task; it will return 1 if it is not able to perf orm its operation.*/ if (result == 0) { cout << "The WSASTartup function is working properly." << endl; } else { cout << "The WSAStartup function was not able to initiate." << endl;

//The WSACleanup function to stop using some socket files (.dll, .lib). WSACleanup(); //The system command will stop the console/terminal , so an individual //can see the previous text about the WSASTartup fu nction. system( "PAUSE"); return 0; } /************************************************** ******************

************************The Socket Function******* ******************* ************************************************** ******************/ //Declaring or building the AG_Socket variable, whi ch will be used in //the future to create a network socket. int AG_Socket = NULL; //Using the socket function to build a network sock et. AG_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP ); //The socket function will return invalid_socket if there is an error. if (AG_Socket == INVALID_SOCKET) { cout << "There is a problem related to the socket function. " << endl;

Page 287: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

286

WSACleanup(); system( "PAUSE"); return 0; } else {

cout << "The socket function is working properly." << endl; } /************************************************** ****************** *********************The SOCKADDR_IN Structure**** ******************* ************************************************** ******************/ //Declaring or building the AG_Socket_Address varia ble. //The SOCKADDR_IN Structure will be used to specify an address family, an IP address, //and a port number; then, a socket will connect to the SOCKADDR_IN protocols.

SOCKADDR_IN AG_Socket_Address; //The sin_family parameter of the SOCKADDR_IN struc ture to specify an address family: AG_Socket_Address.sin_family = AF_INET; //Using the sin_port parameter of the SOCKADDR_IN s tructure to specify a port number: AG_Socket_Address.sin_port = htons(2323); //Using the sin_addr parameter of the SOCKADDR_IN s tructure to specify an IP Address: AG_Socket_Address.sin_addr.s_addr = inet_addr( "127.0.0.1" );

//Declaring the AG_Socket variable, which will be u sed to bind or tie a network socket. int AG_Bind = NULL; //Using the bind function to bind a network socket: //In the first parameter, we have to specify an unb ound socket, which is AG_Socket. //In the second parameter, we have to specify an en dpoint-address; therefore, we are //going to use the SOCK_ADDR_IN variable on the sec ond parameter. //Parameter 3: we have to specify a length, so we c an use the sizeof function. AG_Bind = bind(AG_Socket,(SOCKADDR *)(&AG_Socket_A ddress), sizeof (AG_Socket_Address)); /*The WSASTartup function will return 0 if it is ab le to perform its task; it will return 1 if it is not able to perf orm its operation.*/ //The bind function will return 0 if it is able to bind a network socket; on the //other hand, it will return SOCKET_ERROR if it is not able to tie a socket. if (AG_Bind == 0) { cout << "The bind function is working properly." << endl; } else { cout << "The bind function was not able to bind a network s ocket." << endl; WSACleanup(); system( "PAUSE"); return 0; } /************************************************** ******************

Page 288: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

287

************************The Listen Function******* ******************* ************************************************** ******************/ //Using the listen function to add some functions t o our socket; in this case, //a network socket will start listen for incoming c onnections.

//In the first parameter, we have to specify a sock et. //In the second parameter, we have to specify a val ue related to the limit //of pending or incoming connections. listen(AG_Socket,1); //The listen function will return 0 if it is able t o perform its task, but //it will return SOCKET_ERROR if it is not able to perform its operation. if (AG_Bind == 0) { cout << "The listen function is working properly." << endl; }

else { cout << "The listen function is not working properly." << endl; WSACleanup(); system( "PAUSE"); return 0; } /************************************************** ****************** ***************************A Second Socket******** *******************

************************************************** ******************/ //Defining or creating the AG_Accept_Socket variabl e, which is a //temporary socket that is waiting or accepting con nections. SOCKET AG_Accept_Socket; //If a connection is establish, the accept function will perform its task. while (1) { AG_Accept_Socket = SOCKET_ERROR; while (AG_Accept_Socket == SOCKET_ERROR) { cout << "Accepting incoming connections..." << endl; AG_Accept_Socket = accept(AG_Socket,NULL,NULL); } cout << "Client connected!" << endl; AG_Socket = AG_Accept_Socket; break ; } /************************************************** ****************** ****************************Sending Data********** ******************* ************************************************** ******************/ //Declaring the AG_Info variable; then, we will put some data on that variable. char *AG_Info; //The next line will be sent to the client thanks t o the send function. AG_Info = "Welcome to the server!\n" ;

Page 289: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

288

//Using the send function to send data to the clien t. send(AG_Socket,AG_Info,strlen(AG_Info),0); /************************************************** ****************** *************************Closing and Cleaning***** *******************

************************************************** ******************/ //Using the shutdown function to stop sending data to a client. //In the first parameter, we have to type a network socket. //In the second parameter, we are using the SD_SEND value to stop //sending data to a network socket. shutdown(AG_Socket,SD_SEND); //The closesocket function to close a network socke t: closesocket(AG_Socket); //The WSACleanup function to stop using some socket files (.dll, .lib).

WSACleanup(); //The system command will stop the console/terminal , so an individual //can see the previous text about the WSASTartup fu nction. system( "PAUSE"); return 0; }

Source Code – Client Developed on C++:

#include <iostream> #include <winsock2.h> #include <memory.h> using namespace std; #pragma comment( lib , "wsock32.lib" ) int main() {

/************************************************** ****************** ************************The WSASTartup Function*** ******************* ************************************************** ******************/ //Declaring the AG_WSA variable, which has data abo ut sockets: WSADATA AG_WSA; //The WSASTarup function to start using some socket files (.dll). //The MAKEWORD keyword to specify a socket version (2.0). int AG_Result = WSAStartup(MAKEWORD(2,0),&AG_WSA); /*The WSASTartup function will return 0 if it is ab le to perform its task; it will return 1 if it is not able to perf orm its operation.*/ if (AG_Result == 0) { cout << "The WSASTartup function is working properly." << endl; }

Page 290: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

289

else { cout << "The WSAStartup function was not able to initiate." << endl; //The WSACleanup function to stop using some socket files (.dll, .lib). WSACleanup(); //The system command will stop the console/terminal , so an individual

//can see the previous text about the WSASTartup fu nction. system( "PAUSE"); return 0; } /************************************************** ****************** ************************The Socket Function******* *************** ************************************************** ******************/ //Declaring or building the AG_Socket variable, whi ch will be used in

//the future to create a network socket. int AG_Socket = NULL; //Using the socket function to build a socket. AG_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP ); //The socket function will return invalid_socket if there is an error. if (AG_Socket == INVALID_SOCKET) { cout << "There is a problem related to the socket function. " << endl;

WSACleanup(); system( "PAUSE"); return 0; } else { cout << "The socket function is working properly." << endl; } /************************************************** ****************** *********************The SOCKADDR_IN Structure**** ******************* ************************************************** ******************/ //Declaring or building the AG_Socket_Address varia ble. //The SOCKADDR_IN Structure will be used to specify an address family, an IP address, //and a port number; then, a socket will connect to the SOCKADDR_IN protocols. SOCKADDR_IN AG_Socket_Address; //The sin_family parameter of the SOCKADDR_IN struc ture to specify an address family: AG_Socket_Address.sin_family = AF_INET; //Using the sin_port parameter of the SOCKADDR_IN s tructure to specify a port number: AG_Socket_Address.sin_port = htons(2323); //Using the sin_addr parameter of the SOCKADDR_IN s tructure to specify an IP Address: AG_Socket_Address.sin_addr.s_addr = inet_addr( "127.0.0.1" ); /************************************************** ****************** ************************The Connect Function****** ******************* ************************************************** ******************/

Page 291: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

290

//Declaring or creating the AGC (C = Connect) varia ble. //In the future, it will be used to connect to a se rver. int AGC = NULL;

//Using the connect function to connect to a server . AGC = connect(AG_Socket,(SOCKADDR*)(&AG_Socket_Add ress), sizeof (AG_Socket_Address)); //The connect function will return 0 if it is able to perform its task; on the //other hand, it will return SOCKET_ERROR if it is not able to perform its operation. if (AGC == 0) { cout << "The connection to the server was established." << endl; } else { cout << "The connect function is not working properly." << endl;

WSACleanup(); system( "PAUSE"); return 0; } /************************************************** ****************** **********************Receiving Data from Server** ******************* ************************************************** ******************/

//Declaring or creating the AG_Data variable that w ill receive data //from the server. As you can see, this variable ca n store 512 characters. //In this case, 512 is known as the buffer size. char AG_Data[512]; //Declaring a variable for the recv function: int AG_Recv_Data = NULL; //The memset function will set a buffer size to som e characters. //Parameter 1: Buffer Variable. Parameter 3: number of characters. memset(AG_Data, 0, 511); //Using the recv function to receive data from the server. AG_Recv_Data = recv(AG_Socket,AG_Data,512,0); //The cout statement will print the data received f rom the server. cout << "Data from Server: " << AG_Data; /************************************************** ****************** ************************Closing and Cleaning****** ******************* ************************************************** ******************/ //Using the shutdown function to stop sending data to a client. //In the first parameter, we have to type a network socket. //In the second parameter, we are using the SD_SEND value to stop //sending data to a network socket. shutdown(AG_Socket,SD_RECEIVE); //The closesocket function to close a network socke t: closesocket(AG_Socket); //The WSACleanup function to stop using some socket files (.dll, .lib).

Page 292: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

291

WSACleanup(); //The system command will stop the console/terminal , so an individual //can see the previous text about the WSASTartup fu nction. system( "PAUSE"); return 0;

}

Result – Server:

Result – Client:

Page 293: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

292

Port Scanner

In this section, a coder can start learning how to build a port scanner. What is a port scanner? A port scanner is a

computer security tool that an individual can use to perform some connections to a group of ports. While a port

scanner is using the connect function to perform some connections to a group of ports, this tool is going to say that a

port is open if a connection to an IP address and a port number is established; on the other hand, a port scanner is

going to say that a port is closed if a connection to an IP address and a port number is not established. Thanks to this

computer application, an individual or pentester may know if a local/remote host has some open ports. As you should

know, when a pentester or intruder wants to gain access to a remote host, one of the first steps that he/she is going to

perform is a port scanning. Then, after performing a port scanning, a pentester/intruder may use other security tools to

find some information about a server and exploit some vulnerabilities on that server.

Source Code – Port Scanner developed on C++:

#include <iostream>

#include <winsock2.h>

using namespace std;

#pragma comment( lib , "wsock32.lib" )

int main() {

char host[100] = "127.0.0.1" ; //IP Address

int StartingPort = 78; //Starting Port.

int EndingPort = 83; //Ending Port.

int port; //The port variable for a for-loop statement.

int AGC; //A variable to perform a socket connection.

//Declaring the AG_WSA variable, which has data abo ut sockets:

WSADATA AG_WSA;

Page 294: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

293

//The WSASTarup function to start using some socket files (.dll).

//The MAKEWORD keyword to specify a socket version (2.0).

int AG_Result = WSAStartup(MAKEWORD(2,0),&AG_WSA);

/*The WSASTartup function will return 0 if it is ab le to perform its

task; it will return 1 if it is not able to perf orm its operation.*/

if (AG_Result == 0) {

cout << "The WSASTartup function is working properly." << endl;

}

else {

cout << "The WSAStartup function was not able to initiate." << endl;

//The WSACleanup function terminates use of the win sock2 dll:

WSACleanup();

//The system command will stop the console/terminal , so an individual

//can see the previous text about the WSASTartup fu nction.

system( "PAUSE");

return 0;

}

//Using the for-loop statement to scan some ports:

for (port=StartingPort; port<=EndingPort; port++) {

//Declaring or building the AG_Socket variable, whi ch will be used in

//the future to create a network socket.

int AG_Socket = NULL;

//Using the socket function to build a network sock et:

AG_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_T CP);

//Creating a socket address:

SOCKADDR_IN AG_Socket_Address;

Page 295: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

294

AG_Socket_Address.sin_family = AF_INET;

AG_Socket_Address.sin_port = htons(port);

AG_Socket_Address.sin_addr.s_addr = inet_addr(host );

//Using the connect function to connect to a server :

AGC = connect(AG_Socket,(SOCKADDR*)(&AG_Socket_Add ress), sizeof (AG_Socket_Address));

//The connect function will return 0 if it is able to perform its operation, which is to

//connect to a socket through an IP address and a p ort number. On the other hand,

//the connect function will return SOCKET_ERROR if it is not able to perform its task.

if (AGC == 0) {

cout << "Port Open: " << port << endl;

closesocket(AG_Socket);

}

else {

cout << "Port Closed: " << port << endl;

closesocket(AG_Socket);

}

}

//The system command will stop the console/terminal , so an individual

//can see the previous text about the WSASTartup fu nction.

system( "PAUSE");

return 0;

}

Result – Port Scanner developed on C++:

Page 296: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

295

HTTP Checker

In this chapter, a C/C++ developer can use his/her programming knowledge to learn how to build an HTTP

checker. What is an HTTP checker? It is a computer application that an individual can use to obtain information

about an HTTP server, including its name, version, script, and other details.

Source Code – HTTP Checker developed on C++:

#include <iostream>

#include <winsock2.h>

#include <memory.h>

using namespace std;

#pragma comment( lib , "wsock32.lib" )

int main() {

char host[100] = "127.0.0.1" ; //IP Address.

int port = 80; //Port number.

/************************************************** ******************

************************The WSASTartup Function*** *******************

************************************************** ******************/

//Declaring the AG_WSA variable, which has data abo ut sockets:

WSADATA AG_WSA;

//The WSASTarup function to start using some socket files (.dll).

//The MAKEWORD keyword to specify a socket version (2.0).

int AG_Result = WSAStartup(MAKEWORD(2,0),&AG_WSA);

/*The WSASTartup function will return 0 if it is ab le to perform its

task; it will return 1 if it is not able to perf orm its operation.*/

if (AG_Result == 0) {

cout << "The WSASTartup function is working properly." << endl;

Page 297: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

296

}

else {

cout << "The WSAStartup function was not able to initiate." << endl;

//The WSACleanup function to stop using some socket files (.dll, .lib).

WSACleanup();

//The system command will stop the console/terminal , so an individual

//can see the previous text about the WSASTartup fu nction.

system( "PAUSE");

return 0;

}

/************************************************** ******************

************************The Socket Function******* ***************

************************************************** ******************/

//Declaring or building the AG_Socket variable, whi ch will be used in

//the future to create a network socket.

int AG_Socket = NULL;

//Using the socket function to build a socket.

AG_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP );

//The socket function will return invalid_socket if there is an error.

if (AG_Socket == INVALID_SOCKET) {

cout << "There is a problem related to the socket function. " << endl;

WSACleanup();

system( "PAUSE");

return 0;

}

else {

cout << "The socket function is working properly." << endl;

}

/************************************************** ******************

*********************The SOCKADDR_IN Structure**** *******************

************************************************** ******************/

Page 298: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

297

//Declaring or building the AG_Socket_Address varia ble.

//The SOCKADDR_IN Structure will be used to specify an address family, an IP address,

//and a port number; then, a socket will connect to the SOCKADDR_IN protocols.

SOCKADDR_IN AG_Socket_Address;

//The sin_family parameter of the SOCKADDR_IN struc ture to specify an address family:

AG_Socket_Address.sin_family = AF_INET;

//Using the sin_port parameter of the SOCKADDR_IN s tructure to specify a port number:

AG_Socket_Address.sin_port = htons(port);

//Using the sin_addr parameter of the SOCKADDR_IN s tructure to specify an IP Address:

AG_Socket_Address.sin_addr.s_addr = inet_addr(host );

/************************************************** ******************

************************The Connect Function****** *******************

************************************************** ******************/

//Declaring or creating the AGC (C = Connect) varia ble.

//In the future, it will be used to connect to a se rver.

int AGC = NULL;

//Using the connect function to connect to a server .

AGC = connect(AG_Socket,(SOCKADDR*)(&AG_Socket_Add ress), sizeof (AG_Socket_Address));

//The connect function will return 0 if it is able to perform its task; on the

//other hand, it will return SOCKET_ERROR if it is not able to perform its operation.

if (AGC == 0) {

cout << "The connection to the server was established." << endl;

}

else {

cout << "The connect function is not working properly." << endl;

WSACleanup();

system( "PAUSE");

return 0;

}

Page 299: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

298

/************************************************** ******************

****************************Sending Data********** *******************

************************************************** ******************/

//Declaring the AG_Info variable; then, we will put some data on that variable.

char *AG_Info;

//Declaring the AG_Send variable:

int AG_Send = NULL;

//The next line will be sent to the client thanks t o the send function.

AG_Info = "GET / HTTP/1.1\r\n\r\n" ;

//Using the send function to send data to the clien t.

AG_Send = send(AG_Socket,AG_Info,strlen(AG_Info),0 );

//The send function will return SOCKET_ERROR if it is not able to perform

//its operation, which is to send data to a server/ client.

if (AG_Send == SOCKET_ERROR) {

cout << "The send function was not able to perform its task ." << endl;

WSACleanup();

system( "PAUSE");

return 0;

}

else {

cout << "The send function was able to send data." << endl;

}

/************************************************** ******************

**********************Receiving Data from Server** *******************

************************************************** ******************/

//Declaring or creating the AG_Server_Data variable that will receive data

//from the server. As you can see, this variable ca n store 2323 characters.

//In this case, 2323 is known as the buffer size.

char AG_Server_Data[2323];

//The memset function will set a buffer size to som e characters.

//Parameter 1: Buffer Variable. Parameter 3: number of characters.

Page 300: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

299

memset(AG_Server_Data, 0, 2322);

//Using the recv function to receive data from the server.

recv(AG_Socket,AG_Server_Data,2323,0);

//The cout statement will print the data received f rom the server.

cout << "Data from Server: " << endl << AG_Server_Data;

/************************************************** ******************

************************Closing and Cleaning****** *******************

************************************************** ******************/

//Using the shutdown function to stop sending and r eceiving data:

shutdown(AG_Socket,SD_BOTH);

//The closesocket function to close a network socke t:

closesocket(AG_Socket);

//The WSACleanup function to stop using some socket files (.dll, .lib).

WSACleanup();

//The system command will stop the console/terminal , so an individual

//can see the previous text about the WSASTartup fu nction.

system( "PAUSE");

return 0;

}

Result – HTTP Checker developed on C++:

Page 301: TERAEXE · 2019-11-05 · The Easiest Way to Learn C/C++/C# Alvaro J. Gene 1 TABLE OF CONTENT Introduction

The Easiest Way to Learn C/C++/C# Alvaro J. Gene

300

CONCLUSION

After reading this tutorial, you will expand your knowledge and enhance your programming skills to be able

to build a lot of C applications. As previously stated, the C programming language is considered by many scientists

and engineers as the mother of all modern languages; therefore, if an individual is able to learn C, he/she can learn

many other computer programming languages without a lot of effort because the most popular programming

languages have a similar syntax to C due to those languages are derived from C. For instance, those computer

programming languages have a similar syntax to C: Java, JavaScript, PHP, Python, Perl, and others. In addition to

those languages, the two C successors are very similar to C; in other words, the C++ and C# are very similar to the C

programming language, so if an individual is able to learn C, he/she will learn C++ and C# easily.

Even though there are many computer programming languages in the world, learning about C/C++/C# is one

of the best choices that a coder can make because those programming languages have a promising future of

prosperity and success thanks to Microsoft Corporation, which is using its energies to work on Visual Studio. Visual

Studio, an integrated development environment developed around nineteen ninety-seven, provides a great variety of

libraries and tools that an individual or programmer can use to build professional computer applications. Furthermore,

an important fact that a coder should know about Visual Studio is that Microsoft Corporation have been supported

Visual Studio for many years; the last edition of Visual Studio was built on 2015.

One of the most interesting facts about this tutorial is that a coder can start learning computer security, also

known as computer hacking. In The Easiest Way to Learn C/C++/C#, an individual can expand his/her knowledge

and start building some of the most basic security tools, including a port scanner and an HTTP checker. In this field,

there are many corporations/companies/organizations that are hiring or employing those kinds of programmers with

computer security knowledge because those entities have sensitive information inside of their computers, such as

employee records, patient records, passwords, credit/debit card numbers, and more.