+D=FJAH - New Age International

4 downloads 1421 Views 423KB Size Report
programs. C++ which supports OOPS is very popular. There are two versions: Borland's Turbo C++ and Borland C++. Some vital features about them are data ...
+D=FJAH Introduction to OOPS and C++

+D=FJAH Introduction to OOPS and C++

Object Oriented Programming is considered as important as the development of the first high level language at the early days of the computer age. Why Object Oriented Programming when we were just about to settle down comfortably with good old procedural languages like Pascal, Basic, Fortran, Cobol? One of the chief weaknesses of Procedural languages is its complexity which OOPS is able to cope with easily. The objectives of OOPS is clear, reliable and easy-to-understand programs. C++ which supports OOPS is very popular. There are two versions: Borland’s Turbo C++ and Borland C++. Some vital features about them are data hiding, encapsulation and polymorphism. Does this sound Greek and Latin to you? Don’t bother, you will understand these terms as you study programs using these concepts. Does one need any prior knowledge in programming languages? Yes. It would be good if you have some background in Pascal or C, particularly in the Turbo environment. Others like Basic, Fortran or any one of them would be an added advantage. A fair knowledge about the use of MS-DOS is also expected. Can’t an absolute novice to computers learn OOPS and C++ ? Why not! You only need enthusiasm like in any other activity. The very fact that you possess this book and have started reading it, shows that you have necessary enthusiasm to learn C++. Therefore do not hesitate, take the plunge now.

What is the minimum system requirement? 1. Borland’s Turbo C++: good for beginners. 2. Borland’s C++: good for the experienced users. It includes assembler, debugger, utilities to develop MS window programs. 3. PC XT, AT or PS/2 or compatibles. 4. Turbo C++ (tc) requires MS-DOS 2 or higher versions.

2

Introduction to OOPS and C++

3

5. Borland C++ (bc ) requires MS-DOS 3 or higher versions. 6. 640 kilo bytes RAM, 6MB Hard Disk or Floppy Disk drive. 7. 80 column monochrome or colour monitor. 8. Mouse - optional. 9. MS - windows - optional. Would knowledge of C help learning C++? Yes and No. A knowledge of C would help understanding pointers, structures of C++. But a lack of knowledge in C need not be a deterrent. Actually C and C++ are two different languages, though they have similar syntax. You’ll find that you may have to unlearn many things in C, while learning C++. The transformation is going to be exciting and thrilling. As we said C++ supports OOPS. OOPS have objects and classes. What do these imply? Now recall that in a procedural language, a program gets the data, processes it and gives the result as an output. This is done by a sequence of instructions called a program. Here data has a second class status. Whereas in OOPS data is considered the most important factor and given a prime status. In procedural language we have subroutines, subprograms or procedures. Whereas in C++ these are considered to be functions. Functions are also known as modules. A module does a specific task. The module or function is regarded as the cornerstone of structured programming. The function do things that any one program statement does. C++ will liberally use concepts like global variable and local variable. A global variable is defined outside a function and is accessible by all functions. But allowing such free access is liable to corrupt the data. Whereas in C++ such free access to data and consequent mutilation of data is prevented by the concept of data hiding. A local variable is defined within a function. It is accessed only by the particular function in which it is defined. Other characteristics of C++ are its extensibility, which means it is able to create new data types. OOP languages are able to combine data and functions that operate on these data, called objects. The functions that access and operate on objects is called member functions. Therefore we say data is hidden, thereby preventing accidental alterations. When data and functions are combined into one entity, we call it as encapsulation. In Small Talk OOP language member functions are called methods. We say that objects are members of classes. We have the liberty to include any data type and functions as objects of a class. However, defining class does not create objects. Now what is a class? It is a collection of similar objects. One of the unique characteristics of class is inheritance. To understand class and inheritance consider a class called Business.

4

Object Oriented Programming and C++ Business (Base Class)

Street hawker

Door to door Salesman

Petty shop owner

Regular shop

Company private

Corporate company

(Derived Classes)

Thus you find that Business is the base class and from it several classes can be derived. Each has its own peculiarities and traits. Each subclass may again be divided into further subclasses. Thus we say that the derived class inherits some features of the base class, but add new features of their own. To extend this discussion further, assume that several different sections of a procedural program perform the same action. Then examine them and pull out the common elements and organize them into one function. Thereafter the different sections can invoke this common functions to perform the common action, besides performing individual processing as well. Another useful feature of C++ is its reusability. You have known library functions in procedural languages. In C++ you can create classes, which can be used by other programmers, just like invoking any library functions. Further inheritance enables a programmer to adopt an existing class and add new features and capabilities to it. Other peculiarities of C++ are creation of new data types, polymorphism and overloading. We’ll see how new data types are created during the programming studies. Polymorphism means using operators and functions in different ways, depending on what they are operating on. An overloaded operator implies giving the capability to an existing operator to operate on new data. Thus you are now aware of the power and potentialities of this new language of the future. Of course the best way to learn any computer language is to sit with the terminal and sweat it out. As the famous author Fowler of Oxford English Dictionary said, “There is no royal way to learning”. In case you do not have free access to a terminal, you need not be bogged down. You may use this book to familiarize with the syntax of C + + and equip yourself so that when a terminal is available, you can put it to the best use.

REVIEW QUESTIONS 1.1

What is the chief weakness of procedural language?

1.2 1.3

Which version of Borland C++ is sufficient for a beginner? What are the 3 most important features of C++?

1.4

Does working in Turbo environment require a knowledge about using MS-DOS?

Introduction to OOPS and C++ 1.5 1.6 1.7

5

What additional features are built into Borland C++? Which version of MS-DOS would suffice for Turbo C++? Which version of MS-DOS is required for Borland C++?

1.8 What minimum capacity of RAM is needed to run C++? 1.9 What minimum capacity of Hard Disk is required for C++? 1.10 Is a mouse an absolute necessity? 1.11 What is common about C and C++? 1.12 Do procedural languages give prime status to data? 1.13 What two factors distinguish OOPS from procedural language? 1.14 How are subroutines, subprogram or procedures branded in C++? 1.15 What is considered the cornerstone of structured programming? 1.16 What do you consider the major drawback of global variable? 1.17 How can mutilation of data due to global definition be prevented? 1.18 What is the ability to create new data types in C++ called? 1.19 What do you call data when member functions operate on these data? 1.20 What is the name of such functions that operate on objects? 1.21 How do you brand combination of data and functions into one entity? 1.22 Does defining a class create objects? 1.23 What is the collection of similar objects called? 1.24 What will you say is the unique characteristics of class? 1.25 Suppose you create a class which can be used by other programmers like a library function, what is it called? 1.26 What do you call it when operators and functions are used in different ways? 1.27 What do you call an existing operator that is given capability to operate on new data?

Answer 1.1 Complexity

1.2 Turbo C++

1.3 Data hiding, encapsulation and polymorphism 1.4 Yes 1.5 1.6 1.8

Assembler, debugger, utilities to develop window programs 2 or higher 1.7 3 or higher 640 KB 1.9 6 MB

1.10 Only Optional 1.12 No

1.11 Same syntax 1.13 Objects and classes.

1.14 Functions or modules 1.16 Data corruption 1.18 Extensibility

1.15 Functions or modules 1.17 Data hiding 1.19 Objects

1.20 Member functions 1.22 No

1.21 Encapsulation 1.23 A class

1.24 Inheritance 1.26 Polymorphism

1.25 Reusability 1.27 Overloaded operator