Nano teach C++

Customizable training materials to learn C++

Nano teach C++ found result for your search here:

C++ Tutorial

This tutorial series will help you to get started in C++ to develop system applications.

 














C++ Example:
/* 
* File: main.cpp
* Author: Nano
* Just click image to copy & paste
*/ 




#include <iostream>
int main() {
std::cout<<"This is my first c++ Program.";
std::cout<<std::endl<<"and its very easy"; 
}
Read More »

C++ Introduction

C++ is a multi-paradigm programming language that supports object oriented programming (OOP), created by BJarne Stroustrup in 1983 at Bell Labs, C++ is an extension (superset) of C programming and the programs written in C language can run in C++ compilers.
The development of C++ actually began four years before its release, in 1979. It did not start out with the name C++; its first name was C with Classes. In the late part of 1983, C with Classes was first used for AT&T’s internal programming needs. Its name was changed to C++ later in the same year. C++ was not released commercially until the late part of 1985.
C++ implements “data abstraction” using a concept called “classes“, along with other features to allow object-oriented programming and is considered a high level language. Classes help programmers with the organization of their code. They can also be beneficial in helping programmers to avoid mistakes.
The original C++ compiler, called Cfront, was written in the C++ programming language. C++ compilation is considered efficient and fast. Its speed can be attributed to its high-level features in conjunction with its low-level components. When compared to other computer programming languages, C++ can be viewed as quite short. This is due to the fact that C++ leans towards the use of special characters instead of keywords.


Uses of C++ language

C++ is used by programmers to create computer software. It is used to create general systems software, drivers for various computer devices, software for servers and software for specific applications and also widely used in the creation of video games.

Object-oriented Programming

C++ completely supports object-oriented programming, with four pillars of object-oriented development:
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Standard Libraries

Standard C++ consists of three important parts:
  1. All the building blocks including variables, data types and literals etc is provided by the core language.
  2. The C++ Standard Library is provides rich set of functions manipulating files and strings.
  3. The (STL) Standard Template Library provides a rich set of methods manipulating data structures.
Read More »

C++ Installation


To start learning C++ programming, you only need to install the C++ compiler in your System.

What is a Compiler?

A compiler is a computer program that transforms human readable (programming language) source code into another computer language (binary) code.
This tutorial is written for Windows, Unix / Linux and MAC users. All code has been tested and it works correctly on all three operating systems.

C++ Compiler Installation on Windows

To use C++ compiler in Windows, you can install any one software mentioned below.

C++ Compiler Installation on UNIX/Linux

If you are using UNIX / Linux, then most probably C++ compiler called GCC will already in your system. To check if you have it installed, you can type cc or gcc at command prompt.
$ gcc -v
If for some reason it is not Installed on your system, you can download it from gcc.gnu.org/install.

C++ Compiler Installation on MAC

You can install Xcode development environment from Apple’s website, to use GNU C/C++ compiler.
You can download Xcode from developer.apple.com/technologies/tools.
Read More »

C++ Program Structure

This tutorial will explain you about the C++ program structure.
Basically a C++ program involves the following section.
  • Documentations
  • Preprocessor Statements
  • Global Declarations
  • The main() function
    • Local Declarations
    • Program Statements & Expressions
  • User Defined Functions

Here is a simple program which outputs a line of text.

Example:


/* 
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM
*/ 

    #include <iostream>

    int main() {
    
    std::cout<<"This is my first c++ Program.";
    std::cout<<std::endl<<"and its very easy"; 

    }
Program Output:
This is my first c++ Program.
and its very easy

Let’s look into various parts of the above C++ program.

/* Comments */ Comments are a way of explaining what makes a program. Comments are ignored by the compiler and used by others to understand the code.
#include<iostream> is a preprocessor directive. It tells the preprocessor to include the contents of iostream header file in the program before compilation. This file is required for input output statements.
int/void main() int/void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C++ program should contain only one main function.
Braces Two curly brackets “{…}” are used to group all statements together.

std::cout<<”This is my first c++ Program”;

The above line is a statement in C++. A statement must always terminate with a semicolon (;) otherwise it causes a syntax error. This statement introduces two new features of C++ language, cout and << operator.
You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas.
We used std:: before cout. This is required when we use #include <iostream> .
It specifies that we are using a name (cout) which belongs to namespace std. Namespace is a new concept introduced by ANSI C++ which defines the scope of identifiers which are used in the program. std is the namespace where C++ standard libraries are defined.
Operator << is the insertion stream operator. It sends contents of variable on its right to the object on its left. In our case, right operand is the string “This is my first c++ Program” and left operand is cout object. So it sends the string to the cout object and cout object then displays it on the output screen.

namespace

using namespace std;

If you specify using namespace std then you don’t have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.
Example:


/* 
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM
*/   

    #include <iostream>
    using namespace std; 

    int main() {  
     
    cout<<"This is my first c++ Program.";
    cout<<endl<<"and its very easy";
    
    }

Return Statement

return 0 At the end of the main function returns value 0.
Read More »

C++ Manipulators

Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of display.

Some of the more commonly used manipulators are provided here below:

endl Manipulator

endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an escape sequence) instead of endl for the same purpose.

setw Manipulator

This manipulator sets the minimum field width on output.
Syntax:
setw(x)
Example:
/* 
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 12:58 PM
*/
#include <iostream>
#include <iomanip>
using namespace std;

int main() {

 float basic, ta,da,gs;
 basic=10000; ta=800; da=5000;
  gs=basic+ta+da;
  cout<<setw(10)<<"Basic"<<setw(10)<<basic<<endl
   <<setw(10)<<"TA"<<setw(10)<<ta<<endl
   <<setw(10)<<"DA"<<setw(10)<<da<<endl
   <<setw(10)<<"GS"<<setw(10)<<gs<<endl;
  return 0;
}
Program Output:
Basic 10000
TA 800
DA 5000
GS 6800

setfill Manipulator

This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.
Example:
/* 
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 12:58 PM
*/
#include <iostream>
#include <iomanip>
using namespace std;

int main() {

 cout << setw(20) << setfill('*') << "w3schools.in" << setw(20) << setfill('*')<<"Test"<< endl;
}
Program Output:
********w3schools.in******************Test
Read More »

C++ Data Types

Data types in any of the language means that what are the various type of data the variables can have in that particular language. Information is stored in a computer memory with different data types. Whenever a variable is declared it becomes necessary to define data type that what will be the type of data that variable can hold.

Data Types available in C++ are:

  1. Primary Data Type character, integer, floating point, boolean, double floating point, void, wide character
  2. Additional Data Types typedef, enumerated

Character Data Types

Data Type (Keywords) Description Size Typical Range
char Any single character. It may include a letter, a digit, a punctuation mark, or a space. 1 byte -128 to 127 or 0 to 255
signed char Signed character. 1 byte -128 to 127
unsigned char Unsigned character. 1 byte 0 to 255
wchar_t Wide character. 2 or 4 bytes 1 wide character

Integer Data Types

Data Type (Keywords) Description Size Typical Range
int Integer. 4 bytes -2147483648 to 2147483647
signed int Signed integer. Values may be negative, positive, or zero. 4 bytes -2147483648 to 2147483647
unsigned int Unsigned integer. Values are always positive or zero. Never negative. 4 bytes 0 to 4294967295
short Short integer. 2 bytes -32768 to 32767
signed short Signed short integer. Values may be negative, positive, or zero. 2 bytes -32768 to 32767
unsigned short Unsigned short integer. Values are always positive or zero. Never negative. 2 bytes 0 to 65535
long Long integer. 4 bytes -2147483648 to 2147483647
signed long Signed long integer. Values may be negative, positive, or zero. 4 bytes -2147483648 to 2147483647
unsigned long Unsigned long integer. Values are always positive or zero. Never negative. 4 bytes 0 to 4294967295

Floating-point Data Types

Data Type (Keywords) Description Size Typical Range
float Floating point number. There is no fixed number of digits before or after the decimal point. 4 bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. More accurate compared to float. 8 bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits)

Boolean Data Type

Data Type (Keywords) Description Size Typical Range
bool Boolean value. It can only take one of two values: true or false. 1 byte true or false
Note: Variables sizes might be different in your pc from those shown in the above table, depending on the compiler you are using.
Below example will produce correct size of various data type, on your computer.
Example:
#include <iostream>

    using namespace std;
    
    int main()
    {    
      cout << "Size of char is " << sizeof(char) << endl;
      cout << "Size of int is " << sizeof(int) << endl;
      cout << "Size of float is " << sizeof(float) << endl;
      cout << "Size of short int is " << sizeof(short int) << endl;
      cout << "Size of long int is " << sizeof(long int) << endl;
      cout << "Size of double is " << sizeof(double) << endl;
      cout << "Size of wchar_t is " << sizeof(wchar_t) << endl;
      return 0;
    }
Program Output:
Size of char is 1
Size of int is 4
Size of float is 4
Size of short int is 2
Size of long int is 4
Size of double is 8
Size of wchar_t is 4

Enum Data Type

This is an user defined data type having finite set of enumeration constants. The keyword ‘enum‘ is used to create enumerated data type.
Syntax:
enum enum-name {list of names}var-list;
enum mca(software, internet, seo);

Typedef

It is used to create new data type. But it is commonly used to change existing data type with another name.
Syntax:
typedef [data_type] synonym;
 or
typedef [data_type] new_data_type;
Example:
typedef int integer;
integer rollno;
Read More »

C++ Variables

In C++ Programming language variable is used to store data in memory location.

Rules of Declaring variables in C++

  • Variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
  • The first character must be a letter or underscore.
  • Blank spaces cannot be used in variable names.
  • Special characters like #, $ are not allowed.
  • C++ keywords can not be used as variable names.
  • Variable names are case sensitive.
  • A variable name can be consist of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.

Variable Definition in C++

Syntax:
type variable_name;
type variable_name, variable_name, variable_name;
Example:
/* variable definition and initialization */
int    width, height=5;
char   letter='A';
float  age, area;
double d;

/* actual initialization */
width = 10;
age = 26.5;
Read More »