Raspberry Pi Week 7 – Day 5 (Beginning Object Orientated Programming (OOP))
C++ is very similar to C in terms of syntax and design. It was developed by a Danish computer scientist named Bjarne Stroustrup to overcome some of the limitations of the C programming language. One of the main objectives of C++ was to add object orientation to the C language. Object Object-oriented programming or OOP is a design philosophy that uses the concept of objects. Before this programming was historically procedural meaning it would take read the program at the top of the code and continue through each line. The input, process it and output the data. Using objects allows the programmer to build, combine and re-use objects. An example of an object would be a Dog. A dog has characteristics that determine that it is a dog such as 4 legs, they can bark, they eat and they can walk. A cat can also do most of these things except bark. The cat and the dog are animals and therefore share common traits of animals. An animal object may be have created with common information such as 4 legs, eat and walk. The dog and the cat can use these characteristics with minimal programming because an animal already knows how to eat and walk. All you would need to do is add the programming that will enable the dog to bark and the cat to meow.
An object is basically a “thing”, a something that can perform actions and has state. State means something name describes the object such as a name, a colour, the breed or the size. Actions are what define the objects behaviour. A dog can eat a sandwich. A dog is an object and a sandwich is an object. Everything you look at is an object and can be described by its actions, also known as behaviours and its properties. An example of object behaviour could be to walk, roll, run, light up, produce sound or wag tail. In C++ you will use a class to define your objects. A class represent the type of object by creating a blue print of what the object can do. For example, think of a car production plant. After they have decided that they need a new car they will first design the car using a CAD program. When they have finalised the design they will produce many cars based on this blue print design. Think of a class as the CAD drawing from which every object is built from.
A class in C++ looks like this. class Car { } Even cars are made up of other objects. Wheels, windows, seats, doors etc. are all objects. What this means is that the car object can be made up of other objects.
Object-oriented programming is an in depth subject that we will dive into at a later stage but for now just keep in mind that C++ can use this method. OOP makes programming easier by breaking larger problems into smaller more manageable problems. Remember that Object-oriented programming represents the concept of objects that uses data fields and methods. OOP is a fascinating area of computer science that we will explore again in depth later.
|