Plan for today • Get you started with C++ C++ is not Java – Compilation environment – The very, very basics – Preparation for Lab 1 History of C++ History of C++ • 1969 – UNIX • Why bring this up – Created to play space wars – C is a “low level” system language – B (Basic Combined Programming Language) • Program as manipulator of memory • 1970 • All memory management done by hand – UNIX port to PDP-11 – C++ is not only based on C but is a proper • 1972 superset – C (rewrite of B) • If you can do it in C you can do it in C++ • Early 1980s • Added Object Oriented paradigm. – C++ invented as a “superset of C” – Take home message: C++ is not Java!!!! C++ is not Java Not classes -- just files 10. Not classes -- just files • One class, two files – Header file (.h) • Contains class declaration (interface++) – Source file (.c, .cpp, .C, .cxx) • Contains class definition – implementation of methods 1
Not classes -- just files Not classes -- just files • #include “filename.h” • Cpp – The C Preprocessor – Inserts text from one file into another before – Reads all C code before compilation compilation – Directives – Contain info needed by other files to compile • Including text files • Libraries – function signature – #include • Classes – class interface (I.e. header file) • Conditional compilation – #if / #ifdef / #ifndef / #else / #endif • Macros – #define C++ is not Java Not packages -- just namespaces 10. Not classes -- just files • Like Java, C/C++ has a multitude of useful auxillary functions and classes in libraries 9. Not packages -- just namespaces • Unlike Java, C++ does not have the notion of packages. • C / C++ also doesn’t have nice javadocs • Use man instead C++ is not Java Not packages -- just namespaces namespace foo { 10. Not classes -- just files class bar { … }; 9. Not packages -- just namespaces } 8. We don’t need no stinkin’ VM foo::bar x; using namespace foo; bar x; 2
Compilation – Java C++: We don’t need no stinkin’ VM Other Java Other classes object linked Foo.C Foo.o Foo.java g++ foo.o .. javac Foo.class g++ java Foo Foo.java Foo.C Foo.exe JVM Object file linking (platform Java Foo.h C++ (platform independent) (platform compiler compiler dependent) dependent) Executable file (platform dependent) Compilation – Java vs C++ Compilation – C++ • Java • C++ • Use CC/g++ to compile individual files into – Compilation unit is the – Compilation unit is the object files class file • Use CC/g++ to link object files into an – External classes – External classes executable file located using a require header and predefined path precompiled object file – Need to specify executable name, otherwise – JVM needed to execute – Executable file need to will be named a.out execute. • Run the executable file. Using C Libraries Managing C++ Projects • make • Library header files must be #included for compilation – Files to be compiled – Compiler options – #include “mylib.h” – Libraries – #include <math.h> – Dependencies • Library object files must be linked when linking. – Make will build an executable – CC file file file –lm mylib.o – gmakemake – Makes Makefiles! 3
C++ is not Java Universal Error Messages • If there is a problem… 10. Not classes -- just files – Bus error (core dumped) 9. Not packages -- just namespaces – Segmentation fault (core dumped) 8. We don’t need no stinkin’ VM 7. Universal Error Messages C++ is not Java The lonely function syndrome 10. Not classes -- just files • No top level “main” class 9. Not packages -- just namespaces – main() in its own file 8. We don’t need no stinkin’ VM – main (int argc, char *argv[]) 7. Universal Error Messages • Functions need not belong to a class 6. The lonely function syndrome Functions Functions • Declaration vs definitions • In C++ function arguments are pass by value: – Declaration is the function signature • Required before function can be used • Often included in a header file void foo (int j) • Can exist in multiple files int i = 7; { foo (i); • char *strcpy (char * to, char* from); cout << “Arg is “ << j << endl; – Definition cout << i; j = 12; • Actual code } • Must defined once and only once void – indicates that a function does not return a value 4
C++ is not Java Sizing up a variable 10. Not classes -- just files • sizeof 9. Not packages -- just namespaces – Will return the size of a data item or object 8. We don’t need no stinkin’ VM • sizeof (char) = 1 7. Universal Error Message • sizeof (bool) = 1 6. The lonely function syndrome 5. Sizing up a variable C++ is not Java Basic IO • Two types of I/O 10. Not classes -- just files – C-style (stdio) 9. Not packages -- just namespaces • #include <stdio.h> 8. We don’t need no stinkin’ VM • fprintf (FILE *f, const char *format, …); • fscanf (FILE *f, const char *format, …); 7. Universal Error Message • FILE *stdin; 6. The lonely function syndrome • FILE *stdout; • FILE *stderr; 5. Sizing up a variable – Only standard datatypes supported 4. Streaming I/O – man –s 3C stdio Basic I/O Basic I/O #include <stdio.h> #include <stdio.h> int a; int a = 7; float b; float b = 6.4; char foo[10]; char *foo = “myString” scanf (“%d\t%f\t%x\n”, &a, &b, foo); printf (“%d\t%f\t%s\n”, a, b, foo); 7 6.4 myString 7 6.4 myString 5
Basic IO Basic I/O • Two types of I/O #include <iostream> using std – C++ style (I/O Streams) • << for output int a = 7; • >> for input float b = 6.4; • cin – standard input char *foo = “myString”; • cout – standard output cout << a << ‘\t’ << b << ‘\t’ << foo << endl; • cerr – standard error 7 6.4 myString Basic I/O C++ is not Java #include <iostream> 10. Not classes -- just files using std 9. Not packages -- just namespaces 8. We don’t need no stinkin’ VM int a; 7. Universal Error Message float b; 6. The lonely function syndrome char foo[10]; 5. Sizing up a variable cin >> a >> b >> foo(10); 4. Streaming I/O 3. Questionable statements 7 6.4 myString Statements Statements • if (condition) statement • Exiting a loop • if (condition) statement else statement – break – exit the loop • switch (condition) statement – continue – perform next iteration of a loop • while (statement) statement – goto – go anywhere • do statement while (expression) • for ( ; ; ) statement – Statements can be nested blocks of code • I.e { … } 6
Statements Statements • Logical conditions • Finally, my favorite C++ statement – Are short cuircuited – var = (condition) ? statement1 : statement2 – Same as: • if (condition) – if ( ( a < b) && ( c > d)) { var = expression1 …} else var = expression2 • If (a > b), (c > d) will not get tested. • min = (a <= b) ? a : b; C++ is not Java My Hero Zero 10. Not classes -- just files 9. Not packages -- just namespaces 8. We don’t need no stinkin’ VM 7. Universal Error Message 6. The lonely function syndrome 5. Sizing up a variable 4. Streaming I/O 3. Questionable statements 2. My hero zero Statements Statements • Logical conditions • Logical conditions – int a; – False if condition evaluates to 0 – if (a) { ... } // same as – True if condition evaluates to a non-zero value. – if (a != 0) { ... } – Can be interpreted as condition != 0 – int *b; – if (b) { ... } // same as – if (b != 0) { ... } 7
Statements C++ is not Java • Logical conditions 10. Not classes -- just files 9. Not packages -- just namespaces – Assignments and declarations can be made in a logical 8. We don’t need no stinkin’ VM condition 7. Universal Error Message – The following is valid: 6. The lonely function syndrome • if ( double d = somefunction (a)) { … } 5. Sizing up a variable 4. Streaming I/O – A most common mistake 3. Questionable statements 2. My hero zero • if ( a = b ) { … } // is not the same as 1. They didn’t write it for you! • if ( a == b) { … } Some code -- first.cpp Some code -- volume.h • #include <iostream> • #ifndef VOLUME_INCLUDED • #include "volume.h" • #define VOLUME_INCLUDED • int main (int argc, char *argv[]) • { • float ht, wd; • namespace Volume { • long int dp; • double v; • // Get dimensions • // Prototype for calVolume function • std::cout << "Enter height (x.xx), width (x.xx), depth (x)"; • std::cin >> ht >> wd >> dp; • double calcVolume (double, double, long); • // Calculate • v = Volume::calcVolume (ht, wd, dp); • } • // Print results • std::cout << "Volume is " << v << std::endl; • std::cout << "Note that height is still " << ht << std::endl; • #endif • return 0; • } Some code -- volume.cpp Running • namespace Volume { gmakemake > Makefile make • double calcVolume (double height, first double width, long depth) Enter height (x.xx), width (x.xx), depth (x) • { 2.51 3.45 8 • height = 7.7; Volume is 69.276 • return height * width * depth; Note that height is still 2.51 • } • } 8
Questions? • Next time – C++ Variables. 9
Recommend
More recommend