Beginning with C++.

 Beginning with C++.

1] What is C++ ?

               C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA in the early 1980's. The result was C++. Therefor, C++ is an extension C with a major addition of the class construct features of Simula67.However, later in 1983, the name was changed to C++. The idea of C++ comes from the C increment operator++, thereby suggesting that is an augmented (incremented) version of C.

               During the early 1990's, the language underwent a number of improvements and changes. In November 1997 , the ANSI/ISO standards committee standardized these changes and added several new features to the language specifications.

               C++ is a superset of C. Most of what we already know about C applies to C++ programs. However, there are a few minor differences that will prevent a C program to run under C++ compiler. We shall see these differences later as and when they are encountered.

               The most important facilities that C++ adds on to C are classes, inheritance, function overloading, and operator overloading. These features enable creation of abstract data types, inherit properties from existing data types and support polymorphism, thereby making C++ a truly object-oriented language.

               The object-oriented programming features in C++ allow programmers to build large programs with clarity, extensibility and ease of maintenance, incorporating the spirit and efficiency of C. The addition of new features has transformed C from a language that currently facilitates top-down structured design, to one that provides bottom-up , object-oriented design.

2] Application Of C++.

               C++ is a versatile language for handling very large programs. It is suitable for virtually any programming task including development of editors, compilers, databases, communication systems and any complex real-life application systems. 

  • Since C++ allows us to create hierarchy-related objects, we can build special object-oriented libraries which can be used later by many programmers.
  • While C++ is able to map the real-world problem properly, the C part of C++ gives the language the ability to get close to the machine-level details.
  • C++ programs are easily maintainable and expandable. When a new feature needs to be implemented . It is very easy to add to the existing structure of an object.
  • It is expected that C++ will replace C as a general-purpose language in the near feature.
3] A SIMPLE C++ PROGRAM.
☺Let us begin with a simple example of a C++ program that prints a string on the screen.
👉Printing string :
               #include<iostream>                             // include header file
               using namespace std;
               int main()
               {
                         cout<<"C++ is better than C.\n"; // C++ statement
                         return 0;
               }                                                                 // End of example
☺This simple program demonstrates several C++ features.
👉Program Features:
               Like C, the C++ program is a collection of functions. The above example contains only one function, main(). As usual, execution begins at main(). Every C++ program must have a main(). C++ is a free-form language. With a few exceptions, the compiler ignores carriage returns and white spaces. Like C, the C++ statements terminate semicolons.
👉Comments:
               C++ introduces a new comment symbol // (double slash). Comments start with a double slash symbol and terminate at the end of the line. A comments may start anywhere in the line, and whatever follows till the end of the line is ignored. Note that there is no closing symbol.
               
               The double slash comment is basically a single line comments. Multiline comments can be written as follows:
               // This is an example of 
               // C++ program to illustrate 
               // Some of its features 
               The C comment symbol /*,/* are still valid and are more suitable for multiline comments. The following comments is allowed:
               /* This is an example of 
                    C++ program to illustrate 
                    some of its features
               We can use either or both styles in our programs. Since this is a book on C++ , we will use only the C++ style. For example , the double slash comment cannot be used in the manner as shown below:
               for(j=0; j<n; /* loops n times */ j++)
👉Output Operator:
               cout << "C++ is better than C.";
               causes the string in quotation marks to be displayed on the screen. Here, the standard output stream represents the screen. It is also possible to redirect the output to other output devices. We shall later discuss streams in detail.
     
               The operator << is called the insertion or put to operator. It inserts the contents of the variable on its right to the object on its left.

                The object cout has a simple interface. If string represents a string variable ,then the following statement will display is contents.
                cout << string;
               
               It is important to note that we can still use printf() for displaying an output C++ accepts this notation. However, we will use cout<<to maintain the spirit of C++.

👉The iostream File:
                A C++ program typically contains preprocessor directive statements at the beginning. Such statements are preceded with a # symbol to indicate the presence of a preprocessor directive to the compiler , which in turn lets the preprocessor handle the # directive statement.
               All C++ programs begin with a #include directive that includes the specified header file contents into the main program.
We have used the following #include directive in the program:
               #include <iostream>
               The header file iostream should be included at the beginning of all programs that use input/output statements. Note that the naming conventions for header files may vary. Some implementations use iostream.hpp; yet others iostream.hxx. We must include appropriate header files depending on the contents of the program and implementation.

👉Table 1: Commonly used old style header files:
👉Table 2: New header files included in ANSI C++:

               Apart from #include, the other preprocessor directives such as #define , #error etc. Work the same way as they would in C.But, their usage in C++ is quite limited due to the availability of other means for achieving the same functionality.

👉Namespace:
               Namespace is a new concept introduced by the ANSI C++ standards committee. For using the identifiers defined in the namespace scope, we must include the using directive , like

               using namespace std;

               Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI C++ programs must include this directive. This will bring all the identifiers defined in std to the current global scope. using and namespace are the new keywords of C++. 

👉Return Type of main()
               In C++, main() returns an integer type value to the operating system. Therefore, every main() in C++ should end with a return(0) statement, otherwise a warring or an error might occur. Since main() returns an integer type value, return type for main() is explicitly specified as int. Note that the default return type for all functions in C++ is int. The following main without type and return will run with a warning.
               main()
               {
                    .....
                    .....
               }

4] More C++ Statements.
               Let us consider a slightly more complex C++ program. Assume that we would like to read two numbers from the keyboard and display their average on the screen. C++ statements to accomplish this is shown in program:
👉Example:
               #include <iostream>
               using namespace std;
               int main()
               {
                         float number1, number2, sum, average;
                         cout << "Enter two numbers:";  // prompt
                         cin >> number1;                         // Reads numbers
                         cin >> number2;                         // from keyboard

                         sum = number1 + number2;
                         average = sum/2;
            
                         cout << "Sum = " << sum << "\n";
                         cout << "Average = " << average << "\n";
              
                         return 0;
               }
               Output:
               Enter two numbers: 6.5, 7.5
               Sum = 14
               Average = 7  

👉Variables:
               The program uses four variables, namely number1, number2, sum, and average. They are declared as type float by the statement.

               float number1, number2, sum, average;

               All variables must be declared before they are used in the program.

👉Input Operator:
     The statement
          cin >> number1;
     is an input statement and causes the program to wait for the user to type in a number. The number keyed in is placed in the variable number1. The identifier cin is a predefined object in C++ that corresponds to the standard input stream. Here, this stream represents the keyboard.
               The operator >> is known as extraction or get from operator. It extracts the value from the keyboard and assigns it to the variable on its right . This corresponds to the familiar scanf() operation. Like  <<, the operator >> can also be overloaded.

👉Cascading of I/O Operators:
               We have used the insertion operator << repeatedly in the last two statements for printing results.

               cout << "Sum = " << "\n";

               The statement first sends the string "Sum =" to cout and then sends the value of sum. Finally, it sends the newline character so that the next output will be in the new line. The multiple use of << in one statements is called cascading. When cascading an output operator, we should ensure necessary blank spaces between different items. Using the cascading technique, the last two statements can be combined as follows:
               cout << "sum = " << sum << "\n";
                       << "Average = " << average << "\n";
               This one statements but provides two lines of output. If you want only one line of  output, the statement will be:
               cout << "Sum = " << sum << ","
                       << "Average = " << average << "\n";
               
☺The output will be:
               Sum = 14, Average = 7
☺We can also cascade input operator>>as shown below:
               cin >> number1 >> number2;
               
               The values are assigned from left to right. That is, if we key in two values, say, 10 and 20, then 10 will be assigned to number1 and 20 to number2.

5] An Example With Class.
    
               One of the major features of C++ in classes. They provide a method of binding together data and functions which operate on them. Like structures in C, classes are user-defined data types.

👉Example:
               #include<iostream>
               using namespace std;
               class person
               {
                         char name[30];
                         int age;

                         public:
                              void getdata (void);
                              void display (void);
               };
               void person :: getdata (void)
               {
                         cout << "Enter name: ";
                         cin >> name;
                         cout << "Enter age: ";
                         cin >> age;
               }
               void person :: display (void)
               {
                         cout << "\nName: " << name;
                         cout << "\nAge: " << age;
               }
               int main()
               {
                         person p;
                         p.getdata();
                         p.display();

                         return 0;
               } 
               
               Output:
               Enter Name: Riya
               Enter age: 18

               Name: Riya
               Age: 18
  
6] Structure of C++ programming.

               As it can be seen from the program, a typical C++ program would contain four sections as shown in figer. These section may be placed in separate code files and then compiled independently or jointly.

👉Figer 1:
  
               It is a common practice to organize a program into three separate files. The class declarations are placed in a header file and the definitions of member functions go into another file. This approach enables the programmer to separate the abstract specification of the interface from the implementation details. Finally , the main program that uses the class is placed in a third file which 'includes' the previous two files as well as any other files required.

👉Figer 2:

               This approach is based on the concept of client-server model as shown in figer2 . The class definition including the member functions constitute the server that provides services to the main program known as client. The client uses the server through the public interface of the class.

7] Compiling And Linking.

               The process of compiling and linking again depends upon the operating system. A few popular systems are discussed in this section.

👉Unix AT&T C++:
               The process of implementation of a C++ program under UNIX is similar to that of  a C program. We should use the "CC" command to compile the program. Remember, we use lowercase "CC" for compiling C programs . The command

               CC  example. C

at the UNIX prompt would compile the C++ program source code contained in the file example.C. The compiler would produce an object file example.o and then automatically link with the library functions to produce an executable file. The default executable file name is a.out.

  A program spread over multiple files can be compiled as follows:
               
               CC  file1. c  fule2. o
          
               The statement compiles only the file file1.C and links it with the previously compiled file2.o file. This is useful when only one of the files needs to be modified . The files that are not modified need not be compiled again.

👉Turbo C++ and Borland C++ :
      
               Turbo C++ and Borland C++ provide an intergrated program development environment under MS DOS. They provide a built-in editor and a menu bar which includes options such as file, Edit, compile and run.

               We can create and save the source files under the File Option and edit them under the Edit option . We can then compile the program under the Compile Option and execute it under the Run option . The Run option can be used without compiling the source code. In this case , the RUN command causes the system to compile, link and run the program in one step. Turbo C++ being the most popular compiler , creation and execution of programs under Turbo C++ system are discussed in detail in Appendix B.

👉Visual C++ :
               It is a Microsoft application development system for C++ that runs under Windows. Visual C++ is a visual programming environment in which basic program components can be selected through menu choices, buttons, icons, and other predetermined methods. Development and execution of C++ programs under Windows are briefly explained in Appendix C.

👉KEY TERMS:
 

 
8] Creating The Source Life.

               Like C programs, C++ programs can be created using any text editor. For example, on the UNIX, we can use vi or ed text editor for creating and editing the source code. On the DOS system. we can use edlin or any other editor available or a word processor system under non-document mode.

               Some systems such as Turbo C++ provide an integrated environment for developing and editing programs. Appropriate manuals should be consulted for complete details.

               The file name should have a proper file extension to indicate that it is a C++ program file. C++ implementations use extensions such as .c, .C, .cc, .cpp and .cxx . Turbo C++ and Borland C++ use .c for C programs and .cpp for C++ programs. Zortech C++ system uses .cxx while UNIX AT&T version uses .C and .cc . The operating system manuals should be consulted to determine the proper file name extensions to be used.         
               



Comments