r1 - 27 Oct 2008 - 19:35:49 - MarcoAndreFerreiraDiasYou are here: TWiki >  Sandbox Web > CursoCPP

The first program

Your first program:
#include 
int main(int argc, char*argv[]){
  std::cout <<" Hello, world!" <

You can compile it

g++ -ansi -pedantic -Wall helloworld.cc
./a.out
You can direct to an executable name
g++ -ansi -pedantic -Wall helloworld.cc -o hello.exe
./hello.exe
the -pedantic stops compilation if you are using something off ansi standard. -Wall for warnings. Try not ignore them!

Fun with Preprocessor only

g++ -E preprocessor.cc

Using (http://jacek.web.cern.ch/jacek/courses/c++/src/preprocessor.cc), remove all comentaries (//) For example:

#define PI "Hello world"
#ifdef DEFINED
All this will be kept
#else
it is removed
#endif
Compile it using
gcc -E hello.cc
You can use the preprocessor directive if you will use the same constant all the time:
#include 
//This a comentary
/* this is another comentary */
#define PI "Hello world"
int main(int argc, char*argv[]){
  std::cout <
and you can undefine this (example in our code)
#include 
//This a comentary
/* this is another comentary */
#define PI "Hello world"
int main(int argc, char*argv[]){
  std::cout <

For standard places

#include 
if you want to specify a location
#include "/home/blah"

Types

C++ is very stupid to know what type is by itself.

#include 
int main(int argc, char*argv[]){
  double my_string=12.22;
  std::cout <<2*my_string <
* Always you will use ; for ending statements inside main() *
Inside main() this statements are local. Outside main() they are global.
#include 
int x=3;
void local(){
  std::cout<<" 2: x= " << x < Functions 

#include 
#define PI 3.14
int  square(int n);
int main(int argc, char*argv[]){
  int my=12;
  int my_square=square(my);
  std::cout<<"The square of " << my <<" is " <

Loops

#include 
const int STOP =3;
int main(){
  //the while loop
  int i=0;
  while(i<10){
    //   std::cout<

Scope

The declaration inside {} shadows the global declaration
#include 
const int STOP =3;
int main(){
  int i=4;
  {
    int i=5;
std::cout<
it is a source of confusion: you can declare something inside your loop for example, and when you try to use it outside the scope, this variable is not the
same!

Namespace

using namespace std
so, you can avoid
std::cout
and for short
cout
but don' t use it! You are saving time, in the wrong road.

References

This is the analog of pointers in C. The object and the reference shares the same memory location, saving CPU cicles:
#include 
const int STOP =3;
onst int STOP =3;
int main(){
  int i=4;
  int& r =i;
  int c = i;
std::cout <
You will see this output:
4
4
4
2
2
4

Pointer

but pointers even exists, for low level manipulations:

#include 
const int STOP =3;
int main(){
  int i=4;
  int* ip;//a pointer to integer
  //the memory address
  std::cout<<&i<
Warning: this will compile, but will crash (null referecing)
#include 
int main(int argc, char*argv[]){
  int *i; //create pointer to integer
  i=0;//position is 0
  std::cout<<*i<

Arrays

Follow the example:
#include 
int main(int argc, char*argv[]){
  int a[4];
  a[0]=1;
  a[1]=10;
  a[2]=100;
  a[3]=1000;
  for(int i=0; i<4; i++)
  std::cout<
or, more for the same:
#include 
int main(int argc, char*argv[]){
  int a[]={1,10,100,1000};
  for(int i=0; i<4; i++)
  std::cout<
simple, isn' t? You can use arrays as pointers [consider p[i]=*(p+i)]:
#include 
int main(int argc, char*argv[]){
  int i=2;
  int* a;
  a =&i;
  std::cout<
You can't allocate dynamic size array:
#include 
int main(int argc, char*argv[]){
  int size =5;
  int array[size];
  return EXIT_SUCCESS;
}
but without -pedantic option you can compile, g++ allows it.

argc and argv

argc os " ARGument Count" and stores the number of arguments wich were passed at startup. " ARGument Vector" stores the arguments themselves, it captures th e comand line argumments. For instance:

#include 
int main(int argc, char*argv[]){
  std::cout<
If you compile it and execute
> ./a.out teste
teste
or, for a variable number of arguments:
#include 
int main(int argc, char*argv[]){
  for(int i=1; i< argc; i++)
    std::cout<

What EXIT_STATUS means?

It is the Success code of the host operational system that the program is actually running. For a Linux, the program below should report " 0"
#include 
int main(int argc, char*argv[]){
  std::cout<

Constants

Are values that cannot be changed at our will:
int main(int argc, char*argv[])
  int i;//An ordinary integer.
i=1;//its value can be changed at will
i=2;
const int j=1;//const integer
//j=2;//Error: cannot change value of const variables
int const k=1;//alternative syntax
//k=2;//error
return EXIT_SUCCESS;
}

Polymorphism

on a limited set you can achieve it using a macro
#include 
#define greater(a,b) ((a) <(b) ? (b) : (a))
int main(int argc, char*argv[]){
std::cout<
And see template0a.cc. In this case,running
nm hello.exe |grep greater
in a Unix shell you will see three strings greater.(for example _Z7greaterdd the last dd compares two double, ii two integers). But is annoyng write the same function to all types. So using the templates magic you can do it for short. This black magic is done here:
#include 

template
T greater(T a, T b) {
  if (a > b) {
    return a;
  }
  return b;
}


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

  std::cout << greater(1   , 2)     << std::endl;
  std::cout << greater(1.5 , 2.5)   << std::endl;
  std::cout << greater("hi", "bye") << std::endl;

  return EXIT_SUCCESS;

}

Classes

Class definitions needs the semi-colon at the end of its definition:
#include 

class Rectangle{
public:
  double base, height;
};
int main(int argv, char*argc[]){
  Rectangle r;
  r.base=12;
  r.height=14;
  std::cout<
class is a sort of " new type" (" Rectangle r"  don' t seems the same
declaraton  " int a"? ). Its concept is very near " struct" (for example
coment " public"  and change " class" by " struct" )
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions

tip TWiki Tip of the Day
Custom rendered bullets
The RenderListPlugin can render bulleted lists in a variety of different ways. Use %RENDERLIST{ parameters ... Read on Read more

 
Home
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback