Connecting Tech Pros Worldwide Help | Site Map

constructor help

  #1  
Old October 11th, 2005, 12:15 AM
anddos@gmail.com
Guest
 
Posts: n/a
ive got this sample from a book i am learning from c++ , and need abit
of help on constructor

#ifndef CAR_HPP
#define CAR_HPP

class Car {
public:
Car();
Car(int initRadioFreq, int horsepower); <
// ...
protected:
bool isRunning_;

};
#endif

to me this constructor looks like the way you call a function in main..
i dont understand , can anyone help

  #2  
Old October 11th, 2005, 12:55 AM
David White
Guest
 
Posts: n/a

re: constructor help


anddos@gmail.com wrote:[color=blue]
> ive got this sample from a book i am learning from c++ , and need abit
> of help on constructor
>
> #ifndef CAR_HPP
> #define CAR_HPP
>
> class Car {
> public:
> Car();[/color]

This is a default constructor, meaning that no parameters need to be
supplied to it. Usually such a constructor will initialize data members with
default values.
[color=blue]
> Car(int initRadioFreq, int horsepower); <[/color]

This is a constructor that will use the passed parameters to initialize the
object.
[color=blue]
> // ...
> protected:
> bool isRunning_;
>
> };
> #endif
>
> to me this constructor looks like the way you call a function in
> main.. i dont understand , can anyone help[/color]

You can use either constructor to create an object, e.g.,
Car car1; // uses default constructor;
Car car2(1500, 10000); // uses other constructor

A class can have any number of constructors that have different numbers and
types of parameters. The creator of the object chooses the appropriate one
for his purpose.

DW


  #3  
Old October 11th, 2005, 12:55 AM
Ali Çehreli
Guest
 
Posts: n/a

re: constructor help


<anddos@gmail.com> wrote in message
news:1128985458.858786.213980@g47g2000cwa.googlegr oups.com...[color=blue]
> ive got this sample from a book i am learning from c++ , and need abit
> of help on constructor
>
> #ifndef CAR_HPP
> #define CAR_HPP
>
> class Car {
> public:
> Car();
> Car(int initRadioFreq, int horsepower); <
> // ...
> protected:
> bool isRunning_;
>
> };
> #endif
>
> to me this constructor looks like the way you call a function in main..
> i dont understand , can anyone help[/color]

Yes, it looks like a function call; but cannot be called like a function.
You can construct an object using that constructor like this:

Car my_car(4000, 200);

Or, you can construct an anonymous Car object as if you are calling a
function:

// A function that takes a Car
void drive(Car const &)
{
/* ... */
}

/* ... */

// Construct a temporary nameless Car and drive it:
drive(Car(5000, 150));

Ali

  #4  
Old October 11th, 2005, 12:55 AM
Mike Wahler
Guest
 
Posts: n/a

re: constructor help


<anddos@gmail.com> wrote in message
news:1128985458.858786.213980@g47g2000cwa.googlegr oups.com...[color=blue]
> ive got this sample from a book i am learning from c++ , and need abit
> of help on constructor
>
> #ifndef CAR_HPP
> #define CAR_HPP
>
> class Car {
> public:
> Car();
> Car(int initRadioFreq, int horsepower); <
> // ...
> protected:
> bool isRunning_;
>
> };
> #endif
>
> to me this constructor looks like the way you call a function in main..[/color]

Which constructor? There are two declared (but not defined)
in your example. Why does anything there look to you like
the way a function is called? Your example contains no
function calls at all. Well, the first constructor (the
default one) declaration might look like a function call because
constructors are 'special' in that they don't have return
types (or names, btw). The way you know it's not a function
call is because it's inside a class (or struct) definition,
and not inside any function.

Once the constructors are defined, they are (indirectly)
called when you define a type 'Car' object, e.g.:


Car c1; /* default-constructs a 'Car' object */

Car c2(1000, 200); /* constructs a 'Car' object,
using supplied arguments */


[color=blue]
> i dont understand , can anyone help[/color]

You can't 'understand' by simply looking at the examples
in a book. You need to also read (and likely re-read and
think about) the accompanying text. BTW which book are
you reading?

-Mike


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Constructor help. Please help. izzyk answers 3 October 15th, 2008 04:45 AM
pdf to hhp for boa-constructor help Eric_Dexter@msn.com answers 0 September 3rd, 2006 05:05 PM
Constructor help Ook answers 6 October 4th, 2005 09:55 PM
STL & Copy constructor..help Naren answers 3 July 19th, 2005 05:52 PM