370 likes | 609 Views
C, C++. Two of most commonly used languages in numerical computing portability optimizability freely available compilers low-level enough for near-metal design high-level enough for abstract reasoning. First C program. #i n c l u d e <s t d i o .h > // get standard I/O facilities
E N D
C, C++ • Two of most commonly used languages in numerical computing • portability • optimizability • freely available compilers • low-level enough for near-metal design • high-level enough for abstract reasoning
First C program #i n c l u d e <s t d i o .h > // get standard I/O facilities i n t m a i n () { c o n s t i n t m a x = 2 0 ; // maximum name length is 19 characters c h a r n a m e [m a x ]; p r i n t f ("P l e a s e e n t e r y o u r f i r s t n a m e :\ n "); s c a n f ("%s ",n a m e ); // read characters into name p r i n t f ("H e l l o %s \ n ",n a m e ); r e t u r n 0 ; }
First C++ program // First C++ program /* **************************8 #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }
C vs C++ • C is not a proper subset of C++ • C++ is more strict about types • strict is usually better for numerical programming • C++ handles memory allocation, extendable objects more gracefully and easily than C • Larger standard library pool for C++ • C libraries can still be used • C++ contains object-oriented functionality
Code Structure – Within Files • Function declaration • Function definition
Code Structure - Files • Code Files • Header Files • Object Files
External Functions and Libraries • Common libraries are • stdio, math • You will also build your own sets of object files, to be accessed from within another code file
Compilation Process • Compiling • Linking
Conventions • header files are .h in C, C++ • C code - .c • C++ code - .cc, .cpp, .cxx, .C • gcc – use for compiling, linking C • g++ - use for compiling, linking C++ • Use -Wall
Downsides to C, C++ • Memory Management • Interfaces Tools • Graphics Tools
Memory Management • The bane of C, C++ programs • you allocate memory manually • new, malloc • you de-allocate memory manually • delete, free • memory allocated that is not manually freed is consumed but unusable until the program ends • At program end, all system resources are returned to the operating system • Always look for new/delete pairs • Get in the habit of thinking about memory usage
Interfaces • Command-line interface in all standard C, C++ packages • Graphical interfaces (GUIs) vary by platform • C interfaces are most difficult to program • C++ interfaces are far from trivial • best built using GUI developers using commercial software
Graphics • Graphics in C, C++ suffer from same platform issues as GUI • Best to modularize • compute in C, C++ • use MATLAB, S-Plus, R, others to generate graphs
Class example in C++ // In cube .h class Cube { public: Cube(); // constructor ~Cube(); // destructor void setSide(double s); double getSide(); double Area(); double Volume(); // Data members private: double Side; };
Class Example - Part 2 // in cube.cpp #include <iostream> #include "cube.h" Cube::Cube() { } Cube::~Cube() { } void Cube::setSide(double s) { Side = s <= 0 ? 1 : s; } double Cube::getSide() { return Side; } double Cube::Area() { return 6 * Side * Side; } double Cube::Volume() { return Side * Side * Side; }
Class Example - Part 3 // in cubeApp.cpp #include <iostream> #include "cube.h" int main() { Cube c = new Cube(); }
Compiling and Linking g++ -c cube.cpp cubeApp.cpp –Wall g++ cube.o cubeApp.o -o cubeApp ./cubeApp
Next Step • Convert beam model program to C++ code!
Resources • Bjarne Stroustrup's C++ links • http://www.research.att.com/~bs/bs_faq.html • C++ Tutorials • http://functionx.com/cpp/index.htm • http://www.cplusplus.com/doc/tutorial/