Friday 17 February 2023

Concept of Object Oriented Programming

 

Unit 1: Concept of Object Oriented Programming (12)

1.1     Programming Languages and Software Crisis

1.2     Procedure Vs Object Oriented Programming Language 

1.3     Feature of Object Oriented Programming

1.4     Popular Object Oriented Programming Language and features

1.5     Advantage and Disadvantage of OOPs

1.6     Introduction of C++ and Compilers

1.7     Programming Structure in C++

1.8     Comparison on C and C++

1.9     Additional Data types, token in C++

1.10   Insertion and Extraction Operators

Practical Works:

·       Install the compiler of C++.

·       Use Insertion and Extraction Operator.

·       Compare the C and C++ Compiler and structure  

 

 

 

1.1 Programming language and Software Crisis

 

Developments in software technology continue to be dynamic. New tools and techniques are announced in quick succession. This has forced the software engineers and industry to continuously look for new approaches to software design and development, and they are becoming more and more critical in view of the increasing complexity of software systems as well as the highly competitive nature of the industry. These rapid advances appear to have created a situation of crisis within the industry.

The following issued need to be addressed to face the crisis:

       How to represent real-life entities of problems in system design?

       How to design system with open interfaces?

       How to ensure reusability and extensibility of modules?

       How to develop modules that are tolerant of any changes in future?

       How to improve software productivity and decrease software cost?

       How to improve the quality of software?

       How to manage time schedules?

 

 

1.2 Procedure Vs Object Oriented Programming Language 

 

 

Both OOP (Object Oriented Programming) and POP (Procedural Oriented Programming) are languages (high-level) in the world of programming- widely used in application development. These languages have a different approach based on the nature of code development- and thus, they work differently. Before we understand the difference between procedural and object oriented programming, let us know more about them.

What is Procedural Programming?

You can define Procedural Programming as a programming model derived from structural programming. It follows the concept of the calling procedure. The procedures, also called functions, routines, or subroutines, consist of a series of computational steps that they need to carry out. During the execution of a program, one can call any given procedure at any point- either by other procedures or by itself.

Procedural Programming Languages – BASIC, FORTRAN, COBOL, ALGOL, Pascal, and C.

What is Object Oriented Programming?

You can define Object Oriented Programming as a programming model that follows the concept of objects. The objects contain codes in the form of methods and data in the form of attributes. In the case of Object Oriented Programming, it designs computer programs by using the concept of the objects interacting with the real world. There are various Object Oriented Programming languages. But the most popular ones among all are class-based. It means that the objects are instances of the classes determining their types.

Object Oriented Programming Languages – Java, Python, C#, C++, JavaScript, PHP, Ruby, Dart, Perl, Swift, Scala, Objective.

Difference Between Procedural and Object Oriented Programming

Parameter

Procedural Programming

Object Oriented Programming

Definition

This programming language makes use of a step by step approach for breaking down a task into a collection of routines (or subroutines) and variables by following a sequence of instructions.

This programming language uses objects and classes for creating models based on the real-world environment.

Security

Procedural Programming does not offer any method of hiding data.

Hiding data is possible with Object Oriented Programming due to the abstraction.

Approach

The Procedural Programming follows a Top-Down approach.

The Object Oriented Programming follows a Bottom-Up approach.

Type of Division

It divides any large program into small units called functions.

It divides the entire program into small units called objects.

Inheritance

It does not provide any inheritance.

It achieves inheritance in three modes- protected, private, and public.

Overloading

The case of overloading isn’t possible in the case of Procedural Programming.

Overloading is possible in the form of operator overloading and function overloading in the case of Object Oriented Programming.

Examples

Some common examples of Procedural Programming are C, Fortran, VB, and Pascal.

The examples of Object Oriented Programming languages are Java, C++, VB.NET, Python, and C#.NET.

 

1.3 Features of Object Oriented Programming

 

Features of Object-oriented Programming

Object Oriented Programming

  1. Objects
  2. Classes
  3. Data abstraction
  4. Data encapsulation
  5. Inheritance
  6. Polymorphism
  7. Dynamic binding
  8. Message communication

 

1.   Objects

 Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data; they may also represent user-defined data such as vector, time and lists.

 Each object contains data(attributes) and code to manipulate the data (methods).

Object Oriented Programming

2.   Classes

 A class represents a set of related objects. The object having similar attributes can be grouped as a class. The class of an object defines what attributes an object has. The entire set of data and code of an object can be made a user-defined data type with the help of a class.

The data and the operation of a class can be declared as one of the three types:

  1. Public,
  2. Protected and
  3. Private:

 

3.    Data Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations. Since the classes use the concept of data abstraction, hence called as Abstract Data Types (ADT).

 

4.   Data Encapsulation

The wrapping up of data and functions into a single unit (as a class) is known as encapsulation. These methods provide the interface between the object’s data and the program. This insulation of the data from direct access by the program is called data hiding

5.   Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class. In OOP, the basic idea behind the inheritance is re-usability. Using inheritance, we can add additional features to an existing class without modifying it by deriving a new class from the existing one. The new class will have the combined features of both the classes. Each subclass defines only those features that are unique to it.

 

6.   Polymorphism

Polymorphism means the ability to take more than one form. An operation may exhibit different behavior in different instances. The behavior depends upon the types of the data used in the operation.

The process of making an operator to exhibit different behavior at different instance is called operator overloading. Another example of polymorphism is function overloading, where a single function can perform various different types of task.

7.   Dynamic Binding

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of call at run-time. It is also called late binding. This is associated with polymorphism and inheritance. A function call associated with a polymorphic reference depends on the dynamic type of that reference.

8.   Message Communication

Message communication is another feature of object-oriented programming which enables the set of objects to communicate with each other. The concept of message communication makes it easier to talk about building systems that directly model or simulate their real-world counter-parts.

1.6 Benefits of OOP

 

OOP offers several benefits to both the program designer and the user. The principal advantages are:

 

       Through inheritance, we can eliminate redundant code extend the use of existing

Classes.

       The principle of data hiding helps the programmer to build secure program that can not be invaded by code in other parts of a programs.

       It is possible to have multiple instances of an object to co-exist without any interference.

       It is possible to map object in the problem domain to those in the program.

       It is easy to partition the work in a project based on objects.

       Software complexity can be easily managed.

 

 

1.7 Program Structure of C++

 

A typical C++ program would contain four sections. This 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 (member function definition).

Finally, the main program that uses the class is places in a third file which “includes: the previous two files as well as any other file required.

 

                     Include Files 

                  Class declaration

 

        Member functions definitions

 

 

             Main function program

 

 

 

 

 

1.8 Comparision on c and c++

 

Below is the table of differences between C and C++: 
 

C

C++

C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs.

C++ was developed by Bjarne Stroustrup in 1979.

C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming.

C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.

C is a subset of C++.

C++ is a superset of C.

C contains 32 keywords.

C++ contains 63 keywords.

For the development of code, C supports procedural programming.

C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms.

C is a function-driven language.

C++ is an object-driven language

 

 

1.9 Introduction of C++

 

1.       C++ is an object-oriented programming language.

2.        It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980’s.

3.       Therefore, C++ is an extension of C with a major addition of the class construct feature of Simula67.

4.       The idea of C++ comes from the C increment operator ++, thereby suggesting that C++ is an augmented version of C.

5.        C+ + is a superset of C. Almost all c programs are also C++ programs. However, there are a few minor differences that will prevent a c program to run under C++ complier.

1.10       Insertion & Extraction In C++

The insertion operator << is the one we usually use for output, as in:

Cout<< “this is output” <<endl;

It gets its name from the idea of inserting data into the output stream.

The extraction operator >> is the one we usually use for input, as in:

Cin>> X;

It gets its name from the idea of extracting data from the input stream.

1.9.1 Application of C++

 

C++ is a versatile language for handling very large programs; it is suitable for virtually any programming task including development of editors, compilers, databases, communication systems and any complex real life applications systems.

 

       Since C++ allow us to create hierarchy related objects, we can build special object-oriented libraries which can be used later by many programmers. 

       While C++ is able to map the real-world problem properly, the C part of C++ gives the language the ability to get closed to the machine-level details.

       C++ programs are easily maintainable and 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 the near future.

 

 1.10 Simple C++ Program

Let us begin with a simple example of a C++ program that prints a string on the screen.

 

      

Printing A String 

#include<iostream> Using namespace std;

int main()

{

cout<<”c++ is better than c \n”;

return 0;

}

 

                                                        Program 1.10.1

       This simple program demonstrates several C++ features.

 

1.10.1 Program feature

 

Like C, the C++ program is a collection of function. The above example contain only one function main(). As usual execution begins at main(). Every C++ program must have a main(). C++ is a free form language. With a few exception, the compiler ignore carriage return and white spaces. Like C, the C++ statements terminate with semicolons.

 

1.10.2 Comments

 

C++ introduces a new comment symbol // (double slash). Comment start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the line, and whatever follows till the end of the line is ignored. Note that there is no closing symbol.

 

 The double slash comment is basically a single line comment. Multiline comments can be written as follows:

 

                // This is an example of

                // C++ program to illustrate

               // some of its features

 

The C comment symbols /*,*/ are still valid and are more suitable for multiline comments. The following comment is allowed:

 

              /* This is an example of                     C++ program to illustrate

                     some of its features

              */

 

1.10.3 Output operator

 

The only statement in program 1.10.1 is an output statement. The statement

 

                  Cout<<”C++ is better than C.”;

 

Causes the string in quotation marks to be displayed on the screen. This statement introduces two new C++ features, cout and <<. The identifier cout(pronounced as C out) is a predefined object that represents the standard output stream in C++. Here, the standard output stream represents the screen. It is also possible to redirect the output to other output devices. The operator << is called the insertion or put to operator.

 

1.10.4 The iostream File

 

We have used the following #include directive in the program:

 

#include <iostream>

 

The #include directive instructs the compiler to include the contents of the file enclosed within angular brackets into the source file. The header file iostream.h should be included at the beginning of all programs that use input/output statements.

 

1.10.5 Namespace

 

Namespace is a new concept introduced by the ANSI C++ standards committee. This defines a scope for the identifiers that are used in a program. For using the identifier 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 keyword of C++.

 

1.10.6 Return Type of main()

 

In C++, main () returns an integer value to the operating system. Therefore, every main () in C++ should end with a return (0) statement; otherwise a warning an error might occur. Since main () returns an integer type for main () is explicitly specified as int. Note that the default return type for all function in C++ is int. The following main without type and return will run with a warning:

 

main () {

 …………..

 ………….

}

 

 

1.11 More C++ Statements 

 

Let us consider a slightly more complex C++ program. Assume that we should like to read two numbers from the keyboard and display their average on the screen. C++ statements to accomplish this is shown in program 1.11.1

 

 

                                                           AVERAGE OF TWO NUMBERS

 

                 #include<iostream.h> // include header file

               

                Using namespace std;

 

               Int main()

               

               {

 

                              Float number1, number2,sum, average;

                              Cin >> number1;            // Read Numbers

                              Cin >> number2;            // from keyboard

                              Sum = number1 + number2;

                              Average = sum/2;

                                 Cout << ”Sum = “ << sum << “\n”; 

                                 Cout << “Average = “ << average << “\n”;

 

                             Return 0;

 

               }             //end of example

 

The output would be:

Enter two numbers: 6.5  7.5

Sum = 14

Average = 7

 

                                                                       Program 1.11.1

1.11.1 Variables

 

The program uses four variables number1, number2, sum and average. They are declared as type float by the statement.

 

                float number1, number2, sum, average;

 

All variable must be declared before they are used in the program.

 

1.11.2 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 (pronounced ‘C in’) 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 (or takes) the value from the keyboard and assigns it to the variable on its right fig 1.8. This corresponds to a familiar scanf() operation. Like <<, the operator >> can also be overloaded.

 

               Object                                              Execution operator                                    Variable

               

 

 

     Keyboard

 

 

Fig 

 

 

 

Cin

  >>

 

 

 

45.5

 

 

 

                            1.8 Input using extraction operator

 

 

1.11.3 Cascading of I/O Operators

 

We have used the insertion operator << repeatedly in the last two statements for printing results.

 

The statement

 

                   Cout << “Sum = “ << sum << “\n”;

 

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 statement 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 is one statement but provides two line 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 iperator >> 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 munber1 and 20 to number2.

 

1.12 An Example with Class 

 

One of the major features of C++ is 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.

 

Program 1.12.1 shows the use of class in a C++ program.

 

                                                          USE OF CLASS

 

                 #include<iostream.h> // include header file

               

              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 << “\nNameame: “ << name;

                               cout << “\nAge: “ << age;           

               }

 

               Int main()

              {                           person p;

                             p.getdata();

                             p.display();

 

                             Return 0;

 

               }             //end of example

 

PROGRAM 1.12.1

 

 

 

The output of program is:

 

 Enter Name: Ravinder

 Enter age:30

 Name:Ravinder

 Age: 30

 

 

The program define person as a new data of type class. The class person includes two basic data type items and two function to operate on that data. These functions are called member function. The main program uses person to declare variables of its type. As pointed out earlier, class variables are known as objects. Here, p is an object of type person. Class object are used to invoke the function defined in that class.

 

1.13 Structure of C++ Program

 

As it can be seen from program 1.12.1, a typical C++ program would contain four sections as shown in fig. 1.9. This 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 (member function definition).

Finally, the main program that uses the class is places in a third file which “includes: the previous two files as well as any other file required.

 

                     Include Files 

                  Class declaration

 

        Member functions definitions

 

 

             Main function program

 

 

                Fig 1.9 Structure of a C++ program

 

 

 This approach is based on the concept of client-server model as shown in fig. 1.10. 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.

Fig. 1.10

 The client-server model

 

 

 

 

 

 

 

Server

 

 

            Class Definition

 

   Member Function

 

 

 

 

 

 

 

 

          Client

 

 

Main function Program

 

 

1.14 Creating the Source File 

 

Like C programs can be created using any text editor. Foe example, on the UNIX, we can use vi or ed text editor for creating using any text editor for creating and editing the source code. On the DOS system, we can use endlin or any other editor available or a word processor system under non-document mode.

 

 Some systems such as Turboc C++ provide an integrated environment for developing and editing programs

 The file name should have a proper file extension to indicate that it is a C++ implementations use extensions such as .c,.C, .cc, .cpp and .cxx. Turboc C++ and Borland C++ use .c for C programs and .cpp(C plus plus) for C++ programs. Zortech C++ system use .cxx while UNIX AT&T version uses .C (capital C) and .cc. The operating system manuals should be consulted to determine the proper file name extension to be used.

 

1.15 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++

 

This process of implementation of a C++ program under UNIX is similar to that of a C program. We should use the “cc” (uppercase) 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 filename is a. out.

 

A program spread over multiple files can be compiled as follows:

 

CC file1.C file2.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 integrated program development environment under MS DOS. They provide a built-in editor and a menu bar 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. 

 

Summary  

       Software technology has evolved through a series of phases during the last five decades.

       POP follows top-down approach where problem is viewed as sequence of task to be performed and functions are written for implementing these tasks.

       POP has two major drawbacks:

       Data can move freely around the program.

       It does not model very well the real-world problems.

       OOP was inventing to overcome the drawbacks of POP. It follows down -up approach.

       In OOP, problem is considered as a collection of objects and objects are instance of classes.

       Data abstraction refers to putting together essential features without including background details.

       Inheritance is the process by which objects of one class acquire properties of objects of another class. 

       Polymorphism means one name, multiple forms. It allows us to have more than one function with the same name in a program.

       Dynamic binding means that the code associated with a given procedure is not known until the time of the run time.

       Message passing involves specifying the name of the object, the name of the function and the information to be sent.

       C++ is a superset of C language.

       C++ ads a number of features such as objects, inheritance, function overloading and operator overloading to C.

       C++ supports interactive input and output features and introduces anew comment symbol // that can be used for single line comment.

       Like C programs, execution of all C++ program begins at main() function.

 

 

 

No comments:

Post a Comment