call derived member functions

Pages: 12
I define a function in base class as virtual and I redefine it in the derived class. When I call this function in func.cpp to use its members, the call fails. I want to know how can I call a function whose details are within derived class through the base one in func.cpp?? I use the example (please see below) to call the members of fun:
in func.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 class Base 
{
public:
  
  

virtual void fun(double member1, double member2, double member3, ....);


private:

  double member_1;
  double member_2;
  double member_3;
  ....
};


class derived : public Base
{ 
public:
 

  
void fun(double member1, double member2, double member3, ....)

{

 member1 = value1;
 member1 = =value2;
 member1 = value3;
}

};

in func.cpp

1
2
3
4
5
6
7
8
 void base::fun(double member1, double member2, double member3, ....)
{
 member_1 = member1,
member_2 = member2;
member_3 = member3;


}


when I print to see the value of member_1 and ... , the compiler shows that member_1=0.0.
Last edited on
Your example doesn't show how you called those methods. It matters.
I used ''void base::fun(double member1, double member2, double member3, ....)'' as calling by does not give anything, also I define a pointer of base class to derived one by using the following, but does also not work, what was wrong please:


1
2
3
4
5
6
7
 Base* b = new Derived;
 b->fun(double member1, double member2, double member3, ....)
member_1 = b->member1,
member_2 = b->member2;
member_3 = b->member3;

delete b;

Last edited on
The error of the compiler when I use pointer is:
class Base’ has no member named ‘member1’; did you mean ‘member_1?
what I have to do please?
You can't do polymorphism with members, you have to use methods.
@kbw
How can I do it please? I want to call implemented functions in derived classes to func.cpp file by using in this manner when I calling:
1
2
3
4
5
6
7
8
void base:: func(member 1 ....................member6)
{
}
double base:: func1(x1,x2)
{
}
//and so on 
}
Last edited on
Members should not be public, they exist to support some functionality provided by the object. But that functionality should be accessed thru methods.

Methods can be overloaded, so derived classes can provide alternative implementations for alternative algorithms.

But never use public data for object oriented programming.
I used ''void base::fun(double member1, double member2, double member3, ....)'' as calling by does not give anything,


That is not a function call, it looks more like the start of a function definition.

If one had :

1
2
3
int addtwo(const int a) {
   return a+2;
} 


The function call would be:

1
2
int b {};
b = addtwo(10);


To call a function that is a member of a class, one needs an object first.

I think you really need to get these basics understood first.

You also really need to ask question properly:

Post code that can be compiled. If you don't understand how to call a function, there may lots of other things wrong too.
Use code tags, so we can read it, and compile it here

Have a look at the reference on this site and at learncpp.com
@kbw
Do you mean class member function or what that should not be public. If you mean for class member function I define it as virtual in protected definition in base class (please see below)but it does not give the expected result. Can you explain me please with the below example how can I overload function that define it in the derived classes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 class Base 
{
public:
  
 //define public methods 
protected:
virtual void fun(double member1, double member2, double member3, ....);


private:

  double member_1;
  double member_2;
  double member_3;
  ....
};


class derived : public Base
{ 
public:
 

  
void fun(double member1, double member2, double member3, ....)

{

 member1 = value1;
 member1 = =value2;
 member1 = value3;
}

};


In func.cpp

1
2
3
4
5
6
7
8
9
10
 
void base::fun(double member1, double member2, double member3, ....)
{
 member_1 = member1,
member_2 = member2;
member_3 = member3;


}
 
@TheIdeasMan
I am sorry dear, the posts are re-edited using code tags. The code est composed with many files and classes and the purpose is to creat derived classes that define my problem but I want to know from your knowledge and the knowledge of the other members of forum how can I do this.Please how can overload details of functions that implement in derived classes?
Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

struct MyBase {
	MyBase (int aa, int bb) : a(aa), b(bb) {}
	virtual void show() const { std::cout << "base: " << a << "  " << b << '\n'; }

	int a {}, b {};
};

struct MyDer : public MyBase {
	MyDer(int aa, int bb, int cc) : MyBase(aa, bb), c(cc) {}
	void show() const override { std::cout << "derived: " << a << "  " << b << "  " << c << '\n'; }

	int c {};
};

int main() {
	const MyBase* elems[] {new MyBase(1, 2), new MyDer(3, 4, 5), new MyBase(6, 7), new MyDer(8, 9, 0)};

	for (const auto& e : elems) {
		e->show();
		delete e;
	}
}



base: 1  2
derived: 3  4  5
base: 6  7
derived: 8  9  0

Last edited on
@seeplus
Thank you,
I want to define MyDer(6,7) , and MyDer(8,9,0) in file.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
struct MyDer : public MyBase {
{	
public:
MyDer(int aa, int bb, int cc) 
{
aa=3;
bb=4;
cc=5          
};

//and then use these values in the defintion of function in file.cpp
MyBase ::MyBase()
{}
virtual::~MyBase()
{}
MyBase::MyBase(int aa, int bb, int cc)
Base* b = new MyDer
b->MyBase(aa, bb,cc)
printf("value =\n", b->aa);
delete b
}

Please How can I achieve this? Please I need your support to aim the target.
Last edited on
It is not at all clear what your "target" is. If we can't see it, then compiler definitely can't.
@keskiverto
I want to overload functions, that are defined in derived classes and these functions are composed by many members, to file.cpp which contains the core of problem . please I need your help.
Thank you,
Last edited on
Your constructor (L5-10) makes no sense.
You're assigning values to the arguments to the constructor.
The values go out of scope when the constructor exits.

L 9,18,19,21 are missing semicolons.

L20 makes no sense without a declaration of the class MyBase.

Last edited on
@AbstractionAnon
how Can I define aa, bb &cc in the derived class please?If I put a constructor/destructor of derived class is the pb will be resolved like this , please see below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct MyDer : public MyBase {
{	
public:
MyDer::MyDer()
{}
virtual::~MyDer()
{}
MyDer(int aa, int bb, int cc) 
{
aa=3;
bb=4;
cc=5;         
};

//and then use these values in the defintion of function in file.cpp
MyBase ::MyBase()
{}
virtual::~MyBase()
{}
MyBase::MyBase(int aa, int bb, int cc)
{
int aa1;
Base* b = new MyDer;
b->MyBase(aa, bb,cc);
aa1=aa;
delete b;
}
Last edited on
Perhaps it's better if you describe what you want to do with words first. We seem to have trouble understanding precisely what the problem is here.

So I suggest, forget the code for now, and just explain what it is you're trying to do. Once we understand that, we'll be better placed to help.
What you posted does not compile.

You still have not fixed the problem in MyDer's constructor.

L24 appears to be an attempt to call MyBase's constructor which is what you're in.
This is a recursive call.

L25: What's the point of aa1? It goes out of scope at line 27.

What's the point of lines 23, 24,26? You accomplish nothing.

As previously requested, please post your declaration for MyBase.
Last edited on
@kbw
I want to use IPOPT to my own problem, so I would like to optimize variables that minimize a function. The purpose is to creat derived classes to define my probem, its parameters, grad of function and constraint .... and all of these would be called to the predifened methods implemented in file.cpp. I hope that the purpose is enough clear.
Thank you very much Sir
@AbstractionAnon
yes sure sir that would no be compiled, but I just want to pose what I want to do. please see my previous comment to see what I want to attempt.
Thank you very much
Pages: 12