1

CHAPTER

Introduction to C++

C
H
A
P
T
E
R

O
U
T
L
I
N
E
—•  1.1 Evolution of C++
—•  1.2 ANSI Standard
—•  1.3 Object-Oriented Technology
—•  1.4 Disadvantage of Conventional Programming
—•  1.5 Programming Paradigms
—•  1.6 Preface to Object-Oriented Programming
—•  1.7 Key Concepts of Object-Oriented Programming
—•  1.8 Advantages of OOP
—•  1.9 Object-Oriented Languages
—•1.10 Usage of OOP
—•1.11 Usage of C++

 

1.1 EVOLUTION OF C++

C++ is an object-oriented programming language and is considered to be an extension of C. Bjarne Stroustrup at AT&T Bell Laboratories, Murray Hill, New Jersey (USA) developed it in the early eighties of twentieth century. Stroustrup, a master of Simula67 and C, wanted to combine the features of both the languages into a more powerful language that could support object-oriented programming with features of C. The outcome was C++ as per Figure 1.1. Various ideas were derived from SIMULA67 and ALGOL68. Stroustrup called the new language ‘C with classes’. However, in 1983, the name was changed to C++. The thought of C++ came from the C increment operator ++. Rick Mascitti coined the term C++ in 1983. Therefore, C++ is an extended version of C. C++ is a superset of C. All the concepts of C are applicable to C++ also.

For developing complicated applications object-oriented languages are most convenient and easy. Hence, a programmer must be aware of the new concepts of object-oriented techniques.

1.2 ANSI STANDARD

ANSI stands for American National Standards Institute. This Institute was founded in 1918. The main goal for establishing this Institute was to suggest, reform, recommend, and publish standards for data processing in the USA. This committee sets up standards for the computer industry.

The recognized council working under the procedure of the American National Standard Institute (ANSI) has made an international standard for C++. The C++ standard is also referred to as ISO (International Standard Organization) standard. The process of standardization is gradual and the first draft of the planned ANSI standard was made on 25 January 1994. This book will continue to refer to ANSI standard code, which is more commonly used. The ANSI standard is an attempt to ensure that C++ is portable.

images

Fig. 1.1 Evolution of C++

1.3 OBJECT-ORIENTED TECHNOLOGY

Nature is composed of various objects. Living beings can be categorized with different objects.

Let us consider an example of a teaching institute having two different working sections. They are teaching and non-teaching. Further sub-grouping of teaching and non-teaching can be made for the ease of management. The various departments of any organization can be thought as objects working with certain goals and objectives.

Usually, an institute has faculties for different departments. Also, laboratory staff for the assistance in conducting of practicals and site development section for beautification of the campus are essential. The Principal is a must for the overall management of the Institute. The accounts department is also required for handling monetary transactions, and salaries of the employees. The sports section is entrusted the responsibility of sports activities, the Registrar for administration and his staff for dealing with administrative-matters of the Institute and so on are required. Each department has an In-Charge who has clear-cut responsibilities. Every department has its own work as stated above. When the work is distributed to different departments as shown in Figure 1.2, it becomes easy to accomplish goals and objectives. The activities are carried out smoothly. The burden of one particular department has to be shared among personnel. The staff in the department are controlled properly and act according to the instructions laid down by the management. The faculty performs activities related to teaching. If higher authority needs to know the details regarding the theory, practical, seminar and project workload of individuals of the department then some responsible person from the department furnishes the same. This way some responsible person from the department accesses the data and provides the higher authority with requisite information. Only authorized persons have access to data base.

As shown in Figure 1.2 an institute is divided into different departments such as library, classroom, computer laboratory, etc. Each department performs its own activities in association with other departments. This theory of objects can be extended to every walk of life and can be implemented with the help of software. In general, objects are in terms of entities.

images

Fig. 1.2 Relationship between different sections

1.4 DISADVANTAGE OF CONVENTIONAL PROGRAMMING

Traditional programming languages such as COBOL, FORTRAN etc. are commonly known as procedure oriented languages. The program written in these languages consists of a sequence of instructions that tells the compiler or interpreter to perform a given task. When program code is large then it becomes inconvenient to manage it. To overcome this problem, procedures or subroutines were adopted to make a program more understandable to the programmers. A program is divided into many functions. Each function can call another function as shown in Figure1.3. Each function has its own task. If the program is too large the function also creates problems. In many programs, important data variables are declared as global. In case of programs containing several functions, every function can access the global data as simulated in Figure 1.4. Generally in a program of large size, it becomes difficult to understand the various data used in different functions. As a result, a program may have several logical errors.

Following are the drawbacks observed in monolithic, procedural, and structured programming languages:

(1) Large size programs are divided into smaller programs known as functions. These functions can call one another. Hence, security is not provided.

(2) Importance is not given to security of data but on doing things.

(3) Data passes globally from one function to another.

(4) Most functions have access to global data.

images

Fig. 1.3 Flow of functions in non-OOP languages

images

Fig. 1.4 Sharing of data by functions in non-OOP languages

1.5 PROGRAMMING PARADIGMS

(1) Monolithic Programming

(A) In monolithic programming languages such as BASIC and ASSEMBLY, the data variables declared are global and the statements are written in sequence.

(B) The program contains jump statements such as goto, that transfer control to any statement as specified in it. Figure 1.5 shows a program of monolithic type. The global data can be accessed from any portion of the program. Due to this reason the data is not fully protected.

images

Fig. 1.5 Program in monolithic programming

(C) The concept of subprograms does not exist and hence useful for smaller programs.

(2) Procedural Programming

(A) In procedural programming languages such as FORTRAN and COBOL, programs are divided into a number of segments known as subprograms. Thus it focuses on functions apart from data. Figure 1.6 describes a program of procedural type. It shows different subprograms having access to the same global data. Here also, the data is not fully protected.

(B) The control of program is transferred using unsafe goto statement.

(C) Data is global and all the subprograms share the same data.

(D) These languages are used for developing medium size applications.

(3) Structured Programming

(A) In structured programming languages such as PASCAL and C larger programs are developed. These programs are divided into multiple submodules and procedures.

(B) Each procedure has to perform different tasks.

(C) Each module has its own set of local variables and program code as shown in Figure 1.7. Here, the different modules are shown accessing the global data.

(D) User-defined data types are introduced.

images

Fig. 1.6 Program in procedural programming

1.6 PREFACE TO OBJECT-ORIENTED PROGRAMMING

The prime factor in the development of object-oriented programming approach is to remove some of the shortcomings associated with the procedure oriented programming. OOP has data as a critical component in the program development. It does not let the data flow freely around the systems. It ties data more firmly to the functions that operate on it and prevents it from accidental change due to external functions. OOP permits us to analyze a problem into a number of items known as objects and then assembles data and functions around these items as shown in Figure 1.8. The basic objective of OOP is to treat data and the program as individual objects. Following are the important characteristics of object-oriented programming:

images

Fig. 1.7 Program in structured programming

images

Fig. 1.8 Relation between data and member function in OOP

(1) OOP pays more importance to data than to function.

(2) Programs are divided into classes and their member functions.

(3) New data items and functions can be comfortably added whenever essential.

(4) Data is private and prevented from accessing external functions.

(5) Objects can communicate with each other through functions.?

OBJECT-ORIENTED PROGRAMMING

Object-oriented programming language is a feature that allows a mode of modularizing programs by forming separate memory area for data as well as functions that is used as object for making copies of modules as per requirement.

1.7 KEY CONCEPTS OF OBJECT-ORIENTED PROGRAMMING

There are several fundamental or key concepts in object-oriented programming. Some of these are shown in Figure 1.9 and are discussed below:

(1) Objects

OBJECT

Objects are primary run-time entities in an object-oriented programming.

Objects are primary run-time entities in an object-oriented programming. They may stand for a thing that has specific application for example, a spot, a person, any data item related to program, including user-defined data types. Programming issues are analyzed in terms of object and the type of transmission between them. Figure 1.10 describes various objects. The selected program objects must be similar to actual world objects. Objects occupy space in memory. Every object has its own properties or features. An object is a specimen of a class. It can be singly recognized by its name. It declares the state shown by the data values of its characteristic at a specific time. The state of the object varies according to procedure used. It is known as the action of the object. The action of the object depends upon the member function defined within its class.

images

Fig. 1.9 Features of object-oriented programming

Given below are some objects that we use in our daily life.

images

Fig. 1.10 Commonly available objects

(2) Classes

CLASS

A class is grouping of objects having identical properties, common behavior, and shared relationship.

A class is the accomplishment of abstract data type. It defines the nature and methods that act on the data structure and abstract data type respectively. Specimens are also known as objects. In other words, a class is a grouping of objects having identical properties, common behaviour, and shared relationship. The entire group of data and code of an object can be built as a user-defined data type using class. Objects are nothing but variables of type class. Once a class has been declared, the programmer can create a number of objects associated with that class. The syntax used to create an object is similar to the syntax used to create an integer variable in C. A class is a model of the object. Every object has its own value for each of its member variables. However, it shares the property names or operations with other instances of the class. Thus, classes define the characteristics and actions of different objects.

images

Fig. 1.11 Objects and their properties

In Figure 1.11 some common objects, their properties, and the actions they perform have been described. A car can be distinguished from another car on the basis of its properties, for example, company, model, color, capacity etc. and also on the basis of its actions for example, speed, average, break etc.

Figure 1.12 describes different classes and related data that come under them.

(3) Method

METHOD

An operation required for an object or entity when coded in a class is called a method.

An operation required for an object or entity when coded in a class is called a method. The operations that are required for an object are to be defined in a class. All objects in a class perform certain common actions or operations. Each action needs an object that becomes a function of the class that defines it and is referred to as a method.

In Figure 1.13, the class, its associated data members, and functions are shown in different styles. This style is frequently used while writing a class in the program.

The class A contains private data members and public methods or member functions. Generally, the data members are declared private, and member functions are declared public. We can also define public data members and private member functions. The data member of any class uses its member functions or methods to perform operations.

images

Fig. 1.12 Classes and their members

images

Fig. 1.13 Representation of methods in different manners

(4) Data Abstraction

DATA ABSTRACTION

Abstraction directs to the procedure of representing essential features without including the background details.

Abstraction directs to the procedure of representing essential features without including the background details. Classes use the theory of abstraction and are defined as a list of abstract properties such as size, cost, height, and few functions to operate on these properties. Data abstraction is the procedure of identifying properties and methods related to a specific entity as applicable to the application.

A powerful method to achieve abstraction is by the manipulation of hierarchical classifications. It permits us to break the semantics of multiple systems into layers, by separating them into multiple controllable parts. For example, a computer as shown in Figure 1.14 is made of various parts such as cpu, keyboard, and so on. We think it as a single unit, but it has several sub units. These units when combined together perform the required task. By assembling subparts we can make a single system.

images

Fig. 1.14 Computer and its parts

Hierarchical abstraction of complicated systems can also be used in computer software. The data from conventional procedure oriented programs can be converted by abstraction mechanism into its partial objects. A series of operation steps may develop a set of messages between these objects. Each object shows its own attributes.

(5) Encapsulation

ENCAPSULATION

The packing of data and functions into a single component is known as encapsulation.

C++ supports the features of encapsulation using classes. The packing of data and functions into a single component is known as encapsulation. The data is not accessible by outside functions. Only those functions that are able to access the data are defined within the class. These functions prepare the interface between the object's data and the program. With encapsulation data hiding can be accomplished. Data hiding is an important feature. By data hiding an object can be used by the user without knowing how it works internally.

In C++, the fundamental of encapsulation is class. A class defines the structure of data and member functions. It is common to all its objects. Class is a logical structure whereas an object is a physical quantity. The goal of the class is to encapsulate complication. The class also has mechanism for hiding the data. Each member in the class may be private or public. Any non-member function cannot access the data of the class. The public section of the class must be carefully coded not to expose the inner details of the class. Figure 1.15 explains different sections of encapsulation.

(6) Inheritance

INHERITANCE

Inheritance is the method by which objects of one class get the properties of objects of another class.

images

Fig. 1.15 Encapsulation: Private and Public sections

Inheritance is the method by which objects of one class get the properties of objects of another class. In object-oriented programming, inheritance provides the thought of reusability. The programmer can add new properties to the existing class without changing it. This can be achieved by deriving a new class from the existing one. The new class will possess features of both the classes. The actual power of inheritance is that it permits the programmer to reuse a class that is close to what he wants, and to tailor the class in such a manner that it does not bring any unwanted incidental result into the rest of the class. Thus, inheritance is the feature that permits the reuse of an existing class to make a new class. The Figure 1.16 shows an example of inheritance.

In Figure 1.16 red, yellow and blue are the main colours. Orange colour is created from the combination of red and yellow, green is created from yellow and blue, and violet is created from red and blue. Orange colour has attributes of both red and yellow, which produces a new effect. Thus, many combinations are possible.

images

Fig. 1.16 Inheritance

(7) Polymorphism

POLYMORPHISM

Polymorphism allows the same function to act differently in different classes.

Polymorphism makes possible the same functions to act differently on different classes as shown in Figure 1.17. It is an important feature of OOP concept and has the ability to take more than one form. Polymorphism accomplishes an important part in allowing objects of different classes to share the same external interface. It is possible to code a non-specific (generic) interface to a set of associated actions.

images

Fig. 1.17 Polymorphism in OOP

(8) Dynamic Binding

DYNAMIC BINDING

Binding means connecting one program to another program that is to be executed in reply to the call.

Binding means connecting one program to another program that is to be executed in reply to the call. Dynamic binding is also known as late binding. The code present in the specified program is unknown till it is executed. It is analogous to inheritance and polymorphism.

In Figure 1.17, polymorphism allows single object to invoke similar function from different classes. The program action is different in all the classes. During execution time, the code analogous to the object under present reference will be executed.

(9) Message Passing

Object-oriented programming includes objects which communicate with each other. Programming with these objects should be followed in steps shown below:

(1) Declaring classes that define objects and their actions.

(2) Declaring objects from classes.

(3) Implementing relation between objects.

images

Fig. 1.18 Message passing

Data is transferred from one object to another. A message for an object is the demand for implementation of the process. Message passing as shown in Figure 1.18 contains indicating the name of the object, function, and required data elements. Objects can be created, released, and interacted with each other. An object is workable, as long as it is active. In object-oriented programming, there is a panorama of independent objects that communicate with each other by swapping messages. Objects invoke member functions. They also negate if the calling object is not a member of the same class. Thus a message is a solicitation to an object to call one of its member functions. A message contains name of the member function and parameters of the function. Execution of member function is just a response generated due to receipt of a message. It is possible when the function and the object are of the same class.

(10) Reusability

REUSABILITY

Object-oriented technology allows reusability of the classes by extending them to other classes using inheritance.

Object-oriented technology allows reusability of classes by extending them to other classes using inheritance. Once a class is defined, the other programmer can also use it in their programs and add new features to the derived classes. Once verified the qualities of base classes need not be redefined. Thus, reusability saves time. In Figure 1.19 class A is reused and class B is created. Again class B is reused and class C is created.

images

Fig. 1.19 Reusability

images

Fig. 1.20 Relationships between two classes

(11) Delegation

In OOP, two classes can be joined either by - inheritance or delegation, which provide reusability of the class.

In inheritance, one class can be derived from other class and relationship between them is known as kind of relationship. For example, if class Y is derived from class X, then class Y is known as kind of X, as shown in Figure 1.20 (a).

The second type of relationship is has a relationship. When object of one class is used as data member in other class, such composition of objects is known as delegation. As shown in Figure 1.20 (b) class C has two data members that are objects of class A and B. Such a relationship between two classes is known as has a relationship.

(12) Genericity

The software components of a program have more than one version depending on the data types of arguments. This feature allows declaration of variables without specifying exact data type. The compiler identifies the data type at run-time. The programmer can create a function that can be used for any type of data. The template feature in C++ allows generic programming.

1.8 ADVANTAGES OF OOP

Object-oriented programming provides many advantages to the programmer and the user. This technology solves many problems related to software development, provides improved quality, and low cost software. OOP has the following advantages:

(1) Object-oriented programs can be comfortably upgraded.

(2) Using inheritance, redundant program codes can be eliminated and the use of previously defined classes may be continued.

(3) The technology of data hiding facilitates the programmer to design and develop safe programs that do not disturb code in other parts of the program.

(4) The encapsulation feature provided by OOP languages allows programmer to define the class with many functions and characteristics and only few functions are exposed to the user.

(5) All object-oriented programming languages can create extended and reusable parts of programs.

(6) Object-oriented programming enhances the thought process of a programmer leading to rapid development of new software in short span of time.

1.9 OBJECT-ORIENTED LANGUAGES

There are many languages which support object-oriented programming. Table 1.1 and 1.2 describe the OOP languages and their features.

Following object-oriented languages are widely accepted by the programmer:

images C++

images Smalltalk

images Charm ++

images Java

Table 1.1 Properties of pure OOP and object based languages

images

Table 1.2 Properties of extended traditional languages

images

SMALLTALK Smalltalk is purely an object-oriented programming language. C++ makes few compromises to ensure quick performance and small code size. Smalltalk uses run-time binding. Smalltalk programs are considered to be faster than C++ and needs longer time to learn. Smalltalk programs are written using Smalltalk browser. Smalltalk uses dynamic objects and memory is allocated from free store. It also provides automatic garbage collection and memory is released when object is no longer in use.

CHARM++ Charm ++ is also an object-oriented programming language. It is portative. The language provides features such as inheritance, strict type checking, overloading, and reusability. It is designed in order to work efficiently with different parallel systems, together with shared memory systems and networking.

JAVA Java was developed by Patrick Naughton, James Gosling, Chris Warth, Mike Sheridan, and Ed Frank at Sun Microsystems. Java is an object-oriented programming language and supports maximum OOP characteristics. Its statement structure is like C and C++, but it is easier than C++. Java excludes few confusing and unsafe features of C and C++, for example, pointers.

Java allows client/server programming and is used for Internet programming. The Java programs are downloaded by the client machines and executed on different types of hardware. This portability is achieved by translation of Java program to machine code using compiler and interpreter.

The Java compiler converts the source program to JVM (Java virtual machine). The JVM is a dummy CPU. The compiler Java program is known as byte code. The Java interpreter translates byte code into object code. Compiler and interpreter do the conversion of Java program to object code.

1.10 USAGE OF OOP

Object-oriented technology is changing the style of software engineers to think, analyze, plan, and implement the software. The software developed using OOP technology is more efficient and easy to update. OOP languages have standard class library. The users can reuse the standard class library in their program. Thus, it saves lot of time and coding work.

The most popular application of object-oriented programming is windows. There are several windowing software based on OOP technology. Following are the areas for which OOP is considered:

(1) Object-Oriented DBMS

(2) Office automation software

(3) AI and expert systems

(4) CAD/CAM software

(5) Network programming

(6) System software

1.11 USAGE OF C++

C++ is a flexible language. Lengthy programs can be easily controlled by the use of C++. It creates hierarchy -associated objects and libraries that can be useful to other programmers. C++ helps the programmer to write bug-free program, which are easy to maintain.

SUMMARY

(1) C++ is an object-oriented programming language invented by Bjarne Stroustrup at AT&T Bell Laboratories.

(2) The recognized council working under the procedure of the American National Standard Institute (ANSI) has made an international standard for C++.

(3) The disadvantages with conventional programming language is that the program written in these languages consists of a sequence of instructions that tells the compiler or interpreter to perform a given task. When program code is large it becomes inconvenient to manage it.

(4) The prime factor in the development of object-oriented programming approach is to remove some of the shortcoming of the procedure oriented languages.

(5) In monolithic programming languages such as BASIC and ASSEMBLY, the declared data variables are global and the statements are written in sequence.

(6) In procedural programming languages such as FORTRAN and COBOL, programs are divided into a number of segments known as subprograms. Thus it also focuses on functions apart from data.

(7) Large size programs are developed in structured programming such as PASCAL and C. Programs are divided in multiple submodules and procedures.

(8) OOP acts with data as a critical component in the program development and does not let data to flow freely around the systems.

(9) Object: Objects are primary run-time entities in an object-oriented programming. They may stand for a thing that has a specific application.

(10) Class: A class is a grouping of objects having identical properties, common behavior, and shared relationship. The entire group of data and code of an object can be built as a user-defined data type using class.

(11) Method: An operation required for an object or entity when coded in a class is called a method.

(12) Data Abstraction: Abstraction directs to the procedure of representing essential features without including the background details.

(13) Encapsulation: C++ supports the features of encapsulation using classes. The packing of data and functions into a single component is known as encapsulation.

(14) Inheritance: Inheritance is the method by which objects of one class get the properties of objects of another class. In object-oriented programming, inheritance provides the thought of reusability.

(15) Polymorphism: Polymorphism makes possible the same functions to act differently on different classes. It is an important feature of OOP concept.

(16) Object-oriented technology allows reusability of the classes by extending them to other classes using inheritance.

(17) C++, Smalltalk, Eiffel and Java are widely used OOP languages.

(18) Object-oriented technology is changing the style of software engineers to think, analyze, plan, and implement the software. The software developed using OOP technology is more efficient and easy to update.

(19) C++ is a flexible language. Lengthy programs can be easily controlled by C++.

EXERCISES

[A] Answer the following questions.

(1) What is object-oriented programming?

(2) Explain the key concepts of OOP.

(3) What is ANSI standard?

(4) What are the disadvantages of conventional programming languages?

(5) Explain the evolution of C++.

(6) List the names of popular OOP languages.

(7) List the unique features of an OOP paradigm.

(8) What is an object and a class?

(9) Compare and contrast OOP languages with procedure oriented languages.

(10) Mention the types of relationships between two classes.

(11) What is structure oriented programming? Discuss its pros and cons.

(12) How is global data shared in procedural programming?

(13) Describe any two object-oriented programming languages.

(14) What are the differences between Java and C++?

(15) Why pointers are passed by in Java?

(16) Mention the advantages of OOP languages.

(17) What do you mean by message passing?

(18) Distinguish between inheritance and delegation.

(19) Can we define member functions in private section?

(20) Can we declare data member in public section?

[B] Answer the following by selecting the appropriate option.

(1) C++ language was invented by

(a) Bjarne Stroustrup

(b) Dennis Ritche

(c) Ken Thompson

(d) none of the above

(2) The languages COBOL and BASIC are commonly known as

(a) procedure oriented languages

(b) object-oriented languages

(c) low level languages

(d) none of the above

(3) The packing of data and functions into a single component is known as

(a) encapsulation

(b) polymorphism

(c) abstraction

(d) none of the above

(4) The method by which objects of one class get the properties of objects of another class is known as

(a) inheritance

(b) encapsulation

(c) abstraction

(d) none of the above

(5) The mechanism that allows same functions to act differently on different classes is known as

(a) polymorphism

(b) encapsulation

(c) inheritance

(d) none of the above

(6) Object-oriented programming is popular because

(a) user can define user-defined data types

(b) programming statements are easy

(c) it is easy to find bugs and errors

(d) all of the above

(7) The existing class can be reused by

(a) inheritance

(b) polymorphism

(c) dynamic binding

(d) abstraction

(8) Composition of objects in a class is known as

(a) delegation

(b) inheritance

(c) polymorphism

(d) none of the above

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.202