Object-Oriented Programming (OOP) Notes PDF

Document Details

LawfulVerism

Uploaded by LawfulVerism

GLS University

Prof. Neha Vaswani, Prof. Rima Shelat, Dr. Shital Patel

Tags

object-oriented programming programming paradigms C++ computer science

Summary

These lecture notes cover fundamental concepts of Object-Oriented Programming (OOP), including programming paradigms (like monolithic and procedural), object-oriented programming advantages/disadvantages, and relevant C++ programming information. The document also provides a brief overview of different programming concepts, like classes, objects, data hiding, and polymorphism.

Full Transcript

GLS UNIVERSITY iMSCIT Sem-III 221601301 Object Oriented Programming Prof. Neha Vaswani Prof. Rima Shelat Dr. Shital Patel Unit – I Overview of C++ Language Overview of C++ Language  Introduction to Programming Paradigm...

GLS UNIVERSITY iMSCIT Sem-III 221601301 Object Oriented Programming Prof. Neha Vaswani Prof. Rima Shelat Dr. Shital Patel Unit – I Overview of C++ Language Overview of C++ Language  Introduction to Programming Paradigms  Features of OOP  Structure of C++ Program  Writing the First C++ Program  Header Files used in C++ Program  Compiling and Execution C++ Program  Input/Output Statements  Operators in C++  Conditional Branching Statements  Iterative Statements  Break, Continue and GOTO Statements Introduction to Programming Paradigms  A programming paradigm is a style, or “way,” of programming.  Paradigm can also be termed as method to solve some problem or do some task. Programming paradigm is an approach to solve problem using some programming language  The style of writing programs and the set of capabilities and limitations that a particular programming language has depends on the programming paradigm it supports. I. Monolithic Programming II. Procedural Programming III. Object Oriented Programming Monolothic Programming  Programs written in assembly and BASIC language.  Consist of global data.  Sequential code with jump functions.  Usage of GOTO statements.  It has only one program module.  It doesn't support the concept of subroutines  Program size is large  Used only for small and simple applications Monolothic Programming Procedural Programming  It is based upon the concept of procedure calls, in which statements are structured into procedures (also known as subroutines or functions).  It used for large programs.  Procedural programming uses a list of instructions to tell the computer what to do step-by-step.  It used in High level programming languages like C, COBOL and FORTRAN etc.  Data is not secure, as it is freely moved from one function to another.  Every function can access the global data so they can change their values, leaving the resultant data, with more bugs.  Difficult to identify what data is used by which function Procedural Programming Advantages of Procedural Programming  Relative simplicity, and ease of implementation of compilers and interpreters.  The ability to re-use the same code at different places in the program without copying it.  An easier way to keep track of program flow.  Needs less memory. Disadvantages of Procedural Programming  Data is exposed to whole program, so no security for data.  Difficult to relate with real world objects.  Importance is given to the operation on data rather than the data. Object Oriented Programming  Java, C++, C#.Net are languages of object oriented programming.  Primary focus is on data. (data centric)  Combines both data and functions so that both can operate into a single unit. Such a unit is called a Class.  An object is an instance of a class which is treated like a built-in variable which holds many values.  Class in C++ is the same as structure in C; The difference is that class users can hide data through the private option.  Data does not flow freely around the system.  Functions ties data more closely and protects it from outside functions. Object Oriented Programming  Compilers prevent unauthorized functions from accessing or manipulating variables (data) in a program.  Programs are easy to design, maintain and enhance the software.  Objects can communicate with each other through functions.  New data and functions can be easily added whenever necessary.  Follows bottom-up approach in program design. Object Oriented Programming Object Oriented Programming  Everything is represented as an object, is known as truly object-oriented programming language.  Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology to design a program using classes and objects. Advantages of OOPS 1. It models real world well. 2. With OOP , programs are easy to understand. 3. OOP offers classes reusability. Already created classes can be reused without having to write again. 4. OOP facilitates quick Development as parallel development of classes is possible. 5. With OOP, program are easier to test manage and maintain. 6. Much suitable for large projects. Disadvantages of OOPS 1. With OOP , classes tend be overly generalized. 2. The relation among classes become artificial at times. 3. The OOP programs design is tricky. 4. Also one needs to do proper planning and proper design for OOP Programming. 5. To Program with OOP, Programmer need proper skills such as design skills programming skills, thinking in term of objects etc. Procedural vs Object Oriented Procedure Oriented Programming Object Oriented Programming Takes on the top-to-bottom approach Takes on the bottom-to-up approach Splits the tasks into modular forms Splits the tasks into objects that can be (functions). reused into other task. Primary focus on Functions Primary focus on data Emphasis is on doing things. (procedure) Emphasis is on data rather than procedure. Larger programs are divided into Larger programs are divided into smaller programs known as functions. smaller programs known as objects. Data move openly around the system Data is hidden and cannot be accessed from function to function. by external functions. Languages like COBOL, Fortan, C Languages like C++ Does not model real world problems. It models real world problems. Features of OOPS Classes The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. Objects An Object is an identifiable entity with some characteristics and behaviour. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. When a program is executed the objects interact by sending messages to one another. Each object contains data and code to manipulate the data. Polymorphism The word polymorphism means having many forms. A person at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. For example, In C we can not use same function name which is already used but C++ provides the feature to use that function name again with different signature. void sum(int a, int b) void sum(int a, int b, int c) Inheritance The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming. Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. Super Class: The class whose properties are inherited by a sub- class is called Base Class or Superclass. Inheritance supports the concept of “reusability” Abstraction Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. Real-life example of a man driving a car. The man only knows that pressing the accelerator will increase the speed of the car or applying brakes will stop the car but he does not know about the inner mechanism of the car or the implementation of an accelerator, brakes, etc. in the car. Encapsulation Encapsulation is defined as wrapping up data and information under a single unit. In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them. Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. Structure of C++ Program A C++ program can be developed from a basic structure. C++ provides the flexibility of writing a program with or without a class and its member functions definition. The general structure of C++ program with classes: 1.Documentation Section 2.Pre-processor Directives (i) Link Section (ii)Definition Section 3.Global Declaration Section 4.Class declaration or definition 5.Main C++ program function called main ( ) Cont. 1. Documentation Section : In Documentation section we give the Heading and Comments.  Single Line Comment: Comment can be given in single line by using "//". For Example: // Add Two Numbers (Heading of the program)  Multiple Line Comment: Comment can be given in multiple lines starting by using "". The general syntax is: 2. Pre-processor Directives These are compiler pre-processor statements. Link Section: In the Link Section, we can link the compiler function like cout, sqrt ( ), etc with the INCLUDE subdirectory having header files like iostream.h For example: #include Definition Section: The second section is the Definition section by using which we can define a variable with its value. For example: #define PI 3.142 3. Global Declaration Section  Declare some variables before starting of the main program or outside the main program. These variables are globally declared and used by the main function or sub function.  These variables are called global variables.  The general syntax is: data type vl,v2,v3………. vn;  Here data types are int, float, char, double etc. vl,v2,v3,.... vn is the list of global variables(v).  For example: int a, b, c; float x,y,z 4. Class Declaration Section A class is an organization of data and functions which operate on them. Data types are called data members and the functions are called member functions. The combination of data members and member functions constitute a data object or simply an object. The general syntax or general form of the class declaration is as follows: class { } 5. Main C++ program function called main ( )  The main function serves as the starting point for program execution.  It usually controls program execution by directing the calls to other functions or objects in the program Writing the First C++ Program // C++ program to display "Hello World" // Header file for input output functions #include using namespace std; int main() { // Prints hello world cout >) is combined with the object cin. Cin stands for console input device(keyboard). Syntax cin>>variable; Example: int b; cin>>b;  If there is more than one variable then accepted with single cin statement. Example: cin>>a>>b;  Cout Statement The C++ cout statement is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator or left shift operator (

Use Quizgecko on...
Browser
Browser