CSC 204 Lecture 2
CSC 204 Lecture 2
In structured programming design, programs are broken into different functions these functions
are also known as modules, subprogram, subroutines or procedures. Each function is design to
do a specific task with its own data and logic. Information can be passed from one function to
another function through parameters. A function can have local data that cannot be accessed
outside the function’s scope. The result of this process is that all the other different functions are
synthesized in another function. This function is known as main function. Many of the high-level
languages support structured programming.
Structured programming minimizes the chances of the function affecting another. It allows for
clearer programs code. It made global variables to disappear and replaced by the local variables.
Due to this change one can save the memory allocation space occupied by the global variable. Its
organization helps in the easy understanding of programming logic. So that one can easily
understand the logic behind the programs. It also helps the newcomers of any industrial
technology company to understand the programs created by their senior workers of the industry.
The languages that support Structured programming approach are:
•C
• C++
• Java
• C#
• Pascal
Objects
Object-oriented programming shifts the focus of attention to the objects, that is, to the aspects on
which the problem is centered. A program designed to maintain bank accounts would work with
data such as balances, credit limits, transfers, interest calculations, and so on. An object
representing an account in a program will have properties and capacities that are important for
account management.
OOP objects combine data (properties) and functions (capacities). A class defines a certain
object type by defining both the properties and the capacities of the objects of that type. Objects
communicate by sending each other “messages,” which in turn activate another object’s
capacities.
Advantages of OOP
Object-oriented programming offers several major advantages to software development:
■ reduced susceptibility to errors: an object controls access to its own data. More specifically, an
object can reject erroneous access attempts
■ easy re-use: objects maintain themselves and can therefore be used as building blocks for other
programs
■ low maintenance requirement: an object type can modify its own internal data representation
without requiring changes to the application.
The following three steps are required to create and translate a C++ program:
1. First, a text editor is used to save the C++ program in a text file. In other words, the source
code is saved to a source file. In larger projects the programmer will normally use modular
programming. This means that the source code will be stored in several source files that are
edited and translated separately.
2. The source file is put through a compiler for translation. If everything works as planned, an
object file made up of machine code is created. The object file is also referred to as a module.
3. Finally, the linker combines the object file with other modules to form an executable file.
These further modules contain functions from standard libraries or parts of the program that have
been compiled previously.
It is important to use the correct file extension for the source file’s name. Although the file
extension depends on the compiler you use, the most commonly found file extensions are .cpp
and .cc.
Prior to compilation, header files, which are also referred to as include files, can be copied to the
source file. Header files are text files containing information needed by various source files, for
example, type definitions or declarations of variables and functions. Header files can have the
file extension .h, but they may not have any file extension.
The C++ standard library contains predefined and standardized functions that are available for
any compiler.
Modern compilers normally offer an integrated software development environment, which
combines the steps mentioned previously into a single task. A graphical user interface is
available for editing, compiling, linking, and running the application. Moreover, additional tools,
such as a debugger, can be launched.
A C++ program is made up of objects with their accompanying member functions and global
functions, which do not belong to any single particular class. Each function fulfills its own
particular task and can also call other functions. You can create functions yourself or use ready-
made functions from the standard library. You will always need to write the global function main
() yourself since it has a special role to play; in fact it is the main program.
The short programming example on the opposite page demonstrates two of the most important
elements of a C++ program. The program contains only the function main () and displays a
message.
The first line begins with the number symbol, #, which indicates that the line is intended for the
preprocessor. The preprocessor is just one step in the first translation phase and no object code is
created at this time. You can type
#include <filename>
to have the preprocessor copy the quoted file to this position in the source code. This allows the
program access to all the information contained in the header file. The header file iostream
comprises conventions for input and output streams. The word stream indicates that the
information involved will be treated as a flow of data.
Predefined names in C++ are to be found in the std (standard) namespace. The using directive
allows direct access to the names of the std namespace.
Program execution begins with the first instruction in function main(), and this is why each C++
program must have a main function. The structure of the function is shown on the opposite page.
Apart from the fact that the name cannot be changed, this function’s structure is not different
from that of any other C++ function.
In our example the function main () contains two statements. The first statement
cout << "Enjoy yourself with C++!" << endl;
outputs the text string Enjoy yourself with C++! on the screen. The name cout (console output)
designates an object responsible for output.
The two less-than symbols, <<, indicate that characters are being “pushed” to the output stream.
Finally endl (end of line) causes a line feed. The statement
return 0;
terminates the function main() and also the program, returning a value of 0 as an exit code to the
calling program. It is standard practice to use the exit code 0 to indicate that a program has
terminated correctly.
Note that statements are followed by a semicolon. By the way, the shortest statement comprises
only a semicolon and does nothing.