Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have CXClass which is derived from CXClass1 and CXClass2 mentioned below:

class CXClass : public CXClass1 , public CXClass2

CXClass1(i have two variables i.e X and y variables) because it is 2D class.

CXClass2(i have two variables i.e X ,y and z variables) because it is 3D class.

I have GetX() ( to return X value) API in both the classes ( and both the x classes values of x might be different)

somewhere in the main the derived class CXClass GetX() is called mentioned below:

double CXClass::GetX()
{
// in this i have to return both the CXClass1 and CXClass2 APis of GetX()

}

below is the sample:

C#
class CXClass1
{
public:
	int m_i;
	int m_j;
	
	CXClass1() { cout << "I am Derived1 Constructor" << endl; }


	void print() { cout << " I am derived1" << endl; }

	void func1()
	{
		cout << "I am Derived1 func1" << endl;
	}

	 double GetX()
	{
		m_i = 3;
		return m_i;
	}
};



class CXClass2
{
public:
	int m_i;
	int m_j;
	int m_k;


	CXClass2() { cout << "I am Derived2 Constructor" << endl; }
	void print() { cout << " I am derived2" << endl; }

	
	void func2()
	{
		cout << "I am CDerived2 func2" << endl;
	}


	double GetX()
	{
		m_i = 2;
		return m_i;
	}
};

class CXClass : public  CXClass1, public  CXClass2
{
public:

	CXClass()
	{

	}


	double GetX() 
	{
		
		double d = CXClass1::GetX();
		double k = CXClass2::GetX();

		//i have to return two apis values ?
	}

};


void main()
{
	CXClass obj;
	double d = obj.GetX();
	
}


Please give ur suugestions

Thanks
Posted
Updated 17-Jan-16 22:17pm
v5
Comments
Andreas Gieriet 18-Jan-16 1:47am    
Not clear. Please elaborate.
What's the relation between capital X and lower case x?
The variables x, y, z are implementation details of the classes - why does it matter what their value is - you hopefully have some methods which return some meaningful values.
What do you mean by "I cannot change the call"? Which call?
Finally: please make the code samples properly typed, e.g. the cxclass2 is probably not properly spelled.
You expect some help from us, so please make it easy for us to help you by providing proper code examples and stating the problem in a clear and concise way. You expect us to spend time on your problem, so, please spend your time too to ask the question in a decent way (spelling, problem statement, what did you try, etc.).
Thanks
Andi
[no name] 18-Jan-16 2:00am    
i updated the question .please check and let me know
Sergey Alexandrovich Kryukov 18-Jan-16 1:52am    
Not clear. This is not even a question.
—SA
[no name] 18-Jan-16 1:59am    
i changed the question please is that ok ?
Andreas Gieriet 18-Jan-16 2:11am    
You are asking us something you have to answer yourself!
It seems to me that you have your design not under control.
What is the purpose to use multi-inheritance at all? I would not use multi inheritance with concrete classes without cause - it makes all complicated.
If you insist on doing so: your derived class has two GetX() methods "for free" - the derived classes provide these - you have to tell which is meant in the client code. Why would you want to override these with some other meaning?
Some more notes: why do you make one of the base classes virtual, but the other not? Why do you fool the client by actually *setting* the value in the *getter* method (the GetX()) - this is bad practice. Initialize the value in the constructor's initializer list instead!
Cheers
Andi

1 solution

The short answer and bad news is that if you can only return a single value from a function. The good news is that you can return anything that's a value, which is just about anything. So you could write something like...

C++
return std::make_pair( class_1::X(), class_2::X() );


and get what you asked for, even though you don't actually want to do that. Why not? Read on...

Public inheritance only really makes sense where objects of the derived class can be substituted for all places you use instances of the base class. So this means everywhere you write something like:

C++
void frob( base_class &b );

int main()
{
    base_class b;
    frob( b );
}

you can instead write:

C++
int main()
{
    derived_class d;
    frob( d );
}

and the program is still valid. The program doesn't have to have the same behaviour (there'd be no point to inheritance otherwise) but it must still compile and run without error. It's one of the cornerstones of OO design.

In your case you're saying "wherever I use a base_class_1 OR a base_class_2 in my code I want to be able to substitute a derived_class object and the program still remain valid but I want the return types of several of my functions to return something different and all the client code still be valid." That's a big ask so I'd advise looking at your code and seeing if it's really the best idea (HINT: It's not).

The problem sounds like you've got two concepts that are similar and presumably share a fair chunk of code. No idea what the concepts are from you code or description but they seem to be things that rely on a coordinate system. Instead of messing about with multiple inheritance what I'd suggest you look for an abstraction for the coordinate system and the way it represents and manipulates points and parameterise that in what is now your derived class, e.g. something like:

C++
template<class coordinate_system>
class graphical_vector
{
    public:
        graphical_vector( coordinate_system::point_type &start, coordinate_system::point_type &end ): start_(start), end_(end){};

        double length() const
        {
            return distance_from( start_, end_ );
        };

    private:
        const coordinate_system::point_type start_;
        const coordinate_system::point_type end_;
};


As I have no idea of what sort of app you're writing I have no idea if this stands any chance of success. Fairly often when I've seen multiple inheritance being used in the way you advocate this sort of solution works better.

If this is complete balls please feel free to clarify your question a bit (actually a lot) by telling us what you're trying to achieve and why. It may turn out you've got a really cool, valid use case that'll tell the rest of us something and improve what we do or we might be able to suggest other ways for you to go that are better for you and your code in the long run.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900