Thread: quick syntax question

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    39

    quick syntax question

    hi,

    I have this piece of code from a course I'm studying, but I can't make out all the details. Could someone please explain some of the syntax to me?

    Code:
    class A {
     public:
      A();
      A(int i);
      void setVarA (int i);
      int getVarA ();
     private:
      int varA; 
    };
    
    class B : public A {
     public:
      B();
      B(int i);
      B(int vara, int varb);	
      void setVarB (int i);
      int getVarB ();
     private:
      int varB;
    };
    
    A::A():varA(0) {}
    A::A(int i):varA(i) {}
    B::B():A(),varB(0) {}
    B::B(int i):A(),varB(i) {}
    B(int vara, int varb):A(vara), varB(varb){}
    I know about classes, about namespaces and inheritance. So I get the decleration of the classes A & B, with their constructors, methods etc.
    What I don't get is the last bit of code:

    Code:
    A::A():varA(0) {}
    A::A(int i):varA(i) {}
    B::B():A(),varB(0) {}
    B::B(int i):A(),varB(i) {}
    B(int vara, int varb):A(vara), varB(varb){}
    I know A::A() calls A's constructor, but what does the added :varA(0) do? is it a way to define the constructors outside the class? If so, can it be done to other functions as well?
    I'd very much appreciate if someone could put a name to this technique so I can do some reading on it.

    thx in advance

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Those are initialization lists: Initialization Lists in C++ - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    thx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM