Department of Computer Science
Next: C++Basic Components and Layout
Up: C++
Previous: C++
C++ after Java or Eiffel
C++ is an object oriented language like Java, Eiffel and others.
They all have classes, inheritance and subtyping,
so you can design programs in very similar ways in all
languages, the structures will be similar.
However there are lots of differences:
- In C++ it is possible to have non-method functions. This is
because C++ is an extension of the earlier
non object-oriented language C. A program can consist
of functions and global data and no objects if you wish.
- As a result of being a superset of C the
way classes have been added is complicated.
Also the way classes and functions are split into files
is bad.
- C++ is quite a ``dangerous'' language. It has features
that can easily cause run-time errors if mis-used. Bad
programming is possible in any language but some
languages like Eiffel try to make it harder
to make run-time errors.
- All objects in Eiffel are accessed by implicit pointers
(if you assign one object variable to another they both
``point'' to the same object), this is called reference semantics.
In C++ object variables can be declared that hold the whole
object, not just a pointer to it, this is an important
difference and has consequences for programming. It is also
possible to have pointers to objects but these must be
declared explicitly. There is a more complete
explanation later in the section on pointers.
- There are numerous differences of syntax, nearly all
the statements are different in detail, these differences are shown
in the following section.
There follows a trivial program written in both Eiffel
and C++. It defines a class Account, declares an Account
object and then invokes a couple of operations on it.
The programs are followed by some notes on
the similarities and differences.
1 class ACCOUNT
2 creation make
3 feature{ANY}
4 owner: string
5 balance: real
6
7 make(o: string) is
8 do balance := 0.0
9 owner := o
10 end
11
12 deposit(n: real) is
13 do balance:=balance+n; end
14
15 withdraw(n: real) is
16 do if balance-n >= 0
17 then
18 balance := balance-n
19 end
20 end
21 end -- ACCOUNT
22
23 ---- in another file-----
24 class TESTACCOUNT
25 creation make
26 feature
27 kitty: ACCOUNT
28
29 make is
30 do
31 !!kitty.make("beer money")
32 kitty.deposit(2.0)
33 kitty.withdraw(0.7)
34 io.put_string("balance: ")
35 io.put_real(kitty.balance)
36 io.put_string("%N")
37 end
38 end -- TESTACCOUNT
|
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 class Account {
6 private:
7 string name;
8 double amount;
9 public:
10 Account(string o){
11 name = o;
12 }
13 double balance(void) {
14 return amount;
15 }
16 void deposit(double n) {
17 amount = amount + n;
18 }
19 string owner(void) {
20 return name;
21 }
22 void withdraw(double n) {
23 if ( amount-n >= 0 )
24 amount = amount - n;
25 }
26 };
27
28 int main( ) {
29 Account kitty("beer fund");
30
31 kitty.deposit(2.0);
32 kitty.withdraw(0.7);
33 cout << "balance: "
34 << kitty.balance()
35 << "\n";
36 }
|
|
(Programs in account.e,
testaccount.e
and account.cpp).
Notes on the two programs:
- In Eiffel programs only contain classes, each in a file, in C++
there can be any number of classes and non-member functions in
one file. In this file there is one class and the non-member
function main where execution starts.
- The strange
#include lines in C++ are to
make definitions for I/O and strings available
because the compiler won't search other files
looking for them (as in Eiffel). More about these later.
- In Eiffel visibility is controlled by
feature{..}
clauses, in C++ there are private: and public:
labels with no qualifiers. In Eiffel data members are
accessible from outside but ``read-only'', in C++
if an attribute is public it is writable as well.
Consequently data members must be private and if
their value is required a method must be provided, that is why
there is an attribute amount and a function balance.
- Eiffel has creation methods, in C++ the equivalent is called
a constructor as has the same name as the class.
- Declarations in C++ are ``backwards'', a,b: integer is
int a,b;. Eiffel's real is double in C++.
Even the result type of a method (function) precedes the
function name, eg. string owner().
- In C++ all declarations and simple statements must be
terminated by semi-colons.
- The bodies of methods and functions are surrounded by
{...},
declarations and statements occur anywhere in the body.
- If a method (or function) doesn't have a result it must
be preceded by void.
- Assignment is a single ``='', in comparisons the equality
test is ``==''.
- Conditionals don't need the keyword then but the condition
must be surrounded by paentheses ``(..)''.
- The declaration kitty:ACCOUNT in Eiffel declares a
variable with no object, the
!!kitty.make(..) creates
an object and calls the creation method. Whereas in C++ Account kitty(..)
declares a ``big'' variable holding the object and calls the constructor.
Next: C++Basic Components and Layout
Up: C++
Previous: C++
Page generated: 2002-11-04 by Bob Dickerson
© University of Hertfordshire Higher Education Corporation (1998)
Disclaimer