Computer Programming:
Second Semester Course Outline: https://goo.gl/PiHOqc
C++_How_to_Program_8th_Edition_Paul_Deitel_Harvey_Deitel : https://goo.gl/l8OoBT
C++ Essential by Sharam Hekmat (.pdf) Download Link: https://goo.gl/d8kBh9
What Is Programming?
In simple Words the process of writing computer programs, is called programming.
Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs. Programming involves activities such as analysis, developing understanding, generating algorithms, verification of requirements of algorithms including their correctness and resources consumption, and implementation (commonly referred to as coding) of algorithms in a target programming language. Source code is written in one or more programming languages. The purpose of programming is to find a sequence of instructions that will automate performing a specific task or solving a given problem.
What Is 'C++' ?
C++ is an object oriented programming ,
[( Object-oriented programming or OOP is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self").]
C++ is a language, developed by Bjarne Stroustrup, and is an extension of C language. It is therefore possible to code C++ in a "C style" or "object-oriented style." In certain scenarios, it can be coded in either way and is thus an effective example of a hybrid language.
C++ is a general purpose object oriented programming language. It is considered to be an intermediate level language, as it encapsulates both high and low level language features. Initially, the language was called 'C with classes’ as it had all properties of C language with an additional concept of 'classes’. However, it was renamed to C++ in 1983.
Why We Use "using namespace std;" in C++?
When programs start to get really big, it gets difficult to keep track of all the functions and variables that you have already declared. This sometimes results in two different things being declared that have the same name. When this happens, the program will give you an error because the compiler can't tell which is which anymore.
This gets even worse when there are lots of different libraries involved in the program. If your audio library and your physics library both have functions called get_volume, then the compiler won't know which one to call, and you'll get a link error. This is even worse, because you often don't have the source code for these libraries, so the error is impossible to fix.
In C, the usual approach was to just add a prefix to the start of every function name, like physics_get_volume and audio_get_volume. The prefix would be the name of the library. This is a pain, and we wanted something a bit better designed in C++.
A namespace is a more flexible way of adding prefixes. So you can name the functions physics::get_volume and audio::get_volume. The difference is that if you know you're going to be writing a bunch of audio-specific source code, you can add "using namespace audio" at the top of your file, and then just call get_volume without the audio:: prefix for the rest of that file. It saves on typing. Another difference is that you can leave off all the prefixes while writing code inside the namespace itself, which again makes things easier to read.
The std namespace is special; it is short for the word "standard." The built in C++ library routines are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map, etc. Because these tools are used so commonly, it's popular to add "using namespace std" at the top of your source code so that you won't have to type the std:: prefix constantly. And because these functions are kept in a namespace, if you really want to use "vector" as a variable name, you still can. Namespaces give you more freedom to use short, accurate names.
One side note: it is bad manners to put "using namespace" declarations in header files*. It forces all includers of that header file to use that namespace, which might result in naming ambiguities that are hard to fix. This practice is called "namespace pollution." Instead, always use the fully prefixed names in header files (std::string not string) and save the using declarations for the source files.
*One exception: If you are the owner of a given namespace (i.e. it's your library), and you add a "using namespace X" declaration inside of your namespace, you can do that in a header file. It's still namespace "pollution," but it's only polluting your own namespace, and you own that namespace. This is permissible, but be careful.
No comments:
Post a Comment