Connecting Tech Pros Worldwide Help | Site Map

constructor help

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 10th, 2005, 11:15 PM
anddos@gmail.com
Guest
 
Posts: n/a
Default constructor help

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 10th, 2005, 11:55 PM
David White
Guest
 
Posts: n/a
Default 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 10th, 2005, 11:55 PM
Ali Çehreli
Guest
 
Posts: n/a
Default 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 10th, 2005, 11:55 PM
Mike Wahler
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.