Connecting Tech Pros Worldwide Forums | Help | Site Map

constructor help

anddos@gmail.com
Guest
 
Posts: n/a
#1: Oct 11 '05
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


David White
Guest
 
Posts: n/a
#2: Oct 11 '05

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


Ali Çehreli
Guest
 
Posts: n/a
#3: Oct 11 '05

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

Mike Wahler
Guest
 
Posts: n/a
#4: Oct 11 '05

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