KEMBAR78
Intro to C++ - language | PDF
C++	
  -­‐	
  for	
  Java	
  Developers:	
  
               Intro	
  to	
  C++	
  
            Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
C++	
  
•  1998	
  ANSI/ISO	
  Standard:	
  
    –  Core	
  language	
  
    –  Standard	
  Library	
  
•  Many	
  C++	
  libraries	
  exist	
  that	
  are	
  not	
  part	
  of	
  
   the	
  standard	
  	
  
•  New	
  standard:	
  ISO/IEC:2011	
  C++11	
  
C++	
  Standard	
  Library	
  
•  Containers	
  
    –  array,	
  bitset,	
  deque,	
  forward_list,	
  list,	
  …	
  vector	
  
•  General	
  
    –  algorithm,	
  funcUonal,	
  iterator,	
  locale,	
  memory…	
  
•  Strings	
  
    –  string	
  
•  Input	
  and	
  Output	
  
    –  ios,	
  iostream,	
  ostream,	
  sstream..	
  
•  Numerics	
  
    –  complex,numeric,	
  valarray	
  
•  Language	
  support	
  
    –  excepUon,	
  limits,	
  new,	
  typeinfo	
  
C	
  Standard	
  Library	
  
•  Macros,	
  typedefiniUons,	
  funcUons	
  for	
  tasks	
  
   like	
  string	
  handling,	
  mathemaUcal	
  
   computaUons,	
  memory	
  allocaUons..	
  
•  See:	
  
   –  hYp://en.wikipedia.org/wiki/C_standard_library	
  
Diagram	
  from:	
  
hYp://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html	
  
GCC	
  Compiler	
  
•  The	
  GNU	
  Compiler	
  CollecUon	
  (GCC)	
  is	
  a	
  
   compiler	
  system	
  produced	
  by	
  the	
  GNU	
  Project	
  
   supporUng	
  various	
  programming	
  languages.	
  
•  C	
  (gcc),	
  C++	
  (g++),	
  ObjecUve-­‐C	
  (gobjc),	
  Fortran	
  
   (gfortran),	
  Java	
  (gcj)	
  
•  CompaUble	
  IDEs	
  
    –  Dev-­‐C++	
  (Win),	
  NetBeans,	
  Eclipse,	
  QtCreator,	
  
       Xcode	
  
Installing	
  
•  Ubuntu	
  
    –  sudo	
  apt-­‐get	
  install	
  g++	
  
•  Mac	
  OS	
  X	
  
    –  Install	
  Xcode	
  and	
  install	
  command	
  line	
  tools	
  
•  Windows	
  
    –  For	
  example	
  minGW:	
  hYp://sourceforge.net/
       projects/mingw/	
  
Compiling	
  C++	
  Program	
  
•  Simple	
  
   –  g++ mysourcecode.cpp
•  BeYer	
  
   –  g++ -ansi -pedantic -wall
      mysourcecode.cpp -o myapp
•  Running	
  
   –  ./myapp
Code	
  Style	
  
•  Java	
  has	
  “standard”	
  for	
  code	
  style,	
  C++	
  does	
  
   not.	
  
•  Use	
  can	
  use	
  separate	
  apps	
  for	
  “preffy”	
  your	
  
   source	
  code	
  
•  For	
  example:	
  ArUsUc	
  Style	
  
    –  hYp://astyle.sourceforge.net/	
  
Makefiles	
  
•  All	
  these	
  command	
  line	
  arguments	
  can	
  be	
  
   long	
  and	
  hard	
  to	
  write.	
  
•  Use	
  Make!	
  UUlity	
  that	
  automaUcally	
  builds	
  
   apps.	
  
•  hYp://en.wikipedia.org/wiki/Make_(sohware)	
  
makefile
WHAT	
  ABOUT	
  QT?	
  
Qt	
  Hello	
  World	
  
#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QPushButton hello("Hello world!");
    hello.resize(100, 30);

    hello.show();
    return app.exec();
}
SOME	
  C++	
  SYNTAX	
  
cout	
  for	
  output,	
  cin	
  for	
  input	
  
•  Output	
  
    –  cout	
  <<	
  “hello!”;	
  
•  Input	
  
    –  cin	
  >>	
  someVariable;	
  
Datatypes	
  
•  Fundamental	
  types	
  
    –  int,	
  short,	
  long	
  
    –  float,	
  double,	
  long	
  double	
  
    –  bool	
  
    –  char	
  
•  Derived	
  types	
  
    –  arrays,	
  pointers,	
  references..	
  
•  Class	
  types	
  
    –  class,	
  struct,	
  union	
  
const	
  and	
  enum	
  
// Like final in Java
const int NUMBER = 100;
// Using enums
enum DAY { MON = 1, TUE, WED, THU, FRI,
SAT, SUN };
DAY today = MON;
CondiUons	
  in	
  C++	
  
int a = 0;
if(a = 0)
{
   cout << “What the..” << endl;
}
Arrays	
  
const int LENGTH = 5;
int numbers[LENGTH];
numbers[0] = 12;
numbers[1] = 88;
..
About	
  Strings	
  
•  C++	
  has	
  two	
  kind	
  of	
  Strings	
  
    –  char	
  arrays	
  
    –  string	
  –	
  class	
  
C	
  type	
  strings	
  
•  char	
  myString[6]	
  =	
  “Jussi”;	
  
•  //	
  Why	
  6??	
  The	
  array	
  contains	
  now	
  Jussi0!	
  
•  char	
  myString[]	
  =	
  “Jussi”	
  
cin.get	
  
// Reads maxLength number of chars
// from the user
cin.get(myString, maxLength);
// This leaves user given enter ‘n’ to input
// stream, use cin.get again to get rid of
//it..

Intro to C++ - language

  • 1.
    C++  -­‐  for  Java  Developers:   Intro  to  C++   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2.
    C++   •  1998  ANSI/ISO  Standard:   –  Core  language   –  Standard  Library   •  Many  C++  libraries  exist  that  are  not  part  of   the  standard     •  New  standard:  ISO/IEC:2011  C++11  
  • 3.
    C++  Standard  Library   •  Containers   –  array,  bitset,  deque,  forward_list,  list,  …  vector   •  General   –  algorithm,  funcUonal,  iterator,  locale,  memory…   •  Strings   –  string   •  Input  and  Output   –  ios,  iostream,  ostream,  sstream..   •  Numerics   –  complex,numeric,  valarray   •  Language  support   –  excepUon,  limits,  new,  typeinfo  
  • 4.
    C  Standard  Library   •  Macros,  typedefiniUons,  funcUons  for  tasks   like  string  handling,  mathemaUcal   computaUons,  memory  allocaUons..   •  See:   –  hYp://en.wikipedia.org/wiki/C_standard_library  
  • 6.
  • 8.
    GCC  Compiler   • The  GNU  Compiler  CollecUon  (GCC)  is  a   compiler  system  produced  by  the  GNU  Project   supporUng  various  programming  languages.   •  C  (gcc),  C++  (g++),  ObjecUve-­‐C  (gobjc),  Fortran   (gfortran),  Java  (gcj)   •  CompaUble  IDEs   –  Dev-­‐C++  (Win),  NetBeans,  Eclipse,  QtCreator,   Xcode  
  • 9.
    Installing   •  Ubuntu   –  sudo  apt-­‐get  install  g++   •  Mac  OS  X   –  Install  Xcode  and  install  command  line  tools   •  Windows   –  For  example  minGW:  hYp://sourceforge.net/ projects/mingw/  
  • 13.
    Compiling  C++  Program   •  Simple   –  g++ mysourcecode.cpp •  BeYer   –  g++ -ansi -pedantic -wall mysourcecode.cpp -o myapp •  Running   –  ./myapp
  • 14.
    Code  Style   • Java  has  “standard”  for  code  style,  C++  does   not.   •  Use  can  use  separate  apps  for  “preffy”  your   source  code   •  For  example:  ArUsUc  Style   –  hYp://astyle.sourceforge.net/  
  • 16.
    Makefiles   •  All  these  command  line  arguments  can  be   long  and  hard  to  write.   •  Use  Make!  UUlity  that  automaUcally  builds   apps.   •  hYp://en.wikipedia.org/wiki/Make_(sohware)  
  • 17.
  • 19.
  • 20.
    Qt  Hello  World   #include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton hello("Hello world!"); hello.resize(100, 30); hello.show(); return app.exec(); }
  • 21.
  • 22.
    cout  for  output,  cin  for  input   •  Output   –  cout  <<  “hello!”;   •  Input   –  cin  >>  someVariable;  
  • 23.
    Datatypes   •  Fundamental  types   –  int,  short,  long   –  float,  double,  long  double   –  bool   –  char   •  Derived  types   –  arrays,  pointers,  references..   •  Class  types   –  class,  struct,  union  
  • 24.
    const  and  enum   // Like final in Java const int NUMBER = 100; // Using enums enum DAY { MON = 1, TUE, WED, THU, FRI, SAT, SUN }; DAY today = MON;
  • 25.
    CondiUons  in  C++   int a = 0; if(a = 0) { cout << “What the..” << endl; }
  • 26.
    Arrays   const intLENGTH = 5; int numbers[LENGTH]; numbers[0] = 12; numbers[1] = 88; ..
  • 27.
    About  Strings   • C++  has  two  kind  of  Strings   –  char  arrays   –  string  –  class  
  • 28.
    C  type  strings   •  char  myString[6]  =  “Jussi”;   •  //  Why  6??  The  array  contains  now  Jussi0!   •  char  myString[]  =  “Jussi”  
  • 29.
    cin.get   // ReadsmaxLength number of chars // from the user cin.get(myString, maxLength); // This leaves user given enter ‘n’ to input // stream, use cin.get again to get rid of //it..