Combining a Class with a Program


Now that you know how to define a class and its attributes and methods , let s place a class definition in a program. A class definition is placed outside the main part of the program. We use the term main part because a program takes on a different form depending on what language is used to write the program.

The main part of a C++ program is the main function. A function is similar in concept to a method. The main function is the entry point into the program. The main part of a Java program is the main method of a class for applications, and it s the Applet.init method for applets.

A class definition must appear outside the main function in a C++ program and outside the Java application class definition in a Java program. We ll use a Java program called MyJavaApplication to illustrate where to place a class definition in a program.

The first part is the Java application class definition called MyJavaApplication , and the other part is the RegistrationForm class definition, which is defined in the previous section. Both are shown next .

The RegistrationForm class definition is placed outside of the MyJavaApplication class definition. Both are classes, each representing different things. The MyJavaApplication class represents the Java program, and the RegistrationForm class represents a registration form used to register students for class.

 class MyJavaApplication { 
public static void main (String args[]) {
RegistrationForm regForm = new RegistrationForm();
regForm.dropCourse(102,1234);
}
}
class RegistrationForm {
void dropCourse(int courseNumber, int studentNumber) {
System.out.println("Course " + courseNumber + " has been
dropped from student " + studentNumber);
}
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net