C++ Tutorials
Created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented computer language used in the development of enterprise and commercial applications. Microsoft’s Visual C++ became the premier language of choice among developers and programmers.
As a procedural programming language, C++ uses program structures such as i/o (input/output), assignment statement, iterative statements, conditional statements and subprograms. Data structures of C++ include integer, real, char, arrays, structs and pointers.
Employment opportunities are numerous and well paid for C++ programmers and developers looking to work in the field of Software Engineering or as an IT Professional. Oftentimes, C++ Professionals will also be familiar with C, Linux, Unix, Java, .NET and VB (Visual Basic). Developers working with C++ can expect to participate in a variety of programming opportunities: developing systems for trading applications for an Investment Bank, developing cutting edge software applications for groundbreaking new technologies (Smartphone, PDA, etc.) to creating applications for 3-D Imaging Software or spectroscopic systems.
C++ Tutorials available in this section include explanations for simple to more advanced concepts of C++ in detail with sample coding information. A new programmer or developer interested in learning about C++ programming language and finding out why C++ is one of the most widely used programming languages for creating large-scale applications can utilize the tutorials and articles on C++ made available in this section.
OOP is a better way of solving computer problems compared to a procedural programming language such as C. OOP uses classes which contain members (variables) and methods (functions). OOP uses a modular type of programming structure. OOP is a type of programming in which programmers define not only the data type of a data structure, but also the types of operations that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit…
Object-Oriented Programming is centered on new concepts such as classes, polymorphism, inheritance, etc. It is a well suited paradigm for the following: Modeling the real world problem as close as possible to the perspective of the user. Interacting easily with computational environment using familiar metaphors Constructing reusable software components and easily extendable libraries. Easily modifying and extending implementations of components without having to recode everything from scratch. Definition of OOP: OOP uses objects as its fundamental building blocks. Each object is an instance of some class. Classes allow the mechanism of data abstraction for creating new data types. Inheritance allows…
In this tutorial you will learn about Objects, Classes, Inheritance, Data Abstraction, Data Encapsulation, Polymorphism, Overloading, and Reusability. Before starting to learn C++ it is essential to have a basic knowledge of the concepts of Object oriented programming. Some of the important object oriented features are namely: Objects Classes Inheritance Data Abstraction Data Encapsulation Polymorphism Overloading Reusability In order to understand the basic concepts in C++, a programmer must have a good knowledge of the basic terminology in object-oriented programming. Below is a brief outline of the concepts of object-oriented programming languages : Objects: Object is the basic unit of…
In this C++ Tutorial you will learn about type conversions in C++, what is a type conversion, how to achieve this, automatic conversion otherwise called as implicit conversion and type casting otherwise called as explicit conversion. What is type conversion It is the process of converting one type into another. In other words converting an expression of a given type into another is called type casting. How to achieve this There are two ways of achieving the type conversion namely: Automatic Conversion otherwise called as Implicit Conversion Type casting otherwise called as Explicit Conversion Let us see each of these…
Class Constructors and destructors in C++ In this C++ tutorial you will learn about Class Constructors and destructors in C++ viz., Constructors, What is the use of Constructor, General Syntax of Constructor, Destructors, What is the use of Destructors and General Syntax of Destructors. Constructors: What is the use of Constructor The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor. General Syntax of Constructor A constructor is a special member function that takes the same name as the class name. The…
In this C++ Tutorial you will learn about Concepts of Arrays in C++, what is an array, how to access an array element, declaration of array and how to access array elements. What is an array? An array is a group of elements of the same type that are placed in contiguous memory locations. How to access an array element? You can access an element of an array by adding an index to a unique identifier. For example Suppose Exforsys is an array that has 4 integer values in it that is of int data type, then Exforsys is internally…
In this C++ tutorial you will learn about Operator Overloading in two Parts, In Part I of Operator Overloading you will learn about Unary Operators, Binary Operators and Operator Overloading – Unary operators. Operator overloading is a very important feature of Object Oriented Programming. Curious to know why!!? It is because by using this facility programmer would be able to create new definitions to existing operators. In other words a single operator can take up several functions as desired by programmers depending on the argument taken by the operator by using the operator overloading facility. After knowing about the feature…
In this C++ Tutorial you will learn about the concepts of function – What is a function?, Features of a Function, Declaring a Function, Defining a function and Calling the function. What is a function? A function is a structure that has a number of program statements grouped as a unit with a name given to the unit. Function can be invoked from any part of the C++ program. Features of Function: To understand why the program structure is written separately and given a name, the programmer must have a clear idea of the features and benefits of function. This…
In this C++ Tutorial you will learn about the Concept of Function with Arguments – What is an argument, Declaring a function, Function Definition and Calling the function. Previous chapters of this tutorial have discussed simple function definition writing, declaration of function and function call. In this chapter, the definition, declaration and call function with arguments will be addressed. What is an argument? An argument is the value that is passed from the program to the function call. This can also be considered as input to the function from the program from which it is called. How to declare a…
In this C++ Tutorial you will learn about Scope of Variables in Function, Local Variables – Scope of Local Variables, Global Variables – Scope of Global Variables. The scope of the variables can be broadly be classified as Local Variables Global Variables Local Variables: The variables defined local to the block of the function would be accessible only within the block of the function and not outside the function. Such variables are called local variables. That is in other words the scope of the local variables is limited to the function in which these variables are declared. Let us see…
The static member functions have a class scope and they do not have access to the ‘this’ pointer of the class. When a member is declared as static that static member of class have only one such data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated. Defining Static Function: This is done by using the keyword static before the member function which is to be declared as static function….
Abstraction is one of the most powerful and vital features provided by object-oriented C++ programming language. Modularity is very important in any programming language, it provides flexibility to users for using the programming language. This aspect is well achieved with high performance by the concept of abstraction in C++. In object-oriented programming language the programmer can abstract both data and code when needed. What is Abstraction The concept of abstraction relates to the idea of hiding data that is not needed for presentation. The main idea behind data abstraction is to give a clear separation between properties of data type…
In this C++ tutorial, you will learn about variables, constants and data types in C++, rules for defining variable names, short int, int, long int, float, double, long double, char, bool, declaring variables and constants. Variables A variable is the content of a memory location that stores a certain value. A variable is identified or denoted by a variable name. The variable name is a sequence of one or more letters, digits or underscore, for example: character_ Rules for defining variable name: A variable name can have one or more letters or digits or underscore for example character_. White space,…
In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. Features of Class: Classes contain data known as members and member functions. As a unit, the collection of members and member functions is an object. Therefore, this…
Introduction Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions existing inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly. Neither…
Introduction Polymorphism is the ability to use an operator or method in different ways. Polymorphism gives different meanings or functions to the operators or methods. Poly, referring to many, signifies the many uses of these operators and methods. A single method usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts. Below is a simple example of the above concept of polymorphism: 6 + 10 The above refers to integer addition. The same + operator can be used with different meanings with strings: “Exforsys” +…
What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class. For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango…
In this C++ tutorial, you will learn about Structures, declaring a Structure, how to declare Structure Variable and how to access the structure members in C++. What is a Structure? A structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name. How to declare and create a Structure Declaring a Structure: The structure is declared by using the keyword struct followed by…
In this C++ tutorial, Structures Part II, you will learn how to use structure in an efficient way, and features of structures explained with examples. Structure Members Initialization: As with arrays and variables, structure members can also be initialized. This is performed by enclosing the values to be initialized inside the braces { and } after the structure variable name while it is defined. For Example: #include using namespace std; struct Customer { int custnum; int salary; float commission; }; void main( ) { Customer cust1={100,2000,35.5}; Customer cust2; cust2=cust1; cout
In this C++ tutorial, you will learn how to access Class members, dot operator or class member access operator, difference between struct and class and scope resolution operator. It is possible to access the class members after a class is defined and objects are created. General syntax to access class member: Object_name.function_name (arguments); The dot ('. ') used above is called the dot operator or class member access operator. The dot operator is used to connect the object and the member function. This concept is similar to that of accessing structure members in C programming language. The private data of…
In this C++ tutorial, you will learn about standard input stream and standard output stream explained along with syntax and examples. It is C++ programming language uses the concept of streams to perform input and output operations using the keyboard and to display information on the monitor of the computer. What is a Stream? A stream is the source or destination of a series of data, either characters, or in the case of binary files, a sequence of bytes that represent memory content. The standard input and output stream objects of C++ are declared in the header file iostream. Standard…
In this C++ tutorial, you will learn about operators, the assignment operator, arithmetic operators, compound assignment operators, increment and decrement operator, the functionality of prefix and postfix operators, relational and equality operators. The operators available in C++ programming language are: Assignment Operator denoted by = Arithmetic operators denoted by +, -, *, /, % Compound assignment Operators denoted by +=, -=, *=, /=, %=, >>=, =, = Greater than or equal to
In this C++ tutorial, you will learn about logical operators, && operator, || operator, conditional operator, comma operator, bitwise operator and sizeof() operator. Logical Operators The logical operators used are: !, &&, || The operator ! is called NOT operator. This has a single operand which reverses its value. Logical Operators !true gives the value of false !false gives the value of true The operator && corresponds with Boolean logical operation AND. This operator returns the value of true only if both its operands are true or else it returns false. The following table reflects the value of && operator:…
In this C++ tutorial, you will learn what a manipulator is, endl manipulator, setw manipulator, setfill manipulator and setprecision manipulator are all explained along with syntax and examples. What is a Manipulator? Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of display. There are numerous manipulators available in C++. Some of the more commonly used manipulators are provided here below: endl Manipulator: This manipulator has the same functionality as the ‘n’ newline character. For example: cout
In this C++ tutorial, you will learn about decision making statements if statement, if..else statement, switch statement, conditional operator along with syntax and examples. Decision-making is an important concept in any programming language and to accomplish this, C++ uses the following decision making statements: if statement if..else statement switch statement conditional operator Explanation of decision-making statements in C++ programming language: if statement: if (condition) { statement 1; statement 2; … } If the condition returns true value then all the statements inside the braces of if block are executed. Otherwise, if the condition returns false, the statements from inside the…
In this C++ tutorial, you will learn about Multidimensional arrays, what is Multidimensional array, how is Multidimensional arrays represented in C++, how to access the elements of a Multidimensional array. What is a Multidimensional Array? A Multidimensional array is an array of arrays. How are Multidimensional Arrays represented in C++ Suppose a programmer wants to represent the two-dimensional array Exforsys as an array with three rows and four columns all having integer elements. This would be represented in C++ as: int Exforsys[3][4]; It is represented internally as: Exforsys Data Type: int How to access the elements in the Multidimensional Array…
In object-oriented programming language, whenever a block of statements has to be repeated a certain number of times or repeated until a condition becomes satisfied, the concept of looping is used. The following commands used in C++ for achieving looping: for loop while loop do-while loop for loop: The syntax of for loop is for (initialization; condition; increase or decrease) { statement block; } Initialization is the primary value set for a variable. Initialization is only executed one time. After initialization is performed, it is not executed again. The condition specified is checked and if the condition returns a value…
In this C++ tutorial, you will learn about string representation and handling, how a string is represented, end string notation, initializing char array, character representation, string literals, example program to understand the character array concept and the accessing of character array in detail and how to represent an array of Strings. How is a string represented? Strings, or sequences of characters, are represented as arrays of char elements. For example, when a programmer wants to represent a char array Exforsys having 30 elements it is declared: char Exforsys[30]; This means the character array has 30 storage location allocated where it…
Operator overloading is a very important aspect of object-oriented programming. Binary operators can be overloaded in a similar manner as unary operators. In this C++ tutorial, you will learn about Binary Operators Overloading, explained along with syntax and example. Operator Overloading – Binary Operators Binary operators, when overloaded, are given new functionality. The function defined for binary operator overloading, as with unary operator overloading, can be member function or friend function. The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number…
In this C++ tutorial, you will learn about storage classes, types of storage class variables – Automatic, External and Static explained with examples. Storage classes: In the context of scope of variables in functions exists the important concept of storage class. What is Storage Class? Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program. Types of Storage Class Variables in…
In this C++ tutorial, you will learn about function passing types, two types of arguments passing in functions – passed by value and passed by reference are discussed here. The arguments passed to a function can be performed in two ways: Passed By Value Passed By Reference Passed By Value: In the earlier chapter, all examples for the function with arguments were passed by value. Arguments passed by value are the copies of the values of variables and are passed to the function. The variables defined in the calling function are not passed in this manner. For example: int s=5,…
In this C++ tutorial, you will learn about the Inline function, what is an inline function, reason for the need of inline functions, what happens when an inline function is written, general format of inline function explained with example. What is Inline Function? Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program. Reason for the need of Inline Function: Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling…
What are Virtual Functions? Virtual, as the name implies, is something that exists in effect but not in reality. The concept of virtual function is the same as a function, but it does not really exist although it appears in needed places in a program. The object-oriented programming language C++ implements the concept of virtual function as a simple member function, like all member functions of the class. The functionality of virtual functions can be overridden in its derived classes. The programmer must pay attention not to confuse this concept with function overloading. Function overloading is a different concept and…
In this C++ tutorial, you will learn about pure virtual functions, declaration of a pure virtual function and virtual base class, virtual base class and how to implement a virtual base class, explained with examples. What is a Pure Virtual Function: A Pure Virtual Function is a Virtual function with no body. Declaration of Pure Virtual Function: Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class. General Syntax of Pure Virtual Function takes the form: class class_name //This denotes the base class of C++…
In this C++ tutorials, you will learn about friend functions, the need for friend function, how to define and use friend functions and few important points regarding friend functions, explained with examples. The Need for Friend Function: As discussed in the earlier sections on access specifiers, when a data is declared as private inside a class, then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. A programmer may have a situation where he or she would need to access private…
Topics Syntax How to access Example Keep in Mind Static member functions have a class scope and they do not have access to the ‘this’ pointer of the class. When a member is declared as static, a static member of class, it has only one data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated. Defining Static Function Static function is defined by using the keyword static before the member…
Concept of Pointers: Every storage location of memory has an associated address. The address is a number that grows sequentially. For every program placed in memory, each variable or function in the program has an associated address. The address of operator: The address of operator or Reference operator is denoted by the notation &. When the user wants to get the address of a variable, then the reference operator & can be used. The operator & is used to find the address associated with a variable. The syntax of the reference operator is as follows: &variablename This means that the…
In this C++ tutorial, you will learn about two interesting types of pointers; void pointers and Null Pointer. These pointers will be discussed in conjunction with syntax, usage and example. Pointer to Void General Syntax: void* pointer_variable; Void is used as a keyword. Referring back to pointer definitions and usage, it is known that the data type the pointer variable defines is the same as the data type the pointer points to. The address placed in a pointer must have the same type as the pointer. For example: int i; float f; int* exf; float* test; then exf=&i; Is correct…
Need for Memory Management operators The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant. In programming there may be scenarios where programmers may not know the memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used…
In this C++ tutorial, you will learn how to access the value of variables pointed by the pointer variables using pointer concept discussed in detail. It is possible to access the value of variables pointed by the pointer variables using pointer. This is performed by using the Dereference operator in C++ which has the notation *. The general syntax of the Dereference operator is as follows: *pointer_variable In this example, pointer variable denotes the variable defined as pointer. The * placed before the pointer_variable denotes the value pointed by the pointer_variable. For example: exforsys = 100; test = exforsys; x…