Object Oriented Programming
70Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" in the design of computer programs.
- An object is a data structure that contains data and methods. Data can be any kind of stored information such as numbers, characters, strings, or other user-defined structures. Methods are functions of the object that manipulate this data, recieve input, and return an output.
ex. Fido is an object that represents my dog. Its data members are height, weight, and color. It contains the methods eat(), bark(), and run(). Calling eat() it causes Fido to increase its weight, and calling run() causes Fido to decrease its weight.
- An object is specified by a class definition. This class definition is a general outline to what an object can do, and what information it can store.
ex. Fido is an object that is outlined by the Dog class. We say that Fido is an instance of Dog.
// sample class definition in C++
class Dog
{
private:
int height;
int weight;
char color;
public:
void eat(string food);
void run();
void bark();
};
// creating an object (instance of a class)
Dog fido = new Dog();
// access methods of fido
fido.eat("pizza");
fido.run();
The main concepts of OOP are: Information Hiding, Encapsulation, Inheritance, and Polymorphism.
- Information Hiding is the idea that all the data members are kept private, so that changes to the data can only occur through the methods of the object. This ensures that outside functions can not make unexpected changes to the data.
ex. In an ATM system, you wouldn't want your balance to be changed by just any function. Instead, only the deposit() and withdrawl() methods can make changes to your balance.
// Assume balance is data in the BankAccount class. // If balance is defined as private, the following would produce an error. balance = balance + 500; // instead, use the deposit method to increase the value of balance deposit(500);
- Encapsulation refers to the concept that, throught
abstraction, we can define a method that will produce a given result
regardless of its exact implementation. This creates a model that is
similar to a capsule. As long as the user knows how to use the
interface, it does not matter what the underlying structure is.
ex. The sort() function will return a sorted list regardless of what method of sorting is used in the implementation
-Inheritance is the idea that one class (child) can inherit the properties of another class (parent).
ex. the Dog class from the earlier example could have been the child of a parent class called Animal. Since dogs are animals, the dog would inherit all the properties of the Animal class. An animal would pass on the attribues height and weight, and the method eat(), since these are universal to all animals. However, the bark() method must be in the Dog class because it is not universal to all Animals. This forms an "is - a" relationship because you can say a dog "is an" animal. However, the Pencil class could not inherit from the Animal class because a pencil is not an animal.
- Polymorphism is the idea that the methods of a child class can share the same name as methods from a parent class. If this is the case, the method from the child class will take the place of the one from the parent.
ex. Both the Animal class and the Dog class could have the method called eat(). However, you may want the Dog's version of eat() to take preference since a dog would eat differently than a whale would.
- Object-Oriented Programming - Wikipedia, the free encyclopedia
Wikipedia Entry for Object-Oriented Programming
PrintShare it! — Rate it: up down flag this hub









Sunny says:
2 months ago
Wow! Was really helpful. Thanks npc0mpl3te.