Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CITS2210
Object-Oriented Programming
Topic 12
C++:      
 Basic Objects and Classes
Summary: This topic shows how to use classes and objects in C++ in a
way that corresponds closely to Java classes and objects.  This allows
programming in a similar style to Java, and is sufficient to write just about
any program in C++. 
1
C++ classes in a Java style
C++ allows many different styles of programming.
– Even within the object-oriented style, it allows different choices in
terms of how objects are allocated, passed to functions, etc.
– To keep things simple to start with, we will first focus on a style that
corresponds closely to standard Java classes.
– This should be sufficient to get you started with C++ programming –
you can “think” in Java but write in C++.
– This style is certainly not enough to understand every C++ program.
– This style is simple and uniform, but does not always lead to the most
efficient programs.
2
C++ classes: introductory example in Java
Consider the following class from the solution to lab 1 in Java:
public class Location {
    private int numBikesHere = 3;
    public int numBikes() { return numBikesHere; }
    public boolean hasBikes() 
       { return numBikesHere > 0; }
    public void incrementBikes() { numBikesHere++; }
    public void decrementBikes() { numBikesHere--; }
}
Also consider the following simple main program.
public class Main {
public static void main(String[] args) {
Location loc = new Location();
System.out.println(loc.numBikes());
    if(!loc.hasBikes) {
loc.decrementBikes();
System.out.println(loc.numBikes());
}
}
}
3
C++ classes: introductory example in C++
The following is the equivalent C++ class code.  First the specification of
the class, in the “header” file.
// Location.h
class Location {
private:
    int numBikesHere;
public: 
    Location();
    int numBikes();
    bool hasBikes();
    void incrementBikes();
    void decrementBikes();
}; 
The implementation of the constructor and methods goes in a file with the
extension .cpp
// Location.cpp
#include "Location.h"
Location::Location() { numBikesHere=3; }
int Location::numBikes() { return numBikesHere; }
bool Location::hasBikes() { return numBikesHere > 0; }
void Location::incrementBikes() { numBikesHere++; }
void Location::decrementBikes() { numBikesHere--; }
4
Introductory example - main program
The main program also goes in a file with the extension .cpp
// LocationTest.cpp
#include 
#include "Location.h"
using namespace std;
int main() {
  Location * loc = new Location();
cout << loc->numBikes() << endl;
 if(loc->hasBikes()) {
    loc->decrementBikes();
    cout << loc->numBikes() << endl;
  }
return 0;
}
You can then compile the program with the following shell command:
gcc -Wall LocationTest.cpp Location.cpp -o location
To execute the program use:
./location
(Or use the build and run functions from Xcode or similar.)
5
Introductory example – file types
Firstly, note that in the header file only an interface for the class is given.
– The purpose of this file is specify all the information needed when
compiling any part of the program that uses the class.
– Parts of the program using the class will include the header file
using #include.
– The implementation file also needs the specification, so includes it.
– The constructor and method declarations include the class name, then
“::”, then the name of the method or constructor.
–  Each constructor and method is matched up with the corresponding one
in the named class, and the declared types must match.
– Compiling the .cpp file yields a .o file that contains the executable
machine code for each constructor and method.
– These are then linked together to create the final program.
6
Example – interface and implementation
Next, note that the class specification in the header file is very similar to a
Java interface, although note the following.
– There is no need to specify “public” for the whole class.  Any file that
includes the header file will be able to use the class.
– private and public apply to all the methods that follow, not just a
single one.
– The intitializer for numBikesHere is in the implementation for the
constructor, rather than in the header.
– C++ uses bool instead of boolean
– There is a semi-colon after the whole class specification.
In the implementation of the class, each method is implemented exactly as
in the original.
– The constructor includes the initializer for numBikesHere
– (We will see another way to initialize later.)
– The header file is included.
7
Introductory example – main program
In the main program note the following.
– The header file is included so that we can create a location object.
– We also include a header from the C++ libraries so that we can print.
– Use < and > for library headers, “ and “ for your own headers.
– We use the std namespace so we can print to standard output.
– This is similar to importing a package in Java
– The main program is simply a function – it is not in any class.
– In Java we only have methods within classes.
– The main program returns an integer – 0 means “success”.
– cout is the standard output.  << prints to it (and is overloaded).
– endl is an “end of line”.
– new actually returns a pointer to a Location.
– Location * means “a pointer to a location”.
– This is exactly like an object reference in Java.
– To access methods (and fields) we use -> instead of “.”
8