5 Replies - 1176 Views - Last Post: 24 November 2008 - 02:15 AM Rate Topic: -----

#1 daddymac1213   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Overloaded functions

Post icon  Posted 23 November 2008 - 06:05 PM

// Adams, Christopher
// Lab4 exercise2
// This program creates two triangles based on user input. The first triangle is displayed and the second triangle is the inverse of the first triangle.

#include "stdafx.h"
#include <iostream>
using namespace std;


void printTriangle (int);
void printTriangle2 (int);
int base = 0;

int main(void)
{
	cout << "Enter the size of your base: ";
	
	cin >> base;
	
	cout << endl;
	printTriangle (base);
	printTriangle2 (base);
}


void printTriangle (int prntTri)
{
	for(int i = 0; i < prntTri; i++)
{
	 for(int j = 0; j <= i; j++)
 {
	  cout << "*";
   }
   cout<<endl;
   
}
}

void printTriangle2 (int prntTri)
{
	for(int i = 0; i < prntTri; i++)
{
	 for(int j = prntTri; j > i; j--)
	 {
	  cout << "*";
   }
   cout<<endl;
   
}
}





I am supposed to write a program that uses functions to display a triangle based on the user's input and the inverse of that same triangle. I got this program to do just that, but the second function is supposed to be an overloaded function of the first function. I'm not exactly sure how overloaded functions work, can anybody help?

Is This A Good Question/Topic? 0
  • +

Replies To: Overloaded functions

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Overloaded functions

Posted 23 November 2008 - 06:08 PM

Overloaded functions have the same name, but take different paramters (either in type or number or both).

Example:

void printTriangle(int);
void printTriangle(int, int);
void printTriangle(double); //etc... program will select function based on context



However, both of your functions take the same number and type of parameters so it will need to be changed.
Was This Post Helpful? 0
  • +
  • -

#3 daddymac1213   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: Overloaded functions

Posted 23 November 2008 - 06:12 PM

I'm stiil not sure if I understand what you're getting at. I can't change the type for the second triangle to double instead of int. like so?
#include "stdafx.h"
#include <iostream>
using namespace std;


void printTriangle (int);
void printTriangle (double);
int base = 0;

int main(void)
{
	cout << "Enter the size of your base: ";
	
	cin >> base;
	
	cout << endl;
	printTriangle (base);
	printTriangle (base);
}


void printTriangle (int prntTri)
{
	for(int i = 0; i < prntTri; i++)
{
	 for(int j = 0; j <= i; j++)
 {
	  cout << "*";
   }
   cout<<endl;
   
}
}

void printTriangle (double prntTri)
{
	for (double i = 0; i < prntTri; i++)
{
	 for(double j = prntTri; j > i; j--)
	 {
	  cout << "+";
   }
   cout<<endl;
   
}
}


This post has been edited by daddymac1213: 23 November 2008 - 06:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Overloaded functions

Posted 23 November 2008 - 08:23 PM

Well you have to change other things also. Your input variable is still an integer and the second overloaded function would never be called. Especially since you are only printing a shape based on an integer, I don't see why operator overloading is necessary at all. Even as an academic constraint, it makes no sense for this assignment.
Was This Post Helpful? 0
  • +
  • -

#5 daddymac1213   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: Overloaded functions

Posted 23 November 2008 - 08:50 PM

I know but that was part of the instructions included in the assignment. I found another way to display the same output, just not by overloading the first function. Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#6 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Overloaded functions

Posted 24 November 2008 - 02:15 AM

hmm,
you can merge the two functions into one to be ...


void PrintTriangle (int Length,bool Direction){
if(Direction){
for(...){ "print triangle to UP" }
else
for(...){ "print triangle to DOWN" }
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1