Click here to Skip to main content
16,016,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am beginner to Visual C++. I am working on interfaces. I didn't got one thing..
Suppose
//Problem Details

interface Shape 
{
    virtual void Draw();
};

interface Rectangle : Shape 
{
    virtual void Draw();
};

class Maths : Rectangle
{
public:  
    void Draw() {
        //Code for Draw
    }
};

//This was my problem
-----------------------------------------------------------
//We can just write the above whole code as...

class Maths
{
public:
    void Draw() {
        //Code for Draw
    }
};

-------------------------------------------------------------

I have class Maths with its own Draw() method. If we can write the above code as mentioned above in second case, then what the need of interfaces.

After all the methods are finally to be defined in the class itself, then whats the need of interface...?
Posted
Updated 20-Jun-11 4:44am
v2

You need to understand a couple of concepts for this. One is a MSDN article found here: Programming against Interfaces[^] and reading up on polymorphism[^] would also help.

[Edit]
Just a small hint at what interfaces will help you with: The application using interfaces will not have to be aware of the concrete implementation of a class. This is especially true if dependency injection[^] is used to construct the application at runtime.
[/Edit]

Cheers!

--MRB
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 20-Jun-11 22:50pm    
I voted 5 by then noticed that I had to fix a type "implementation" and dependency injection URL. I felt it was kind of difficult to me to read that article, and later noticed that it was in German which I never learned. :-)
--SA
Stefan_Lang 21-Jun-11 6:13am    
Wait, you spent some difficult time reading, and only 'later' discovered it was german? Just about how much 'later' are you talking? ;-p
Manfred Rudolf Bihy 21-Jun-11 8:08am    
:)
Sorry for the bungle!
Sergey Alexandrovich Kryukov 22-Jun-11 17:48pm    
I don't remember how long; many words looked familiar but general meaning slips out; interesting feeling... :-)
--SA
Manfred Rudolf Bihy 21-Jun-11 8:09am    
Thanks for fixing it. How could that ever have happend (again)? :)
The concept and keyword of "interface" doesn't exist in pure C++. It's only native to Java and COM (and maybe a few others, I suspect anything that runs in the CLI would support this type of thing but I'm not sure). The MS definition is struct based (which is pretty much a bare class with no instantiation).

Read a little more about it:
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Interface_Class[^]
Using Interfaces in C++[^]
http://www.codeguru.com/forum/archive/index.php/t-241695.html[^]
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/06bf1dea-1d20-4ec3-b9a1-3d673d7fcd8d/[^]
 
Share this answer
 
v2
First of all, the keyword interface is Microsoft-specific, and only used in managed C++, so this is not part of the C++ standard.

Second, interface can't be used standalone, you have to write interface class or interface struct (which are equivalent).

Third, technically, you can entirely omit the interface keyword without changing the semantics of the code! It is just a crutch that MS invented to enforce certain properties in a class definition.

Lastly, to understand the meaning of defining interface classes, you really have to understand polymorphism and the basic concepts behind object-oriented programming. What you're asking is really the basis of object orientation, so you should try and search for a good book, online course, or tutorial on that topic before advancing. There are already a few useful links in solution 2, but they may already be too advanced for you, or too specific.

You'd better get started with the basis of object oriented programming, such as this tutorial.
 
Share this answer
 
If you Google it, you might find some ways to do it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900