Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Parm
1. C++ Overview
• History and major features of C++
• Input and output
• Preprocessor directives
• Comments
January 26, 2012 CS410 – Software Engineering 1
Lecture #2: Hello, C++ World!
2. History of C++
1972: C language developed at Bell Labs
•Dennis Ritchie wrote C for Unix OS
•Needed C for work with Unix
late 70s: C becomes popular for OS development by
many vendors
•Many variants of the language developed
•ANSI standard C in 1987-89
3. History of C++ (continued)
early 80s: Bjarne Stroustrup adds OO features to C
creating C++
90s: continued evolution of the language and its
applications
•preferred language for OS and low level
programming
•popular language for application development
•low level control and high level power
4. History of C++
• 1980: “C-with-classes” developed by Bjarne Stroustrup
• 1983: C-with-classes redesigned and called C++
• 1985: C++ compilers made available
• 1989: ANSI/ISO C++ standardization starts
• 1998: ANSI/ISO C++ standard approved
January 26, 2012 CS410 – Software Engineering 4
Lecture #2: Hello, C++ World!
5. Conceptually what is C++
Alternatives:
•is it C, with lots more options and features?
•is it an OO programming language with C as
its core?
•is it a development environment?
On most systems it is a development environment,
language, and library, used for both procedural
and object oriented programming, that can be
customized and extended as desired
6. Versions of C++
ANSI C++
Microsoft C++ (MS Visual C++ 6.0)
Other vendors: Borland, Symantec, Turbo, …
Many older versions (almost annual) including
different version of C too
Many vendor specific versions
Many platform specific versions
For this class: Unix / Linux based versions
•g++
8. Other OO Languages
Smalltalk
•pure OO language developed at PARC
Java
•built on C/C++
•objects and data types
Eifel and others
9. What you can do with C++
Apps (standalone, Web apps, components)
Active desktop (Dynamic HTML, incl Web)
Create graphical apps
Data access (e-mail, files, ODBC)
Integrate components w/ other languages
10. Major Features of C++
• Almost upward compatible with C
•Not all valid C programs are valid C++
programs.
• Why?
• Because of reserved words in C++ such as
‘class’
• Extends C with object-oriented features
• Compile-time checking: strongly typed
• Classes with multiple inheritance
• No garbage collection, but semi-automatic
storage reclamation
January 26, 2012 CS410 – Software Engineering 10
Lecture #2: Hello, C++ World!
11. Disadvantages of C++
Tends to be one of the less portable languages
Complicated?
•40 operators, intricate precedence, pointers,
etc.
•can control everything
•many exceptions and special cases
•tremendous libraries both standard, vendor
specific, and available for purchase, but all are
intricate
Aspects above can result in high maintenance
12. Advantages of C++
Available on most machines
Can get good performance
Can get small size
Can manage memory effectively
Can control everything
Good supply of programmers
Suitable for almost any type of program (from
systems programs to applications)
13. Sample C++ Program
#include <iostream> if (enteredPasswd == deptChairPasswd)
#include <string> {
using namespace std; cout << "Thank you!" << endl;
passwdOK = true;
int main() }
{ else
string deptChairName, deptChairPasswd, {
enteredPasswd; cout << "Hey! Are you really " <<
bool passwdOK; deptChairName << "?n";
int attempts = 0; cout << "Try again!n";
passwdOK = false;
deptChairName = GetDeptChairName(); }
deptChairPasswd = GetDeptChairPasswd(); }
while (!passwdOK && attempts < 3);
cout << "How are you today, " << if (passwdOK)
deptChairName << "?n"; SetNewParameters();
do else
{ {
cout << "Please enter your password: "; Shout(“Warning! Illegal access!”);
cin >> enteredPasswd; CallCampusPolice();
attempts++; }
return 0;
}
January 26, 2012 CS410 – Software Engineering 13
Lecture #2: Hello, C++ World!
14. Input and Output
• Some standard functions for input and output are
provided by the iostream library.
• The iostream library is part of the standard library.
• Input from the terminal (standard input) is tied to
the iostream object cin.
• Output to the terminal (standard output) is tied to
the iostream object cout.
• Error and warning messages can be sent to the user
via the iostream object cerr (standard error).
January 26, 2012 CS410 – Software Engineering 14
Lecture #2: Hello, C++ World!
15. Input and Output
• Use the output operator (<<) to direct a value to
standard output.
• Successive use of << allows concatenation.
• Examples:
•cout << “Hi there!n”;
•cout << “I have “ << 3 + 5 << “ classes today.”;
•cout << “goodbye!” << endl; (new line & flush)
January 26, 2012 CS410 – Software Engineering 15
Lecture #2: Hello, C++ World!
16. Input and Output
• Use the input operator (>>) to read a value from
standard input.
• Standard input is read word by word (words are
separated by spaces, tabs, or newlines).
• Successive use of >> allows reading multiple words
into separate variables.
• Examples:
•cin >> name;
•cin >> nameA >> nameB;
January 26, 2012 CS410 – Software Engineering 16
Lecture #2: Hello, C++ World!
17. Input and Output
How can we read an unknown number of input
values?
int main()
{
string word;
while (cin >> word)
cout << “word read is: “ << word << “n”;
cout << “OK, that’s all.n”;
return 0;
}
January 26, 2012 CS410 – Software Engineering 17
Lecture #2: Hello, C++ World!
18. Input and Output
The previous program will work well if we use a file
instread of the console (keyboard) as standard input.
In Linux, we can do this using the “<“ symbol.
Let us say that we have created a text file “input.txt”
(e.g., by using gedit) in the current directory.
It contains the following text:
“This is just a stupid test.”
Let us further say that we stored our program in a file
named “test.C” in the same directory.
January 26, 2012 CS410 – Software Engineering 18
Lecture #2: Hello, C++ World!
19. Input and Output
We can now compile our program using the g++
compiler into an executable file named “test”:
$ g++ test.C –o test
The generated code can be executed using the
following command:
$ ./test
If we would like to use the content of our file “input.txt”
as standard input for this program, we can type the
following command:
$ ./test < input.txt
January 26, 2012 CS410 – Software Engineering 19
Lecture #2: Hello, C++ World!
20. Input and Output
We will then see the following output in our terminal:
Word read is: This
Word read is: is
Word read is: just
Word read is: a
Word read is: stupid
Word read is: test.
OK, that’s all.
January 26, 2012 CS410 – Software Engineering 20
Lecture #2: Hello, C++ World!
21. Input and Output
If we want to redirect the standard output to a file, say
“output.txt”, we can use the “>” symbol:
$ ./test < input.txt > output.txt
We can read the contents of the generated file by
simply typing:
$ less output.txt
We will then see that the file contains the output that
we previously saw printed in the terminal window.
(By the way, press “Q” to get the prompt back.)
January 26, 2012 CS410 – Software Engineering 21
Lecture #2: Hello, C++ World!
22. Input and Output
If you use keyboard input for your program, it will
never terminate but instead wait for additional input.
Use the getline command to read an entire line from
cin, and put it into a stringstream object that can be
read word by word just like cin.
Using stringstream objects requires the inclusion of
the sstream header file:
#include <sstream>
January 26, 2012 CS410 – Software Engineering 22
Lecture #2: Hello, C++ World!
23. Input and Output
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string userinput, word;
getline(cin, userinput);
stringstream mystream(userinput);
while (mystream >> word)
cout << "word read is: " << word << "n";
cout << "OK, that’s all.n";
return 0;
}
January 26, 2012 CS410 – Software Engineering 23
Lecture #2: Hello, C++ World!
24. Input and Output
By the way, you are not limited to strings when using
cin and cout, but you can use other types such as
integers.
However, if your program expects to read an integer
and receives a string, the read operation will fail.
If your program always uses file input and output, it is
better to use fstream objects instead of cin and cout.
January 26, 2012 CS410 – Software Engineering 24
Lecture #2: Hello, C++ World!
25. File Input and Output
#include <iostream> if (!outfile)
#include <fstream> {
#include <string> cerr << “error: unable to open
using namespace std;
output file”;
int main() return –2;
{ }
ofstream outfile(“out_file.txt”);
ifstream infile(“in_file.txt”); string word;
while (infile >> word)
if (!infile) outfile << word << “_”;
{
cerr << “error: unable to open return 0;
input file”; }
return –1;
}
January 26, 2012 CS410 – Software Engineering 25
Lecture #2: Hello, C++ World!
26. Preprocessor Directives
Preprocessor directives are specified by placing a
pound sign (#) in the very first column of a line in our
program.
For example, header files are made part of our
program by the preprocessor include directive.
The preprocessor replaces the #include directive with
the contents of the named file.
There are two possible forms:
#include <standard_file.h>
#include “my_file.h”
January 26, 2012 CS410 – Software Engineering 26
Lecture #2: Hello, C++ World!
27. Preprocessor Directives
If the file name is enclosed in angle brackets (<, >),
the file is presumed to be a standard header file.
Therefore, the preprocessor will search for the file in
a predefined set of locations.
If the file name is enclosed by a pair of quotation
marks, the file is presumed to be a user-supplied
header file.
Therefore, the search for the file begins in the
directory in which the including file is located (project
directory).
January 26, 2012 CS410 – Software Engineering 27
Lecture #2: Hello, C++ World!
28. Preprocessor Directives
The included file may itself contain an #include
directive (nesting).
This can lead to the same header file being included
multiple times in a single source file.
Conditional directives guard against the multiple
processing of a header file.
Example:
#ifndef SHOUT_H
#define SHOUT_H
/* shout.h definitions go here */
#endif
January 26, 2012 CS410 – Software Engineering 28
Lecture #2: Hello, C++ World!
29. Preprocessor Directives
The #ifdef, #ifndef, and #endif directives are most
frequently used to conditionally include program code
depending on whether a preprocessor constant is
defined.
This can be useful, for example, for debugging:
January 26, 2012 CS410 – Software Engineering 29
Lecture #2: Hello, C++ World!
30. Preprocessor Directives
int main()
{
#ifdef DEBUG
cout << “Beginning execution of main()n”;
#endif
string word;
while (cin >> word)
cout << “word read is: “ << word << “n”;
cout << “OK, that’s all.”;
return 0;
}
January 26, 2012 CS410 – Software Engineering 30
Lecture #2: Hello, C++ World!
31. Comments
• Comments are an important aid to human readers
of our programs.
• Comments need to be updated as the software
develops.
• Do not obscure your code by mixing it with too
many comments.
• Place a comment block above the code that it is
explaining.
• Comments do not increase the size of the
executable file.
January 26, 2012 CS410 – Software Engineering 31
Lecture #2: Hello, C++ World!
32. Comments
In C++, there are two different comment delimiters:
• the comment pair (/*, */),
• the double slash (//).
The comment pair is identical to the one used in C:
• The sequence /* indicates the beginning of a
comment.
• The compiler treats all text between a /* and the
following */ as a comment.
• A comment pair can be multiple lines long and can
be placed wherever a tab, space, or newline is
permitted.
• Comment pairs do not nest.
January 26, 2012 CS410 – Software Engineering 32
Lecture #2: Hello, C++ World!
33. Comments
The double slash serves to delimit a single-line
comment:
• Everything on the program line to the right of the
delimiter is treated as a comment and ignored by
the compiler.
A typical program contains both types of comments.
In general, use comment pairs to explain the
capabilities of a class and the double slash to explain
a single operation.
January 26, 2012 CS410 – Software Engineering 33
Lecture #2: Hello, C++ World!