15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

538<br />

Using Objects<br />

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

typical class looks like this:<br />

class className<br />

{<br />

public:<br />

datatype propertyname;<br />

void methodname();<br />

};<br />

The class lists one or more properties and the type of data that property can<br />

hold, such as an integer or floating point number. A class also lists one or<br />

more method names, which contains code <strong>for</strong> manipulating an object in<br />

some way. The class defines the method name and its parameter list, but the<br />

actual code <strong>for</strong> that method appears outside the class definition, such as<br />

class className<br />

{<br />

public:<br />

datatype propertyname;<br />

void methodname();<br />

};<br />

void classname::methodname()<br />

{<br />

Commands;<br />

}<br />

After you define a class, you can create an object from that class by declaring<br />

a variable as a new class type, such as<br />

className objectname;<br />

So if you created an animal class, you could create an object<br />

(things_at_the_zoo) from that class as follows:<br />

animal things_at_the_zoo;<br />

The C++ language allows both single and multiple inheritance. With single<br />

inheritance, you can declare a class name and state the class to inherit from<br />

with a colon, such as<br />

class className : public classtoinheritfrom<br />

{<br />

// Code goes here<br />

};

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

Saved successfully!

Ooh no, something went wrong!