📄 ch06.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><A HREF="ch05.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch05.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch07.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch07.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Day 6</A>
<UL>
<LI><A HREF="#Heading2">Basic Classes</A>
<UL>
<LI><A HREF="#Heading3">Creating New Types</A>
<UL>
<LI><A HREF="#Heading4">Why Create a New Type?</A>
</UL>
<LI><A HREF="#Heading5">Classes and Members</A>
<UL>
<LI><A HREF="#Heading6">Declaring a Class</A>
<LI><A HREF="#Heading7">A Word on Naming Conventions</A>
<LI><A HREF="#Heading8">Defining an Object</A>
<LI><A HREF="#Heading9">Classes Versus Objects</A>
</UL>
<LI><A HREF="#Heading10">Accessing Class Members</A>
<UL>
<LI><A HREF="#Heading11">Assign to Objects, Not to Classes</A>
<LI><A HREF="#Heading12">If You Dont Declare It, Your Class Wont Have It</A>
</UL>
<LI><A HREF="#Heading13">Private Versus Public</A>
<LI><A HREF="#Heading14">Listing 6.1. Accessing the public members</A>
<LI><A HREF="#Heading15">of a simple class.</A>
<UL>
<LI><A HREF="#Heading16">Make Member Data Private</A>
</UL>
<LI><A HREF="#Heading17">Listing 6.2. A class with accessor methods</A><A HREF="#Heading18">.</A>
<UL>
<LI><A HREF="#Heading19">Privacy Versus Security</A>
</UL>
<LI><A HREF="#Heading20">The class keyword</A>
<LI><A HREF="#Heading21">Implementing Class Methods</A>
<LI><A HREF="#Heading22">Listing 6.3. Implementing</A>
<LI><A HREF="#Heading23">the methods of a simple class.</A>
<LI><A HREF="#Heading24">Constructors and Destructors</A>
<UL>
<LI><A HREF="#Heading25">Default Constructors and Destructors</A>
</UL>
<LI><A HREF="#Heading26">Listing 6.4. Using constructors and destructors</A><A HREF="#Heading27">.</A>
<LI><A HREF="#Heading28">const Member Functions</A>
<LI><A HREF="#Heading29">Interface Versus Implementation</A>
<LI><A HREF="#Heading30">Listing 6.5. A demonstration of violations of the interface</A><A
HREF="#Heading31">.</A>
<LI><A HREF="#Heading32">Why Use the Compiler to Catch Errors?</A>
<LI><A HREF="#Heading33">Where to Put Class Declarations and Method Definitions</A>
<LI><A HREF="#Heading34">Inline Implementation</A>
<LI><A HREF="#Heading35">Listing 6.6. Cat class declaration in CAT.HPP</A><A HREF="#Heading36">.</A>
<LI><A HREF="#Heading37">Listing 6.7. Cat implementation in CAT.CPP.</A>
<LI><A HREF="#Heading38">Classes with Other Classes as Member Data</A>
<LI><A HREF="#Heading40">Listing 6.8. Declaring a complete class</A><A HREF="#Heading41">.</A>
<LI><A HREF="#Heading42">Listing 6.9. RECT.CPP</A><A HREF="#Heading43">.</A>
<LI><A HREF="#Heading44">Structures</A>
<UL>
<LI><A HREF="#Heading45">Why Two Keywords Do the Same Thing</A>
</UL>
<LI><A HREF="#Heading46">Summary</A>
<LI><A HREF="#Heading47">Q&A</A>
<LI><A HREF="#Heading48">Workshop</A>
<UL>
<LI><A HREF="#Heading49">Quiz</A>
<LI><A HREF="#Heading50">Exercises</A>
</UL>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 6</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Basic Classes</FONT></H2>
<P>Classes extend the built-in capabilities of C++ to assist you in representing
and solving complex, real-world problems. Today you will learn
<UL>
<LI>What classes and objects are.
<P>
<LI>How to define a new class and create objects of that class.
<P>
<LI>What member functions and member data are.
<P>
<LI>What constructors are and how to use them.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Creating New Types</FONT></H3>
<P>You've already learned about a number of variable types, including unsigned integers
and characters. The type of a variable tells you quite a bit about it. For example,
if you declare <TT>Height</TT> and <TT>Width</TT> to be unsigned integers, you know
that each one can hold a number between 0 and 65,535, assuming an integer is two
bytes. That is the meaning of saying they are unsigned integers; trying to hold anything
else in these variables causes an error. You can't store your name in an unsigned
short integer, and you shouldn't try.</P>
<P>Just by declaring these variables to be unsigned short integers, you know that
it is possible to add <TT>Height</TT> to <TT>Width</TT> and to assign that number
to another number.</P>
<P>The type of these variables tells you:
<UL>
<LI>Their size in memory.
<P>
<LI>What information they can hold.
<P>
<LI>What actions can be performed on them.
</UL>
<P>More generally, a type is a category. Familiar types include car, house, person,
fruit, and shape. In C++, the programmer can create any type needed, and each of
these new types can have all the functionality and power of the built-in types.
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Why Create a New
Type?</FONT></H4>
<P>Programs are usually written to solve real-world problems, such as keeping track
of employee records or simulating the workings of a heating system. Although it is
possible to solve complex problems by using programs written with only integers and
characters, it is far easier to grapple with large, complex problems if you can create
representations of the objects that you are talking about. In other words, simulating
the workings of a heating system is easier if you can create variables that represent
rooms, heat sensors, thermostats, and boilers. The closer these variables correspond
to reality, the easier it is to write the program.
<H3 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Classes and Members</FONT></H3>
<P>You make a new type by declaring a class. A class is just a collection of variables--often
of different types--combined with a set of related functions.</P>
<P>One way to think about a car is as a collection of wheels, doors, seats, windows,
and so forth. Another way is to think about what a car can do: It can move, speed
up, slow down, stop, park, and so on. A class enables you to encapsulate, or bundle,
these various parts and various functions into one collection, which is called an
object.</P>
<P>Encapsulating everything you know about a car into one class has a number of advantages
for a programmer. Everything is in one place, which makes it easy to refer to, copy,
and manipulate the data. Likewise, clients of your class--that is, the parts of the
program that use your class--can use your object without worry about what is in it
or how it works.</P>
<P>A class can consist of any combination of the variable types and also other class
types. The variables in the class are referred to as the member variables or data
members. A <TT>Car</TT> class might have member variables representing the seats,
radio type, tires, and so forth.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B><I>Member variables </I>, also
known as<I> data members</I> , are the variables in your class. Member variables
are part of your class, just like the wheels and engine are part of your car.
<HR>
</DL>
<P>The functions in the class typically manipulate the member variables. They are
referred to as member functions or methods of the class. Methods of the <TT>Car</TT>
class might include <TT>Start()</TT> and <TT>Brake()</TT>. A <TT>Cat</TT> class might
have data members that represent age and weight; its methods might include <TT>Sleep()</TT>,
<TT>Meow()</TT>, and <TT>ChaseMice()</TT>.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B><I>Member functions</I> , also
known as <I>methods </I>, are the functions in your class. Member functions are as
much a part of your class as the member variables. They determine what the objects
of your class can do.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Declaring a Class</FONT></H4>
<P>To declare a class, use the <TT>class</TT> keyword followed by an opening brace,
and then list the data members and methods of that class. End the declaration with
a closing brace and a semicolon. Here's the declaration of a class called <TT>Cat</TT>:</P>
<PRE><FONT COLOR="#0066FF">class Cat
{
unsigned int itsAge;
unsigned int itsWeight;
Meow();
};
</FONT></PRE>
<P>Declaring this class doesn't allocate memory for a <TT>Cat</TT>. It just tells
the compiler what a <TT>Cat</TT> is, what data it contains (<TT>itsAge</TT> and <TT>itsWeight</TT>),
and what it can do (<TT>Meow()</TT>). It also tells the compiler how big a <TT>Cat</TT>
is--that is, how much room the compiler must set aside for each <TT>Cat</TT> that
you create. In this example, if an integer is two bytes, a <TT>Cat</TT> is only four
bytes big: <TT>itsAge</TT> is two bytes, and <TT>itsWeight</TT> is another two bytes.
<TT>Meow()</TT> takes up no room, because no storage space is set aside for member
functions (methods).
<H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">A Word on Naming
Conventions</FONT></H4>
<P>As a programmer, you must name all your member variables, member functions, and
classes. As you learned on Day 3, "Variables and Constants," these should
be easily understood and meaningful names. <TT>Cat</TT>, <TT>Rectangle</TT>, and
<TT>Employee</TT> are good class names. <TT>Meow()</TT>, <TT>ChaseMice()</TT>, and
<TT>StopEngine()</TT> are good function names, because they tell you what the functions
do. Many programmers name the member variables with the prefix <TT>its</TT>, as in
<TT>itsAge</TT>, <TT>itsWeight</TT>, and <TT>itsSpeed</TT>. This helps to distinguish
member variables from nonmember variables.</P>
<P>C++ is case-sensitive, and all class names should follow the same pattern. That
way you never have to check how to spell your class name; was it <TT>Rectangle</TT>,
<TT>rectangle</TT>, or <TT>RECTANGLE</TT>? Some programmers like to prefix every
class name with a particular letter--for example, <TT>cCat</TT> or <TT>cPerson</TT>--whereas
others put the name in all uppercase or all lowercase. The convention that I use
is to name all classes with initial-capitalization, as in <TT>Cat</TT> and <TT>Person</TT>.</P>
<P>Similarly, many programmers begin all functions with capital letters and all variables
with lowercase. Words are usually separated with an underbar--as in <TT>Chase_Mice</TT>--or
by capitalizing each word--for example, <TT>ChaseMice</TT> or <TT>DrawCircle</TT>.</P>
<P>The important idea is that you should pick one style and stay with it through
each program. Over time, your style will evolve to include not only naming conventions,
but also indentation, alignment of braces, and commenting style.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>It's common for development companies
to have house standards for many style issues. This ensures that all developers can
easily read one another's code.
<HR>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">Defining an Object</FONT></H4>
<P>You define an object of your new type just as you define an integer variable:</P>
<PRE><FONT COLOR="#0066FF">unsigned int GrossWeight; // define an unsigned integer
Cat Frisky; // define a Cat
</FONT></PRE>
<P>This code defines a variable called <TT>Gross Weight</TT> whose type is an unsigned
integer. It also defines <TT>Frisky</TT>, which is an object whose class (or type)
is <TT>Cat</TT>.
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Classes Versus Objects</FONT></H4>
<P>You never pet the definition of a cat; you pet individual cats. You draw a distinction
between the idea of a cat, and the particular cat that right now is shedding all
over your living room. In the same way, C++ differentiates between the class <TT>Cat</TT>,
which is the idea of a cat, and each individual <TT>Cat</TT> object. Thus, Frisky
is an object of type <TT>Cat</TT> in the same way in which <TT>GrossWeight</TT> is
a variable of type unsigned <TT>int</TT>.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An <I>object</I> is an individual
instance of a class.
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Accessing Class
Members</FONT></H3>
<P>Once you define an actual <TT>Cat</TT> object--for example, Frisky--you use the
dot operator (<TT>.</TT>) to access the members of that object. Therefore, to assign
50 to Frisky's <TT>Weight</TT> member variable, you would write</P>
<PRE><FONT COLOR="#0066FF">Frisky.Weight = 50;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -