1 / 21

C, C++

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

Download Presentation

C, C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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 ; }

  3. First C++ program // First C++ program /* **************************8 #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }

  4. 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

  5. Code Structure – Within Files • Function declaration • Function definition

  6. Code Structure - Files • Code Files • Header Files • Object Files

  7. 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

  8. Example – Beam Model

  9. Compilation Process • Compiling • Linking

  10. Notes

  11. 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

  12. Downsides to C, C++ • Memory Management • Interfaces Tools • Graphics Tools

  13. 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

  14. 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

  15. 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

  16. 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; };

  17. 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; }

  18. Class Example - Part 3 // in cubeApp.cpp #include <iostream> #include "cube.h" int main() { Cube c = new Cube(); }

  19. Compiling and Linking g++ -c cube.cpp cubeApp.cpp –Wall g++ cube.o cubeApp.o -o cubeApp ./cubeApp

  20. Next Step • Convert beam model program to C++ code!

  21. 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/

More Related