Amazon

Friday, July 8, 2016

C++ Programming Language: Simple, Short, and Straightforward Way of Learning C++ Programming

C++ Programming Language: Simple, Short, and Straightforward Way of Learning C++ Programming


Copyright 2016 - KDP Edition


Sherwyn Allibang



Edition License Notes
This book is authorized for your own personal use. This book may not be re-sold or offered away to other individuals. Much thanks to you for regarding the diligent work of the author.

Introduction


This book is for absolute beginners with or without prior knowledge in programming, as this book uses Simple words, Short sentences, and Straightforward paragraphs. The triple S way of learning C++ programming. C++ (pronounced as cee plus plus) is a general-purpose programming language. The main object of C++ is to add object-orientation to its predecessor C language. C++ has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation. There are also other useful features added to C++ such as inheritance and polymorphism. The topics covered in this book includes brief introduction to C++ programming, variables, data types, control structures, functions, pointers, and file management of external files. This book starts its discussion from short history to installation of the needed software resource and a step by step screen shots of how to write C++ programming code, compile and execute C++ programs. It presents graphical representation of algorithms for simpler learning. This book is packed with working and running C++ program samples and after reading this book, the reader would be able to develop and create C++ language programs based particularly from problems given in computer science courses, hence, adopting to other programming language will be a lot easier. This book is your initial step in your programming career. 


This chapter presents a brief overview of C++ programming. C++ was designed with a basis toward system programming with high performance, efficiency and flexibility of use as its design highlights. This chapter presents the things needed in starting C++ programming.

Brief history of C++

The C++ programming language has a history path in 1979. Bjarne Stroustrup, a Danish computer scientist, is working with the language called "Simula", which is a language principally intended for simulations. The Simula 67 language underpins object-oriented programming paradigm. Stroustup started working with "C" with classes. C is considered as middle-level language which has the power of Low-level language but has high-level language features and C is considered as the predecessor of C++. Bjarne Stroustrup, at Bell Labs designed C++ in the early 80s, as an object oriented language based on the C language, which powered pretty much all systems software since its creation in 1972. The first C with Classes compiler was called Cfront, which was derived from a C compiler called CPre. C++'s goal is to add object-oriented programming into C language. The ++ operator in the C language is an operator for incrementing a variable, which gives some insight into how Stroustrup regarded the language. In 1985, Stroustrup's reference to the language entitled "The C++ Programming Language" was published.

Reasons to use C++

To start with, C++ is highly recommended for beginning programmers in starting their programming career because C++ covers both procedural and object-oriented programming approach. Once someone learned the concepts behind programming language with C++, adopting or migrating to other programming language will be a lot easier whether it's object-oriented or procedural-oriented programming, as most of the things needed are pretty similar.
C++ has been around a while and it has solved several industry challenges considering that it is also the primary language that drives the mobile experience today. In addition to high performance server and middleware, C++ is a great choice for client side development on the two most popular mobile platforms Android and iOS and the two most popular desktop platforms, Windows and Mac OS X. With the recent introduction of developer tools that enable, among other things, visual design for clients, middleware, and server side apps; C++ is extremely productive and really makes it a great choice for modern app development, so learning the basics of C++ will make someone be competitive in the industry's stiff competition.
This book will bring the world of C++ programming closer to you.

Beginning to Program in C++ Language

All the C++ programs are written into text files with extension ".cpp" for example "hello.cpp", where ".cpp" is the extension file name. These text files are called source files. The source files contains the C++ source codes and will later be transformed into a running executable file. The program development cycle of C++ language are:
1. Write the source codes into a text editor (ex: notepad) to create the source file.
2. Compile the source file to create the object file.
3. The object file is link into an executable (ex: .exe for windows) file.
4. Run the program and see the result.

Based from the program development cycle of C++, notice that there are several things needed which includes a text editor, a compiler, and a linker. The good news is that, for Windows users, there is a software that comprises the three. It is called Dev-C++ which is available at http://bloodshed.net and for the download direct link at http://bloodshed-dev-c.en.softonic.com/download.
Dev-C++ is a C++ source code editor, a compiler, and a linker. All the things needed in creating a C program.
Note: You may ought to use other compilers as the sample codes in this book will work just fine.



Chapter 2 - Our First C++ Program


This chapter presents the "know how" of writing, compiling, and running C++ language programs. It presents a detailed discussions on the steps to be undertaken.

Definition of programming terms

The following terms are defined conceptually and operationally in order to have a common understanding of the terms used in this book.
Argument. Arguments are actual value being pass to a function.
Bug. A bug is a general term used to describe any unexpected problem with the program during execution or when the programming is already running.
Block. A block is composed of two or more C statements. Also called compound statements. A block is enclosed with a pair of { } braces.
CamelCase. CamelCase is the naming convention for compound word which uses capital letters to delimit the word parts. For example, "studName", "studAge", and "intNum".
Clause. A section in the statement.
Code. It is the term used for statements written that make up a C program.
Condition. A statement that evaluates to either true (non zero) or false (zero).
Data Type. It the type of data being stored to a storage location like variable.
Debug. This refers to the process of examining and removing errors from a program's source code.
Expression. It is a statement that result to a numeric value.
Function. Function perform a specific task.
Label. Label is used a give details to a certain line of code in C language, hence this labels are ignored by the compiler, thus it does not affect the output of the program. Usually used for internal documentation.
Null. In computer programming, null is the term used for an uninitialized, undefined, empty, or meaningless value.
Parameter. Parameters are argument declaration of a function.
Statement. A statement is a complete direction instructing the computer to carry out some task. In C, statements are usually written one per line, although some statements span multiple lines. This term can be interchangeably use with code.
Syntax. Syntax is the grammar, structure, or order of the elements in a C language or any other programming language statement. (Semantics is the meaning of these elements.)
Variable. Variables are storage location for data in C programs.

The components of a C++ program

Lets discuss the components of a C++ programs through a sample program. This program prints the word "Hello World!" into the screen. The code listings is presented below

Code listings of figure 2.1
#include<iostream>
using namespace std;
int main()
  {
  //The line below prints a string
  cout<<"Hello World!";
  getchar();
  return 0;
  }

Line 1. #include<iostream>
This line includes a C++ library for the use of the program. The inclusion of library in C++ program usually comes first.

Syntax #include directive:
#include<<lib-name>>

The <lib-name> in the syntax should be replaced with the library name. The other term for library is "include file" because literally, a library is a separate disk file that contains information needed by the program or the compiler.
The #include directive instructs the C++ compiler to add the contents of library or include file (sometimes called "Header files") into the program during compilation. The library files usually comes with the compiler, hence in Dev-C++, several libraries comes together with its default installation. These library files should have a ".h" file extension.
In line 1, the library included is the "iostream" which stands for input/output stream. This library is used in dealing with programs that accepts input and provides output, hence this is the most used library. Other libraries are discussed in the succeeding chapters of this book.
Note: Other program examples from other sources includes the ".h" extension for its library, but take note that the standard now no longer include the .h extension.

Line 2. using namespace std;
C++ uses namespaces to organize different names used in programs. Every name used in the iostream standard library file is part of a namespace called std (probably stands for "standard"). Consequently, the cout object is really called std::cout. The using namespace std; statement avoids the need for putting std:: before every reference to cout, so that the cout alone can be used in the code.

Line 3. int main()
This line defines the main body of the program. The only component that is required in every C++ program is the main() function. In its simplest form, the main() function consists of the name main followed by a pair of empty parentheses ( ) and a pair of braces { } presented in lines 4 and 9. Within the braces are statements that make up the main body of the program. Under normal circumstances, program execution starts at the first statement in the main() function and terminates at the last statement in main().
Usually functions are invoked or called by other functions, but main() is special. When the program starts, main() is called automatically.
main() function, like all functions, must state what kind of value it will return. The return value type for main() in the sample program is int which signifies integer number, but it's not necessary for a program that just prints the word "Hello World", hence, the value 0 in line 8 is returned just to satisfy the format. Returning values from functions is discussed in detail in the succeeding chapters of this book

Lines 4 and 9. { }
The curly braces { } are used to enclose the program lines that make up every C++ function including the main() function. A group of one or more statements enclosed within braces is called a block. Line 4 signifies the start of the content of the main function and line 9 signifies its end.

Line 5. //The line below prints a string
The // (double slash), are called comments. Anything that follows the // becomes a comment. Comments are text details usually used by programmers to reference a line code statement in the source file. The comments are ignored by the compiler, hence does not affect the output of the program. The program will execute and run properly with or without comments. In line 5, the comments are used to give a reference or details that the next line will print a string on-screen.

Syntax for single-line comment:
//<string-comment>

The <string-comment> in the syntax should be replaced with a text entries that represents the comment of the programmer in the code. The comment can be on a single line or multi-line. Line 5 is an example of single line comment. The multi-line comment uses the /*  */ (slash asterisk asterisk slash, wherein anything enclose inside is considered comment. It can span multiple lines. The format is presented below.

Syntax for multi-line comment:
/* <string-comment-string-comment
-string-comment>  */

Line 6. cout<<"Hello World!";
The cout is pronounced “C-out". cout statement is a library object stream under "iostream" library that displays information on-screen. The cout statement can display a simple text message or the value of one or more program variables. The cout, followed by the << (output redirection operator) in which whatever follows it is written on-screen. If it is needed to print string of characters, be sure to enclose them in double quotes ", as shown in line 6.

Syntax of cout in printing literal text:
cout<<"<string-to-print>";

In the syntax, anything inside the " " double quotes is considered a literal text and will be printed on-screen. The <string-to-print> should be replaced with the text entries to be printed on-screen. The cout statement must end with a ; semi-colon.
Note: If line 2 using namespace std; is omitted, the cout statement should be written as std::cout<<"<string-to-print>";

Line 7. getchar();
The getchar() "get character", is library function that accepts a character from the keyboard. If the getchar() is omitted from the code, the program will run, but upon printing the word "Hello World!", the program will immediately terminate. This is how the display console of Dev-C++ was designed. The program output closes immediately and may not be seen by the user. Hence the getchar() is used as the trigger that when the output is presented on-screen, the user will have to press a key in the keyboard (except for space bar) before the program terminates. The getchar() is simply used as a terminator and is used for the rest of the sample programs in this book. The getchar() function must end with a ; semi-colon.
Note: If you are using other compilers that has a different display console behavior, you may ought to remove the getchar().

Line 8. Return 0
Technically in C++, the main() function has to return a value because most of the compiler requires it to be declared with a return value, and it is a common tradition to declare it as int main() indicating that it should return a integer value. The return value is usually passed back to the operating system. Traditionally, 0 indicates that the program is indeed successful.
Note: You don't have to return 0 explicitly, because that will happen automatically when the main() function terminates. But keep in mind that the main() function is the only function where omitting return is allowed. It is also a common practice for C++ programmers to include the return 0 in the main() function, hence for the rest of the sample programs in this book includes the return 0 in its main() function.

Writing, compiling, and running our first program using Dev-C++ for Windows
In writing the source file, open the Dev-C++ and create a new source file as presented in figure 2.2.

ENJOYED READING?
Get the entire book at amazon.com


Table of Contents:
Brief history of C++
Reasons to use C++
Beginning to Program in C++ Language
Installing the Dev-C++ for Windows
Installing compiler for Linux
Self-assessment questions
Definition of programming terms
The components of a C++ program
Writing, compiling, and running our first program using Dev-C++ for Windows
Correcting errors
Writing, compiling, and running our first program with Linux
Compiling and running the program on Linux
Statements
Null Statements
White spaces
Self-assessment questions
Variable
Variable Declaration and definition
Scope of variables
Constants
Keywords
typedef declarations
Self-assessment questions
Displaying text on-screen
Accepting user input
Self-assessment questions
Array
Single-Dimensional
Multi-Dimensional Array
The C-Style Character String
Declaring and defining a string
Defining a string using input functions
C Strings' pre-defined functions in C++
Self-assessment questions
Expressions
Operators
Assignment Operator
Mathematical Operators
Binary
Unary
Mathematical operators precedence level and parentheses
Relational Operators
Logical Operators
Type Casting
Pre-defined Mathematical Functions
Self-assessment questions
The if() statement
Single-alternative
Dual-alternative
Multiple-alternative
Nested if() statement
The switch() statement
Things to consider in conditional statement:
Self-assessment questions
What is a Loop?
Counter-controlled loop
Nested for() loop statement
Condition-controlled loop
Pre-test loop
Post-test loop
The Infinite Loop
Self-assessment questions
User-defined function
User-defined function prototype
User-defined function definition
Calling a user-defined function
Things to consider in functions
Self-assessment questions
Structures
Declaring a structure
Defining a structure
Accessing Structure Members
Compound declaration and definition of structure
Self-assessment questions
What is a class?
Creating New Types
Classes and Members
Declaring of a Class in C++
Definition of a Class member method(s) in C++
Defining an Object of a class
Accessing Class Members
Private versus public access controls
Constructor and destructor
Self-assessment questions
What is exceptions?
How Exceptions Are Used
The basic steps in using exceptions
Self-assessment questions
Pointers in C++
Pointer Declaration
How pointers works?
Pointer Arithmetic
Self-assessment questions
C++ Files and Streams
Opening a File
Closing a File
Writing to a File
Reading from a File
Self-assessment questions

GET YOUR COPY NOW!
at amazon.com