introduction to c++research.bbalaguer.com/teaching/f12_cse30/lectures/lecture-01.pdf ·...
TRANSCRIPT
Introduction to C++
Tuesday, June 19, 12
History Lesson
C developed by Dennis Ritchie at AT&TBell Labs in the 1970s.
Used to maintain UNIX systems
Many commercial applications written in C
C++ developed by Bjarne Stroustrup at AT&TBell Labs in the 1980s.
Overcame several shortcomings of C
Incorporated object oriented programming
C remains a subset of C++
Tuesday, June 19, 12
History Lesson
Where did C++ come from?
Derived from the C language
C was derived from the B language
B was derived from the BCPL language
Why the ‘++’?
++ is the post-increment operator
Therefore, C++ is C, ++
Tuesday, June 19, 12
C++ Compiler
Compiler accepts almost any pattern of line breaks and indentation
However, this invites bad programming practices
We don’t want to learn bad programming habits, they are hard to unlearn
Tuesday, June 19, 12
C++ Compilation
However, three easy things to remember:
Good programmers format programs so they are easy to read
Good programmers typically:
Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves
Indent statements
Use only one statement per line
And, most importantly, Good programmers get good grades
Tuesday, June 19, 12
Very Simple Program
#include <iostream>using namespace std;
int main(){ cout << “Hello, world!\n”; return 0;}
Tuesday, June 19, 12
Slightly more complex program
Tuesday, June 19, 12
Things to notice about the example
Variables are declared before they are used
Typically variables are declared at the beginning of the program
Statements (can be multi-line) end with a semi-colon
The #include directive: #include <iostream>
Tells compiler where to find information about items used in the program
iostream is a library containing definitions of cin and cout
Tuesday, June 19, 12
C++ Syntax
using namespace std;Tells the compiler to look for methods and data types in the “std” namespace
A namespace allows for us to have methods, classes, and data types with the same name that exist in separate “namespaces”
In C++, our program begins with a main() method:int main()
Which must return an integer value at the end of the its execution:
return 0;
Tuesday, June 19, 12
C++ Syntax Highlights
By now you’ve probably noticed that C++ looks a lot like Java, though not identical by any means
That means that a lot of your old knowledge of simple logical structures (do, while, for, if, else, etc.) will transfer
However, there are still some gotchas to look out for!
When the compiler fails, it will try to give you a meaningful error message
However, that being said, sometimes they’re hard to understand, so be patient.
Tuesday, June 19, 12
Writing C++ Code
C++ source code is written with a text editor, we don’t need a fancy IDE
Example editors:
vi is my UNIX editor of choice
emacs is okay, but bloat-ware
nano is an absolute last resort
The compiler on your system converts the source code to object code.
The linker combines all the object code into an executable program.
Tuesday, June 19, 12
C++ vs. Java
C++ is a compiled language, meaning that when you compile C++ code, it effectively becomes machine instructions
Java, on the other hand, is compiled into intermediate byte-code which is then executed by a virtual machine.
C++ looks a lot like Java syntactically, but it is not identical!
Java is fully object-oriented from the ground-up, meaning that Java "intuitively" knows what objects are (the Object class) and every class implicitly inherits from this Object class.
C++, on the other hand, is really just C with "objects" grafted on as an after-thought
Tuesday, June 19, 12
#include vs import
Java -- import java.io.*; In Java, this tells the compiler to include the objects in /java/io in its search path, so when Java encounters an object type it doesn't already know about, it looks in /java/io for a matching class
The code in java.io.* is not imported into your source file, just the search path is modified.
C++ -- #include <iostream.h> In C++, the #include statement is a pre-processor directive which means that the compiler scans your file(s) for #.... statements and figures out what they mean before actually compiling your code
#include tells the compiler "Take the file I specify and put its entire contents right here in place of this line
Tuesday, June 19, 12
#include “”
#include has two ways to specify the file, each means something subtly different.
#include "file.h" -- the quotes tell the #include statement that the file to be included is being specified relative to the file including it.
For example, if in foo.cpp there is a line #include "foo.h", the compiler would look in the same directory as foo.cpp for the file foo.h
Tuesday, June 19, 12
#include <>
#include <file.h> -- the angle brackets tell the #include statement to search the include path for the file to be included
The include path is pre-determined by the compiler but can be modified at compile time.
gcc uses the -I (capital i) flag to add directories to the include search path. i.e gcc -I $HOME/Code/include would add $HOME/Code/include to your include search path.
N.B: the current directory is typically NOT in the default search path
Tuesday, June 19, 12
A note about macros
Technically, there is an intermediate phase between the compiler and the editor, and that is the “pre-processing” phase
During this phase, the pre-processor looks for any line beginning with # and processes those statements
For example, we have a #if, #else, #endif set of macros, which we can use to comment out code:
#if 0
int function(int x, int y){ return x + y; }
#endif
Tuesday, June 19, 12
Macros
These macros get processed before the code gets compiled
Files get included, blocks of code are visible or hidden from the compiler (#if blocks)
We will discuss this more in depth as the semester progresses
Tuesday, June 19, 12
Other macros
There are a number of pre-processor macros that we will use, here are some common ones:
#include -- include a source or header file
#if / #endif -- pre-processor conditional
#ifdef / #ifndef -- if defined / if not defined
#pragma -- give certain compilers specific instructions
Tuesday, June 19, 12
Header Files
Header files provide a way to disclose the functionality of an object, or library function to others without disclosing how the internals work. We will discuss later on why this is useful.
The skeleton of the functions, classes and data are outlined in the header file (ex: foo.h)
This file only reveals what the class looks like, for example, what functions it has, what their "signatures" are (how they are called, what parameters they take in what order, etc)
Any developer can take the .h file and know how to use your class, library, etc.
Typically documentation is also generated from the .cpp files to provide more in-depth information about the class / library
Tuesday, June 19, 12
Organization
Header file (.h) Declaration of your class or library (its structure)
No code goes in the header file (except in a few cases)
Every .h file should be fully autonomous, so include any external things your class or library will need to function (i.e string.h, iostream.h, etc.)
Source file (.cpp) Definition of your class or library (the code itself)
Must include the corresponding .h file
To not re-include external requirements, put the #include statements in your .h files for any dependencies
Tuesday, June 19, 12
Example .h file
#include <string.h>
class Person{
public: Person(){} // Default constructor Person(string _name); // Constructor that takes a name Person(string _name,int _age); // Constructor that takes a name and an age void print(); void setName(string _name); string getName(); private: string name; int age;
};
Tuesday, June 19, 12
Example .cpp file#include "person.h"
void Person::print(){ cout << name << "is" << age << "years old." << endl;}
string Person::getName(){ return name;}
Person::Person(string _name){ name = _name;}
Person::Person(string _name, int _age){ name = _name; age = _age;}
Tuesday, June 19, 12
Things to remember about C++
C++ is not compiled to an intermediate byte-code, but rather into operating-system specific instructions
This makes C++ binaries not very portable as they are tied to the libraries and operating system internals they were built on and for.
This does not make C++ as a language less portable, in fact C/C++ is quite portable unless you rely on operating-system specific code.
Our source code is compiled into object-files which are then linked together to form an executable that can be run
Tuesday, June 19, 12
Compiling your Code
Under Linux you will be using GCC, the GNU C Compiler
Do not compile your header files, only your .cpp files. Compiling your header files leads to very confusing results. If you see a .gch file mixed in with your other files, that means you've accidentally compiled a .h file.
The command line you want to use to produce a binary file (executable program) is:
g++ source.cpp -o binaryname
which takes source.cpp and compiles it into a binary executable called binaryname
Tuesday, June 19, 12