473,471 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Oct 10 '05 #1
3 1318
an****@gmail.com wrote:
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();
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.
Car(int initRadioFreq, int horsepower); <
This is a constructor that will use the passed parameters to initialize the
object.
// ...
protected:
bool isRunning_;

};
#endif

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


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
Oct 10 '05 #2
<an****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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


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

Oct 10 '05 #3
<an****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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..
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 */
i dont understand , can anyone help


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
Oct 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
1
by: Carl Fenley | last post by:
I've been programming exclusively in C# for the last few years but am now working on a project where I am required to write all code in VB.NET. I'm trying to create a class with multiple...
7
by: gopal | last post by:
Can one constructor of a class call another constructor of the same class to initialize the this object? I read in the FAQ , to the above question the following ans was given Nope. Let's...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.