How can we create different functionality to objects of a same class?

Questions by ykirankumarmca

Showing Answers 1 - 6 of 6 Answers

kalayama

  • Dec 13th, 2006
 

What is the necessity for doing it? If you want an object to have all the featrures of a class + something more or something less, all you need to do is create another class inheriting the other. To incorporate this we have inheritance, ain't it?I don't think you can incorporate any further functionality to an object apart from the ones defined in the class.

J.C

  • Dec 15th, 2006
 

overload the functions

  Was this answer useful?  Yes

Shahzad Hassan

  • Nov 1st, 2007
 

Functionality of an object can be changed by changing the property or attribute value

  Was this answer useful?  Yes

Manohar

  • Nov 17th, 2007
 

By using the concept of virtual functions, if you consider a class as an abstract class, we can override all the methods in its derived class, hence each of the objects of derived objects can have seperate functionalities.

tehoclub

  • Oct 31st, 2008
 

You can use a callback function as below,

typedef void (*DoFunc)();

void Draw();
void Paint();

class MyClass { 
void DoSomething(DoFunc fa) { fa(); } 
}

MyClass a, b;
a.DoSomething(Draw);
b.DoSomething(Paint);



  Was this answer useful?  Yes

yzesong

  • Aug 8th, 2009
 

We may forget, the question is really talking about objects of same class. So I think all the answer on virtual functions are wrong. tehoclub may get close to the point of the question in my opinion.

  Was this answer useful?  Yes

Victor F

  • Apr 26th, 2015
 

By using reflection.

  Was this answer useful?  Yes

Nikhil Singhal

  • May 8th, 2015
 

By overloading the constructor.

Asad

  • Jun 7th, 2015
 

Using State Pattern

  Was this answer useful?  Yes

Code Guru

  • Dec 29th, 2015
 

By creating derived classes that implement the specific behaviour required.

  Was this answer useful?  Yes

joseph

  • Aug 27th, 2016
 

Can also be done using constructor overloading concept (by sending in different number of parameters)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions