newbie stuff, plz help

Can anyone please explain to me this code from line 6 to line 12, i have no idea what is going on( i have never seen such syntax before)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  class Vehicle { 
protected : 
string license ; 
int year ; 
public :
 Vehicle( const string &myLicense , const int myYear)
 
 : license(myLicense), year(myYear) {} 
  const string getDesc() const 
{return license + " from " + stringify(year);} 
const string &getLicense() const {return license;} 
 const int getYear() const {return year;} 
 }; 
6 - the class constructor
8 - initialisation list - you are assigning the constructor parameters (myLiscense and myYear) to your member variables

9 to 12 - just normal class method implementations.

have a google at the 2 things i've put in bold.



it looks a bit confusing as normally you wouldn't put the implementation inside the class, this would all go in a .cpp file normally.
Last edited on
It would probably help if it was formatted differently too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Vehicle { 
protected: 
   string license; 
   int year; 

public:
   Vehicle( const string &myLicense , const int myYear) : license(myLicense), year(myYear) {} 

   const string getDesc() const 
   {
      return license + " from " + stringify(year);
   } 

   const string &getLicense() const 
   {
      return license;
   } 

   const int getYear() const
   {
      return year;
   } 
}; 


It's a little longer, as we're spreading the functions over multiple lines rather than keeping them condensed. But, as mutexe said, the implementations would more than likely go in a cpp file, rather than in the header declarations.
Last edited on
Any time you see the class name followed by a ":" it is a constructor.

It is basically a function that initializes all of the classes variables when a new instance of that class is called.

For example:

1
2
3
4
5
6
7
8
9
10
class Example{
     int x;
     char c;
public: 
     void printHello(){cout<<"Hello!\n";}
    
    Example(//when there are no arguments it is a default constructor )
    :x(6),
    c('a'){printHello();}
};


In the above "Example" class every time a new instance of the class is created, by default the x value will be set to 6, the c value will be set to a, and the program will output "Hello!".

You can add additional constructors like this:

1
2
3
4
5
6
pretend we are still in class Example

Example(int a, char b)
: x(a),
c(b){}


now doing something like Example ex=Example(11,b);
would create a new instance of the Example class where x=11, c=b, and no function is called on creation.

hope this helps!
Last edited on
Any time you see the class name followed by a ":" it is a constructor.

I see what you're saying, but this isn't true. Consider:

1
2
3
4
5
6
class Vehicle : public SomeOtherClass
{ 

...
...
};


Is this anything to do with a constructor?
RadWayne, some of the code in your first example is incorrect.

Constructor should be:
 
Example() : x(6), c('a'){}

Not:
 
Example(): int( 6 ), char( 'a' ){}


Similar issue in your second example.
Last edited on
oops, I'll fix that. Thanks!
I think i understand most of it now. Thanks!
One more question, i would like to know why is & used for myLicense and getLicense() but not myYear? is it just because of preference of which to pass by reference or some technical requirement?
thank you
Last edited on
i wouldn't use pass by reference for getter methods.
Why so? and why is it used in these cases? i don't understand
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

means you don't make a copy of the variable when you pass by reference, which would happen if you pass by value. if a method takes a structure or large object it would be inefficient to pass by value.
It doesn't matter with primitive data type as they are so small.

to be honest i dont think i've ever seen initializing by reference, which looks like what's happening on line 6 with your constructor. i'd assume you'd need a reference variable declared but your liscense declaration on line 3 is not. i could be wrong though. i'm interested to get some opinions on this.
Last edited on
Topic archived. No new replies allowed.