c++- how to use the object which call the function? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have function get_time() in a class which return a private member.
How can I use the object which called this function in the implementation of the function.
For example in case comp object which have a member call name call get_time function (comp.get_time()):
I want to be able to get the comp.name in the implementation of the det_time_function. How can I?
e.g
class comp
{
public:
string name;
}
class calc : public comp
{
private:
int time;
public:
int get_time(){
///here I want to get the name of the object which call the calc
///should I use this.name?
}
}
calc calc_obj;
calc.get_time();

Derived classes can not access the private members of their base classes. However, if you use protected declaration instead of private, then you could do that. But another way (which you should practice and learn) is by providing a getter() function in your parent class which return its object's name, then you can use that getter to call it from the child class and get the private member of the parent class; name in your case.
A basic getter() looks something like this:
YourVariableTypeHere get_VariableName()
{
return this->VariableName;
}

int get_time(){
///here I want to get the name of the object which call the calc
///should I use this.name?
}
No. To fetch the base class public data attribute, you would use
comp::name
Note, however, that in general, public data attributes should be avoided (because this disables encapsulation).
Also, in this question, it appears to be possible that some other object or function can call the derived class method, not simply the base class. So perhaps your question is non-sequitor, or perhaps misleading.
One way to provide a 'label' to handle both cases is to include a string in the method.
calc::get_time("caller-name");
Now the name is provided to the method, and the method need not fetch it from the base class, nor from which ever invoking object called get_time().

Related

c++ why would you pass a reference to a base class to a constructor of a derived class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In c++ is there a reson why you would pass a reference to a base class to a constructor of a derived class? Why would you want to do this?
Here is a basic example:
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
// This class ingerits from Base and implements fun()
class Derived: public Base
{
int y;
public:
Derived(Base& object);
void fun() { cout << "fun() called"; }
};
Typically, arguments are passed to constructors because the state of the arguments can be used to initialize the state of the object that is being constructed. Same applies to this case.
Non-const reference arguments can be (and nearly always are) used to modify the referred object.
In c++ is there a reason why you would pass a reference to a base class to a constructor of a derived class?
There is usually one reason why reference to an object would be passed to a constructor, does not really matter if that object type related to consttructed one or not - to construct this type you need information from that object. Using lvalue reference instead of const one could mean either bad design or ctor would need to modify passed object or keep non-const reference/pointer to it.
I would think the question is "why pass the base class reference instead of the derived class reference?". If so, the reason Base& is passed instead of Derived& is that the former allows you to pass an OtherDerived& reference, given that OtherDerived inherits Base. This is called polymorphism and is quite a thing in C++.
Here's pretty much the same question, but with a function instead of a constructor.

Has a inheritance, not is a [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know that when creating derived classes in C++ a structure like this:
class A {
...
}
class B: public A {
...
}
Would mean that there is a relation ship between A and B such that B "is a" A. I am wondering what if I wanted an A-B relationship such that B has a A not that B is a A.
My objective is to write a code that computes wave-function and corresponding properties of that wave-function for a particular molecule. I want my molecule to have a wave-function object. Can I i just create the molecule and wave-function classes separately, and then call the wave-function constructor in the molecule constructor?
class Molecule {
Molecule(...);
Molecule::Molecule(...)
{
wavefunction WF(...);
}
};
IF i can do this is it the correct way to create this has-a relationship? How would I access the wave-function object? Like this:
mol.WF
assuming I had created a molecule with the name mol. This is my first whack at a program with more than one custom class and I am confusing myself very fast.
I'm not sure I understand, but why don't simply declare a variable of type wavefunction in Molecule ?
class Molecule {
public:
wavefunction wf;
//...
};
Then, you can init it in constructor and manipulate it as a variable of Molecule. You can use it from outside the cass with mol.wf
This is called composition and this is what is used for "has a" relationship.
Edit: At the VERY last, you could use private inheritance, but composition by adding a field is much more practical and should be used instead almost always. And it would not be possible to use the wf fields outside of Molecule.
Yes, this is possible. It looks like all you're missing is a simple example:
class Molecule {
public:
wavefunction WF; // This is a member variable; each Molecule will
// "have a" wavefunction named WF
// When the constructor is called, the constructor of all member
// variables is called too. We can explicitly specify arguments to
// them using the 'initializer-list' (the part after ':')
Molecule()
: WF(/* constructor arguments... */)
{
// Here inside the constructor (and all methods of this class)
// you can refer to WF directly or via 'this' (the implicit pointer
// to the current molecule's instance):
WF.foo();
this->WF.foo();
}
};
You can then refer to the WF through its Molecule's instance like so:
Molecule m;
m.WF.foo();
You can implement a has a relationship by making the type a member of the type that has it.
class A {
...
}
class B {
A a;
...
}

How to call a function in all inheritant classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For example let's say I have this class:
class base{
public:
update();
};
and I have a lot of classes that are inherit from this one:
class A : public base
{
//<...>
}
class B : public base
{
//<...>
}
and so i want to know - can i somehow call the function base::update(); in all the classes without writing it all out (A::update(); B::update();) etc.
Like calling one function that would do the update(); in all the classes that inherit it from the base class.
Thanks!
Edit:
What I'm doing is changing my entity system into component based one and I want to know whether there's an easier way to call the (let's say) update(); function, that is inherited, in all the members. Instead of doing enemy01.update(); enemy02.update(); etc. to just write down a single update(); that'd work on all classes that have it(inherited it), sorta like a message to all of them to call the function.
A virtual function?
class base{
public:
virtual void update();
};
And then this function:
void update_class(base &b) {
return b.update();
}
You can call update_class on any class that inherits base.

In c++, are the virtual function, function overriding and polymorphism related with each other? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Generally in C++, we see same name functions with same arguments in different classes along with virtual keyword. Is it an example of polymorphism? If it is, how and is there any other way to understand polymorphism in c++??
Suppose that you have the following scheme:
class Base {
public:
virtual int foo() = 0;
};
class Derived1 : public Base {
public:
virtual int foo() { return 1; }
};
class Derived2 : public Base {
public:
virtual int foo() { return 2; }
};
Lets say now, that you want to store objects of Derived1 and Derived2 in a container, lets say a std::vector and every time you iterate through the vector to call for each one of its objects function foo. Normally, you'd need two vectors, one for objects of class Derived1 and another one for objects of class Derived2.
Look now how polymorphism comes to our rescue. We can store the addresses of objects Derived1 and Derived2 as pointers to Base objects, and then store these pointers in a std::vector<Base*> v. Now every time we iterate the vector of pointers and call member function foo (e.g., for(auto base : v) base->foo()) the right function foo is called for each one of the objects. This is one of the many aspects of polymorphism which is called upcasting
I hope that the above example of runtime polymorphism gave you a clue, as for how virtual functions and function overriding are related to inheritance and polymorphism.
Update:
Templates are a form of compile time polymorphism take a look at this SO question what is the difference between templates and polymorphism.

What is a good convention (or requirement) for the location of the const and virtual keywords in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know the virtual keyword does not need to be resused in a derived class member function declaration if it overrides a virtual base function but is it good practice to do so to make it clear that it is virtual? Also, what about the presence of the const keyword in declaration and/or definition? I think Alexandrescu mentions something about this but I can't recall what it was?
Your question seems very confused. virtual is optional when overriding a base-class method. const is never optional if you need it. This does not do what you think it does:
struct A
{
virtual void Func() const;
};
struct B : public A
{
virtual void Func();
};
The struct B has two functions named Func. One of them will be called when the object it is called on is const, and the other will be called when it is not const. Nothing in this code has been overridden; these are two separate virtual functions.
You cannot just ignore the const and expect everything to work out fine.
Indeed, this example also shows why you should use virtual when you're overriding in derived classes. In this case, it's fairly obvious that you intended to override a base class function, but you got the function signature wrong. Without the virtual there, there would not be an immediate indication that you intended to override something.
It's not a huge help, but it's something.
C++11 provides a better solution (in that it actually solves the problem) with the override pseudo-keyword.
struct A
{
virtual void Func() const;
};
struct B : public A
{
virtual void Func() override; //Gives a compiler error, since it is not overriding a base class function.
};