Wednesday, 27 April 2016

Basics Of C++

Basics of C++

In this section we will cover the basics of C++, it will include the syntax, variable, operators, loop types, pointers, references and information about other requirements of a C++ program. You will come across lot of terms that you have already studied in C language.


First C++ program

include <iostream>
using namespace std;
int main()
{
cout << "Hello this is C++";
}
Header files are included at the beginning just like in C program. 
Here iostream.h is a header file which provides us with input & output streams. Header files contained declared function libraries, which can be used by users for their ease.
We,also use the conio.h header file used for the getch() at the end when we are using void return type with the main() function. 
Example :cout << "A";
main(), is the function which holds the executing part of program

Predefined Streams in C and C++:


C and C++ compiler has some predefined streams which indicate some standard devices. These predefined streams are automatically opened when C/C++ program is started. The <stdio.h> header file contains the definition of these predefined streams. Predefined streams used in C and C++ and its meanings are mentioned below. 

Name                                      Meaning
stdin                                                             Standard input device
stdout                                                           Standard output device
stderr                                                           Standard error output device
stdaux                                                           Standard auxiliary device
stdprn                                                           Standard printer


When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods and Instance variables mean.
  • Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating. An object is an instance of a class.
  • Class - A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support.
  • Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Instance Variables - Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

C++ Program Structure:

#include<iostream>
#include<conio.h>

// main() is where program execution begins.

void main()
{
   cout << "Hello World"; // prints Hello World
   getch();
}

C++ Keywords:

Keywords are the reserved words which C++ compiler uses for its internal purposes.

Some of the ketwords supported by C++ are:-

asmelsenewthis
autoenumoperatorthrow
boolexplicitprivatetrue
breakexportprotectedtry
caseexternpublictypedef
catchfalseregistertypeid
charfloatreinterpret_casttypename
classforreturnunion
constfriendshortunsigned
const_castgotosignedusing
continueifsizeofvirtual
defaultinlinestaticvoid
deleteintstatic_castvolatile
dolongstructwchar_t
doublemutableswitchwhile
dynamic_castnamespacetemplate


Here are a few other C++ features that are useful to know.
1.     When you define a class Stack, the name Stack becomes usable as a type name as if created with typedef and enums.

2.     You can define functions inside of a class definition, whereupon they become inline functions, which are expanded in the body of the function where they are used. The rule of thumb to follow is to only consider inlining one-line functions, and even then do so rarely.
As an example, we could make the Full routine an inline.
class Stack {
   ...
   //Variable Declarations;
   //Fuctions Declarations;
   ...
};
3.     Comments can begin with the characters // and extend to the end of the line. These are usually more handy than the /* */ style of comments.

4.     C++ provides some new opportunities to use the const keyword from ANSI C. The basic idea of const is to provide extra information to the compiler about how a variable or function is used, to allow it to flag an error if it is being used improperly. You should always look for ways to get the compiler to catch bugs for you. After all, which takes less time? Fixing a compiler-flagged error, or chasing down the same bug using gdb?
For example, you can declare that a member function only reads the member data, and never modifies the object:
class Stack
{
   ...
   accept() const;  // accept() never modifies member data
   ...
};
As in C, you can use const to declare that a variable is never modified:
    const int InitialHashTableSize = 8;

This is much better than using #define for constants, since the above is type-checked.

Input/output in C++ can be done with the >> and << operators and the objects cin and cout. For example, to write to stdout:
    cout << "Hello world no.=" << 3;

This is equivalent to the normal C code:

                 printf("Hello world no.= %d", 3);



No comments:

Post a Comment