unit-1.1
unit-1.1
Advantages of OOPS
1. 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.
6. Solving problems
Problems can be efficiently solved by breaking down the problem into
smaller pieces. If a complex problem is broken down into smaller pieces
or components, it becomes a good programming practice. Considering
this fact, OOPS utilizes this feature where it breaks down the code of the
software into smaller pieces of the object into bite-size pieces that are
created one at a time. Once the problem is broken down, these broken
pieces can be used again to solve other problems. Also, the more minor
codes can get replaced through the modules with the same interface
having the implementation details.
7. Security
Because of the concept of data abstraction in OOPS, only a limited
amount of data is shown to the user. The rest data is not exposed while
exposing only the required amount of data. Therefore, it allows the
maintenance of security. The concept of abstraction is used to hide the
complexity from other users and demonstrate the element’s information
as per the requirements. It also helps in avoiding repetitive code.
Another concept provided in OOPS is the feature of encapsulation that
allows the protection of the data in the classes from getting accessed by
the system. All the internal contents in the class can be safeguarded. In
Java, encapsulation is mainly used for restricting access to the class
fields directly while setting all the fields of the class to private.
The code in the OOPS is an easy maintenance coding due to the
presence of a coding base in a central way. Therefore it is easy to create
procedure code that can be easily maintained.
A lot of benefits are further associated with the use of Object-Oriented
Programming.
Syntax
class class_name {
data_type data_name;
return_type method_name(parameters);
}
Object − An object is an instance of a class. It is an entity with characteristics and behaviour
that are used in the object oriented programming. An object is the entity that is created to
allocate memory. A class when defined does not have memory chunk itself which will be
allocated as soon as objects are created.
Syntax
class_name object_name;
Example
#include<iostream>
using namespace std;
class calculator {
int number1;
int number2;
char symbol;
public :
void add() {
cout<<"The sum is "<<number1 + number2 ;
}
void subtract() {
cout<<"The subtraction is "<<number1 - number2 ;
}
void multiply() {
cout<<"The multiplication is "<<number1 * number2 ;
}
void divide() {
cout<<"The division is "<<number1 / number2 ;
}
calculator (int a , int b , char sym) {
number1 = a;
number2 = b;
symbol = sym;
switch(symbol){
case '+' : add();
break;
case '-' : add();
break;
case '*' : add();
break;
case '/' : add();
break;
default : cout<<"Wrong operator";
}
}
};
int main() {
calculator c1(12 , 34 , '+');
}
Output
The sum is 46
Encapsulation In object oriented programming, encapsulation is the concept of wrapping
together of data and information in a single unit. A formale defination of encapsulation would
be: encapsulation is binding togather the data and related function that can manipulate the
data.
Let’s understand the topic with an easy real life example,
In our colleges, we have departments for each course like computer science, information tech. ,
electronics, etc. each of these departments have their own students and subjects that are kept
track of and being taught. let's think of each department as a class that encapsulates the data
about students of that department and the subjects that are to be taught. Also a department has
some fixed rules and guidelines that are to be followed by the students that course like timings,
methods used while learning, etc. this is encapsulation in real life, there are data and there are
ways to manipulate data.
Due to the concept of encapsulation in object oriented programming another very important
concept is possible, it is data abstraction or Data Hiding. it is possible as encapsulating hides
the data at show only the information that is required to be displayed.
Polymorphism The name defines polymorphism is multiple forms. which means polymorphism
is the ability of object oriented programming to do some work using multiple forms. The
behaviour of the method is dependent on the type or the situation in which the method is called.
Let’s take a real life example, A person can have more than one behaviour depending upon the
situation. like a woman a mother, manager and a daughterAnd this define her behaviour. This is
from where the concept of polymorphism came from.
In c++ programming language, polymorphism is achieved using two ways. They are operator
overloading and function overloading.
Operator overloading In operator overloading and operator can have multiple behaviour in
different instances of usage.
Function overloading Functions with the same name that can do multiple types based on
some condition.
Inheritance it is the capability of a class to inherit or derive properties or characteristics other
class. it is very important and object oriented program as it allows reusability i.e. using a
method defined in another class by using inheritance. The class that derives properties from
other class is known as child class or subclass and the class from which the properties are
inherited is base class or parent class.
C plus plus programming language supports the following types of inheritance
• single inheritance
• multiple inheritance
• multi level inheritance
• Hierarchical inheritance
• hybrid inheritance
Abstraction Data abstraction or Data Hiding is the concept of hiding data and showing only
relevant data to the final user. It is also an important part object oriented programing.
let's take real life example to understand concept better, when we ride a bike we only know that
pressing the brake will stop the bike and rotating the throttle will accelerate but you don't know
how it works and it is also not think we should know that's why this is done from the same as a
concept data abstraction.
What is C++
C++ is a general purpose, case-sensitive, free-form programming language that supports
object-oriented, procedural and generic programming.
C++ is a middle-level language, as it encapsulates both high and low level language features.
C++ history
History of C++ language is interesting to know. Here we are going to discuss brief history of
C++ language.
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories
of AT&T (American Telephone & Telegraph), located in U.S.A.
C++ programming is "relative" (called a superset) of C, it means any valid C program is also a
valid C++ program.
No. C C++
7) In C, scanf() and printf() are C++ mainly uses stream cin and
mainly used for input/output. cout to perform input and output
operations.
10) C does not provide the feature of C++ supports the feature of
namespace. namespace.