Programming 版 (精华区)
发信人: zhangyan (数电 我心中永远的痛), 信区: Programming
标 题: Q&A with Bjarne Stroustrup: Part 2
发信站: 哈工大紫丁香 (2001年06月23日19:53:52 星期六), 站内信件
Question: I'm studying C++ and we've just started learning about
classes. Can you explain classes in simple terms?
Answer: When you design a system, you have some concepts in mind. For
example, if you're writing a game, you may be thinking in terms of
gnomes, knights, goblets, and rooms. The key idea of data abstraction
and object-oriented programming is that the key ideas/concepts in your
mind should be directly represented in your program. In this case we
need four classes:
class Gnome { /* ... */ };
class Knight { /* ... */ };
class Goblet { /* ... */ };
class Room { /* ... */ };
Given that, we can declare variables of these types (in C++, a class
is simply a user-defined type) exactly as we can for built-in types,
such as int and char:
Gnome Hilfs;
Knight Arn;
For a type to be useful, it must have some operations. For example, a
Gnome and a Knight might have operations hit and take, whereas a
Goblet might have fill and empty. To make sense, each class must
provide/support a suitable set of operations. Given those, we might
write something like this:
Goblet g;
Arn.take(g); // Arn takes the Goblet
Hilfs.hit(Arn); // Hilfs attacks Arn
A class needs to specify how an object is to be represented, and also
provide the basic operations. For example:
Class Knight {
private: // representation
Arm left_arm, right_arm;
Leg left_leg, right_leg;
int health; // measure of general health
vector<Thing*> bag;
// ...
public:
void take(Thing&);
void hit(Character&);
// ...
};
Note the way in which other classes can simplify the specification of
the representation of a Knight. Had I not had an Arm class handy (or
pretended that I had), I would have had to specify how an arm was to
be represented, how it was recorded, whether something was held by an
arm (in its hand), etc.
Now note the use of Thing and Character. It's obvious what I'm trying to
do (I hope). I'm referring to the general notion of something you can
pick up, put in a bag, and so on (Thing) and the general notion of a
person that can move around in a game, fight, eat, pick up things, and
so on (Character). This is the start of what is sometimes called
polymorphism, often considered the key feature of object-oriented
programming: The ability to define one class as a special case of
another—a Knight is a Character, a Gnome is a Character, a Goblet is
a Thing, etc. In C++, we represent that idea by deriving one class
from another. That is, we would define the classes like this:
class Thing { /* ... */ };
class Character { /* ... */ };
class Gnome : public Character { /* ... */ }; // a Gnome
is a kind of Character
class Knight : public Character { /* ... */ }; // a Knight
is a kind of Character
class Goblet : public Thing { /* ... */ }; // a Goblet
is a kind of Thing
class Room { /* ... */ };
You can now use a Gnome or a Knight wherever a Character is required and
a Goblet wherever a Thing is required.
Any good textbook will provide more detailed explanations—and hopefully
more realistic examples of classes.
The standard library provides several useful classes to help you get
started. These represent "computer science" concepts, rather than
application concepts, and are useful in most programs. Among my
favorites are vector, map, and string.
Question: I have several books on C++(including The C++ Programming
Language [Addison-Wesley, 2000, ISBN 0-201-70073-5]). I very much want
to start learning from your book. But it seems hard to me to begin
with it.
Answer: There is no single book that's best for everyone, and there
couldn't be. People's backgrounds, interests, and needs differ too
much for that. In general, when trying to choose a C++ book, look at the
book reviews on the Association of C and C++ Users (ACCU) site (there's
also a link on my C++ page). These reviews are written by experienced
programmers.
My books, including The C++ Programming Language, are written for people
who are already experienced programmers in some language, such as C,
Pascal, or Java. It's not aimed at people who are just learning to
program—especially not for people who are trying to learn programming
on their own. In general, it's hard to overestimate the value of a
good teacher or mentor.
If you're a programmer and find The C++ Programming Language hard going,
have a look at Andrew Koenig and Barbara E. Moo's Accelerated C++:
Practical Programming by Example [Addison-Wesley, 2000, ISBN
0-201-70353-X], which is probably the best current introduction to
modern C++ style. If that one seems too hard or if you're not already
a programmer, look hard for a good teacher or mentor.
When looking for an introductory C++ book, look for one that uses the
standard library from the very start. Look for string and vector in
the first couple of chapters—finding them is a good sign. Finding
arrays and C-style strings is a bad sign.
Question: Why do we need generic pointers? Is it impossible to print the
value of generic pointers? What's the difference between enumerations
in C and C++?
Answer: I'm not sure exactly what you mean by a "generic pointer." C++
doesn't have a type of pointer that can point to an object of any type
and perform operations on the object pointed to. By using a pointer to a
base class you can manipulate objects of derived classes, but you
need to define the interface provided by that base class—usually as a
set of virtual functions. See the explanation of abstract base classes
on my FAQ, or look it up in a C++ textbook.
If you're referring to void* then that's simply a type for referring
to "raw memory"; to do anything more than pass a void* around you must
cast it to a specific type.
You can print the value of a void* to an ostream (but not the value of
what it points to):
Void f(void* p)
{
cout << "the value of p is " << p << "\n";
}
The main difference between enums in C and C++ is that in C++ an enum is
a separate type and not just a synonym for int. That implies that you
cannot assign an int to an enum:
Enum Val { a, b, c };
Val v = b;
int x = 5;
x = b; // ok: all Vals are ints
v = 5; // error: not all ints are Vals
(and in fact Val doesn't have a value 5)
In C, the last assignment is legal.
If you have more questions on these topics, please do ask them at
InformIT.
Question: Was C a good language for systems programming?
Answer: C was and is a good language for systems programming. However,
in my opinion, C++ is a much better language for that. The basic
argument for that view is that C++ allows you to express your ideas more
directly in code, without loss of efficiency. My paper "Learning
Standard C++ as a New Language," which can be found on my home pages,
provides some arguments, code examples, and measurements supporting that
view.
Question: How soon will time override existing technology? Will C/C++ or
any version of existing program languages be usable in new
technologies, such as those occurring in biology and electronics?
Answer: Software is a key part of all new technologies. For example, you
don't do serious development of new airplanes, cars, dishwashers,
analytical equipment, or medicines without software. Most new equipment,
from bread machines to cell phones and cars, contains computers,
often several computers. Again, software is key.
Let me take the opportunity to point out that there is no language
called C/C++. Thinking of the two languages in a single breath like that
is usually a mistake, and a mistake that will often make it harder
for you to understand how to use C++ well.
Software, design approaches, and programming languages persist far
longer than you might imagine. There are many systems today that are
programmed in C because that was the language they started out with 20
years ago. With that in mind, I'm confident that there will be massive
use of both C and C++ 20 years from now. The simple fact that C and
C++ are languages with formal standards and controlled by ISO
standards committees is immensely important in ensuring long-term
stability and controlled evolution. The same cannot be said for
proprietary languages, which tend to mutate to serve the corporate
interests of their owners.
Question: Is it possible to overload the ** operator in C++?
Answer: No, because ** is not an operator in C++ and there is no
mechanism for defining new operators in C++. For an explanation of
that design decision, see The Design and Evolution of C++
[Addison-Wesley, 1994, ISBN 0-201-54330-3]. In C++, you'll have to write
pow(x,y) rather than x**y.
Consider this:
#include<cmath>
#include<iostream>
using namespace std;
struct Index {
double d;
Index(double dd) :d(dd) { }
};
struct II {
double d;
II(double dd) :d(dd) { }
};
II operator*(Index i) { return II(i.d); }
double operator*(double d , II i) { return pow(d,i.d); }
int main ()
{
Index i = 3;
cout << 2**i << "\n";
}
It looks as if it's overloading ** but really it's just using * twice
with different meanings.
It does print 8.
Question: I'd like some information about the future of C++. I want to
choose a tool that will really help me both in programming and for
getting a good job. For instance, what's the difference between C++
and VC?
Answer: C++ is a programming language supporting C-style programming,
data abstraction, object-oriented programming, and generic programming.
VC is Microsoft's implementation of the C programming language—if it
still exists except as a part of VC++. VC++ is Microsoft's
implementation of the C++ programming language. In addition to
Standard C++ (as defined by the ISO C++ standards committee), VC++
provides a software development environment, many (proprietary)
libraries, and a few (proprietary) language extensions. VC++ is one of
many C++ implementations. For example, you can get C++ implementations
from Borland, GNU, Metrowerks, HP, Intel, Sun, and many others.
There is no tool that can guarantee you a good job. In my opinion, the
best strategy is to learn the fundamental concepts and techniques of
design and programming and see how they apply in several languages. That
will take time, but a professional programmer and designer should be
comfortable with a range of tools and with several programming
languages. Knowing just one language is a prescription for bigotry.
C++ can be a good place to start, but for a professional, it cannot be
the only language.
--
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:207.982毫秒