Object-Oriented Programming
In C++ Object-orientedprogramming revolves around data. The main programming unit of OOP is the object. An object is a representation of a real-time entity and consists of data and methods or functions that operate on data. This way, data, and functions are closely bound and data security is ensured.
Inheritance
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 Using inheritance object of one class can inherit or acquire the properties of the object of another class. Inheritance provides reusability of code.
As such we can design a new class by acquiring the properties and functionality of another class and in this process, we need not modify the functionality of the parent class. We only add new functionality to the class.
#include
Using namespace std;
int main()
{
cout <<"C++ is better then C ";
US OF CLASS
#include// include header file
using namespace std;
class person
{
char name[30];
Int age;
public:
void getdata(void);
void display(void);
};
void person :: getdata(void)
{
cout << “Enter name: “;
cin >> name;
cout << “Enter age: “;
cin >> age;
}
Void person : : display(void)
{
cout << “\nNameame: “ << name;
cout << “\nAge: “ << age;
}
Int main()
{
person p;
p.getdata();
p.display();
return 0;
} //end of example
In C++ Object-orientedprogramming revolves around data. The main programming unit of OOP is the object. An object is a representation of a real-time entity and consists of data and methods or functions that operate on data. This way, data, and functions are closely bound and data security is ensured.
As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. 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.
OOP, everything is represented as an object and when programs are executed, the objects interact with each other by passing messages. An object need not know the implementation details of another object for communicating
OOP supports various features which are listed below:
• Classes
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Classes & Objects
An object is a basic unit in object-oriented programing. An object contains data and methods or functions that operate on that data. Objects take up space in memory.
A class, on the other hand, is a blueprint of the object. Conversely, an object can be defined as an instance of a class. A class contains a skeleton of the object and does not take any space in the memory.
• A Class is a user-defined data-type which has data members and member functions.
• Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behaviour of the objects in a Class.
• Object take up space in memory and have an associated address like a record in pascal or structure or union in C.
• When a program is executed the objects interact by sending messages to one another.
Abstraction
Abstraction is the process of hiding irrelevant information from the user. For Example, when we are driving the car, first we start the engine by inserting a key. We are not aware of the process that goes on in the background for starting the engine.
Using abstraction in programming, we can hide unnecessary details from the user. By using abstraction in our application, the end user is not affected even if we change the internal implementation.
Inheritance
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 Using inheritance object of one class can inherit or acquire the properties of the object of another class. Inheritance provides reusability of code.
As such we can design a new class by acquiring the properties and functionality of another class and in this process, we need not modify the functionality of the parent class. We only add new functionality to the class.
• 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 sub class is called Base Class or Super class.
• 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. By doing this, we are reusing the fields and methods of the existing class
Polymorphism
Polymorphism means many forms.
Polymorphism is an important feature of OOP and is usually implemented as operator overloading or function overloading. Operator overloading is a process in which an operator behaves differently in different situations.
Similarly, in function overloading, the same function behaves differently in different situations.
C++ supports operator overloading and function overloading.
• Operator Overloading: The process of making an operator to exhibit different behaviours in different instances is known as operator overloading.
• Function Overloading: Function overloading is using a single function name to perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.
Polymorphism means many forms.
Polymorphism is an important feature of OOP and is usually implemented as operator overloading or function overloading. Operator overloading is a process in which an operator behaves differently in different situations.
Similarly, in function overloading, the same function behaves differently in different situations.
C++ supports operator overloading and function overloading.
• Operator Overloading: The process of making an operator to exhibit different behaviours in different instances is known as operator overloading.
• Function Overloading: Function overloading is using a single function name to perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.
Dynamic Binding
OOP supports dynamic binding in which function call is resolved at runtime. This means that the code to be executed as a result of a function call is decided at runtime. Virtual functions are an example of dynamic binding.
Message Passing
In OOP, objects communicate with each other using messages. When objects communicate, information is passed back and forth between the objects. A message generally consists of the object name, method name and actual data that is to be sent to another object.
Advantages Of OOP
Let us discuss some of the advantages of OOP.
#1) Reusability
OOP allows the existing code to be reused through inheritance. We can easily acquire the existing functionality and improve on it without having to rewrite the code again. This results in less bloated code.
#2) Modularity
As we modularize the program in OOP, it’s easy to modify or troubleshoot the program if a problem occurs or new feature or enhancement is to be added. Modularization also helps in code clarity and makes it more readable.
#3) Flexibility
OOP helps us with flexible programming using the polymorphism feature. As polymorphism takes many forms, we can have operators or functions that will work with many objects and thus save us from writing different functions for each object.
#4) Maintainability
Maintaining code is easier as it is easy to add new classes, objects, etc without much restructuring or changes.
#5) Data and Information Hiding
OOP aids us in data hiding thereby keeping information safe from leaking. Only the data that is required for the smooth functioning of the program are exposed to the user by hiding intrinsic details.
Conclusion
OOP is the most important and flexible programming paradigm of modern programming. It is specifically useful in modeling real-world problems and thus is very popular.
A Simple C++ program
A Simple C++ program
#include
Using namespace std;
int main()
{
cout <<"C++ is better then C ";
return 0;
}
Output.
C++ is better then C
}
Output.
C++ is better then C
US OF CLASS
#include
using namespace std;
class person
{
char name[30];
Int age;
public:
void getdata(void);
void display(void);
};
void person :: getdata(void)
{
cout << “Enter name: “;
cin >> name;
cout << “Enter age: “;
cin >> age;
}
Void person : : display(void)
{
cout << “\nNameame: “ << name;
cout << “\nAge: “ << age;
}
Int main()
{
person p;
p.getdata();
p.display();
return 0;
} //end of example
The output of program is:
Enter Name: Ravinder
Enter age:30
Name:Ravinder
Age: 30
Thanks.
No comments:
Post a Comment