• Now Online : 20
  • admin@codemyne.net

Introduction to C++

Object orientation and object oriented programming languages are playing an increasingly important role in the computing industry. With the gradual decline in hardware costs, the cost of the computing systems is largely dominated by software.

More powerful tool, operating systems and programming languages are evolving to keep up with the pace of hardware development. Software for different applications needs to be developed under these environments, which is complex process. As a result, the relative cost of software (both development and maintenance) is increasing substantially when compared to the cost of the hardware of computing system.

Software maintenance is a process of modifying or extending the capabilities of the existing software. It requires mastery over the understanding and modifying the existing software, and finally revalidating the modified software.

The Software crisis, right from the beginning, is providing an imputes for the development of software engineering principles, tools and better programming paradigms to build more reliable reusable systems. The sate-of –the-art solution to overcome software crisis is the object oriented paradigm.

Getting acquainted with C++

Object oriented programming is a new way of solving problems with computers instead of trying to mod the problem into something familiar to the computer, the computer is adapted to the problem. Object oriented programming is designed around the data being operated up on as opposed to the operations themselves. Instead of making certain types of data fit into specific and rigid computer operations, these operations are designed to fit to the data. This is an it should be, because the sole purpose of a computer program is to manipulate data.

Beginning with C++

C++ is an Object oriented programming language. Initially named ‘C with classes’, C++ was developed by Bjarnestroustrup at AT&T Bell laboratories in the early eighties. The features of c++ were combination of the objectoriented features of a language called simula 67 and the power and elegance of C. Therefore c++ is extension of c with major addition of the class construct features of simula 67.

Some of the most important features that c++ adds on a c language are classes, function over loading, and operator overloading. These features enable developers to create abstract classes, inherit properties from existing classes and support polymorphism.

C++ is a versatile language for handling varies 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.

The First program “Hello World” on screen

#include<iostream.h>
Void main()
{ 
    Cout<<"Hello World "; // C ++ Statemet
}                       

Program Features

Like C, the C++ program is a collection of function. In the case, the program contains only one function, Main(). Every program must have a main(), which is where execution starts. As in c, a statement ends with a semicolon.

Comments

In c++, A new symbol for comments ‘//’ (double slash) is introduced .Comments may start with n double slash symbol and terminate at the end of the line. A comment may start anywhere in a line and whatever follows the symbol is ignored. Note that there is no closing symbol.

We can use Multiline comments also,Multiline comments will be like

/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/                            

It consists starting and ending symbol.

The output parameter

The only statement in the above example is the output statement. The statement
Cout<<'Hello World';

Causes the string “Hello World” to be displayed on the screen. This statement introduces two features specific to c++, Cout and '<<'. The identifier cout is a predefined object that represents the standard output stream in c++. A stream is an abstraction that refers to a flow of data. Here, the standard output stream or device represents the screen. It is also possible to redirect the output to other output device.

The operator '<<' (the bitwise left shift operator in c) is called the insertion or put to operator. This multiplicity in the behavior of an operator is one of the special features of c++, which will be discussed in the later parts of this book. The insertion operator inserts the contentsof the variable on its right to the object on its left.

The operator '<<' has a simple in interface, i.e. it is not necessary to specify the data type of the variable on its right (as was the case while sing the printf()function). The insertion operator automatically “assumes” the data type of the variable. One advantage of this is that user-defined data types can be used with this operator, which was not possible with the printf() function.

For example, if var1 is a is a variable of a data type defined by the user, say a structure, it cannot be printed on the screen as a whole using prient {}. The individual members have to display separately. But with cout, it is possible to display the structure as a whole.

The #include directive

The #include directive instructs the compiler to read another source file in addition to the one that contains the #include directive. The name of the additional source file must be enclosed between double quotes or angle brackets. For example,

#include”iostream.h”
#include<iostream.h>                       

Causes the contents of the file iostream.h to be added to the program in which it is included. It contains declaration. For the identifier cout and the operator '<<'. This header file must be include at the beginning of all programs that use input/output statements.

Input Using Cin

Like cout, the identifier cin (pronounced as “see in”) is a predefined object that corresponds to the standard input stream, which in this case represents the keyboard.

The following program illustrates the usage of cin

// this program demonstrates the usage of the cin operator
// this program takes a string as inpur and display it 
#inclde <iostream.h>
void main()
{
charstring [50];
    cout<<"enter the string to be displayed \n";//prompts for
    cin>>string;
    cout<<"\n"<<string ;
}

The “>>” (bitwise right-side operator in c) or the extraction operator (Used with cin) works similar to the %s specifies as used with the scanf() function. When the program prompts for input, try entering “this is a string” when the string i9s redisplayed, only the word this will be displayed. The rest of the string is not shown because “>>” stops reading input when the first white space is encountered. Thus, “is a string” is never read by the program.

In next article c++ keywords, datatypes, variables, type qualifiers are explained

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment