COSC 1315

Programming Fundamentals

Basic Object-Based Program Structure

Revised:  January 28, 2007
By Richard G. Baldwin

File:  Pf00110.htm
Practice Text


Preface

This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.

Another browser window

I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.

Overall Program Organization

Before getting into the programming details, I will summarize the overall program organization that you should learn by the time you complete this lesson.  Please refer to the code in Listing 4 while reviewing the following information.

Important characteristics

The organization of the code in Listing 4 has the following important characteristics:

Vital that you understand this

It is vital that you understand this program organization before leaving this lesson.  If you don't understand it, please discuss it again and again with your instructor until you do understand it.

Why is it so vital?

An understanding of this program organization is vital in order for you to be able to progress from this course in Programming Fundamentals to the world of Object Oriented Programming (OOP).  In the opinion of this author, an understanding of this program organization is fundamental to the new world of programming that depends heavily on OOP.

Not an Object-Oriented program

Note, however, that this lesson, and this course does not teach Object-Oriented Programming (OOP)An Object-Oriented program exhibits three critical attributes:

Although this lesson and this course does teach you how to encapsulate data and functions in objects instantiated from classes, it stops far short of teaching you about the other two critical attributes of OOP.

Will refer to it as Object-Based (OB)

I will refer to the material taught in this lesson and in this course as being Object-Based (OB) instead of being Object-Oriented (OO).

This Object-Based program organizational structure is very similar to the type of program organizational structure that you are likely to encounter when you make the leap into C#, Java, PHP, or Object-Oriented C++.  In fact, this is probably the simplest program organization that you are likely to encounter when you enter the world of OOP.

Please make certain that you understand this program organization before leaving this lesson.

Sample Programs

This lesson illustrates a very simple object-based program written in three different programming languages:  C#, C++, and Java.  In addition, it illustrates a somewhat more substantive object-based program written in C++.

No global functions other than main

The C++ programs are written in such a way as to make them consistent with the other two programming languages.  In particular, they are written in such a way as to prohibit the use of global variables and global functions other than the main function.  (Unfortunately, it is impossible to eliminate the global main function in C++.)

A simple C# program

Listing 1 shows a simple C# program that displays the text Hello World on the screen.

/*File:  Hello1.cs
This is a very simple C# program.  It illustrates
the basic structure of a C# program, which 
requires a controlling class with a Main method.
Also, global variables and global methods are
not allowed.

To compile this program on a computer with the
MS.NET framework installed, enter the following
command at the command prompt:

csc Hello1.cs

A successful compilation will produce a single
output file named Hello1.exe

To run the program, enter the following command
at the command prompt in the directory 
containing the exe file:

Hello1 
************************************************/

public class Hello1{
  public static void Main(){
    System.Console.WriteLine("Hello World");
  }//end Main
}//end class Hello01

Listing 1

No global variables or methods allowed

C# does not allow global variables or global methods.

Compile from the command line

No fancy IDE is required to compile this program.

To compile this program on a computer with the MS.NET framework installed, enter the following command at the command prompt:

csc Hello1.cs

This will produce a single output file named Hello1.exe.

Running the program

No fancy IDE is required to run the program.

To run the program, enter the following command at the command prompt in the directory containing the exe file:

Hello1

A Main method

Note that the C# program contains a Main method, but it is spelled with an upper-case M.

The Main method must be defined inside a class so as not to be global.

No object is required

Note also that the Main method is static.  This means generally the same thing in all three languages.

In particular, it means that a static method can be accessed without a requirement to instantiate an object of the class.

In the case of C#, the static Main method is accessed by the runtime system when the program is executed.

Program execution begins and ends in the Main method.

A simple Java program

Listing 2 shows a simple Java program that displays the text Hello World on the screen.

/*File:  Hello1.java
This is a very simple Java program.  It 
illustrates the basic structure of a Java 
program, which requires a controlling class 
with a main method. Also, global variables and 
global methods are not allowed.

To compile this program on a computer with the
the Java JDK installed, enter the following
command at the command prompt:

javac Hello1.java

A successful compilation will produce a single
output file named Hello1.class

To run the program, enter the following command
at the command prompt in the directory 
containing the class file:

java Hello1 
************************************************/
class Hello1{
  public static void main(String[] args){
    System.out.println("Hello World");
  }//end main
}//End Hello1 class

Listing 2

No global variables or methods allowed

Java does not allow global variables or methods.

No fancy IDE required to compile the program

To compile this program on a computer with the Java JDK installed, enter the following command at the command prompt:

javac Hello1.java

A single output class file

A successful compilation will produce a single output file named Hello1.class

No fancy IDE required to run the program

To run the program, enter the following command at the command prompt in the directory containing the class file:

java Hello1

A main method

A Java application requires a static method named main.

The main method must be defined inside a class to avoid being global.

The main method is called by the runtime system

The main method is declared static.

The keyword static has the same meaning as with C#.

No object is required to call a static method.

The static main method is called by the runtime system when the program is executed.

Execution begins and ends in the main method.

A simple C++ program

Listing 3 shows a simple C++ program that displays the text Hello World on the screen.

Even though this is a simple program, it is still somewhat more complex than the corresponding C# and Java programs that do the same thing.

Approach prohibits global methods other than main

This is not the simplest form of a C++ program.

C++ allows both global variables and global functions. 

This program was written in such a way as to prohibit the use of all global variables and global functions except for the main function.

Unfortunately, as mentioned earlier, the main function must always be a global function in C++.

/*File:  Hello1.cpp
This is a very simple C++ program.  It illustrates
the basic structure of a C++ program, which
has been written in such a way as to prohibit the
use of all global variables and global functions
except for the main function, which must always
be a global function.

To compile this program, use an IDE such as MS
Visual Studio 6.0.  You can then execute the
program from within VS 6.0.  When you do, the
following text will be displayed on the screen:

Hello World
Press any key to continue

A successful compilation and execution from
within VS 6.0 will produce ten new files plus
one new directory named Debug.

You can also execute the compiled program by
making Debug the current directory and entering
the following command at the command prompt:

hello1

************************************************/

#include <iostream>
using namespace std;

class Hello1{ 
  public:
  static void classMain(){
    cout << "Hello World" << endl;
  }//End classMain function
};//End Hello1 class
//---------------------------------------------//

int main(){
  Hello1::classMain();
  return 0;
}//end main

Listing 3

Compile and execute

To compile this program, you can use a fancy IDE such as MS Visual Studio 6.0.  (You can also use other less-fancy approaches such as Dev C++.)

If you compile using MS Visual Studio 6.0, you can then execute the program from within VS 6.0.

When you do, the following text will be displayed on the screen:

Hello World

Many files will be produced

A successful compilation and execution from within VS 6.0 will produce ten new files plus one new directory named Debug.

Execute from the command line

If you compile using MS Visual Studio 6.0, you can also execute the compiled program by making Debug the current directory and entering the following command at the command prompt:

hello1

Unlike with MS Visual Studio 6.0, if you compile using Dev C++ a new directory named Debug is not created.  In that case, you can execute the program from the command line by entering the command given above in the same directory that contains your source code.

More information about C++ functions

There are essentially three kinds of functions in C++:

Global functions

You saw several examples of global functions in the lesson entitled C++ Functions.

Global functions are essentially accessible by any code in any function that knows the name of the global function.

Class functions

Class functions are functions that are defined inside a class and declared using the static keyword.

Only one copy of such functions exist no matter how many objects are instantiated from the class.

Class functions only have direct access to other static members of the same class.  They do not have direct access to instance members of the class.

Class functions can be declared public, private, or protected

Public class functions can be accessed by any code in any function that knows the name of the function and the name of the class in which the function is defined.

Public static functions can be accessed in the total absence of an object of the class.

Access to private class functions and protected class functions is somewhat more restricted.  I will defer discussion of such functions until a more advanced course.

Instance functions

Instance functions are functions that are defined inside a class without using the static keyword.

For efficiency purposes, only one copy of the function exists. 

However some "smoke and mirrors" is used to make it appear that every object instantiated from the class has its own copy of every instance function that is defined in the class.

It appears that every object has its own copy

What this means is:

Instance functions can also be declared public, private, or protected.

Public instance functions can be invoked by any code that has access to the object to which the functions belong.

Access to private instance functions and protected instance functions is somewhat more restricted.

Information hiding

It is common practice in object-oriented programming to cause an object to encapsulate:

The outside world can gain (sometimes restricted) access to the private instance variables using the public accessor functions.

Example of information hiding

For example, a public accessor function might be used to allow a value to be entered into an instance variable that represents the age of a person provided that the value is in the range of 0 to 110.

Values outside that range would be rejected.

A somewhat more substantive object-based C++ program

The C++ program in Listing 4 illustrates the basic structure of a C++ program that:

Instantiation of object in dynamic memory

Listing 4 also illustrates the instantiation of an object of a class in dynamic memory.

Invocation of an instance function

Finally, Listing 4 also illustrates the invocation of an instance function belonging to that object.

Program output

The program displays the following text on the screen:

Hello World

/*File:  Hello2.cpp
This C++ program illustrates the basic structure 
of a C++ program that prohibits the use of global
variables and global functions other than main, 
and also avoids the use of static functions for 
any purpose other than to control the flow of 
the program acting as a pseudo main function.

It also illustrates the instantiation of an 
object of a class in dynamic memory along with 
the invocation of an instance function belonging
to that object.

The program displays the following text on the
screen:

Hello World

************************************************/

#include <iostream>
using namespace std;

class Hello2{ 
  public:
  static void classMain(){
    //Instantiate an object of the Hello2 class 
    // and save its reference in a pointer 
    // variable.
    Hello2* ptrToObject = new Hello2();
    //Now invoke the instance function named 
    // doSomething belonging to the object.
    ptrToObject -> doSomething();

  }//End classMain function
  //-------------------------------------------//

  //An instance function belonging to the Hello2
  // object.
  void doSomething(){
    cout << "Hello World\n";
  }//end doSomething function
};//End Hello2 class
//---------------------------------------------//

int main(){
  Hello2::classMain();
  return 0;
}//end main

Listing 4

The global main function

The global main function is required in C++.

The global main function is invoked when the program is started.

The classMain function

The classMain function emulates the main method in Java and the Main method in C#.

As in Java and C#, the classMain function controls the overall flow of the program.

Invoking the classMain function

The classMain function is static.

The global main function uses the scope resolution operator (::) to invoke the classMain function without instantiating an object of the class.  In this sense, it emulates the runtime systems in C# and Java.

In layman's terminology

The two statements in the function named classMain look pretty ugly.  However, the interpretation of those two statements is completely straightforward.

Creating the object

The first statement in the function named classMain reads as follows:

Hello2* ptrToObject = new Hello2();

In layman's terms, this is what that statement means:

And that's all there is to the first statement.

Accessing the object

The second statement in the function named classMain reads as follows:

ptrToObject -> doSomething();

In layman's terms, this is what that statement means:

Following that point in time, the behavior of the program depends entirely on the code that is written into the function named doSomething.  In this case, the code in the function named doSomething causes the program to print the following message on the screen:

Hello World

Other programs will probably do more.

Instantiation of object in dynamic memory

The classMain function uses the new operator to instantiate an object of the Hello2 class in dynamic memory.

The address of the object in dynamic memory is saved in a pointer variable named ptrToObject.

Invocation of instance function

The classMain function uses the pointer variable and the pointer-to-member operator (->) to invoke the instance function named doSomething on the object that was instantiated from the class named Hello2.

The doSomething function

The doSomething function displays the following output on the screen:

Hello World

-end-


Copyright 2005, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-