Lecture 3 introduction to Programming languages C++
Lecture 3 introduction to Programming languages C++
Languages
Introduction to C++
Lecture 3
Very brief history of C++
GasMolecule ch4
GasMolecule co2
“pseudo-code”
spectrum = ch4.IR(1000,3500)
Name = co2.common_name
Object-oriented programming “Class Car”
d = 3, f = 5;
x = 'x';
A=5.1
String E= “GeeksforGeeks”
Character C=‘a’, ‘A’, ‘z’
C=a+b
Debugging?
Debugging is the process of finding and removing errors in a program.
In this process, the program is thoroughly checked for errors. Then
errors are pointed out and debugged.
Different types errors which can occur during the execution of a
program
• There are three types of errors which can occur during the execution of
a program.
• Syntax Errors
• Runtime Errors
• Logical errors
syntax error
• A syntax error occurs when the program violates one or more
grammatical rules of the programming language. These errors are
detected at compile time, i.e., when the translator (compiler or
interpreter) attempts to translate the program.
Runtime error
A runtime error occurs when the computer is directed to perform an illegal operation
by the program such as dividing a number by zero. Runtime errors are the only
errors which are displayed immediately during the execution of a program. When
these errors occur, the computer stops the execution of the programming and can
display a diagnostic message that will help in locating the error.
Logical error
• The logical error happens when a program implements a wrong logic.
The translator (compiler or interpreter) does not report any error
message for a logical error. These errors are the most difficult to locate.
Flow Chart
The flowchart is a
pictorial
representation of a
program which
helps in
understanding the
flow of control and
data in the
algorithm.
Algorithm
An algorithm is a finite set
of steps which, if followed,
accomplish a particular task.
An algorithm must be clear,
finite and effective.
Header Files C++ language headers aren’t referred
to with the .h suffix. <iostream>
provides definitions for I/O functions,
including the cout function.
• C++ (along with C) uses header files as to
hold definitions for the compiler to use
while compiling. #include <iostream>
• A source file (file.cpp) contains the code using namespace std;
that is compiled into an object file (file.o).
• The header (file.h) is used to tell the int main()
{
compiler what to expect when it assembles
string hello = "Hello";
the program in the linking stage from the string world = "world!";
object files. string msg = hello + " " + world ;
• Source files and header files can refer to cout << msg << endl;
msg[0] = 'h';
any number of other header files.
cout << msg << endl;
return 0;
}
Slight change
• Let’s put the message into some variables #include <iostream>
of type string and print some numbers.
using namespace std;
• Things to note:
• Strings can be concatenated with a + operator. int main()
{
• No messing with null terminators or strcat() as in
C string hello = "Hello";
string world = "world!";
• Some string notes: string msg = hello + " " + world ;
• Access a string character by brackets or function: cout << msg << endl;
• msg[0] → “H” or msg.at(0) → “H” msg[0] = 'h';
cout << msg << endl;
• C++ strings are mutable – they can be
changed in place. return 0;
}
• Press F9 to recompile & run.
A first C++ class: string
• string is not a basic type (more on #include <iostream>
to appear.
A first C++ class: string
Shows this
function
(main) and the
List of string type of msg
methods (string)
List of other
string objects
• Next: let’s find the size() method without scrolling for it.
A first C++ class: string
• Start typing “msg.size()” until it appears in the list. Once it’s highlighted (or you scroll
to it) press the Tab key to auto-enter it.
• On the right you can click “Open declaration” to see how the C++ compiler defines
size(). This will open basic_string.h, a built-in file.
#include <iostream>
int main()
{
• Tweak the code to print the string hello = "Hello" ;
string world = "world!" ;
number of characters in the string msg = hello + " " + world ;
string, build, and run it. cout << msg << endl ;
msg[0] = 'h';
• From the point of view of cout << msg << endl ;
main(), the msg object has cout << msg.size() << endl ;
hidden away its means of return 0;
tracking and retrieving the }
number of characters stored.
• Note: while the string class ▪ Note that cout prints integers
has a huge number of without any modification!
methods your typical C++
class has far fewer!
Break your code.
• Remove a semi-colon. Re-compile. What messages do you get from the compiler
and C::B?
• Fix that and break something else. Capitalize string → String
• C++ can have elaborate error messages when compiling. Experience is the only
way to learn to interpret them!
• Fix your code so it still compiles and then we’ll move on…
Basic Syntax
• C++ syntax is very similar to C, Java, or C#. Here’s a few things up front and we’ll cover
more as we go along.
• Curly braces are used to denote a code block (like the main() function):
{ … some code … }
• Statements end with a semicolon:
int a ;
a = 1 + 3 ;
• Comments are marked for a single line with a // or for multilines with a pair of /* and */ :
// this is a comment.
/* everything in here
is a comment */
void my_function() {
int a ;
• Variables can be declared at any time in a code block. a=1 ;
int b;
}
• Functions are sections of code that are called from other code. Functions always have a
return argument type, a function name, and then a list of arguments separated by
commas:
http://www.cplusplus.com/doc/tutorial/variables/
Need to be sure of integer sizes?
• In the same spirit as using integer(kind=8) type notation in Fortran, there are type definitions that
exactly specify exactly the bits used. These were added in C++11.
• These can be useful if you are planning to port code across CPU architectures (ex. Intel 64-bit CPUs
to a 32-bit ARM on an embedded board) or when doing particular types of integer math.
• For a full list and description see: http://www.cplusplus.com/reference/cstdint/
#include <cstdint>
Name Name Value
int8_t uint8_t 8-bit integer
int16_t uint16_t 16-bit integer
int32_t uint32_t 32-bit integer
int64_t uint64_t 64-bit integer
Type Casting
• C++ is strongly typed. It will auto-convert a variable of one type to another in a limited fashion: if it
will not change the value.
short x = 1 ;
int y = x ; // OK
short z = y ; // NO!
• Conversions that don’t change value: increasing precision (float → double) or integer → floating
point of at least the same precision.
• C++ allows for C-style type casting with the syntax: (new type) expression
double x = 1.0 ;
int y = (int) x ;
float z = (float) (x / y) ;
• But since we’re doing C++ we’ll look at the 4 ways of doing this in C++ next...
Type Casting
double d = 1234.56 ;
• static_cast<new type>( expression ) float f = static_cast<float>(d) ;
• This is exactly equivalent to the C style // same as
cast.
float g = (float) d ;
• This identifies a cast at compile time.
• This will allow casts that reduce precision (ex. double → float)
• ~99% of all your casts in C++ will be of this type.