History of C++

Embed Size (px)

DESCRIPTION

History of C++C++ is an object oriented programming language. It is developed by Bajarne Stroustrup at AT & T Bell’s laboratory in Murray hills New jersey USA in early 1980s. Stroustrup was an admirer of Simula 67 and a strong supporter of ‘C’ wanted to combine the best of both the languages & create a more powerful language that could support object oriented programming (OOP) & still retained the power and alligence of ‘C’. The result was C++. Therefore C++ is an extension of ‘C’ with major addition of the class construct feature of Simula 67.

Citation preview

History of C++

History of C++C++ is an object oriented programming language. It is developed by Bajarne Stroustrup at AT & T Bells laboratory in Murray hills New jersey USA in early 1980s. Stroustrup was an admirer of Simula 67 and a strong supporter of C wanted to combine the best of both the languages & create a more powerful language that could support object oriented programming (OOP) & still retained the power and alligence of C. The result was C++. Therefore C++ is an extension of C with major addition of the class construct feature of Simula 67. Since the class was a major addition to the original C language Stroustrup initially called the new language C with classes. Later in 1963 the name changed to C++. C++ comes from C increment operator ++. During the early 1990s the language underwent a no. of improvements and changes. In November 1997 NC-ISO standard committee standardized these changes & added several new features to the language.Specification:

The object oriented features of C++ allows programmers to built large programme with clarity, extensibility & ease of maintenance incorporating the spirit and efficiency of C.

Applications of C++ C++ is a versatile language for handling very large program. It is suitable for virtuality, any programming task including development of editor, compiler, database, communication system & any complex real life application system.

Since C++ allows us to create hierarchy related objects we can built special object oriented libraries which can be used later by many programs.

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 machine level details. C++ programs are easily maintainable & 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 a near future.Striking features of OOPS Emphasis on data rather than procedure.

Programs are divided into classes & classes can be used with the creation of objects.

Data structures are designed such that they characterized the objects.

Functions that operate on that data of an object are tied together in the data structure.

Data hidden cannot be accessed by external functions. Objects may communicate with each other through functions.

New data & functions can be easily added whenever necessary. Follows bottom-up approach in program design.

Basics of OOPS

Objects

Classes

Data Abstraction and Encapsulation

Inheritance

Polymorphism

Dynamic Binding

Message Passing

Objects: The object is often instance of class. Objects are the basic runtime entities in an object oriented system. They may represent a person, a place, a bank account or any item that program has to handle. Programming problem is analyzed in terms of objects & the nature of communication between them. Program objects should be chosen such that they match closely with real world objects. Objects take up place in the memory & have an associated address like a record in PASCAL or a structure in C. When a program is executed the objects interact by sending message to one another. Each object contains data & code to manipulate the data. The objects can interact without having to know details of each others data or code. It is sufficient to know the type of message excepted & type of response returned by the objects. Classes: Objects maintain data & code to manipulate that data. The entire set of data & code of an object can be made a user defined data type with the help of class. Infact, objects are variables of type class or objects are members of class. Once a class has been defined we can create any no. of objects belonging to that class. Each object is associated with the data of type class with which they are created. A class is thus a collection of object of similar type. Classes are user defined data types and behave like the built in types of the programming language. The syntax used to create an object is no different than syntax used to create an integer object in C.

Eg: int a, b, c;

student s1, s2, s3;

Data Abstraction and Encapsulation: The wrapping of a data and function into a small unit (called class) is known as encapsulation. Data encapsulation is most striking feature of a class. The data is not accessible to the outside world and only those functions which are wrapped in the class can access data. This functions provide the interface between the objects, data and the program. This insulation of the data from the direct access by the program is called data hiding or information hiding.Abstraction: It refers to the act of representing the features without including the background details or explanation. Classes use the concept of abstraction and are defined as a list of abstract attributes.

Eg: Size, weigh or costs and functions to operate on these attribute. They encapsulate all the essential properties of the objects that are to be created. The attributes are sometimes called data members because they hold information. The function that operate on these data are called methods or member functions. Since the classes use the concept of data abstraction, they are known as abstract data type. Inheritance: This is a process by which objects of one acquire the properties of objects of another class. It supports the concept of hierarchical classification. In OOPS the concepts of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from existing one. The new class will have the combined features of both the classes. The real appeal and power of the inheritance mechanism is that it allows the programmer to reuse a class i.e. almost but not exactly what he wants and to tackle the class in such a way that it does not introduce any undesirable side effects into the rest of the classes. Polymorphism: It is another important OOPS concept. It is a Greek word means the ability to take more than one form and operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. The process of making an operator to exhibit different behavior in different instances is known as operator overloading.E.g. A single name can be used to handle different number and different type of arguments.

Shape

Draw ( )

Polymorphism plays important role in allowing the objects having different internal structures to share same external interface. This means that general class of operation may be accessed in the same manner. Even though specific action associated with each operation may differ. Dynamic Binding: Binding refers to linking of procedure called to the code to be executed and response to the call. Dynamic binding also known as late binding means that the code associated with a given procedure call is not known until the time of the call is not known until the time of the call at runtime. It is associated with polymorphism reference. A function call associated with a polymorphic reference depends on a dynamic time of that reference i.e. at runtime the code, the object and their current reference will be called.

Message Passing: An OOPS consists of set of objects that communicate with each other. The process of programming in an object-oriented language therefore involves the following basics steps:1) Creating class: That defines objects and their behavior.2) Creating objects from the class definition.3) Establishing communications among objects.Objects communicates with one another by sending and receiving information much the same way as the people pass message to one another. The concept of message passing makes it easier to talk about building systems that directly modal or simulate their real world counter parts. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired result. Message passing involves specifying the name of the object, the name of the function(message) and information to be send.

NameSpaceIt is the new concept introduced by NCC++ standard and committee. This defines a scope for the identifier that are used in a program. For using the identifier defined in the namespace scope are must include the using directive.Syntax: Using namespace std;

Here, std is the namespace where NCC++ std class libraries are defined. All NCC++ programs must include this directive. This will bring all the identifier defined in std to the current global scope. Using and namespace are the new keywords of C++.Tokens: The smallest individual units in the program are known as token. C++ has the following token:1) Keyword

2) Identifier

3) Constants

4) String

5) Operators

Keywords: ASM, AUTO, BREAK, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, inline, int, long, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, virtual, void, volatile and while.

Basic Datatypes: C++ Datatypes

arrayStructurefunctionUnionpointerClassreferenceenumTypeSizeRange

Char1-128 to 127

Unsigned char10 to 255

Signed char1-128 to 127

int 2-32768 to 32767

Unsigned int20 to 65535

Signed int2-32768 to 32767

Long int4-2147483648 to 2147483647

Signed long int40 to 429496795

Unsigned long int40 to 429496795

Float43.4E-38 to 3.4E+38

Double81.4E-308 to 1.7E+308

Long double103.4E-4942 to 1.1E4932

Reference Variable:C++ introduces a new kind of variable known as reference variable. It provides an alternative name for the previously defined variable.

Syntax: datatype &reference name=variable name

E.g. float total=100; float &sum=total;

total is the float type variable that has already been declared. sum has an alternative name declared to represent the variable total. Both the variable refer to the same data object in the memory.//p1.cpp

Operators::Scope resolution operator

::*Pointer to member declarator

->*Pointer to member operator

.*Pointer to member operator

DeleteMemory release operator

EndlLine feed operator

NewMemory allocation operator

SetwField width operator

//p2.cpp

Like C, C++ is also a block structured language. Blocks and scopes can be used in constructing programs. We know that same variable name can be used to have differentiation meanings in differentiation blocks. The scope of the variable extent from the points of its declaration till the end of the block containing the declaration. A variable declared in site of block is said to be local to that block. In C, global version of the variable cannot be accessed from within the inner block. C++ resolves this problem by introducing a new operator called the scope resolution operator. This can be used to uncover the hidden variable. This operator allows access to the global version of the variable in the inner block.Member Dereferencing OperatorsC++ permits to define a class containing various types of data and functions as members. C++ also permits to access the class members through pointers.

OperatorsFunctions

::*To declare a pointer to a member of a class.

*To access a member using object name and a pointer to that member.

->*To access the member using a pointer to the object and a pointer to that member.

Memory Management Operator New

Delete

All the functions in C used to allocate memory dynamically at runtime are also supported by C++. It also defines two unary operators new and delete that perform the task of allocation and freeing the memory in a better and easier way. Since these operators manipulate memory on the free store they are also known as free store operator. An object can be created by using new, destroyed by using delete as and when required.ManipulatorManipulators are the operators that are used to format the data display. The most commonly used manipulators are endl and setw.

The endl manipulator when used in an output statement ends the line and goto next line, setw is used to set the width.ObjectsInitially, different parts (entities) of a problem are examined independently. These entities are chosen because they have some physical or conceptual boundaries that separate them from the rest of the problem. The entities are then represented as objects in the program. The goal is to have a clear correspondence between physical entities in the problem domain and objects in the program.

In other words, an object can be a person, a place, or a thing with which the computer must deal. Some objects may correspond to real-world entities such as students, employees, bank accounts, inventory items, etc. Whereas, others may correspond to computer hardware and software components. Hardware components include a keyboard, port, video display, mouse etc and software components include stacks, queues, trees etc.Objects mainly serve the following purposes:

Understanding of the real world and a practical base for designers.

Decomposition of a problem into objects depends on judgment and nature of the problem.Every object will have data structures called attributes and behavior called operations.

The different notations of an object uniting both the data and operations are as shown:

Object Name

Attribute 1

Attribute 2

|

|

Attribute N

Operation 1

Operation 2

|

|

Operation N

Object Name

Attribute 1Attribute 2

|

|

Attribute N |

|

Account

AccountNumber

AccountType

Name

Balance

Deposit()

Withdraw()

Enquire()

Account

AccountNumberAccountTypeName

Balance

ClassesThe objects with the same data structure (attributes) and behavior (operations) are grouped into a class. All those objects possessing similar properties are grouped into the same unit.

Person Objects Abstract Person Class

Boy Into Attributes: Name, Age, Gender

Girl Operations: Speak( ), Listen( ),

Walk( ).Vehicle Objects Abstract Vehicle Class

Car Into Attributes: Name, Model, Color Operations: Start ( ), Stop ( ),

Accelerate ( ).Polygon Objects Abstract Polygon Class

Triangle Into Attributes: Vertices, Border,

Hexagon Color, Fillcolor.

Operations: Draw(), Erase(),

Move().In the case of Person class, all objects have similar attributes like Name, Age, Gender and similar operations like Speak, Listen, Walk. So boy and girl objects are grouped into the Person class. Similarly, other related objects such as triangle, hexagon, and so on, are grouped into the Polygon class.

Every object is associated with data and functions which define meaningful operations on that object. For instance, in C++, related objects exhibiting the same behavior are grouped and represented by a class in the given way:class account

{

private: char Name[20]; //data members

int AccountType;

int AccountNumber;

float Balance;

public: Deposit( ); //member functions

Withdraw( );

Enquire( );

};

This declaration is similar to the structure declaration in C. It enables the creation of the class variables called objects.

For E.g. the following statements,

account savings_account;

account current_account;

account fd_account;

create instances of the class account. They define savings_account, current_account and fd_account as the objects of the class account.Each class describes a possibly infinite set of individual objects; each object is said to be an instance of its class and each instance of the class has its own value for each attribute but shares the attribute name and operations with other instances of the class. The following points on classes can be noted: A class is a template that unites data and operations.

A class is an abstraction of the real world entities with similar properties. A class identifies a set of similar objects.

Ideally, the class is an implementation of abstract data type.

Encapsulation and Data AbstractionEncapsulation is a mechanism that associates the code and the data it manipulates and keeps them safe from external interference and misuse.

Creating new data types using encapsulated-items, that are well suited to an application to be programmed, is known as data abstraction. The data types created by the data abstraction process are known as Abstract Data Types (ADTs). Data abstraction is a powerful technique, and its proper usage will result in optimal, more readable, and flexible programs.

//Printing Hello World message in C#include

void main()

{

printf(Hello World);

}

OutputHello World

The Hello World program will also work in C++, since it supports the ANSI-C function library. However, the program could be rewritten using C++ streams. The C++ equivalent of the Hello World program is listed in the program.#includevoid main()

{

cout