0

I'm trying to learn differences between Interfaces and Abstract Classes in C++. I've been studied from a lot of source. I can definitely define what is the interface and what is the abstract class. But i just know as definitional. When i look to the example codes, i can't distinguish which one is the Interface or Abstract Class. Other languages have the keywords (interface and abstract) and you can see the which one is the interface or abstract class. But there are no these keywords in C++. I know the Abstract Class. If you add a pure virtual function in the class, that class will be the Abstract Class. But when i look the example codes for Interface, i see the same things that definition of Abstract Class. How can i distinguish which one is the Interface or Abstract Class when i look the example codes? I would like to see the example codes from you guys. Thanks.

jasonsmask
  • 47
  • 1
  • 9
  • an interface is an abstract class with all function pure virtual – pm100 May 24 '22 at 00:41
  • 4
    Possible duplicate: [Abstract Class vs Interface in C++](https://stackoverflow.com/questions/12854778/abstract-class-vs-interface-in-c). Short answer: C++ has no interfaces, only abstract classes. Some people prefer to call abstract class with only pure virtual methods and nothing else an "interface" (similar to Java convention), but there is no difference at language level. – Yksisarvinen May 24 '22 at 00:42
  • If i want to make pure virtual function all functions of an abstract class then that abstract class will be an interface automatically. Right ? – jasonsmask May 24 '22 at 00:45
  • 4
    This sounds like you're being taught by a Java programmer who thinks they know C++. Java has interfaces and abstract classes. C++ just has classes, and it has multiple inheritance. Save yourself the pain and suffering, and don't pretend C++ is diet Java; learn C++ on its own merits. – Silvio Mayolo May 24 '22 at 00:50
  • 4
    "Interface" is a name for us, humans. C++ doesn't care. If a class has at least one pure virtual method, it's an abstract class. If class has only pure virtual functions and nothing else, it's still an abstract class, not different from a class that has other members. The only difference is that a human can come and call that an "interface", because it makes it easier for that human to understand or communicate some concept. C++ doesn't care if you call it an interface or not, it's still an abstract class from C++ point of view. – Yksisarvinen May 24 '22 at 00:51
  • Let me twist your brain. You can have an interface class that has no pure virtual methods. In other words, in C++ you can have an interface class that is not an abstract class. – Thomas Matthews May 24 '22 at 00:53
  • All C++ tutorials on the internet have the interface section. You should say this to them. @SilvioMayolo – jasonsmask May 24 '22 at 00:54
  • That was really satisfactory explanation. Thanks @Yksisarvinen – jasonsmask May 24 '22 at 01:00
  • 5
    *"All C++ tutorials on the internet have the interface section"* That's why we typically recommend getting [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from instead of using internet tutorials. – Yksisarvinen May 24 '22 at 01:02
  • Yeah you're right. My next job will be to buy a good C++ book. Thanks for recommendations. – jasonsmask May 24 '22 at 01:11
  • I think One of the best things people learning c++ can do is pretend inheritance doesn’t exist. It gets rid of a lot of questions, and also eliminates a lot of bad coding habits that most books/schools/tuts introduce when they explain inheritance. – Taekahn May 24 '22 at 01:13
  • @Taekahn why not go a step further and pretend c++ doesn't exist? /s – Jeremy Friesner May 24 '22 at 02:54

0 Answers0