Answer the question
In order to leave comments, you need to log in
What is the equivalent of Java interfaces in C++?
What's up, software.
In C++, when defining a class in a header file, we can declare an empty constructor.
At the same time, in the source file, we can define this constructor using the so-called "binary scope resolution operator ( :: )", which in turn reminds me of a Java interface.
Example:
person.h - file
class Person
{
public:
Person();
}
_______________________________ #include"Person.h"
Person::person(int i)
{
int k = i;
}
_______________________________ #include"Person.h"
Person::Person(int n, int j)
{
int a = n;
int b = j;
}
#include "Person.h"
int main()
{
Person one;
Person two;
one(1);
two(2,3);
std::cout << one << std::endl;
std::cout << two << std::endl;
}
Answer the question
In order to leave comments, you need to log in
Do not drag knowledge of java into C ++. Get yourself in trouble.
If you want to have constructors with different sets of parameters - declare them all in the header and give them bodies in cpp.
What is the analog of Java interfaces in C++
recursi0n good night. Please read my answer to a similar question: ClassName::functionName or objectName.functionName? You will surely find something for yourself.
In C++, the notation
just means that B is inside A, where A is a namespace, class, or enum. B can be a function, a class, a static variable. (see cppreference ) This notation has no special meaning for constructors. It also has nothing to do with interfaces.
Interfaces in C++ are analogous to analogous to abstract classes :
class Interface {
public:
virtual int method() = 0;
};
class Implementation: public Interface{
public:
int method() override {
// some code
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question