Dear all,
I have a question about inheritance in C++. Suppose there is a base class called "A", with a protected variable "list_" which basically stores a list of objects with type "P", and also there is a method in class A which adds objects to the list.
There are two classes called "A1" and "A2" respectively, which are derived from the base class "A". assume a1 and a2 are instance of A1 and A2, and a1, a2 add "P" objects based on different conditions, so a1 and a2 have their own list of "P" objects with name "list_", is this correct?
And if the above is true, then is it possible to change one field of a specific "P" object from the "list_" in "a1" with some function in class "A2"?
Thanks a lot for any comment.
Best Regards.

Recommended Answers

All 7 Replies

Member Avatar for jencas

Dear all,
I have a question about inheritance in C++. Suppose there is a base class called "A", with a protected variable "list_" which basically stores a list of objects with type "P", and also there is a method in class A which adds objects to the list.
There are two classes called "A1" and "A2" respectively, which are derived from the base class "A". assume a1 and a2 are instance of A1 and A2, and a1, a2 add "P" objects based on different conditions, so a1 and a2 have their own list of "P" objects with name "list_", is this correct?

Yes.

And if the above is true, then is it possible to change one field of a specific "P" object from the "list_" in "a1" with some function in class "A2"?

Yes, if a2 has access to a1 and a1's list_.

Just declare that function as friend .

hi, jencas,
Thanks for your reply, it's good to know that this is possible. ^_^
so can you please give me a simple example for this? if I have two instances of a1 and a2 from class A1 and A2 respectively, and both a1 & a2 have their own list_ of P objects, so if some condition is met and I want to change a field of a specific "P" object in a1's list by calling a function in a2(as this condition is tested with a function in A2 but the value that I want to change is actually from the list in a1), and you mentioned that if a2 has access to a1 and its list_, so do I create another instance of A1 (call it a11) in class A2 and change the filed in its list_? I am confused here as how can we make sure that the function in A2 can access the list_ which belongs to the instance a1? as both a1 and a2 have the same name variable "list_", so how can we differentiate them?

Thank you very much for your help.
Regards.

Hello, all,
it's a bit hard to explain what I am trying to do in words so I did a simple test program in order to make it clear, so here I have a base class like this:

//bash.h
#ifndef BASE_H
#define BASE_H
#include <iostream>
#include <vector>

class Base{
public:
	Base();
	void setNum(int num);
	void addNumber();
	void print();
protected:
	int num_;
	std::vector <int> vec_;
};
#endif


//bash.cpp
#include "base.h"
#include <iostream>
using namespace std;

Base::Base() {}

void Base::setNum(int num){
	num_ = num;
}

void Base::addNumber(){
	vec_.resize(num_);
	for (int i=0; i<num_; i++){
		vec_[i] = i;
	}
}

void Base::print(){
	for (int i=0; i<vec_.size(); i++)
		cout<<vec_[i]<<"\n";
}

and now I have two classes derived from the above base class:
D1:

//D1.h
#ifndef D1_
#define D1_
#include <iostream>
#include "base.h"
#include "D2.h"

class D1 : public Base{
public:
	D1();
	void add(int n);
	friend class D2;
	//friend void D2::change_value(int a);
};
#endif

//D1.cpp
#include "D1.h"
D1::D1() {}

void D1::add(int n){
	setNum(n);
	addNumber();
	print();
}

and D2:

//D2.h
#ifndef D2_
#define D2_
#include <iostream>
#include "base.h"
#include "D1.h"

class D2 : public Base{
public:
	D2();
	void change_value(int a);
private:
};
#endif

//D2.cpp
#include "D2.h"
#include "D1.h"
D2::D2() {}

void D2::change_value(int a){
	/*this function is able to change an entry (say the first entry) in the vector from D1*/

}

so in the main program, if I do a test like this:

#include "base.h"
#include "D1.h"
#include "D2.h"
#include <iostream>
#include <vector>

int main(){
	D1 d1;
	D2 d2;
	d1.add(5);
	d2.change_value(10);    //here it should be able to change the (say first entry) of the vector in d1 to 10, and in the next line to print out the result, the expected result should be 10 1 2 3 4
	d1.print();
	return 0;
}

I tried to declare D2 as a friend class of D1, however I can not create a variable with type D1 in D2, so I do not know how to get the variable vec_ in D1 with correct values (in this case 0 1 2 3 4), and do some change on the data (change it to 10 1 2 3 4 by using a function in D2).
So can anyone please suggest a possible solution on this? Thanks a lot for your help.

I am not so sure that its a good idea to in terms of OO to do this, but here goes ..

Basically what you want is to add a function say void ChangeNumber(int index, int value) to your base class, where index is the position in the vector and value is the new value.

You want to make this function, as well as your setNumber and add functions protected, so that they become private in your derived classes. This way not every object which has access to the class definition of D1 and D2 can try to change the values in there.

Then you want to add a function to D2 say void changevalueinobject(int index, int value, D1& ref2D1), which calls ref2D1.changeNumber(index, value). Make sure D2 is a friend of D1 so that D2 can access the function ChangeNumber which is private in D1.

Also make sure you have a forward declaration of D1 in D2's header file, so that it recognizes variables of type D1.

This will help make sure that only objects of type D2 can change the values in D1.

Hope this helps.

Thank you very much for your reply and it works in the simple test program, actually I am doing some modifications with some files in a network simulator and it contains a large number of files, and i just need to change the files which implements the "MAC layer" of a network, so the situation is a bit different compare to the above simple test, because I do not have a main function to invoke this "change_value" function in D2, so ideally this function call does not take a reference to the D1 class (ref2D1), but only the numbers that I want to change, so when this function is called then one specific value in D1 will be changed. So is this possible to be achieved without taking the "ref2D1" as an argument?
Thank you for your help.

Basically D1 and D2 are types using which you can define objects. Now if you want to call an object of type D1 from class D2, you need to have access to that specific object.

So you could have an array of D1 objects inside D2 and then use that ? I am not very sure what you're trying to do, but you cannot change values of a D1 object without knowing which object you want to change it for. You need to store a reference somewhere, even if you don't want to pass it in as a function argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.