


































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Dive into the fundamentals of Object-Oriented Programming (OOP) with this comprehensive set of notes. Perfect for students, developers, or anyone interested in learning about OOP, these notes cover essential concepts including classes, objects, inheritance, polymorphism, encapsulation, and more. Gain a solid understanding of OOP principles with clear explanations, real-world examples, and code snippets. These notes are ideal for study, reference, or quick review, offering a practical guide to mastering OOP in various programming languages. Whether you're a beginner or brushing up on your skills, this resource provides the knowledge you need to excel in software development. Get started on your journey to becoming an OOP expert with this valuable resource.
Typology: Slides
1 / 74
This page cannot be seen from the preview
Don't miss anything!
bject-oriented programming has a sweeping impact because it appeals at multiple levels and promises faster and cheaper development and maintenance. It follows a bottom-up approach to develop applications.
In this section, we will discuss in-depth what is object-oriented programming?
The word object-oriented is the combination of two words i.e. object and oriented. The dictionary meaning of the object is an article or entity that exists in the real world. The meaning of oriented is interested in a particular kind of thing or entity. In layman's terms, it is a programming pattern that rounds around an object or entity are called object-oriented programming.
The technical definition of object-oriented programming is as follows:
The object-oriented programming is basically a computer programming design philosophy or methodology that organizes/ models software design around data, or objects rather than functions and logic.
An object is referred to as a data field that has unique attributes and behavior. Everything in OOP is grouped as self-sustainable objects.
It is the most popular programming model among developers. It is well suited for programs that are large, complex, and actively updated or maintained. It simplifies software development and maintenance by providing major concepts such as abstraction, inheritance, polymorphism , and encapsulation. These core concepts support OOP.
A real-world example of OOP is the automobile. It more completely illustrates the power of object-oriented design.
.
The major concepts that we have discussed above are known as pillars of OOPs. There are four pillars on which OOP rests.
Abstraction Encapsulation Inheritance Polymorphism
Let's discuss each in detail.
The concept allows us to hide the implementation from the user but shows only essential information to the user. Using the concept developer can easily make changes and added over time.
The word polymorphism is derived from the two words i.e. ploy and morphs. Poly means many and morphs means forms. It allows us to create methods with the same name but different method signatures. It allows the developer to create clean, sensible, readable, and resilient code.
The above figure best describes the concepts of polymorphism. A person plays an employee role in the office, father and husband role in the home.
The OOPs concepts include the following:
An object is a real-world entity that has attributes, behavior, and properties. It is referred to as an instance of the class. It contains member functions, variables that we have defined in the class. It occupies space in the memory. Different objects have different states or attributes, and behaviors.
A class is a blueprint or template of an object. It is a user-defined data type. Inside a class, we define variables, constants, member functions, and other functionality. it binds data and functions together in a single unit. It does not consume memory at run time. Note that classes are not considered as a data structure. It is a logical entity. It is the best example of data binding. Note that a class can exist without an object but vice-versa is not possible.
The following figure best illustrates the class and object in OOP.
There are two types of cohesion, i.e. High and Low.
High cohesion is associated with several required qualities of software including robustness, reliability , and understandability. Low cohesion is associated with unwanted qualities such as being difficult to maintain, test, reuse, or even understand.
High cohesion often associates with loose coupling and vice versa.
Composition is one of the vital concepts in OOP. It describes a class that references one or more objects of other classes in instance variables. It allows us to model a has-a association between objects. We can find such relationships in the real world. For example, a car has an engine. the following figure depicts the same
The main benefits of composition are:
Reuse existing code Design clean APIs Change the implementation of a class used in a composition without adapting any external clients.
The association defines the relationship between the objects. Note that an object can be associated with one or more than one object. The relationship can be unidirectional or bidirectional. There are the following types of association.
One to One One to Many Many to One, and Many to Many
It is an advanced form of association in which each object has its own Lifecycle but there exists ownership as well. In other words, a relationship where a child can exist independently of the parent. It is also termed as has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.
Object-oriented programming is an evolutionary development in software engineering. Using OOP in software development is a good habit because it accomplishes the three major software engineering goals, as we have shown in the following figure.
OOP code is difficult to understand if you do not have the corresponding class documentation. In certain scenarios, these programs can consume a large amount of memory. Not suitable for small problems. Takes more time to solve problems.
There are various object-oriented programming languages are present. But we have enlisted some popular and widely used OOP languages.
According to the TIOBE index, the top twenty OOP languages are Java, C++, C#, Python, R, PHP, Visual Basic.NET, JavaScript, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Kotlin, Common Lisp, MATLAB, and Smalltalk.
Computer graphics applications Object-oriented database User-interface design such as windows Real-time systems Simulation and modeling Client-Server System Artificial Intelligence System CAD/CAM Software Office automation system
In this article, we will discuss the difference between the Tokens, Identifiers , and Keywords in C++. But before discussing their differences, we must know about the Tokens, Identifiers, and Keywords in C++ with their types and characteristics.
Tokens are the smallest individual pieces of a C++ program, serving as the syntax's building blocks. They are fundamental parts that the compiler recognizes and that define the structure and logic of a C++ codebase. Tokens are divided into several sorts, each with its role in program execution.
Tokens in C++ are classified into numerous types:
Keywords:
Keywords are predefined reserved terms in the C++ language that have unique meanings and functionalities. These terms have distinct meanings and cannot be altered or used as identifiers. Among the keywords in C++ are if, else, for, while, int, class, and return. They serve as the language's foundation, defining its core control structures and data types.
Identifiers:
Variables, functions, classes, and user-defined entities all have identifiers. The programmer creates these names, which serve as references to memory regions, allowing manipulation and interaction within the program.
Identifiers adhere to certain guidelines:
They can be characters (both uppercase and lowercase), numbers, or underscores. Identifiers must begin with a letter or a period. They should not be confused with keywords. In C++, identifiers are case-sensitive.
Constants:
In C++, constants are fixed values that do not change during program execution. Integer constants, floating-point constants, character constants, and string literals are some examples.
Operators:
Operators are symbols in C++ that perform specified operations on operands. Arithmetic operators *(+, -, *, /),* relational operators (, >, ==,!=) , logical operators (&&, ||,!) , assignment operators (=, +=, -=) , and others are examples. Operators assist in the execution of various computations and comparisons within a program.
Seperators and Delimiters:
Separators and delimiters are characters that are used to structure and separate various elements of the program. Braces, parentheses (), commas, semicolons; and other symbols are examples. They aid in the organization of code blocks and the definition of program grammar.
Literals:
Literals are explicit values that are utilized in the program. Numeric literals (integer or floating-point numbers), character literals (single characters enclosed in single quotes''), and string literals (sequences of characters encased in double quotations " ") are examples.
There are several types of identifiers in C++. Some main types of the identifiers are as follows:
Variable identifiers:
Variable IDs are names assigned to memory places where data is stored. They indicate values that can change while the program is running.
Function Identifiers:
Function identifiers are names assigned to code blocks that execute certain activities. These names are used to invoke and execute the corresponding code block.
Class Identifiers:
Class identifiers are names that are used to define new data types by creating classes. Classes include data and functions that operate on that data.
Constant Identifiers:
Constant identifiers are names assigned to values that stay constant throughout programme execution. They are made with the const keyword.
Keywords are reserved terms in C++ that have specialized definitions and functions inside the language. These phrases are essential in describing a C++ program's structure, control flow, data types, and functionality. Keywords cannot be redefined or used as identifiers because they are basic components of the language, and they have diverse functions in helping specific programming activities.
Keywords are essential in creating the syntax and logic of C++ programs. They are an important aspect of the grammar of the language and are restricted for special uses. C++ programmers utilize keywords to create data types, control flow structures, access modifiers, and other features. Keywords aid in outlining the program's behavior and functioning, assuring proper execution.
C++ features a set of predefined keywords that are reserved for specific activities. Some of the most important keyword categories are:
Data Type Keywords:
In C++, these keywords are used to specify different data types. int, char, float, double, bool: These are variable declaration keywords that represent integers, characters, floating-point numbers, and Boolean values. void: It indicates that a function returns no value.
Control Flow Keywords:
Control flow keywords are used to direct the execution flow of a program. if, otherwise: These keywords are associated with conditional branching. because, while: These keywords are used to regulate loops. case, break, and default: Switch-case statements utilize keywords to control program flow.
Modifiers and Specifiers Keywords:
These keywords change the behavior of variables, functions, or classes.
Const: It indicates that the value of an entity cannot be changed.
Static: A variable's value is retained between function calls.
public, private, and secure: These keywords are used to specify access levels in classes (part of the notions of Object-Oriented Programming).
Storage Class Keywords:
These keywords govern the duration and scope of variable storage. Storage class keywords include auto, register, static, extern, and mutable.
C++ contains additional keywords that serve a variety of functions.
return: It is a keyword for exiting a function and returning a value.
sizeof: It is a keyword that specifies the size of a data type or variable.
namespace, employing: Keywords for managing and utilizing namespace members.
Keywords for defining user-defined data types include class, struct , and enum.
Let us take an example to demonstrate Tokens, Keywords, and Identifiers in C++:
There are several difference between tokens, identifiers, and keywords in C++. Some main differences are as follows:
Aspect Tokens Identifiers Keywords
Definition
It is the smallest individual unit in C++ syntax.
User-defined names for program elements
These are predefined reserved words in C++.
Roles
These are used to define the structure and logic of a C++ codebase.
It represents variables, functions, and classes.
These are used to define specific functionalities in code.
Examples if, int, =, ; , +, 123, etc.
variableName, functionName, className
if, else, for, int, return, etc.
Types Keywords, operators, literals, etc. User-created names. Reserved words with specific meanings.
Categories
Keywords are a subset of tokens.
Identifiers are a type of token.
Keywords are a distinct token category.
Purpose Define language syntax and semantics.
Represent entities within the program
Reserved for specific language purposes
Usage Construct statements and expressions
Refer to memory locations.
Define control flow, data types, etc.
Cannot be used
It cannot be redefined or modified.
It can be created by the programmer.
It cannot be used as user- defined names.
Sequential Constructs, Decision Making Construct, Iteration / Loop
Construct, Arrays, Functions (User defined Function, Inline Function,
Function Overloading), User Defined Data Types (Structure, Union and
Enumeration).
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
There are 4 types of data types in C++ language.
Types Data Types
Basic Data Type int, char, float, double, etc
Derived Data Type array, pointer, etc
Enumeration Data Type enum
User Defined Data Type structure
underlying system, these types are particularly helpful when you want precise control over the size of numbers.
size of Operator: The sizeof operator is used to calculate a data type or variable's size (in bytes). For instance:
Character and String Types: The C++ language uses the char data type to represent characters. Wide characters (wchar_t) for expanded character sets, and the char16_t and char32_t types for Unicode characters are also introduced by C++. The std::string class or character arrays (char or wchar_t) are used to represent strings.
References and Pointers: Despite the brief mention of derived data types, pointers, and references are crucial concepts in C++. A variable that stores the memory address of another variable is known as a pointer. It permits the allocation and modification of dynamic memory. On the other hand, references offer another method of accessing a variable by establishing an alias. Pointers and references are essential for complicated data structures and more sophisticated memory management.
Enumeration Data Type: An enumeration is a user-defined data type made up of named constants (enumerators) , and it is specified by the enum data type. Enumerations are frequently used to increase the readability and maintainability of code by giving specified data names that make sense.
Structures and Classes as User-Defined Data Types: Although the struct was described in the example as a user-defined data type , C++ also introduces the concept of classes. Classes provide you with the ability to build user-defined types that are more complicated and have member variables and related methods (functions). They serve as the foundation of C++'s object- oriented programming.
Bit Fields: C++ offers a method to declare how many bits should be used by each component of a structure. It is helpful when working with packed data structures or hardware registers.
Data Type Modifiers: C++ supports the usage of data type modifiers like const, volatile, and mutable to modify the behavior of variables. For example, volatile denotes that a variable can be altered outside, but const makes a variable immutable.
Like other programming languages, array in C++ is a group of similar types of elements that have contiguous memory location.
In C++ std::array is a container that encapsulates fixed size arrays. In C++, array index starts from 0. We can store only fixed set of elements in C++ array.
A collection of related data items stored in adjacent memory places is referred to as an array in the C/C++ programming language or any other programming language for that matter. Elements of an array can be accessed arbitrarily using its indices. They can be used to store a collection of any type of primitive data type, including int, float, double, char, etc. An array in C/C++ can also
store derived data types like structures, pointers, and other data types, which is an addition. The array representation in a picture is provided below.
Code Optimization (less code) Random Access Easy to traverse data Easy to manipulate data Easy to sort data etc.
Fixed size
There are 2 types of arrays in C++ programming:
Let's see a simple example of C++ array, where we are going to create, initialize and traverse array.