|
hrishiS wrote: But there is always a small difference between the abstract class and interface
No, there isn't - not if the abstract class has a) no data, and b) declares all of it's methods to be public and abstract.
hrishiS wrote: Anyway, when we talk about interface also, when do we need/use them exactly
When you want to define some API you want a class to conform to, but don't want to constrain the implementation beyond that. For example - look at COM - that's ALL interfaces. For a more practical example, let's look at iterating through some collection of objects:
template<class T>
class IIterable
{
virtual void First() = 0;
virtual bool Next() = 0;
virtual const T& Current() const = 0;
};
This tells you what methods have to be implemented, and should also define the semantics of the methods, but does not constrain the implementation...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
thanks a lot.
Even though I have no knowledge on COM,.I got the idea of it.
Please correct me if my idea is wrong...following is the abstract of what i understood...
- we need interface, in order to make an generic structure of so many class. Later when some one adds one more class, it forces the implementer of the class to follow some generic rules (must have those virtual methods)....
am I Correct?
-----------------------------
I am a beginner
|
|
|
|
|
hrishiS wrote: - we need interface, in order to make an generic structure of so many class. Later when some one adds one more class, it forces the implementer of the class to follow some generic rules (must have those virtual methods)....
Not quite. The interface is used to enforce a contract between code that uses the interface and classes that derive from (and thus have to implement the interface). Using the example I gave before, we could define a Find function like this:
template<class T>
bool Find(IIterable<T> const& i, T const& lookFor)
{
i.First();
do
{
if (i.Current() == lookFor) return true;
} while (i.Next());
return false;
}
The IIterable interface enforces the 'Iterable' contract between Find and any class that implements Iterable.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
One more thing is that,
You have given the example for interface. But when do we need abstract class, ie which has some methods which are implemented in the base class .
-----------------------------
I am a beginner
|
|
|
|
|
When you want to offer a nearly complete implementation, but allowing some customisation by children of the class.
For example:
class DirectoryTreeWalker
{
DirectoryTreeWalker(std::string& path) { … }
void WalkIt() {
private:
virtual void FoundAFile(const std::string& file) = 0;
virtual void FoundADirectory(const std::string& file) = 0;
};
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
oh i got it...thanks a lot Sir for all your words
-----------------------------
I am a beginner
|
|
|
|
|
hrishiS wrote: But there is always a small difference between the abstract class and interface.
An interface is a concept that is taken from other programing languages that natively supports interfaces: an interface simply describes the public method of an object, but doesn't have any functionality. This object should then be implemented by a class which inherits from the interface.
So, it means that the interface only contains pure virtual functions (and no data member).
An abstract class on the other hand is a class that cannot be instanciated directly, so you need to inherit from it. To make a class abstract, it needs to contain at least one pure virtual function (but it can contain other functions and also data members).
hrishiS wrote: Anyway, when we talk about interface also, when do we need/use them exactly?Could you please give me a practical example.
If you really want to implement the concept of an interface, then you need to use pure virtual functions for every function of the interface, because the interface cannot contain any funcitonality. All the functions from the interface are pure virtual functions.
|
|
|
|
|
Hey ,
Its just that when you want extendibilty of a class then u go to write pure virtual functions fr it.. This class can now be base class of all child clss who will just inherit it to implement the functions defined in there own way ... so its there to make a generic approach
|
|
|
|
|
You are talking about a standard virtual function, not a pure virtual function, which is not exaclty the same...
|
|
|
|
|
thanks Tiwari for your reply,
I need little more clarification, if you dont mind.
See, we can achieve the extensibility through a normal inheritance also right . The main reason of inheritance is reusable and extensibility of my base class. Thats what my understanding is. So in this case why we need some function to be declared (make = 0) in base class and again define it in derived class etc etc....we can directly declare and define in the derived class right..
May be one practical example would be appreciated. Or may be a scenario where we cannot go without it.
Thanks
-----------------------------
I am a beginner
|
|
|
|
|
It is used to define an interface that does not have any implementation.
Look at some design patterns - Design Patterns in C#[^].
They all use interfaces.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
In addition to previous replies, the usage of pure virtual functions is needed:
- when your base class should not be instanciated: if a class contains a pure virtual function, you cannot instanciate it.
- when you want to enforce child classes to override the virtual function: if a child class doesn't override the pure virtual function, it cannot be instanciated.
In that specific case, it makes a lot of sense to use pure virtual functions: if you later add some child classes to the base class, the compiler will make sure you provide the required functions.
|
|
|
|
|
thanks again,
but i guess you are talking about the points/feature or what it is.
May be it will be great if you could give an example where we cannot go without it.
-----------------------------
I am a beginner
|
|
|
|
|
Suppose you are creating a class to log messages. The class first formats the message properly (adding current time for instance). This class can then be implemented by different children depending on where you want to log the message: to a file, to the console, ...
So, the base class looks like:
class CLogger
{
public:
void LogMessage(const string& message)
{
string formattedMessage = FormatMessage(message);
OutputMessage(formattedMessage);
}
protected:
string FormatMessage(const string& message) { }
virtual void OutputMessage(const string& message) = 0;
};
The class cannot be instanciated, because it contains a pure virtual function (OutpuMessage). If you want to create children of this class, you only have to override the OutputMessage function (the other functions are already provided by the base class):
class CConsoleLogger : public CLogger
{
protected:
void OutputMessage(const string& message) { }
};
|
|
|
|
|
#include<iostream>
class IDumpable
{
public :
virtual void dump()=0;
};
class A: public IDumpable
{
int _a;
public:
A(int a):_a(a){}
virtual void dump(){ std::cout << _a << std::endl;}
};
class B: public IDumpable
{
static int _counter;
double _d;
int _id;
public:
B(double d):_d(d){_id=B::_counter++;}
virtual void dump(){ std::cout << "(" << _id << ") " << _d << std::endl;}
};
int B::_counter=0;
void dump( IDumpable * pid[], int size)
{
int i;
for (i=0; i<size; i++)
{
pid[i]->dump();
}
}
void main()
{
IDumpable *pid[3];
pid[0] = new A(10);
pid[1] = new B(5.7);
pid[2] = new B(10.745);
dump(pid, sizeof(pid)/sizeof(pid[0]));
}
Here, the function dump can iterate on the passed array without bothering about the exact type of the array elements.
Note that, once the IDumpable interface is written, we can start coding its consumers (for instance the dump function) even if the actual implementations of the interface are not yet provided.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
For example, if you have a Shape base class in a drawing program with a Draw method the Draw method will be pure virtual - each Shape will need to draw itself but there is no appropriate default behaviour.
|
|
|
|
|
thanks
one of my friend gave me the same example ..could u please give the class diagram, what u meant by exactly....and from the main function how do we call to draw different shape(say triangle or cube or rectangle)....just teh skeleton of the classes and calling methodology if u dont mind
-----------------------------
I am a beginner
|
|
|
|
|
See the message above from CPallini
|
|
|
|
|
Have a look at this extremely well authored article:
Generic Picker Dropdown Control[^]
You see a complex base class, with some virtual methods. To make different kinds of drop downs, You just have to implement some simple virtual methods, and the base class will do the heavy lifting.
It's similar to the Shapes examples that float about, but it's an actual implementation.
Give it a go, make your own picker class in the demo project.
Iain.
I have now moved to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need cotract work done, give me a job! http://cv.imcsoft.co.uk/[ ^]
|
|
|
|
|
I want to distinguish between a Shutdown event and a Restart event , what should be done in achieving the same ? As , WM_QUERYENDSESSION doesnot give me any differentiation between Shutdown and Restart. Is there a way to know specifically which one of the two has been initiated ??
Regards,
Kushagra
I hate coding but I luv to develop.
|
|
|
|
|
According to the docs (WM_QUERYENDSESSION Message[^]) it is not possible.
Read the explanation of lParam .
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Actually I have seen many commercial applications that some how internally know about it ...that is why the question arises ... and If they know it ..we should also be able to device the same
|
|
|
|
|
Somebodu please help me out in this
Kushagra
|
|
|
|
|
Kushagra Tiwari wrote: and If they know it ..we should also be able to device the same
Perhaps they are using a device driver of some sort (ie, ring 1 or 2).
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
No, it's possible : everything is possible...
For undocumented stuff, see Adv. Win32 grp (news://nntp.aioe.org/comp.os.ms-windows.programmer.win32)
(this had been posted in ~1994...)
|
|
|
|