Object Oriented Programming (3)
Object Oriented Programming (3)
rogramming
What is OOP?
Look at the following illustration to see the difference between class and objects:
Create a Class
To create a class, use the class keyword
Example
Create a class called "MyClass":
};
To access the class attributes (myNum and myString), use the dot syntax
(.) on the object:
Multiple Objects
You can create multiple objects of one class:
Example
// Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};
int main() {
// Create an object of Car
Encapsulation
Encapsulation in C++ is defined as the wrapping up of data and information in 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. Now,
● The finance section handles all the financial transactions and keeps records of all
the data related to finance.
● Similarly, the sales section handles all the sales-related activities and keeps
records of all the sales.
Two Important property of Encapsulation
1. We can not access any function from the class directly. We need an object to
access that function that is using the member variables of that class.
2. The function which we are making inside the class must use.
3. If we don’t make a function inside the class which is using the member variable
of the class then we don’t call it encapsulation.
4. Encapsulation improves readability, maintainability, and security by grouping
data and methods together.
Information Hiding
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
A. Function Overloading
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is
known as Function Overloading.
Functions can be overloaded by changing the number of arguments
or/and changing the type of arguments.
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a
data type, this ability is known as operator overloading. For example, we
can make use of the addition operator (+) for string class to concatenate
two strings
Function Overriding occurs when a derived class has a definition for one
of the member functions of the base class. That base function is said to
be overridden.
Unified Modeling Language (UML)
Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to
define a standard way to visualize the way a system has been designed. It is quite similar to blueprints
used in other fields of engineering. UML is not a programming language , it is rather a visual language.
UML helps in specifying, visualizing, constructing, and documenting the artifacts of software systems.
Class Diagram
The most widely use UML diagram is the class diagram. It is the building
block of all object oriented software systems. We use class diagrams to
depict the static structure of a system by showing system’s classes, their
methods and attributes. Class diagrams also help us identify relationship
between different classes or objects.
Composite Structure Diagram
form an architecture, the software architect will take several factors into consideration:
se case view
esign view
mplementation view
ocess view
evelopment view
UNIT- 2 Basic Structural Modeling
The main aim of OOP is to bind together the data and the functions that operate on them so that no other part
of the code can access this data except that function.
There are some basic concepts that act as the building blocks of OOPs i.e.
1. Class
2. Objects
3. Encapsulation
4. Abstraction
5. Polymorphism
6. Inheritance
7. Dynamic Binding
8. Message Passing
A Class is a user-defined data type that has data members and member functions.
Data members are the data variables and member functions are the functions used to
manipulate these variables together these data members and member functions define the
properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage, etc and
member functions can apply brakes, increase speed, etc.
t
ct is an identifiable entity with some characteristics and behavior. An Object is an
e of a Class. When a class is defined, no memory is allocated but when it is
ated (i.e. an object is created) memory is allocated.
apsulation
rmal terms, Encapsulation is defined as wrapping up data and information under a
e unit. In Object-Oriented Programming, Encapsulation is defined as binding together
ata and the functions that manipulate them.
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.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create
a new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class
amic Binding
amic binding, the code to be executed in response to the function call is decided at
e. C++ has virtual functions to support this. Because dynamic binding is flexible, it
the drawbacks of static binding, which connected the function call and definition at build