|
Hi there
Below is the code i have written, is there something wrong
with my derived class? i am getting a compiler error :No default Constructor available for the Animal class.
Or is it the base class ,
<br />
class Animal<br />
{<br />
public:<br />
Animal(const string &name, const char &sex, const string &Colour, int age);<br />
~Animal();<br />
<br />
int feed_animal(const int &);<br />
<br />
void display_Info() const;<br />
<br />
private:<br />
string aName;<br />
char aSex;<br />
string aColour;<br />
int aAge;<br />
};<br />
<br />
class Crocodile : public Animal<br />
{<br />
public:<br />
Crocodile(int jaw_size, const string &move_ment);<br />
~Crocodile();<br />
<br />
private:<br />
int jaw;<br />
string move;<br />
<br />
};<br />
<br />
Animal::Animal(const string &name,const char &sex, const string &Colour, int Age)<br />
:aName(name), aSex(sex), aColour(Colour), aAge(age)<br />
{<br />
}<br />
<br />
Animal::~Animal()<br />
{<br />
}<br />
<br />
int Animal::feed_animal(const int &food)<br />
{<br />
return food;<br />
}<br />
<br />
void Animal::display_Info() const<br />
{<br />
cout << "Name : " << aName << endl;<br />
cout << "Sex : " << aSex << endl;<br />
cout << "Colour : " << aColour << endl;<br />
cout << "Age : " << aAge << endl;<br />
}<br />
<br />
<br />
int main(int argc, char *argv[])<br />
{<br />
Animal Crocko("American Aligator", 'M', "Dark Grey", 2);<br />
Crocko.feed_animal(25);
Crocko.display_animal();<br />
<br />
<br />
return 0;<br />
}<br />
thanks in advance
Bhangie
Education begins a gentleman,
conversation completes him
|
|
|
|
|
In C++, a derived class's constructor calls the base class constructor first before proceeding. In your Crocodile class, you have a constructor that takes two arguments. You haven't called the base class constructor explicitly, so the compiler puts a calls to the base class' default constructor. Because there is no default constructor, the code fails to compile.
You can make it by compile by adding a default constructor to Animal , like
class Animal
{
public:
Animal() {}
};
Or you can propagate the call from the derived class to the base class with something like
class Crocodile : public Animal
{
public:
Crocodile(const string &name, const char &sex, const string &Colour, int age,int jaw_size, const string &move_ment) : Animal(name, sex, Colour, age)
{
}
};
By the way, I don't think deriving Crocodile from Animal is a good idea. A crocodile is an instance of animal. It's like inheriting Toyota Corolla and Mercedes Benz from Car.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
S. Senthil Kumar wrote:
It's like inheriting Toyota Corolla and Mercedes Benz from Car.
What about Roll Royce!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
S. Senthil Kumar wrote:
It's like inheriting Toyota Corolla and Mercedes Benz from Car.
What is wrong with that? They are both cars.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
How do you then distinguish between instances and derived classes? My idea is that a class is a "template" and instances of the class are the "live" entities. Going by that logic, a specific car (say a Benz) is an instance of type car. I'd consider AutomaticTransmissionCar and ManualTransmissionCar to be subclasses of Car, for example.
What do you say?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Consider the following hierarchy:
-----------Vehicle -----------
/ | \
Car Boat Aircraft
/ \ / \ / \
Truck Van Sailboat Yacht Helicopter Blimp Note that Yacht is derived from Boat , which is derived from Vehicle , but there are no instances of any of the classes until one is defined. You define an instance like:
Helicopter whirlybird; Here whirlybird is an instance of the Helicopter class.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Yeah, but Yacht is not related to Boat in the same way as Benz is to Car. You can have several models of Yachts which have exactly the same properties but different values for them. But a Yacht is a specific kind of Boat with other properties.
Likewise, a Benz and a Corolla have the properties of a car but have different values.
DavidCrow wrote:
Here whirlybird is an instance of the Helicopter class.
I thought that's what I said too (I'm assuming whirlybird is a helicopter model).
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
"Derived from" and "instance of" are mutually exclusive terms. By definition, an instance of class is the actual occurrence of that class.
In your example, Benz is not derived from anything. It is an instance of the Car class.
S. Senthil Kumar wrote:
I thought that's what I said too
You said something about an instance being a live entity, whatever that is.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
You said "Sure it is. A Yacht is a Boat, and a Benz is a Car, and they are all Vehicles. However, until one of those objects actually exist, there are no instances".
Going by that logic, why not derive whirlybird from Helicopter? whirlybird is a helicopter, after all. Then DavidCrow, SenthilKumar, instead of being instances of Humans, would be subclasses of Humans...
DavidCrow wrote:
By definition, an instance of class is the actual occurrence of that class.
I agree and that's why I don't think Benz should be derived from Car.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
bhangie wrote:
Below is the code i have written, is there something wrong
Provide A Default Constructor to your class, like this
class Animal
{
public:
Animal();
Animal(const string &name, const char &sex, const string &Colour, int age);
~Animal();
---- SNip----------
}
Animal::Animal()
{
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Thank's for the help oaks.
Bhangie
Education begins a gentleman,
conversation completes him
|
|
|
|
|
Hi every one!
I m trying to build a project in Visual C++ using Microsoft Visual C++ 6.0 compiler. when i built a project file, this error occurred:
"This program cannot be run in DOS mode.
Error executing c:\windows\system32\cmd.exe"
some one kindly tell me y this error occurred & wts the remedy to it?
Regards
Usman Arif
Usman Arif
|
|
|
|
|
Hi,
Could someone advise me how to spy at a com port when
another existing running application is using the same com port for serial communication? Preferably with sample coding.
My intention is to build a serial port monitor
Because, it would complain error...com port in use, then my spy program will exit.
I use CreateFile Function.
thanks, Sally
|
|
|
|
|
|
Thanks, Joel.
But I have to write my own program, though I know there are already commercial products available. This is one of my work assignments.
Sally
|
|
|
|
|
hey boss,
u got to write a upper filter serial driver above the existing functional driver. refer to filter drivers in DDK.
Regards,
Adi
|
|
|
|
|
Hi guys
Im working on a program that factorises a large number into two primes. I am experiencing some problems. Im not going to explain all my code here, but in short C++ makes wrong calculations. I think it has something to do with the capacity of the variables (int, unsigned long etc). For example, it tells me that 131221*229123 = 978111.
Why is this? Ive been experiencing with various data types, but I still get these "wrong" calculations. Can anyone tell me what I should do to make this work? Remember I am working with large numbers (up to six digits).
I really appreciate the help.
|
|
|
|
|
The max value for an int = ~2.15 billion
131221*229123 = ~30 billion
Try using an __int64 variable:
Date Type Ranges
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
as Jack Squirrel said, you can do this in the calculator of your choice :
2^32 -> 4294967296 (maz value of an unsigned long)
131221*229123 -> 30065749183 (out of range...)
if you like to use an integer absolutely, use __int64. otherwize, double or long double are good enough...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
131221*229123 = 30065749183 but you get 978111.
Basically your number is truncated. Look at the hex representation.
30065749183 = 0x7000EECBF
978111 = 0x0000EECBF
Use __int64.
|
|
|
|
|
Does anyone have any suggestion on what software I should purchase to start programming in C. I am a beginner. Found that I love to write code and create things. any suggestions would be helpful. Thanks
|
|
|
|
|
Are you hoping to use C, C++ or C# ? C is a bit outdated these days, and would certainly have a steep learning curve. The C++ curve is not insurmountable, but C# would be easier. There are free options for all of these languages, but if you can get your hands on a copy of Visual Studio, for example if you're a student and can buy one at the educational price, it would be well worth your while. The help a good IDE gives you makes learning a whole lot easier. I shudder when I think of the terrible IDE's I used when I was learning, because I got them cheap ( not to imply that Microsoft are the only people to do a good IDE, but VS is excellent, the price is right if you can get education pricing, and it means you have a number of languages at your disposal out of the box ).
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hello,
You can download VS 2005 beta. It's free, and a very good tool for programming in C, C++ and C# (if you want to program in these languages).
I also got the blogging virus..[^]
|
|
|
|
|
it looks like C# is the way to go by both replies. I'll have to check on prices. I'm hoping it won't be to bad.
Thank you for the help.
|
|
|
|
|
Any body knows how to find out if the harddrive is fragmented via C++ or WMI script?
thanks in advanced.
|
|
|
|