Copyright © Programs ++
Design by Dzignine

C++ FAQ

1 - What is C++ ?? 

C++ is a programming language. It  means "C with Classes", reflecting its nature as an evolution of the C language. It was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. He found C lacking in many aspects. C++ incorporates the use of "Object Oriented Programming" a.k.a OOP. Its one of the most widely used programming languages in the world and arguably the best in terms of simplicity and flexibility.
An example of how a C++ programs looks like is as follows :

http://programsplusplus.blogspot.com/2012/01/1-hello-world-c-program.html

2 - What IDE to use for C++ ?
C++ can be used on many IDE's (Integrated Development Environment)  such as:
 1 -- NetBeans - A cross platform IDE that is available for free and is used to develop applications and projects in many languages such as C/C++, Java, JME, PHP, Python, Maven etc.
netbeans.org/features/cpp/

2 -- Visual C++ - Another useful tools for those who want to develop GUI applications  in C++.
msdn.microsoft.com/en-us/vstudio/hh388567 


3 -- Turbo C++ - An ancestor of IDE's, its not widely used anymore and is basically used to develop applications in C++ that are meant to run in DOS environment. However, it is good choice for anyone who has just started programming in C++.

There are many other ide's that are used for writing programs in C++ such as dev-c++, codeblocks etc. Click the following link for more info :


http://wiki.wxwidgets.org/List_of_Integrated_Development_Environments

Whatever IDE's you choose, the concept and the basics remain the same, so pick whichever one suits your needs. 

3 - What are jump statements and how are the used ?
Jump statements are used to abrupt the procedural flow of the program by using redirecting statements(known as jump statements) to redirect the flow of the program. There are 4 jump statements in C++ :
1 - goto - transfers control of the program to the goto tag
2 - continue - continues the loop from the start.
3 - return - breaks the flow and exits the function with a value.
4 - break - breaks the loop when the statement is encountered.

An example showing the use of these statements is given in the link below :


http://programsplusplus.blogspot.com/2012/01/program-to-demonstrate-use-of-jump.html

4 - What is the use of exit() function ??
The exit() function is used to terminate the program. It takes in an integer as an argument. Syntax : exit(integer);
exit(0) - indicates normal program termination
exit(1) - indicates abnormal program termination(with error).
the exit() function is located in <process.h>.  

5 - What are the various escape sequences ??
Certain special characters are represented as escape sequences. An escape sequence begins with a \ (backslash) followed by an alphanumeric character. For example, the \n escape sequence represents the newline character. Note that the two characters of an escape sequence are construed as a single character.
\a --Bell (alert) - Makes a sound from the computer
\b --Backspace - Takes the cursor back
\t --Horizontal - Takes the cursor to the next tab stop
\n --New line - Takes the cursor to the beginning of the next line
\v --Vertical Tab - Performs a vertical tab
\f --Form feed
\r --Carriage return - Causes a carriage return
\" --Double Quote - Displays a quotation mark (")
\' --Apostrophe - Displays an apostrophe (')
\? --Question mark - Displays a question mark
\xHn -- Hexadecimal Number - a number of base 16
\0 -- Null - Displays a null character
 

6 - Are NULL and 0 the same thing ??
Contrary to popular belief, NULL is actually a macro having value 0.It should be defined as #define NULL 0.

7 - Why is // and /* used and what is the difference between the two ?? 
// and /* are used for inserting comments in programs. Comments are ignored by the compiler and are used to convey additional information about the code. With // a single line of comment can be included but with /* ..*/ multi-line comments can be made.
Multi line comments begin with /* and end with */.

8 - How to use getline() function in C++ ??
The getline() function is located in <iostream>and is used for console string input. It also accepts white spaces. The size of the variable must me known beforehand though. Example :


char name[20];
cin.getline(name,sizeof(name));
or
cin.getline(name,20);
Here, sizeof(name) returns the maximum size of the array i.e 20, saves time.!!!

9 - What are ASCII codes ??
ASCII stands for American Standard Information Code for Information Interchange. It is basically a set of characters each having a specific integral value. Since the computer only understands 0's and 1's, ASCII values are used to represent characters (from a-z,A-Z and special symbols such as $,&,* etc.). All these characters have a specific ASCII value. 

 
an example is given here : 
http://programsplusplus.blogspot.com/2012/01/12-to-pring-ascii-values-of-z.html 

http://programsplusplus.blogspot.com/2012/01/11-to-print-ascii-values-of-z.html 

A detailed explanation is given here :
http://en.wikipedia.org/wiki/ASCII

10 - What is exception handling and how is it implemented in C++ ?? 
An exception is basically "hindrance to the normal flow of a program". It is just the abrupt flow of a program which is usually caused when the user gives an unexpected input during run-time but may be caused due to other reasons also. The process of handling exceptions is called exception handling. E.g.  dividing something by zero(infinity) etc. It is implemented in C++ by using exception handlers such as try and catch.


try{ .... } // tests a block of code to search for any exceptions


catch( ... ) { ..... } // catches the exception so it can be handled. Can have a parameter (optional).

throw(VALUE); // throw() is used to throw the exception to the appropriate catch block.
An example of how these are implemented in C++ is in the link below :
http://programsplusplus.blogspot.com/2012/01/13-exception-handling-try-n-catch.html



Further info on exception handling can be found here :
http://www.cplusplus.com/doc/tutorial/exceptions/

11 - What is the difference between Exceptions and Errors ??

The basic difference between an exception and an error is that an error can be detected by the compiler whereas an exception cannot. Furthermore, an exception occurs during program run-time whereas an error is usually syntax based and is detected before run-time. 
Also, exception - recoverable 
        error - usually unrecoverable 
A detailed difference is given below :
http://www.allinterview.com/showanswers/59519.html

12 - What is the difference between std::endl and "\n" in C++ ??
Even though both std::endl and "\n" seem to have the same effect, both of them differ a lot in how they execute. 
std::endl - A function which sends a newline character(\n) and flushes the output buffer.
\n - is an integral constant it also sends a newline character(\n) but does not flush the output buffer and hence the output buffer has to be manually flushed.
Hence std::endl is a much better option when flushing of the output buffer is concerned.
13 - void() main or int main() ??
Early C++ compilers like Turbo C++ allowed the use of void main() but it is a non standard practice to use void() instead of int. The main() function must return an integer and nothing else. So the use of int main() should be preferred. Also one can also use :
int main(int argc, char** argv) ----> this looks alot similar to the standard main syntax of JAVA. 
Hence avoid void main() at all costs.


14 - Use of '&' operator in C++ ?? 
The '&' (ampersand) operator is used in C++ to return the address of a variable. Unlike the assignment operator (=) it returns the address instead of the value of that variable.Its use as :
int x = 7;


std::cout<<x; // the output will be 7 as it will display the value held by the  variable x which is 7
but,
std::cout<<&x; // will generate something like 0x6f51a12, which is the address of the variable x 

Simply put, '&' returns the address.

Click the link below to view a suitable example : 
http://programsplusplus.blogspot.com/2012/01/18-use-of-operator-in-c.html


15 - Difference between #define and const ?? 


'const' - const stands for constant. In C++, a constant variable is one whose value remains unchanged. As the name suggests, the value of a constant variable cannot be changed during the program unlike other variables. For e.g
const int x =5;
Here the variable x is of a constant integer variable of value and since it is declared as a constant variable, hence its value cannot be altered. i.e operations such as x++ or x = x + 8; will generate a syntax error. 


#define - it is actually a macro, where '#' is the pre-processor directive i.e any declaration using #define, is made even before the program is actually compiled. For e.g. :
#define PI 3.14
Now what happens is that every occurrence of PI in the source code is replaced by 3.14 before the program is compiled. This is particularly useful when using mathematical constants in the program. However, since all this happens even before compiling the program, hence there is no syntax checking and type check, hence it can be very dangerous !!! . Also , since the declaration of the #define macro does not take place in the stack, hence it increases the size of the .exe file. Furthermore, it is not type safe, hence the results can be unpredictable. Hence, its is more safe to use a constant variable whenever possible. 



For a more detailed comparison visit :
http://stackoverflow.com/questions/6442328/what-is-the-difference-betweem-define-and-const
 

3 comments:

Comment Here....