Full Transcript

UNIT-1 US05CCSC51:OOP USING C++ Page 1 of 30 1 Structured programming vs. object oriented programming 1.1 Structured programming was a powerful tool that enabled programmers to write complex programs easily. However, the programs became larger...

UNIT-1 US05CCSC51:OOP USING C++ Page 1 of 30 1 Structured programming vs. object oriented programming 1.1 Structured programming was a powerful tool that enabled programmers to write complex programs easily. However, the programs became larger so the structured approach failed to make desired results in terms of bug (error) free, easy-to-maintain and reusable programs. 1.2 Procedure oriented programming:  Conventional programming using high level languages such as COBOL, FORTRAN and c is commonly known as POP. In the POP approach, the problem is viewed as a sequence of things to be done such as reading, calculating and printing.  A number of functions are written to accomplish these tasks.  The primary focus is on functions.  POP basically consists of writing a list of instructions or actions for the computer to follow and these instructions are organized into groups known as functions.  While we concentrate on the development of functions very little attention is given to the data that are being used by the different functions.  In a multifunction program many important data items are placed as global so that they may be accessed by all the functions. Also each function may have its own local data. GLOBAL DATA GLOBAL DATA FUNCTION-1 FUNCTION-2 FUNCTION-3 LOCAL DATA LOCAL DATA LOCAL DATA Relationship of data & functions in pop  In a large program it is very difficult to identify what data is used by which function.  So, the global data are more helpless to an unintentionally change by a function.  Another drawback is that it does not model real world problems very well. Characteristics of POP:- 1. Give importance on doing things (algorithms) 2. Large programs are divided into smaller programs known as functions. UNIT-1 US05CCSC51:OOP USING C++ Page 2 of 30 3. Most of functions share global data. 4. Data move openly around the system from function to function. 5. Functions transform data from one form to another. 6. Employs top-down approach in program design. 1.3 Object-Oriented Programming Paradigm: - 1. The basic reason of inventing object-oriented approach is to remove some drawbacks encountered in pop. 2. OOP treats data as critical element in the program development and doesn’t allow data to flow freely around the system. 3. It ties (binds) the data more closely to the functions that are operated on it and protects data from accidental modification from outside functions. 4. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. 5. The data of an object can be accessed by only the functions associated with the object. Object A Object B DATA DATA F FUNCTIONS FUNCTIONS DATA FUNCTIONS Object C Features of object oriented programming: - 1. Emphasis on data rather than procedure. 2. Programs are divided into objects. 3. Data structures are designed such that they characterized the objects. 4. Functions that operate on the data of an object are tied together in the data structure. 5. Data is hidden and cannot be accessed by external functions. 6. Objects may communicate with each other through functions. 7. Now data and functions can be easily added whenever necessary. 8. It follows bottom-up approach in program design. UNIT-1 US05CCSC51:OOP USING C++ Page 3 of 30 We define “object-oriented programming an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand”. 2 Basic OOP concepts : objects, classes, encapsulation, data hiding, inheritance, polymorphism 2.1 Objects:-  Objects are the basic run-time entities in an object-oriented system.  They may represent a person, a place, a bank account, and a table of data or any item that the program has to handle.  Programming problem is analyzed in terms of objects and the nature of communication between them.  Program objects should be choosing such that they match closely with the real world objects.  For example, we can say that individual student is individual object. OBJECT: STUDENT STUDENT DATA NAME DATE-OF-BIRTH TOTAL MARKS.……………… AVERAGE FUNCTIONS TOTAL AVERAGE DISPLAY DISPLAY ………….. Two ways of representing an object 2.2 Classes: -  It is set of entity like the student is class and individual student is object.  We know that objects contain data and code to manipulate that data.  The entire set of data and code of an object can be made a user-defined data type with the help of a class.  Objects are variables of the type class.  Once a class has been defined, we can create any number of objects belonging to that class.  A class is a collection of objects of similar type.  Example: - mango, apple & orange are members of the class fruit.  The syntax used to create an object is no different than the syntax used to create an integer object in c.  If fruit is defined as a class, then the statement is fruit mango, apple, orange; UNIT-1 US05CCSC51:OOP USING C++ Page 4 of 30 2.3 Data Abstraction and Encapsulation: -  The enclosing or covering of data & functions into a single unit (called class) is known as Encapsulation.  Data encapsulation is the most striking feature of the class.  The data is not accessible to the outside world and only those functions which are enclosed in the class can access it.  These functions 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 or information hiding.  Abstraction refers to the representation of important features without including the background details or explanations.  Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes.  They encapsulate (bind) all the essential properties (attributes) of the objects that are to be created.  These attributes are called data members.  The functions that operate on these data are called methods or member functions. 2.4 Polymorphism: -  Polymorphism means the ability to take more than one form.  An operation may execute in different behavior in different instances.  The behavior depends upon the types of data used in the operation.  Consider the operation of addition which generates a sum of two numbers.  If we pass strings as operands, then this function concate these two strings.  The process of making an operator (addition, subtraction etc.) To exhibit different behaviors in different instances is known as operator overloading.  If the function name is same but they have different number of argument or/ and different types (data types) of arguments, then this type is called function overloading. SHAPE DRAW () CIRCLE OBJECT BOX OBJECT TRIANGLE OBJECT DRAW (CIRCLE) DRAW (BOX) DRAW(TRIANGLE) UNIT-1 US05CCSC51:OOP USING C++ Page 5 of 30 Polymorphism 2.5 Inheritance:  Inheritance is the process by which objects of one class get the properties of objects of other class.  It supports the concept of hierarchical classification.  In OOP, the concept of inheritance provides the idea of reusability.  This means that we can add additional feature to an existing class without modifying it.  This is possible by deriving a new class from the existing one.  The new class will have all the combined features of both the classes.  The following diagram & example describe that how inheritance is done. BIRD ATTRIBUTES FEATHERS LAY EGGS FLYING NONFLYING BIRD BIRD ATTRIBUTES ATTRIBUTES ……………. ……………. ……………. ……………. ROBIN SPARROW PENGUIN KIWI ATTRIBUTES ATTRIBUTES ATTRIBUTES ATTRIBUTES …………….. …………….. …………..... …………….. …………….. …………..... ……………. …………….. Property inheritance  In above diagram the main class is bird, the flying bird and non-flying bird are derived from bird class.  So, both have attributes of bird class.  Bird robin and sparrow can fly, so both are derived from flying bird class. 2.6 Dynamic binding: - UNIT-1 US05CCSC51:OOP USING C++ Page 6 of 30  Binding refers to the linking of a procedure call with the code to be executed as the response.  Dynamic binding (also known as late binding) means that the code associated with a given procedure call at run-time.  It is associated with polymorphism and inheritance.  This means when a function called the appropriate arguments (numbers & types) are matched.  This process is done at run-time so it is called dynamic binding. 2.7 Message passing:  An object oriented program consists of a set of objects that communicate with each other.  The process of programming in an object-oriented language, involves the following basic steps. 1. Creating classes that define objects and their behavior. 2. Creating objects from class definitions, and 3. Establishing communication among objects.  Objects communicate with one another by sending and receiving information.  The concept of message passing makes it easier to talk about building systems that directly model or simulate their real-world counterpart.  A message for an object is a request for execution of a procedure, and so it invokes or calls a function (procedure) as the receiving object which generate the desired result.  Message passing involves the name of the object, the function (message) name, and the information to be sent. Example: - employee e1; class name   object e1. salary (name); object    information (arguments) function name (message) 3 Introduction to c++: structure of a c++ program, data types, variables, constants, expressions, statements and operators. 3.1 Introduction to c++:  C++ is an object-oriented programming language.  It was developed by Bjarne Stroustrup at ‘AT&T’ bell laboratories in Murray Hill, New Jersey, USA in the 1980’s.  C++ is an extension of ‘C’ within a major addition of the class construct feature of simulu67.  The idea of C++ come from the C increment operator ++, thereby suggesting that C++ is an incremented version of C.  C++ is superset of C. UNIT-1 US05CCSC51:OOP USING C++ Page 7 of 30  The most important facilities that C++ adds on to c are classes, inheritance, function overloading and operator overloading.  These features enable creating of obstruct data types; inherit properties from existing data types and support polymorphism. So making of C++ is a truly object- oriented language. 3.1.1 Features of C++:  Like C, the C++ program is a collection of functions.  As usual, execution begins at main ().  Every C++ program must have a main () function.  Like C, the C++ statements terminate with semi columns. 3.1.2 Advantages of OOP  Using inheritance, we can remove unwanted code and expand the use of existing classes.  The principle of data hiding helps the programmer to build secure programs that can’t be separated by code in other parts of the program. So, the procedural languages had not data hiding concepts.  It is possible to create multiple instances (cases/samples) of an object to co- exist (exist at the same time) without any interference.  It is possible to map objects in the problem domain to those in the program.  It is easy to partition the work in a project based on objects.  Object-oriented systems can be easily upgraded from small to large systems.  Software complexity can be easily managed.  We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and get higher productivity. 3.1.3 Disadvantages of OOP: - 1. Compiler overhead. 2. Runtime overhead. 3. Re-orientation of s/w developer to object-oriented thinking. 4. Requires the mastery over the following areas: - Software engineering - Program methodologies 5. Benefits only in long run while managing large s/w projects. 3.1.4 Applications of OOP: - 1. Real time systems. 2. Simulation and modeling. 3. Object-oriented databases. 4. Hypertext, hypermedia and expert-text. 5. Artificial intelligence (AI) and expert systems. 6. Neural networks and parallel programming. 7. Decision support and office automation systems. 8. CIM / CAM / CAD systems. UNIT-1 US05CCSC51:OOP USING C++ Page 8 of 30 3.2 Structure of a C++ program INCLUDE FILES CLASS DECLARATION MEMBER FUNCTIONS DEFINITIONS MAIN FUNCTION PROGRAM - 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 (class definition) from the implementation details (member function definitions). - The main program that uses the class is placed in a third file which “includes” the previous two files as well as any other files required. - This approach is based on the concept of client-server model as shown in figure below. - The class definition including the member functions equivalent to the member server that provides services to the main program known as client. - The client uses the server through the public interface of the class. MEMBER FUNCTIONS Server CLASS DEFINITIONS MAIN FUNCTION PROGRAM Client client-server model 3.3 Data types 3.3.1 Basic Data Types UNIT-1 US05CCSC51:OOP USING C++ Page 9 of 30 Both C and C++ compilers supports all the built-in (also known as basic or fundamental) data types. The modifiers signed, unsigned, long and short may be applied to character and integer basic data types. However, the modifier long may also be applied to double. Data types representation in machine specific in c++. C++ DATA TYPES USER-DEFINED TYPE BUILT-IN TYPE DERIVED TYPE STRUCTURE ARRAY UNION FUNCTION CLASS POINTER ENUMERATION REFERENCE INTEGRAL TYPE VOID FLOATING POINT INT CHAR FLOAT DOUBLE Hierarchy of c++ data types Size and range of c++ basic data types Type Bytes Range -128 to 127 Char 1 0 to 255 Unsigned char 1 -128 to 127 Signed char 1 -32768 to 32767 Int 2 0 to 65535 Unsigned int 2 -31768 to 32767 Signed int 2 -31768 to 32767 Short int 2 0 to 65535 Unsigned short int 2 -32768 to 32767 Signed short int 2 -2147483648 to Long int 4 2147483647 Signed long int 4 -2147483648 to Unsigned long int 4 2147483647 Float 4 0 to 4294967295 Double 8 3.4e -38 to 3.4e +38 Long double 10 1.7e -308 to 1.7e +308 3.4e -4932 to 1.1e +4932 3.3.2 User-defined data types: - - Structures and classes: - UNIT-1 US05CCSC51:OOP USING C++ Page 10 of 30 User-defined data-typs such as struct and union in ‘c’ are legal in c++. Class is also a user-defined data-type which is just like any other data-type, to declare variables. - Enumerated data-type: - It provides a way for attaching names to numbers. The enum keyword (from c) automatically enumerates a list of words by assigning them values 0, 1, 2, and so on… This facility provides an alternative means for creating symbolic constants. Example: - enum shape {circle, square, triangle}; enum colour {red, blue, green, yellow}; void main () { cout code; while (code >= circle && code 1 // shift one bit position to right Shift operators are often used for multiplication and division by powers of two. 3.7 Operators 3.7.1 Arithmetic Operators The C++ language has both unary and binary arithmetic operators. Unary operators are those, which operate on a single operand whereas, binary operators operate on two operands. The arithmetic operators can operate on any built-in data type. Arithmetic operators and their meaning are shown in below table. Unary operator example: Int x=5; Int y; Y= -x; Binary operator example: Int x=5; int y=10; Int c=x+y; Table 1: Arithmetic operators Operator Meaning + Addition - Subtraction * Multiplication / Division UNIT-1 US05CCSC51:OOP USING C++ Page 16 of 30 % Modulus 3.7.2 Relational Operators Using relational operators, we can direct the computer to compare two variables. The C++ relational operators are summarized below, with their meanings. Pay particular attention to the equality operator; it consists of two equal signs, not just one. Table 2: Relational Operators Operator Meaning > Greater than >= Greater than or equal to < Less than >’ 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 (number1). 3.7.6 Cascading of i/o operator: -  The multiple use of > in one statement is called cascading. Examples: - - cout

Use Quizgecko on...
Browser
Browser