C++ Notes
C++ Programming Language
C++ is the most used and most popular programming language developed by Bjarne Stroustrup. C++ is a high-level and object-oriented programming language. This language allows developers to write clean and efficient code for large applications and software development, game development, and operating system programming. It is an expansion of the C programming language to include Object Oriented Programming(OOPs) and is used to develop programs for computers. This C++ Tutorial will cover all the basic to advanced topics of C++ like C++ basics, C++ functions, C++ classes, OOPs and STL concepts.What is C++?
C++ is a most popular cross-platform programming language which is used to create high-performance applications and software like OS, Games, E-commerce software, etc. It was developed by Bjarne Stroustrup, as an extension of C language. C++ give a high level of control over system resources and memory.
Why Learn C++?
- C++ is one of the most used and popular programming languages.
- C++ is used in making operating systems, embedded systems, and Graphical User Interfaces.
- It is an object-oriented programming language that implements all the OOPs concepts such as Abstraction, Encapsulation, and Inheritance, which gives a clear structure to programs and allows code to be reused, lowering development costs and providing security.
- It is portable and can be used to create applications that can be adapted to multiple platforms.
- C++ is easy to learn so that you can choose it as your first programming language.
- It makes programming easy for programmers to switch to C++ because its syntax is similar to C, Java, and C#.
History of C++
The C++ language is an object-oriented programming language & is a combination of both low-level & high-level language – a Middle-Level Language. The programming language was created, designed & developed by a Danish Computer Scientist – Bjarne Stroustrup at Bell Telephone Laboratories (now known as Nokia Bell Labs) in Murray Hill, New Jersey. As he wanted a flexible & a dynamic language which was similar to C with all its features, but with additionality of active type checking, basic inheritance, default functioning argument, classes, inlining, etc. and hence C with Classes (C++) was launched.
C++ was initially known as “C with classes, ” and was renamed C++ in 1983. ++ is shorthand for adding one to variety in programming; therefore C++ roughly means that “one higher than C.”
- The trace of the programming language C++ can be done back to 1979 when Bjarne Stroustrup was doing some development for this thesis for PhD. One in all the words Stroustrup had the chance to figure with, was a language referred to as Simula, that because the name implies, could be a language primarily designed for simulations. The Simula sixty-seven language – that was the variant that Stroustrup worked with is considered the primary language to support the object-oriented programming paradigm. Stroustrup found that this paradigm was helpful for package development; but, the Simula language was way too slow for practice & practical use.
- Shortly subsequently, he began work on “C with Classes“, because what the name implies was meant to be a superset of the C language. His main dream was to get his advanced object-oriented programming, created by him, into the C language, which was during his era was still the programming language that was widely respected for its movability, portability & compactness, by not sacrificing the speed or the low-level practicality. His programming language enclosed inlining, basic inheritance, default function arguments, categories, and reliable sort was checking additionally to all or any the options of the C language.
- The first C with categories compiler was referred to as Cfront, that got derived from a C compiler referred to as CPre. It had been a program designed to translate C with categories code to universal C. A rather attention-grabbing purpose value noting is that Cfront was written mostly in C with classes, creating it a self-hosting compiler (a compiler which will compile itself). Cfront would later be abandoned in 1993 when it became troublesome to integrate new options into it, mainly C++ exceptions. Even so, Cfront created a significant impact on the implementations of future compilers and also the operating system UNIX.
- In 1983, the name of the language got modified from C with categories to C++. The ++ operator within the C language is the associate operator for incrementing a variable, which provides some insight into however Stroustrup regarded the programming language. Several new options got additional around this point, the foremost notable of that area unit virtual functions, perform overloading, references with the const keyword, and single-line comments by using the two forward slashes (which may be a feature taken from the language BCPL).
- In 1985, Stroustrup’s relation to the language entitled The C++ programming language was printed and got released. That very same year, C++ got enforced as an advertisement product and hence starting of it as a commercial element. The programming language wasn’t formally standardized, nevertheless creating the book an essential reference. The programming language was updated once more in 1989 to incorporate protected and static members, still as an associate inheritance from many categories & classes.
- In 1990, The Annotated C++ manual, which was a reference got released all over. In 1990 only, in the same & identical year, Borland’s Turbo C++ compiler would also be commercially discharged as an advertisement product. Turbo C++ additional an excessiveness of other libraries which might have a substantial impact on C++’s development. Though Turbo C++’s last stable unharness was in 2006, the compiler continues to get widely used.
- In 1998, the C++ customary advisory & standards committee printed the first international standard for C++ ISO/IEC 14882:1998, which might be informally called C++98. The Annotated C++ manual was aforementioned to be a significant influence within the development of the quality. The quality template library (also known as the Standard Template Library) that began its intellectual construction in 1979 got additionally enclosed in it. In 2003, the committee well-versed multiple issues that were according with their 1998 customary and revised it consequently. The modified language got dubbed as C++03.
- In 2005, the same committee of C++ discharged a technical report (called as TR1) particularization varied options they were aiming to boost the newest C++ standard. The new rule was informally dubbed C++0x because it was expected to be discharged someday before the tip of the first decade. Ironically, however, the new customary wasn’t fulfilled until mid-2011. Many technical reports were discharged up till then, and a few compilers began adding experimental support for the new options and features.
- In mid-2011, the new C++ customary (dubbed C++11) got finished. The Boost library project created a substantial impact on the new rule, and a few of the new modules were derived directly from the corresponding Boost libraries. A number of the new options that got included were:
- New for loop syntax providing practicality just like foreach loops in specific different languages
- Customary threading library (which up till 2011 each C and C++ were lacking)
- Variadic templates
- Automotive vehicle (AUTO) keyword
- New instrumentation categories & classes
- New C++ time library, atomics support
- Comprehensive organization library
- Regular expression support
- Higher support for unions and array-initialization lists
- C++20 is the latest version of C++. A newer & advanced version of C++ is being released, i.e. C++23.
- C++ is still the third most popular programming language according to the TIOBE Index’s latest study of 2019, behind Java & C, by knocking out Python. All credits for this go to the newly released C++11 version, which according to the users made it much robust, safer, easier simpler, and more expressive.
Bjarne Stroustrup has created excellent work with C++. C could be a low-level programming language, and so, it does not have any classes. It does not contain several options which will create programming more well-off, however, is that the quickest language (assembly is more rapid; however, programming in construction isn’t one thing you’d consider), what Bjarne Stroustrup did he additional the thing orientated half, by treating code like life objects. What makes C++ therefore lovely is that it’s the speed of C and it’s additionally a high-level programming language, therefore allow us to say the most effective of each world. However on the opposite hand, C++ is challenging to be noted to newbies, and for an equivalent code that you wrote on C++, you’ll be able to write in Python for the quarter of the time.
Difference between C and C++
Similarities between C and C++ are:
- Both the languages have a similar syntax.
- Code structure of both the languages are same.
- The compilation of both the languages is similar.
- They share the same basic syntax. Nearly all of C’s operators and keywords are also present in C++ and do the same thing.
- C++ has a slightly extended grammar than C, but the basic grammar is the same.
- Basic memory model of both is very close to the hardware.
- Same notions of stack, heap, file-scope and static variables are present in both the languages.
Writing First C++ Program – Hello World Example
C++ is a widely used Object Oriented Programming language and is relatively easy to understand. The “Hello World” program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn.
The Hello World Program in C++ is the basic program that is used to demonstrate how the coding process works. All you have to do is display the message “Hello World” on the console screen.
To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
C++ Hello World Program
Below is the C++ program to print Hello World.
// C++ program to display "Hello World"
// Header file for input output functions
#include <iostream>
using namespace std;
// Main() function: where the execution of
// program begins
int main()
{
// Prints hello world
cout << "Hello World";
return 0;
}
Hello World
1. // C++ program to display “Hello World”
This line is a comment line. A comment is used to display additional information about the program. A comment does not contain any programming logic.
When a comment is encountered by a compiler, the compiler simply skips that line of code. Any line beginning with ‘//’ without quotes OR in between /*…*/ in C++ is a comment. Click to know More about Comments.
2. #include
This is a preprocessor directive. The #include directive tells the compiler to include the content of a file in the source code.
For example, #include<iostream> tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions. Click to Know More on Preprocessors.
3. using namespace std
This is used to import the entity of the std namespace into the current namespace of the program. The statement using namespace std is generally considered a bad practice. When we import a namespace we are essentially pulling all type definitions into the current scope.
The std namespace is huge. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type. For example, std::cout. Click to know More about using namespace std.
4. int main() { }
A function is a group of statements that are designed to perform a specific task. The main() function is the entry point of every C++ program, no matter where the function is located in the program.
The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Click to know More about the main() function.
5. cout<<“Hello World”;
std::cout is an instance of the std::ostream class, that is used to display output on the screen. Everything followed by the character << in double quotes ” ” is displayed on the output device. The semi-colon character at the end of the statement is used to indicate that the statement is ending there. Click to know More on Input/Output.
6. return 0
This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.
7. Indentation
As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. We must always use indentations and comments to make the code more readable. Must read the FAQ on the style of writing programs.
Important Points
- Always include the necessary header files for the smooth execution of functions. For example, <iostream> must be included to use std::cin and std::cout.
- The execution of code begins from the main() function.
- It is a good practice to use Indentation and comments in programs for easy understanding.
- cout is used to print statements and cin is used to take inputs.
C++ Basic Syntax
C++ is a general-purpose, object-oriented programming language. It was developed in 1979 by Bjarne Stroustrup at AT & T Bell Laboratory. It is a high-level programming language & advanced version of C programming language. As Compared to other programming languages like Java, and Python, it is the fastest programming language.
What is Syntax?
Syntax refers to the rules and regulations for writing statements in a programming language. They can also be viewed as the grammatical rules defining the structure of a programming language.
The C++ language also has its syntax for the functionalities it provides. Different statements have different syntax specifying their usage but C++ programs also have basic syntax rules that are followed throughout all the programs.
Basic Syntax of a C++ Program
We can learn about basic C++ Syntax using the following program.
C++ Comments
Comments in C++ are meant to explain the code as well as to make it more readable. When testing alternative code, it can also be used to prevent execution. The purpose of the comments is to provide information about code lines. Programmers commonly use comments to document their work.Why Comments are used in C++?
Comments in C++ are used to summarize an algorithm, identify a variable’s purpose, or clarify a code segment that appears unclear. Comments are also used for:
- Comments are used for easier debugging.
- It makes a program more readable and gives an overall description of the code.
- Comments are helpful in skipping the execution of some parts of the code.
- Every time a program or code is reused after long periods of time, the comment recaps all the information of the code quickly.
Types of Comments in C++
In C++ there are two types of comments in C++:
- Single-line comment
- Multi-line comment
2. Multi-Line Comment
A multi-line comment can occupy many lines of code, it starts with /* and ends with */, but it cannot be nested. Any text between /* and */ will be ignored by the compiler.
Syntax:
/* Multiline Comment . . . */
Example:
C++
C++ Keywords
Keywords
Keywords(also known as reserved words) have special meanings to the C++ compiler and are always written or typed in short(lower) cases. Keywords are words that the language uses for a special purpose, such as void, int, public, etc. It can’t be used for a variable name or function name or any other identifiers. The total count of reserved keywords is 95. Below is the table for some commonly used C++ keywords.
C++ Keyword
asm double new switch auto else operator template break enum private this case extern protected throw catch float public try char for register typedef class friend return union const goto short unsigned continue if signed virtual default inline sizeof void delete int static volatile do long struct while
Comments
Post a Comment