-
January 13th, 2017, 02:33 AM
#1
please any solve this 1.#QNAN error in the output
Code:
#include<iostream>
using namespace std;
class Planet
{
protected:
string Name;
double Radius;
double Distance;
public:
Planet(string _name, double _radius, double _distance)
{
Name = _name;
Radius = _radius;
Distance = _distance;
}
void Print()
{
cout << "Name of Planet is = " << Name << endl;
cout << "Radius of Planet is " << Radius << endl;
cout << "Distance of Planet from SUN in AU is " << Distance << endl;
}
double Cal_Distance()
{
cout << "Planet's Calculate Distance Function is Called"<< endl;
return( Distance * 15000000 );
}
};
class Inner_Planet : Planet
{
public:
Inner_Planet(string _name, double _radius, double _distance)
: Planet(_name, _radius, _distance)
{
}
void Print()
{
Planet::Print();
}
double Cal_Distance()
{
cout << "Inner Planet's Calculate Distance Function is Called"<< endl;
Planet::Cal_Distance();
}
};
class Mercury : Inner_Planet
{
public:
Mercury(string _name, double _radius, double _distance)
: Inner_Planet(_name, _radius, _distance)
{
}
void Print()
{
Inner_Planet::Print();
}
double Cal_Distance()
{
cout << "Mercury's Calculate Distance Function is Called"<< endl;
Inner_Planet::Cal_Distance();
}
};
int main()
{
double result;
Mercury m("Mercury", 2440, 0.38);
cout << endl;
cout << "********** Values of Data Members *********" << endl;
m.Print();
cout << endl;
cout << endl;
cout << "-================================" << endl<< endl<< endl;
cout << "******** Calculating Distance of Mercury from SUN in Kilometers ***********" << endl;
result = m.Cal_Distance();
cout << "Distance of Mercury From Sun in Kilometers is " << result << endl << endl;
system("pause");
}
Last edited by 2kaud; January 13th, 2017 at 04:20 AM.
Reason: Code tags added
-
January 13th, 2017, 04:29 AM
#2
Re: please any solve this 1.#QNAN error in the output
in the derived classes, Cal_distance() needs to return a value. So
Code:
double Cal_Distance()
{
cout << "Inner Planet's Calculate Distance Function is Called"<< endl;
return Planet::Cal_Distance();
}
...
double Cal_Distance()
{
cout << "Mercury's Calculate Distance Function is Called"<< endl;
return Inner_Planet::Cal_Distance();
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 13th, 2017, 04:50 AM
#3
Re: please any solve this 1.#QNAN error in the output
Originally Posted by 2kaud
in the derived classes, Cal_distance() needs to return a value. So
Code:
double Cal_Distance()
{
cout << "Inner Planet's Calculate Distance Function is Called"<< endl;
return Planet::Cal_Distance();
}
...
double Cal_Distance()
{
cout << "Mercury's Calculate Distance Function is Called"<< endl;
return Inner_Planet::Cal_Distance();
}
Thnx for recitify my code
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|