What would you need to do to turn your program into an Object Orientated program
ID: 3747496 • Letter: W
Question
What would you need to do to turn your program into an Object Orientated program?
You should write a detailed answer for the question including your reasoning and providing code and examples to support your statements. Make sure you defend your position. At the minimum you should describe your position and findings in at least three fully formed scholarly paragraphs that include what you did to research the question (you should have spent some reasonable time searching for an answer) and your answer to the question.
Explanation / Answer
There are many paradigms that we can write our code into. There is Procedural, functional, object oriented, structural, imperative etc. Out of all this, the most used till now is Procedural and Object Oriented(Functional programming has been gaining popularity lately).
So, here we will talk about Object Oriented Programming and what we can do to refactor our program to an Object Oriented Program. As the name suggests, this programming paradigm is more focused on Objects rather than logic. An object is a real life entity that has some feature and also, method/function. OOPs has four main concepts: Abstraction, Encapsulation, Inheritance and Polymorphism.
Abstraction is the hiding of the complexity of the data and showing only the relevant details. You can use a fridge, but does it show how it functions from the inside? That's Abstraction.
Encapsulation is the wrapping up of data in a single unit. A picnic basket encapsulates and protects the food that is inside.
Inheritance is a way in which the child classes can use features and functions of the parent classes. Just like family hierarchy.
Polymorphism is the ability of an object to take many forms, or respond to the same message in different ways. Method overloading is an example.
Understand the fact that object oriented programming is more about binding the data and then using it. It increases modularity and organisation and helps in good and efficient use of objects when needed.
Now, when we turn a program into OOP, we need to make sure that it uses any of these concepts and also, follows classes and objects.
Here are some ways in which we can do it:
1) In case the program has structs, change them to classes. To use them, access them by making objects of classes(structs have similar concept of usage).
For eg: Let's take this program in C++, written with structs.
#include <iostream>
using namespace std;
struct Class1{
int data1;
float d1;
};
struct Class2{
int data2;
float f2;
};
int main(){
Class1 c1;
Class2 c2;
cout<<"Enter data 1:";
cin>>c1.data1;
cout<<"Enter data 2:";
cin>>c2.data2;
return 0;
}
Now, let us remake the same program with classes:
#include <iostream>
using namespace std;
class Class1{
public:
int data1;
float d1;
};
class Class2{
public:
int data2;
float f2;
};
int main(){
Class1 c1;
Class2 c2;
cout<<"Enter data 1:";
cin>>c1.data1;
cout<<"Enter data 2:";
cin>>c2.data2;
return 0;
}
2) Bind all similar data and functions into relatable/relevant classes. For example, in case there is a program for getting employee and student details, keep employee variables and functions in a single class and student variables and functions in another class. This will make the program modular and organized.
3) Try to implement the OOPs concepts.
4) Use access specifiers. There are three of them: public, private and protected. The default one is public.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.