Ist Lecture On C++
Ist Lecture On C++
Date: 17/01/11
Outlines
Introduction Components of a C++ Program Escape Sequences Variable declaration & Memory allocation Namespaces User Interactive Programs Formatting the Output
Introduction
C++ improves on many of Cs features. C++ provides object-oriented programming (OOP). C++ is a superset to C.
History
C was designed by Ritchi, Tompson, Kernighan It was fast, good & powerful
HEADER FILES
#include <iostream> #include <cmath> #include <cstdlib>
Output Statements
The first output statement of previous program
std :: cout << Hello, Welcome to programming with C++!\n ;
The line std::cout means that we are calling an object cout which is defined under namespace std or which belongs to header file <iostream.h> of C++ stander library. The object cout is used with the stream insertion operator(<<).
cout
<<
C++ variables
Escape Sequences
Some character preceded by slash character(\) like the one at the end of the above output statement, i.e. \n having a special meaning for the compiler. Back Slash (\) is an escape character. \n is new line character. Thus, std::cout<<\n; means output shifted to the next line.
Escape Character
\a \b \f
Description of action
Bell or beep is generated by the computer on program execution. Back space. Move cursor to previous position From feed Advance cursor to next page
\n
\r \t \v
\\
\ \? \0
\o
\x \
Output
The expected output is : Hello, Welcome to programming with C++! Hello,\ Welcome to programming with C++!\ Hello Welcome to programming with C++! Hello Welcome to programming with C++!
Comments
A comment introduced by first putting double slash (//) followed by the comment up to the end of the line. If the comment is long and goes to the next line, another double slash (//) is needed before the next line. Alternatively, a long comment may as wellas be between the C type comment symbols,i.e. /* before the start of comment and */ at the end of the comment.
How to include comments and use of void main() in place of int main()
// A program with comments #include<iostream> Void main() // Anything written after double slash is neglected up to the // end of line. The comment can be put anywhere in the program. { std::cout << Hello, Welcome to programming in C++! \n ; /* Notice the blank spaces in the beginning of the first two lines, and also the missing lines. All these blank spaces are neglected by compiler. Comments may also be put in the c-style, i.e. between the symbols /* and */ as done in this comment. */ std::cout<<Are you interested?<<std::endl ; /* When you use void main() do not include the statement return 0 ; , because the void functions do not return any value */ // and compiler will show it as an error if return statement is included. }
The cin
To put in some data we make use of function cin This is used along with the operator >> which is called extraction operator. This is followed by the name of variable in whose memory block the data has to be stored. After typing the data for cin, do press the enterkey, otherwise, the computer will not proceed further.
Keyboard
Namespaces
It enables the programmer to group a set of objects or function under one name. All classes, objects and functions of C++ Standard Library are defined under namespace std . We can simplify the writing of std :: cout to cout and std :: cin to cin by including following the directive in the program: using namespace std ;
Application of namespaces
#include<iostream> Using namespace std ; // use of std namespace namespace NS1 // no semicolon at the end of line { int n = 3 ; float m = 2.5 ; int k = 2 ; double R = n*m*k ; } namespase NS2 // no semicolon at the end of line { float n = 4.0 ; // Names are same as in NS1 //but values are different . int m =2 ; double k = 3.0 ; double R = n*m*k ; } int main() { int Squatre ; int Product ; using namespace NS2 ; Square = n*n + m*m ; // values under NS2 are used Product = NS1 :: k*NS2 :: m ; // k belongs to NS1 and //m to NS2 cout << Square = <<Square<<, \t Product = <<Product<<endl; cout <<R = << NS1 :: R <<endl ; // This R belongs to NS1 cout << R = << NS2 :: R << endl ; // This R belongs to NS2 return 0 ; }
Conclusion
We tried to grasp the fundamentals of C++ programming.