This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 6 years ago.
I am learning to make classes using template codes and one of them is coded with what looks like inheritance syntax:
class KNNLearner(object):
What is the purpose of (object) here?
In python everything is an object, an object is an object. You can also create Meta-classes to change some behavior of your objects/object to implement something.
In you case KNNLearner(object) the (object) permits you to pass the class that you want to KNNLearner to inherit.
Related
This question already has answers here:
choosing appropriate specialized template at runtime
(2 answers)
Create objects from template, type is entered by user input c++
(4 answers)
Closed 3 months ago.
Say I have a templatized class
template<class T>
class MyClass
{
...
}
Now let's say for the sake of simplicity, that the input explicitly mentions that the following data shall be of what type:
for example
int
1 2 3 4 5
or
float
1.5 2.3 4.2 5.9
Now I want to create an object based on the type mentioned in the input
Something like
MyClass<type_mentioned_in_input> obj;
How do I achieve this?
Based on my lesser understanding of C++, here is what I tried:
Create an abstract base class for the MyClass
Mention all the member functions of MyClass as pure virtual functions in the abstract base class
Dynamically initialize an object of the base class at runtime
Clearly this method not only increases the complexity, but also fails with templates.
What should be the desired what to execute this?
You can't:
template are compiler time, the specific content between < .. > is replaced when you build, and that can not be modified runtime based on the input.
The solution to your problem (about having different types defined runtime) is to use an other mechanism, like std::variant.
This question already has answers here:
Can a c++ class include itself as an member?
(4 answers)
Closed 5 years ago.
I'm trying to define a class this way (simplified):
class Student
{
private:
Student* pointer;
public:
set_pointer(Student*);
}
Is this legal in C++ and is it consistent with object oriented design?
Yes, a class may store a pointer to an object of the same type (which may be itself, or some other instance).
This is common in linked-list implementations, where a node stores a pointer to the next node.
As for whether it's "consistent with object oriented design", it is impossible to make a generalisation on the subject; it depends on the program. Personally I cast a suspicious eye on code of this form, but again you can't really generalise.
This question already has answers here:
What "CV_EXPORTS_W" means
(2 answers)
Closed 5 years ago.
I'm trying to understand functionality of OpenCv library. I saw following strange type class declaration code here.
class CV_EXPORTS_W Subdiv2D
{
// code here
};
I don't understand, What does Subdiv2D do with class?
The Subdiv2D is the name of the class. The CV_EXPORTS_W part is probably some macro or an alias specific to Windows implementation.
This question already has answers here:
When should you use 'friend' in C++?
(31 answers)
What's the difference between friendship and inheritance?
(4 answers)
Closed 5 years ago.
I am not exactly sure what the use of a friend function or class is when you can easily just use inheritance (parent/child classes)? From my understanding, a friend function or class allows a non-member function to access the members of another class that it is declared a friend of. Similarly, a child class can access the members of a parent class. I am wondering, in which situation would you pick one over the other?
Because you may want to use some private functions/member of a class without being a child of it....
You should read more about inheritance. You can't consider inheritance as a solution to access "members of a class" as you mentioned.
This question already has answers here:
When do I need anonymous class in C++?
(6 answers)
Unnamed classes
(2 answers)
Closed 9 years ago.
I have been exploring more C++ and encountered a phenomenon like Unnamed classes.
class : public classB{
public:
protected:
private:
};
How are these classes useful? Is it like the concept of lambda functions?
I would like to know the purpose of such classes. The other threads don't really show any usefulness of the feature.