hello,

i'm new to cpp &

I wanna know that in C++

if there is multiple inheritance is done

suppose,

Class A & Class B public,protected member

is inherted in Class C

like,

class A
{
	protected:
		int no1;

	public:
		void get_a(void)
		{
		cout<<"Class A";
		}

};
class B
{
	protected:
		int no2;
	public:
		void get_b(void)
		{

		cout<<"Class  B";
		}

};
class c : public A,b
{
	private:
		int no;


};

so, how compiler will distinguish that get_a(),no1 is come from class A & get_b(),no2 is come from class B

i have surf lot of web site but i can't get any idea pls help to solve this question.

thanx in advance

Dani AI

Generated

Short answer: the compiler gives Class C a separate subobject for each base. That means A's members and B's members live in different parts of the C object. Unqualified name lookup and overload resolution decide which base routine is called. If both bases expose the same name with the same signature, the call is ambiguous at compile time and must be disambiguated.

A small pattern that helps when the two bases provide overloadable functions is to pull one base's overloads into the derived class with a using-declaration. The example below is new to the thread and shows how overload resolution can work across bases:

struct BaseA { void f(int)   { /* ... */ } };
struct BaseB { void f(double){ /* ... */ } };

struct C : BaseA, BaseB {
    using BaseA::f;   // bring BaseA::f into C's overload set
    // or explicitly provide C::f to forward/resolve
};

If both bases declare the exact same non-static data member name, you must qualify which base you mean (for example, c.BaseA::x). Also be careful with the diamond case: if A and B both inherit from X, then C will by default contain two X subobjects. Use virtual inheritance (class A : virtual public X) to get a single shared X subobject when that is what you want.

The forum replies are on the right track: demonstrated qualification, gave a helpful analogy, and noted the "is-a" relationship. For language details and edge cases (name lookup, overload resolution, virtual inheritance), see the C++ reference pages on multiple inheritance and virtual inheritance:

Practical tip: prefer composition or pure-abstract interfaces unless multiple inheritance solves a clear modeling or performance problem.

Recommended Answers

All 5 Replies

C++ is case sensitive, so class c : public A,b should be class C : public A, B

I won't have to.
When you create an object of class C, both get_a, get_b will be available for calling.
If however, both function was named get(), then it would cause ambiguity.

class A
{
	protected:
		int no;

	public:
		void get(void)
		{
		cout<<"Class";
		}

};
class B
{
	protected:
		int no1;
	public:
		void get_a(void)
		{

		cout<<"Class";
		}

};
class C : public A,B
{
	private:
		int no;


};

//inside main
C c;
c.get() //error. which get()?
c.A::get() //oh, so you mean A' get. No problem
commented: Signing off for today, so here's a wave goodbye. See you in a year! +18

so, how compiler will distinguish that get_a(),no1 is come from class A & get_b(),no2 is come from class B

Does it matter? If you look at the relationship you created, Class C is a Class A and a Class B. Therefore Class C has the methods get_a and get_b, which parent they come from is irrelevant.

so, how compiler will distinguish that get_a(),no1 is come from class A & get_b(),no2 is come from class B

i have surf lot of web site but i can't get any idea pls help to solve this question.

thanx in advance

I think below program will help u to understand,

class Father{
private:
    int money;
public:
    void Nokia_Mobile(void){
        cout << "I m Father's Nokia Mobile " << endl;
    }
};

class Mother{
private:
    int money;
public:
    void Samsung_Mobile(void){
        cout << "I m Mother's Samsung Mobile " << endl;
    }
};

class Son:public Father,public Mother{
private:

public:


};

int main(){
    Son john;
    john.Nokia_Mobile();
    john.Samsung_Mobile();
    return 0;
}

here u can access ur father and mother's mobile if they are public(mean if they are not password protected) so u can call and play games on both mobiles.. but cant access ur parents money(in most cases) as money is private for u.

Does it matter? If you look at the relationship you created, Class C is a Class A and a Class B. Therefore Class C has the methods get_a and get_b, which parent they come from is irrelevant.

exactly. It is irrelevant from which parent the method has come. But even if the compiler needed to know that:
Out of the 2 classes from which class C is inherited, get_a() method is defined only in class A and get_b() method is only defined in class B in your code. Hence we can easily see from which parent the method has come

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.