👉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.
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
Post a Comment