15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Real-Life Programming Examples 249<br />

Real-Life Programming Examples<br />

To fully understand object-oriented <strong>programming</strong>, you need to see how to<br />

use OOP in a real <strong>programming</strong> language. Basically, the steps to using OOP<br />

involve<br />

✦ Defining an object with a class file<br />

✦ Creating an object based on a class<br />

✦ Using subprograms (methods) in an object<br />

✦ Inheriting an object<br />

✦ Using polymorphism to rewrite an inherited subprogram<br />

Book II<br />

Chapter 7<br />

Although the following examples use C++, don’t worry about the particular<br />

syntax of each <strong>programming</strong> language example. Because every <strong>programming</strong><br />

language uses different syntax and commands, focus on understanding the<br />

principles behind creating and using objects.<br />

Defining an object with a class<br />

To create an object, you must first create a class, stored in a separate file<br />

that defines<br />

Breaking a<br />

Large Program<br />

into Objects<br />

✦ The data (properties) the object contains<br />

✦ The subprograms (methods) the object uses to manipulate its data<br />

At the simplest level, a C++ class consists of the class keyword along with a<br />

descriptive name <strong>for</strong> your class, such as<br />

class ClassName<br />

{<br />

};<br />

So if you wanted to name your class animal, your class would now look like<br />

this:<br />

class animal<br />

{<br />

};<br />

Next, you need to define the public data (properties) and subprograms<br />

(methods) by using the public keyword. Then you must define the private<br />

data and subprograms by using the private keyword, such as<br />

class animal<br />

{

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!