What is this called?

Mar 26, 2013 at 5:30pm
Which of the following allows change in behavior of member function of a class without changing its declaration
(A) Method overloading
B) Method overriding
C) Create new class
(D) none

According to me its option (B), but I am confused, can anyone clarify it

Thanks a lot in adavnce
Mar 26, 2013 at 5:46pm
Method overloading is having two or more methods with the same name, but differing signatures, ie different return values or parameters.

Method overriding on the other hand is having a method in a derived class have the same name and signature as a method in the super class. This allows derived classes to each have their own implementation of a given method, which could very well result in different behavior from each class. This is a super useful concept.
Mar 26, 2013 at 5:58pm
so I am right I think, answer is B only na?

Thanks for ur valuable time
Mar 26, 2013 at 6:09pm
Well, technically, it's not changing the behaviour of a member of a class. It's creating a new, derived class, with different functionality. It's just that if you use a pointer to the base class, that points to an object of the derived class, then when you call the method, it will execute the method on the derived class, not on the base class.

But you haven't actually changed the behaviour of the method on the original class.

It all depends on how pedantic the person setting the question wants to be.
Mar 26, 2013 at 6:20pm
so what's the final correct answer according to you
Mar 26, 2013 at 6:50pm
It is overloading. Basically what overloading a function does is that it lets you use the same function name and type, but be able to change what each one does depending on the parameters it was called with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class MyClass
{
  int number, grades;
  char **names;
  
public:
  MyClass(int number, int grades, char**names)
  {
    this->number = number;
    this->grades = grades;
    this->names = names;
  }
  
  MyClass()
  {
    number = 10;
    grades = 85;
    names = new char*[number];
  }
};

char *names[20] = {"David", "John", "Brandon", "Kristy"};
MyClass school(83, 90, names);
MyClass school2;
Mar 26, 2013 at 6:59pm
Smac89 wrote:
...without changing its declaration

Overloading is changing the declaration, sort of. The wording on the question is pretty terrible. But I definitely go with the overriding vote since that leaves the signature the same, but provides different behavior.
Mar 26, 2013 at 7:08pm
1
2
void f(); //declaration
void f(int); //declaration 
I am pretty sure it is not overloading and it is overriding, though it definitely requires some assumptions such as assuming that the method is inherited from a parent class.
Mar 27, 2013 at 3:34am
still I am confused GUYS , Overloading or Overriding????
Mar 27, 2013 at 3:59am
B. Overriding
Mar 27, 2013 at 4:27am
LB Thankss a lot
Topic archived. No new replies allowed.