Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class Interface 1

Status
Not open for further replies.

Akusei

Programmer
Jul 15, 2003
90
US
how do I use __interface? In C# if I declare an "interface" like so:

Code:
interface IPeople
{
   void Test();
}

Then declare a class that uses the interface
Code:
class Person : IPeople
{
   Person();
   
   void Test();
}

This makes the compiler happy because I have a class Person that is using the interface IPeople and Person has declared the "Test" function. If I comment out "void Test();" then the compiler has a fit, telling me to declare the Test function.

How can I get the same functionality in C++? I've tried the following with no luck, it compiles without errors or warning, when it should give me at least one error.

Code:
__interface IPeople
{
   void Test();
};

class Person : public IPeople
{
   Person();
   ~Person();

   //void Test();
};

This should throw an error as far as I understand it, but it compiles with no problems. How do I fix this?

BTW, I've also tried this

Code:
class IPeople
{
   virtual void Test() = 0;
};

class Person : public IPeople
{
   Person();
   ~Person();

   //virtual void Test();
};

Still no errors.
 
You will get a compile error if you try to instantiate the Person.

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Ok, now I'm getting somewhere. Thanks for the info, however, now when I try to instantiate the class Person, it tells me that it cannot instantiate an abstract class. The compiler gives this error everytime I try to compile, even when I declare the Test function.
 
Then you're not declaring it properly. Show the code.

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Your absolutely right; That was my dumb fault! I didn't look at the code close enough.

In the Interface I declared "void test();", in the class I declared "void Test();". Sorry about that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top