01 C++ Programming Basics
01 C++ Programming Basics
Basics
2
z
Today’s Lesson
Basic
Input and Data Types Library
Program Comments
Construction Output & Variables Functions
COE 351: Object-Oriented Prgramming
3
z
Learning Outcomes
• Introduce learners to
1 C++
z
C++ Programming Basics
COE 351: Object-Oriented Prgramming
§ It directs the contents of the variable on its right to the object on its left.
z
Output Using cout
z
The using Directive
§ The directive using namespace std; says that all the program statements
that follow are within the std namespace.
§ If we didn’t use the using directive, we would need to add the std name to many program
elements.
§ To avoid adding std:: dozens of times in programs we use the using directive instead.
z
Comments
§ They help the person writing a program, and anyone else who
must read the source file, understand what is going on.
Comments
§ Integer variables represent integer numbers like 1, 30,000, and –27. Such
numbers are used for counting discrete numbers of objects.
z
Integer Variables
Integer Variables
Integer Variables
§ Type char stores integers that range in value from –128 to 127.
§ The ASCII character set is a way of representing characters such as ‘a’, ‘B’,
‘$’, ‘3’, and so on, as numbers. These numbers range from 0 to 127.
z
Character Constants
Character
Variables
Character
Variables
Character variables can be
assigned character
constants as values.
§ This stream represents data coming from the keyboard (unless it has been
redirected).
§ It takes the value from the stream object on its left and places it in the
variable on its right.
z
Input with cin
z
Input with cin
z
Floating Point Types
§ They have both an integer part, to the left of the decimal point, and a
fractional part, to the right.
Float
§ In theory a bool type requires only one bit (not byte) of storage.
§ The setw manipulator causes the number (or string) that follows it in the stream
to be printed within a field n characters wide, where n is the argument to
setw(n).
§ As with cout and other such objects, you must #include a header file that
contains the declaration of any library functions you use.
§ In the documentation for the sqrt() function, you’ll see that the specified
header file is CMATH.
§ If you don’t include the appropriate header file when you use a library
function, you’ll get an error message like this from the compiler: ‘sqrt’
unidentified identifier.
z
Library Files
§ Various files containing library functions and objects link to your program to
create an executable file.
§ These files contain the actual machine-executable code for the functions.
§ The sqrt() function is found in such a file. It is automatically extracted from the
file by the linker, and the proper connections are made so that it can be called
(that is, invoked or accessed) from the SQRT program.
§ Your compiler takes care of all these details for you, so ordinarily you don’t
need to worry about the process.
z
Header Files and Library Files
§ To use a library function like sqrt(), you must link the library file that contains it
to your program.
§ The appropriate functions from the library file are then connected to your
program by the linker.
§ The functions in your source file need to know the names and types of the
functions and other elements in the library file. They are given this information
in a header file.
§ The functions themselves are grouped together in a library file, but the
information about them is scattered throughout a number of header files.
z
Header Files and Library Files
z
Two Ways to Use #include
§ Instead of angle brackets around the filename, you can also use quotation marks, as in
#include “myheader.h”
§ Quotation marks instruct the compiler to begin its search for the header file in the current
directory; this is usually the directory that contains the source file.
§ You normally use quotation marks for header files you write yourself.
§ Quotation marks or angle brackets work in any case, but making the appropriate choice
speeds up the compilation process slightly by giving the compiler a hint about where to find
the file.
z
Exercises
§ Assuming there are 7.481 gallons in a cubic foot, write a program that asks
the user to enter a number of gallons, and then displays the equivalent in
cubic feet.
Any Questions?
z
The End
Contact: tsadjaidoo@knust.edu.gh