ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have some more problems with a function that I made... it isn't working properly. Well anyway it's pretty obvious what it is supose to do so I won't mention it.
The problem is that it keeps returning the wrong answer . I will copy and paste it into a [/CODE] box so you's can try and run it in your own IDE and hopefully tell me what is wrong in it, thanks for your help.
Code:
#include <iostream.h>
#include <stdlib.h>
float areaOfTriangle(float, float);
int main()
{
float perpindicularHeight = 0.00f;
float baseOfT = 0.00f;
float areaOf_T_Answer = 0.00f;
cout<<"Please enter the base of the triangle: ";
cin>>baseOfT;
cout<<"Please enter the perpindicular height of the triangle: ";
cin>>perpindicularHeight;
cout<<"The answer is "<<areaOf_T_Answer<<" cm2";
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
areaOf_T_Answer = areaOfTriangle(perpindicularHeight, baseOfT);
return 0;
}
float areaOfTriangle(float perpindicularHeight, float baseOfT)
{
return (baseOfT * perpindicularHeight) / 2;
}
If you do it in the order as in your code, you'll get whatever value is stored in areaOf_T_Answer already (the one you've assigned). The lines are executed in order!
Sorry for double posting, I didn't want to start a new thread.
I have been trying to learn how to overload function and the point of overloading them. Here is a incomplete program I wrote, it contains overloaded functions. I would like to know how would I return the value when using the overloaded functions (that's why I left the function definition blank).
If you could fill it in or something and re-post it with a bit of explanation on to what you changed/added and why it would be a great help.
Thank You.
Code:
#include <iostream>
using namespace std;
int myOverloadedFunction(int, int);
float myOverloadedFunction(float, float);
double myOverloadedFunction(double, double);
int main()
{ //This is the interger section//
// This section has every thing to do with the integer part of my program
// each of the different variables have two for the multiplying action. //
int multInt = 0;
int multInt2 = 0;
int intAnswer = 0;
cout<<"Please enter a integer number to be multiplied: ";
cin>>multInt;
cout<<"\nPlease enter a second integer number to be multiplied by the first integer entered: ";
cin>>multInt2;
intAnswer = myOverloadedFunction(multInt, multInt2);
//This section contains the floating point part of my program.
float multFloat = 0;
float multFloat2 = 0;
float floatAnswer = 0;
cout<<"\nPlease enter a floating point number to be multiplied: ";
cin>>multFloat;
cout<<"\nPlease enter a second floating point number to be multiplied by the first\n entered: ";
cin>>multFloat2;
floatAnswer = myOverloadedFunction(multFloat, multFloat2);
//This section contains the double part of my program.
double doubleAnswer = 0;
double multDouble = 0;
double multDouble2 = 0;
cout<<"\nPlease enter a double number to be multiplied: ";
cin>>multDouble;
cout<<"Please enter a second double number to be multiplied: ";
cin>>multDouble2;
doubleAnswer = myOverloadedFunction(multDouble, multDouble2);
//This next part should display my the return result of my overloaded functions.
cout<<"Two integer numbers multiplied equal: "<<intAnswer;
cout<<"Two floating point numbers multiplied equal: "<<floatAnswer;
cout<<"Two double numbers multiplied equal: "<<doubleAnswer;
cin.get();
cin.get();
cin.get();
return 0;
}
Last edited by InvisibleSniper; 08-18-2005 at 07:44 AM.
You just write the implementations as you would for a function that isn't overloaded. The compiler knows which one to call based on the parameter list. So yeah, just do something like this
Code:
int main()
{
// Code that calls your overloaded functions
}
int myOverloadedFunction(int x, int y)
{
return x*y;
}
float myOverloadedFunction(float x, float y)
{
return x*y;
}
double myOverloadedFunction(double x, double y)
{
return x*y;
}
Once again sorry for double posting but I didn't want to open a new thread.
Anyway the reason for this post is because I would like to know what does the authour of "SAMS Teach Yourself C++ in 24 Hours" mean when he says:
Code:
long myFunction (int x = 50);
This prototype says "myFunction() returns a long and takes an integer parameter. If an argument is not supplied, use the default value of 50."
I tried to use the defualt parameters in a program but it didn't work. Can you tell me how I would use them, because I don't have a clue. If you could just write a small noobish sample of default parameters in use I will then understand it, thanks.
I use these alot for constructors, when I want to force a constructor to take an argument, even if the default constructor is to be called. I believe creating a class like this will force the constructor we specify to be the default constructor which is important at times.
I just changed it a bit and I'm wondering why it goes back to 50 right after you initialized it to 129. Any way here is what I mean:
Code:
/* simple.cpp */
#include <iostream>
using std::cout;
void PrintSize(int size = 50)
{
cout << size << '\n';
cin.get();
}
int main()
{
PrintSize(); /* prints 50 because of default param */
PrintSize(129); /* prints 129 */
PrintSize(); /*Goes back to 50 on this line... how come?*/
PrintSize(0); /* prints 0 */
cin.get();
return 0;
}
Your third line in main() is taking no arguments, so the default parameter is used as in the first line. You aren't initialising anything in the second line, just passing the value 129 to that function. The next line is simply another call to the function, where you're using the default parameter.
If pass the value 129 to the function, the function prints out 129. Your function header is
Code:
void PrintSize(int size = 50)
which says that if you don't pass a value, to set size = 50. You can't use that 129 after the function has finished executing, because size has gone out of scope. You understand this:
Code:
PrintSize(); // Prints the no. 50
PrintSize(129); // Prints the no. 129
If you then do
Code:
PrintSize(129);
PrintSize();
that will print 129 and then 50. It's just the previous lines of code in reverse order. Each call you make to your function has nothing to do with the last one.
Nylex is right. Here is one more example. This function takes a C++ string as a parameter. You can pass it any string you like and the function will print the string to the console. However if you don't pass a string as an argument, the default parameter declared in the function header will be printed instead.
Code:
#include <iostream>
#include <string>
using namespace std;
void PrintString(string parameter = "I am the default parameter")
{
cout << parameter << '\n';
}
int main()
{
PrintString("This string will be printed to the screen");
PrintString();
PrintString("foo bar");
}
Running that code should print:
This string will be printed to the screen
I am the defalt parameter
foo bar
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.