We want to discuss a diamond problem. Which will cover inheritance,virtual class(2 topics).For this I will use CPP(c++)
You have to have a common idea of inheritance in cpp for this.
Now, why we name this as a diamond problem? Good question. Actually, the hypothetical figure is like a diamond. 😂😂.ok lets see the figure.
Let's assume that we have 4 class. a,b,c,d. b and c will inherit a and d will inherit both c and b
Now, I will upload the code which is simply inheritance..
#include<iostream>
using namespace std;
class a
{
protected:
int apr;
public:
void set_apr(int apr)
{
this->apr = apr;
}
int get_apr()
{
return apr;
}
};
class b : public a
{
protected:
int bpr;
public:
int bpub;
void set_bpr(int bpr)
{
this->bpr = bpr;
}
int get_bpr()
{
return bpr;
}
};
class c : public a
{
protected:
int cpr;
public:
int cpub;
void set_cpr(int cpr)
{
this->cpr = cpr;
}
int get_cpr()
{
return cpr;
}
};
class d :public b, public c
{
protected:
int dpr;
public:
int dpub;
void set_dpr(int dpr)
{
this->dpr = dpr;
}
int get_dpr()
{
return dpr;
}
};
int main()
{
d d1;
d1.cpub = 20;
d1.set_apr(100);
d1.bpub = 200;
d1.set_bpr(150);
d1.set_cpr(250);
d1.dpub = 59;
d1.set_dpr(590);
cout <<"cpub= "<< d1.cpub << endl;
cout << "bpub= "<<d1.bpub << endl;
cout <<"dpub= "<< d1.dpub << endl;
cout <<"dpr= "<< d1.get_dpr() << endl;
cout << "cpr= "<<d1.get_cpr() << endl;
cout << "bpr= "<<d1.get_bpr() << endl;
cout <<"apr= "<< d1.get_apr()<< endl;
return 0;
}
But it shows ambiguity problem..Because when b and c class inherit a. Then 2 copies of a are in b and c. And when d wants to inherit those, then it causes ambiguity. Because d can't be able to isolate which one is from a among those 2. So, compilation error is occurred. For that sake, we have to use virtual class. Don't worry, because I'm here.. So, let's fix this problem.
#include<iostream>
using namespace std;
class a
{
protected:
int apr;
public:
void set_apr(int apr)
{
this->apr = apr;
}
int get_apr()
{
return apr;
}
};
class b :virtual public a // see, we use virtual here//
{
protected:
int bpr;
public:
int bpub;
void set_bpr(int bpr)
{
this->bpr = bpr;
}
int get_bpr()
{
return bpr;
}
};
class c :virtual public a // see, we use virtual here// so, one copy of a will send //
{
protected:
int cpr;
public:
int cpub;
void set_cpr(int cpr)
{
this->cpr = cpr;
}
int get_cpr()
{
return cpr;
}
};
class d :public b, public c
{
protected:
int dpr;
public:
int dpub;
void set_dpr(int dpr)
{
this->dpr = dpr;
}
int get_dpr()
{
return dpr;
}
};
int main()
{
d d1;
d1.cpub = 20;
d1.set_apr(100);
d1.bpub = 200;
d1.set_bpr(150);
d1.set_cpr(250);
d1.dpub = 59;
d1.set_dpr(590);
cout <<"cpub= "<< d1.cpub << endl;
cout << "bpub= "<<d1.bpub << endl;
cout <<"dpub= "<< d1.dpub << endl;
cout <<"dpr= "<< d1.get_dpr() << endl;
cout << "cpr= "<<d1.get_cpr() << endl;
cout << "bpr= "<<d1.get_bpr() << endl;
cout <<"apr= "<< d1.get_apr()<< endl;
return 0;
}
Compile it. Happy coding. 😊😊
You have to have a common idea of inheritance in cpp for this.
Now, why we name this as a diamond problem? Good question. Actually, the hypothetical figure is like a diamond. 😂😂.ok lets see the figure.
Let's assume that we have 4 class. a,b,c,d. b and c will inherit a and d will inherit both c and b
Now, I will upload the code which is simply inheritance..
#include<iostream>
using namespace std;
class a
{
protected:
int apr;
public:
void set_apr(int apr)
{
this->apr = apr;
}
int get_apr()
{
return apr;
}
};
class b : public a
{
protected:
int bpr;
public:
int bpub;
void set_bpr(int bpr)
{
this->bpr = bpr;
}
int get_bpr()
{
return bpr;
}
};
class c : public a
{
protected:
int cpr;
public:
int cpub;
void set_cpr(int cpr)
{
this->cpr = cpr;
}
int get_cpr()
{
return cpr;
}
};
class d :public b, public c
{
protected:
int dpr;
public:
int dpub;
void set_dpr(int dpr)
{
this->dpr = dpr;
}
int get_dpr()
{
return dpr;
}
};
int main()
{
d d1;
d1.cpub = 20;
d1.set_apr(100);
d1.bpub = 200;
d1.set_bpr(150);
d1.set_cpr(250);
d1.dpub = 59;
d1.set_dpr(590);
cout <<"cpub= "<< d1.cpub << endl;
cout << "bpub= "<<d1.bpub << endl;
cout <<"dpub= "<< d1.dpub << endl;
cout <<"dpr= "<< d1.get_dpr() << endl;
cout << "cpr= "<<d1.get_cpr() << endl;
cout << "bpr= "<<d1.get_bpr() << endl;
cout <<"apr= "<< d1.get_apr()<< endl;
return 0;
}
But it shows ambiguity problem..Because when b and c class inherit a. Then 2 copies of a are in b and c. And when d wants to inherit those, then it causes ambiguity. Because d can't be able to isolate which one is from a among those 2. So, compilation error is occurred. For that sake, we have to use virtual class. Don't worry, because I'm here.. So, let's fix this problem.
#include<iostream>
using namespace std;
class a
{
protected:
int apr;
public:
void set_apr(int apr)
{
this->apr = apr;
}
int get_apr()
{
return apr;
}
};
class b :virtual public a // see, we use virtual here//
{
protected:
int bpr;
public:
int bpub;
void set_bpr(int bpr)
{
this->bpr = bpr;
}
int get_bpr()
{
return bpr;
}
};
class c :virtual public a // see, we use virtual here// so, one copy of a will send //
{
protected:
int cpr;
public:
int cpub;
void set_cpr(int cpr)
{
this->cpr = cpr;
}
int get_cpr()
{
return cpr;
}
};
class d :public b, public c
{
protected:
int dpr;
public:
int dpub;
void set_dpr(int dpr)
{
this->dpr = dpr;
}
int get_dpr()
{
return dpr;
}
};
int main()
{
d d1;
d1.cpub = 20;
d1.set_apr(100);
d1.bpub = 200;
d1.set_bpr(150);
d1.set_cpr(250);
d1.dpub = 59;
d1.set_dpr(590);
cout <<"cpub= "<< d1.cpub << endl;
cout << "bpub= "<<d1.bpub << endl;
cout <<"dpub= "<< d1.dpub << endl;
cout <<"dpr= "<< d1.get_dpr() << endl;
cout << "cpr= "<<d1.get_cpr() << endl;
cout << "bpr= "<<d1.get_bpr() << endl;
cout <<"apr= "<< d1.get_apr()<< endl;
return 0;
}
Compile it. Happy coding. 😊😊

Comments
Post a Comment