Document Details

WittyCommonsense

Uploaded by WittyCommonsense

Shree Swaminarayan College of Computer Science

Gaurang Bhatt

Tags

C++ programming object-oriented programming computer science programming languages

Summary

This document is an introductory unit on object-oriented programming (OOP) using C++. It covers the fundamental concepts of OOP, including objects, classes, inheritance, encapsulation, polymorphism, and abstraction. The document also provides an overview of C++ features, such as simple syntax, platform independence, portability, and case sensitivity.

Full Transcript

Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 Unit-1: Principles of Object-Oriented Programming 1. Introduction of OOP, Compare OOP with POP 2. Concept of OOP – Object, Class, In...

Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 Unit-1: Principles of Object-Oriented Programming 1. Introduction of OOP, Compare OOP with POP 2. Concept of OOP – Object, Class, Inheritance, Encapsulation, Polymorphism, Abstraction, Message Passing 3. Structure of C++ Program 4. Data type, Constant, Variable, Statement & Operators 5. Input/output statements 6. Declaration & Creation of Class and Object 7. Data Members and Member functions Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 1 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 1. Introduction of OOP C++ (C plus plus) is an object-oriented programming language. C++ is an advance or next version of C programming language. OOP (Object-oriented programming) language is a methodology or model to develop program using classes and objects. It was developed by Bjarne Stroustrup at AT & T Bell Laboratory in Murray Hill, New Jersey, USA in 1979. C++ is a case sensitive programming language. Most of the C concept applies to C++ also. The most important facilities that C++ adds is the concept of OOP's (Object Oriented Programming).  C++ Features C++ has following features and here we discuss some important features of C++. 1. Simple: C++ program is written in simple English language, so that it is very easy to understand and developed by programmer. 2. Platform dependent and machine independent: A language is said to be platform dependent, if the program is executing on the same operating system where it was developed and compiled. C++ is a platform dependent language. 3. Portability: It is the concept of carrying the instruction from one system into another system. In C++,.cpp file contain source code,.exe file contain application (executable file). When we write and compile any C++ program on window operating system that program can easily run on other window-based system. Hence, C++ is a portable language. 4. Case sensitive: C++ is a case sensitive programming language means lowercase letter is different from uppercase letter. 5. Compiler based: C++ is a compiler-based programming language, it means without compilation no C++ program can be executed. First, we need compiler, to compile the program (machine code) and then we can execute it. 6. Syntax based language: C++ is a strongly tight syntax-based programming language. If any language follows rules and regulation very strictly known as strongly tight syntax- based language 7. Mid-level programming language: C++ is also used for low level programming. It is used to develop system applications such as kernel, driver etc. C++ also support the feature of high-level language. That is why it is known as mid-level programming language. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 2 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 8. Structured programming language: C++ is a structured programming language it breaks the large problem into smaller parts. So, it is easy to understand and modify. 9. Memory Management: C++ supports the feature of dynamic memory allocation. In C++, we can free the allocated memory at any time by calling the free () function. 10. Object Oriented: C++ is object-oriented programming language. OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage large projects. Figure:1 C++ Features  C++ Advantages: There are many advantages of C++. Some of them are listed below: 1. C++ is much suitable for large projects. 2. C++ Programming is easy to maintain and modify. 3. C++ forced to declare functions so that the compiler can check their use. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 3 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 4. C++ has a feature called references that allows more convenient handling of addresses for function arguments and returned values. 5. Template feature reuses same source code with automatic modification for different classes. 6. The main advantage of C++ is that, it supports object-oriented features. 1.1 Compare OOP with POP Sr.No POP OOP 1 Basic Procedure/Structure Object oriented Programming oriented Programming 2 Approach Top-down. Bottom-up. 3 Division Large program is Entire program is divided into objects. divided into units called functions. 4 Support It Emphasizes It supports code reusability, modularity and simplicity, clarity, and flexibility. speed. 5 Entity accessing No access specifiers Access specifiers are "public", "private", mode available "protected". 6 Data hiding & There is no proper Data is hidden in three modes public, security way of hiding the data, private, and protected. hence data security so data is insecure increases. 7 Data sharing Global data is shared Data is shared among the objects through among the functions in the member functions. the program. 8 Encapsulation, There is no provision There is provision of encapsulation, inheritance and of encapsulation, inheritance and polymorphism polymorphism. inheritance and polymorphism 9 Overloading There is not support of There is support of operator and function concept operator and function overloading overloading 10 Friend functions No concept of friend There is a concept of friend function and or friend classes function. friend classes. Note: "friend" keyword is used only in C++ 11 Virtual classes or No concept of virtual Concept of virtual function appear during virtual function classes. inheritance 12 Examples C, VB, FORTRAN, C++, JAVA, VB.NET, C#, Python Pascal Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 4 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 2. Concept of OOP – Object, Class, Inheritance, Encapsulation, Polymorphism, Abstraction, Message Passing It is necessary to understand some of the concepts used extensively in object-oriented programming. These include: 1. Objects 2. Classes 3. Data Encapsulation and abstraction 4. Inheritance 5. Polymorphism 1. Objects: Objects are the basic run-time entities in an object-oriented system. It may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists. So, class variable is known as object. Programming problem is analyzed in terms of objects and nature of communication between them. Objects take up space in memory and have an associated address like a record in Pascal or a structure of C. When a program is executed, the objects interact by sending messages to one another. For example, if “customer” and “account” are two objects in a program, then the customer object may send a message to the account object requesting for the bank balance. Each object contains data and code to manipulate the data. It is sufficient to know the type of message accepted, and the type of response returned by the objects. Figure shows two notations that are popularly used in object-oriented analysis and design. class: STUDENT STUDENT Total DATA Name Date-of-birth Marks ………………… Average FUNCTIONS Total Average Display …………. Display …………. Two way of representing an object Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 5 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 Object: classname variablename; For example; student s1;  Classes: A class is a collection of objects of similar type. For example, mango, apple and orange are members of the class fruit. Classes are user-defined data types and behave like the built-in types of a programming language. The syntax used to create an object is no different than the syntax used to create an integer in C. For example, fruit mango; will create an object mango belonging to the class fruit.  Data abstraction and Encapsulation The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking features of a class. The data is not accessible to the outside world and only those functions which are wrapped 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 act of representing essential features without including the background details or explanations. For example, floating-point numbers are abstracted in programming language. You are not required to know how a floating-point number is represented in binary while assigning a value to it. 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 all the essential properties of the objects that are to be created. The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member functions. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 6 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1  Inheritance: Inheritance is the process by which objects of one class can share the properties of objects of another class. It supports the concept of hierarchical classification. Property inheritance In OOP, the concept 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 the existing one. The new class will have the combined features of both classes. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 7 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 The real appeal and power of the inheritance mechanism is that it allows the programmer to reuses class that is almost but not exactly what he wants and to tailor the class in such a way that it does not introduce any undesirable size effects into the rest of class.  Polymorphism: Polymorphism means ‘one name, multiple methods.’ The concept of polymorphism is implemented using the overloaded functions and operators. Operator Overloading means an operator to exhibit different behaviors in different instances is known as operator overloading. Function overloading means multiple functions with single name to perform different types of tasks are known as function overloading. The following figure shows the idea of polymorphism Shape Draw() Shape Shape Shape Draw(circle) Draw(box) Draw(triangle) Polymorphism  Dynamic binding: Dynamic binding is also known as late binding or run time polymorphism. Dynamic binding means to determine to call the function at run time. Dynamic binding is normally related with inheritance and polymorphism. C++ has virtual functions to support this. Because dynamic binding is flexible, it avoids the drawbacks of static binding, which connected the function call and definition at build time.  Message Passing: Objects are responsible to send and receive information through function calling which is known as message passing or message communication. Message passing involved the name of the object, the name of the function and the information to be sent. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 8 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 3. Structure of C++ Program Normally, C++ program files contain four sections as shown in the following image. These sections are combined into single file or may be placed into separate files and then compiled independently or jointly. Structure of C++ Program The first section is header file section. Some header files are necessary to include before declaration of class. It normally helps to perform to collect data (input though keyboard) and display data (output on screen). The second section is class declaration. Class definition decide building of program or creation of program. Third section is member function definition and it defines functionality of functions of class. Fourth section is main function and it is beginning of program. Normally we declare an object of class in main function. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 9 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1 4. Data type, Constant, Variable, Statement & Operators  Data Type: A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common data types are integer, character, and real. Although C has five basic built-in data types, it is not a strongly typed language, as are Java, and Python. C language permits almost all type conversions. For example, you may freely intermix character and integer types in an expression. Unlike a high-level language, C performs almost no run-time error checking. For example, no check is performed to ensure that array boundaries are not overrun. These types of checks are the responsibility of the programmer. Data types in C++ can be classified under the following characteristics shown in the following image. Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 10 of 31 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-1  C++ Data Type’s Size and range: 1. Primary Data Type or Primitive Data Type: Primitive data type means the data types which are operated at machine level or built-in data types or predefined data types. Some primitive or primary data types size and range are given here. Sr.No Data Type Data Size/Bytes Range 1 char 1 -128 to 127 2 unsigned char 1 0 to 255 3 signed char 1 -128 to 127 4 int 2 -32768 to 32767 5 unsigned int 2 0 to 65535 6 signed int 2 -31768 to 32767 7 short int 2 -31768 to 32767 8 unsigned short int 2 0 to 65535 9 signed short int 2 -32768 to 32767 10 long int 4 -2,147,483,648 to 2,147,483,647 11 signed long int 4 -2,147,483,648 to 2,147,483,647 12 unsigned long int 4 0 to 4294967295 13 float 4 3.4E-38 to 3.4E+38 OR -3.4×10^38 to 3.4×10^38 14 double 8 1.7E-308 to 1.7E+308 OR -1.7×10^308 to 1.7×10^308 15 long double 10 3.4E-4932 to1.1E +4932 OR -3.4^4932 to 1.1^4932 16 Wide character 2 or 4 1 (wchar_t) Note: The memory size of primary data types may change according to 32- or 64-bit operating system. we can get the size of data type by using sizeof() operator. We need to pass the name of data type into sizeof() operator as an argument. For example. cout

Use Quizgecko on...
Browser
Browser