Segment 1
Segment 1
Content:
Introduction to OOP
C++ Console I/O
Introduction to Classes and Objects
Basic concept of Object Oriented Programming
Difference between Structured Programming and Object Oriented Programming
Difference between C/C++
Benefits of OOP
Characteristics of Procedure Oriented Programming
Characteristics of Object Oriented Programming
Applications of Object Oriented Programming
About C++:
It is universal language of programmers.
It is the language that will create the high-performance software.
professional programmer must know.
C++ was invented in 1979 by Bjarne Stroustrup at Bell Laboratories in Murray
Hill, NewJersey. Initially it was called "C with classes." The name was changed to
C++ in 1983.
OOP:
Object Oriented programming (OOP) is a programming methodology that relies on the concept of
classes and objects. “Object-Oriented Programming" refers to a programming model based on
objects and classes, instead of just functions and procedures.It is used to structure a software program
into simple, reusable pieces of code, which are used to create individual instances of objects.So,
complexity is reduced and the programmer can manage larger programs.Most modern programming
languages including Java, C++,C Sharp and PHP, are object-oriented Programming languages.
Class:
A class is a blueprint or template from which objects are created.It is a building block of Object-
Oriented programming. 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 object of that class.
A Class is a user-defined data-type which has data members and member functions.
Example: Smartphone is a class, Object will be e.g., Redmi 5, 6, 7 etc.
Object:
Object is an instance of a class. All data members and member functions of the class can be
accessed with the help of objects. When a class is defined, no memory is allocated, but memory is
allocated when an object is created.
Class and Object Declaration Void charge()
// Class Declaration {
Class Smartphone { //Member Function
Public: String model; // data member }
Public: int price; // data member
Public: int memorySize; // data member }
//Object declaration
Void restart() int main(){
{ Smartphone redmi;
//Member Function redmi.model=”Note7 Pro”.
} redmi.price=21000;
redmi.charge();
redmi.restart();
Encapsulation:
Encapsulation is one of the concepts in OOPs. Encapsulation means to enclose data within an
object.It is the process that binds together the data and code into a single unit and keeps safe from
outside interference and misuse. In this process, the data is hidden from other classes and can be
accessed only through the current class’s methods. Hence, it is also known as data hiding.
Encapsulation acts as a protective wrapper that prevents the code and data from being accessed by
outsiders.Encapsulation is used for access restriction to class members and methods. Access
modifier keywords are used for encapsulation in object oriented programming. For example,
encapsulation in OOP is achieved using private, protected and public keywords.
Example:
When creating a class to represent a person, you may define private data, such as the person's Social
Security Number. You can encapsulate this data as a private variable in the class, which means
outside code can't access it. If you write a method in the person class to perform a bank transaction,
the function could access the data variable as necessary. In this example, the person's private data is
well-encapsulated within the class.
Abstraction :
Data abstraction is one of the most essential and important features of object-oriented programming
in C++. 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.
Consider a real-life example of a man driving a car. The man only knows that pressing the
accelerators will increase the speed of the car or applying brakes will stop the car but he does not
know about how on pressing accelerator the speed is actually increasing, he does not know about
the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is
what abstraction is.
For example, consider the pow() method present in math.h header file. Whenever we need to
calculate thepower of a number, we simply call the function pow() present in the math.h header file and
pass the numbers as arguments without knowing the underlying algorithm according to which the
function is actually calculating the power of numbers.
Polymorphism :
Polymorphism is an important concept of object-oriented programming. The word polymorphism
means having many forms. It simply means more than one form. That is, the same entity (function
or operator) behaves differently in different scenarios.
Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a classroom,
mother or daughter in a home and customer in a market. Here, a single person is behaving differently
according to the situations.
Types of polymorphism :
Example:
Inheritance:
Inheritance is one of the most important features of Object-Oriented Programming. The process of
obtaining the data members and methods from one class to another class is known as
inheritance.The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is a feature or a process in which, new classes are created from the
existing classes. The new class created is called “derived class” or “child class” and the existing
class is known as the “base class” or “parent class.
To avoid redundancy and increase the re-usability of a class, we used inheritance like in the above
example.
Types of Inheritance:
Based on number of ways inheriting the feature of base class into derived class it has several types
they are:
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel Inheritance
Hybrid inheritance
Benefits of OOP:
Benefits of OOP include
Reusability: Code can be reused through inheritance, that means a team does not have to write the
same code multiple times.
Productivity: The productivity of codes increases through the use of Object-Oriented
Programming. This is because the OOP has provided so many libraries that new programs have
become more accessible. Also, as it provides the facility of code reusability, the length of a code is
decreased, further enhancing the faster development of newer codes and programs.
Easily upgradable and scalable: Programmers can implement system functionalities
independently.
Security: Using encapsulation and abstraction, complex code is hidden,
Troubleshooting is easier with the OOP language: Suppose the user has no idea where the bug lies
if there is an error within the code. Also, the user has no idea where to look into the code to fix the
error. This is quite difficult for standard programming languages. However, when Object-Oriented
Programming is applied, the user knows exactly where to look into the code whenever there is an
error. There is no need to check other code sections as the error will show where the trouble lies.
Improved software maintainability: Object oriented software is also easier to maintain. Since the
design is modular, part of the system can be updated in case of issues without a need to make large-
scale changes.
Applications of OOP:
Below are some applications of OOPs:
Real-Time System design: Real-time system inherits complexities and makes it difficult to build
them.OOP techniques make it easier to handle those complexities.
Hypertext and Hypermedia: Hypertext is similar to regular text as it can be stored, searched, and
edited easily. Hypermedia on the other hand is a superset of hypertext. OOP also helps in laying the
framework for hypertext and hypermedia.
AI Expert System: These are computer application that is developed to solve complex problems
which arefar beyond the human brain. OOP helps to develop such an AI expert System
Office automation System: These include formal as well as informal electronic systems that
primarily concerned with information sharing and communication to and from people inside and
outside the organization. OOP also help in making office automation principle.
Neural networking and parallel programming: It addresses the problem of prediction and
approximation of complex-time varying systems. OOP simplifies the entire process by simplifying
the approximation andprediction ability of the network.
Stimulation and modeling system: It is difficult to model complex systems due to varying
specifications of variables. Stimulating complex systems require modeling and understanding
interaction explicitly. OOPprovides an appropriate approach for simplifying these complex models.
Object-oriented database: The databases try to maintain a direct correspondence between the real
world and database object in order to let the object retain it identity and integrity.
Client-server system: Object-oriented client-server system provides the IT infrastructure creating
object- oriented server internet(OCSI) applications.
CIM/CAD/CAM systems: OOP can also be used in manufacturing and designing applications as it
allows people to reduce the efforts involved. For instance, it can be used while designing blueprints
and flowcharts.So it makes it possible to produce these flowcharts and blueprint accurately.
C C++
Data and functions are separated in C Data and functions are encapsulated
because it is a procedural programming together in form of an object in C++.
language.
Functions in C are not defined inside Functions can be used inside a structure
structures. in C++.
Namespace features are not present inside Namespace is used by C++, which avoid
the C. name collisions.
Header file used by C is stdio.h. Header file used by C++ is iostream.h.
Virtual and friend functions are not Virtual and friend functions are supported
supported by C. by C++.
scanf() and printf() functions are used for cin and cout are used for input/output in
input/output in C. C++.
Class definitions – Basic building blocks OOP and a single entity which has data and operations on
data together
Objects – The instances of a class which are used in real functionality – its variables and operations
Abstraction – Specifying what to do but not how to do ; a flexible feature for having a overall view
of an object’s functionality.
Encapsulation – Binding data and operations of data together in a single unit – A class adhere this
feature
1. Cout<<” x=”<<x;
2. int m=5; int n=10; int s=m+n;
3. Std:: cin>>x>>y; or cin>>x>>y;
4. Cout<<”/n Name:”<<name;
5. Cout<<”Enter value:”; cin>>x;
6. /*Addition*/ int z=x+y;
2.
#include <iostram>
Using namespace std;
int main(0){
int math=90;
Int physics=99;
Int chemistry=80;
Cout<<math<<” ”<<physics<<” ”<<chemistry<<endl;
}
Prepared By