Overloading a function

I need to overload a function and I'm not sure how to do it. I started it and I'm not sure where to go from there. Can someone explain to me, in basic, dumbed-down terms what would work best for me to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<string.h>
using namespace std;

struct box
{
   char name[20];
   box *link;
};

class linkList {
  box *head;

public:

  linkList() {
    head=0;
    length=0;
  }
 //delete first item in the list
  void delete() {
   if (head=0) {
     box = *p;
     p = head;
     head = head >link; // [*head.link]
     del p;
  }
}
  void insert(char *p int pos) {
  if [pos >= | pos <=length +1);

}

  void insert(char *p) {
    box *pb = new box;
    strcpy( (*pb).name, p);
    (*pb).link = head;
    head=pb;
    length++;
  }

  bool empty() {
    return head==0;
  }

  void print() {
    box *curr=head;
    while (curr!=0) {
      cout<< (*curr).name <<endl;
  curr = (*curr).link;
    }
  }
};


int main() {
  linkList ll;
  cout<<ll.empty()<<endl;
  //make the list
ored is "<<fname<<endl;
     ll.insert(fname);
 



This is where my overload starts

1
2
  void insert(char *p int pos) {
  if [pos >= | pos <=length +1);


I just don't know what to tell it to do after that. Any help is appreciated.
Well, all overloading is is when you have two functions with the same name but given different parameters

looks like you almost had it, you need to add a comma though between each parameter as demonstrated below

1
2
3
4
5
6
7
8
9
10
11
12
13
void insert(char *p, int pos) {    //added a comma on this line between the parameters
  if [pos >= | pos <=length +1);

}

  void insert(char *p) {
    box *pb = new box;
    strcpy( (*pb).name, p);
    (*pb).link = head;
    head=pb;
    length++;
  }
Different parameter how? Like giving it two different things to do? And I know I start on the one function but it's not finished, I need more to it, I just don't know what else to do with it. Early C++ was easier...this stuff I've never done with this * and all.
Like erock said, it's just two functions with the same time. By just doing the following, we are declaring overloaded functions:

1
2
void insert(char *p);
void insert(char *p, int pos);


We can specify which function to call by providing different parameters:
1
2
ll.insert(fname); // This would call void insert(char *p);
ll.insert(fname,1); // This would call void insert(char *p, int pos); 


Is your questions more about the content of that second function? We could help you with that but we'd need to know exactly what it's supposed to do.

Well, overloading a function does not necessarily mean giving it two different things to do, but typically each function will do something different since there are more parameters. Literally, overloading is something that most OOP languages allow you to do. It is so you can have two different functions with the same name but yet perform different tasks depending on which one is called. If you call insert(&char); then this means you will call the first "version" of insert and it work perform the tasks associated with such function. But if you want to call the other insert function you will need to provide two arguments since this is the requirement for this function to be called likes so insert(&charOne, &firstPos).


When you have something like int *something it means you are declaring a pointer. You can look in the documentation section of this site and get the nit and gritty on pointers, but basically they are like variables except they hold the address of the variable.

You may have noticed above when doing examples of those functions calls I used "&" in front of the what are assumed to be variables. That is because you want to pass the address to the pointers since the parameters for the function are declared as pointers. The "&" allows to reveal the address of any variable.

Hope this helped. It is hard to explain when one is not familiar with your current experience.
closed account (Dy7SLyTq)
Literally, overloading is something that most OOP languages allow you to do.
not just oop. a lot of non oop do that too
I wanted to give the benefit of the doubt =).
I just don't know what else to do with it.


Judging by the two argument lists
1
2
void insert(char *p);
void insert(char *p, int pos); 

and the code you have for the single argument version, I'd guess that you're supposed to use the second argument to determine where to insert a new name in the list.

To do that, you're going to want to walk the list until you've passed <pos> entries, then insert the new entry at that point. You will need to handle a few special cases:
1) What if the list is empty?
2) What if <pos> is negative?
2) What if <pos> exceeds the number of entries in the list?



Topic archived. No new replies allowed.