Skip to main content
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. 😊😊


Comments

Popular posts from this blog

Remote Dictionary Server

  Today I want to discuss about REDIS Cache. In a simple way, it’s like making web apps more effective. What they say ? From Redis  official website , “Redis is an open-source, in-memory data structure store, used as a database, cache and message broker. It supports data structures . I t can be thought as a NOSql DB which store data as a key value pair in the system memory. Now, let’s talk about caching. Difference Between MongoDB And Redis MongoDB is a disk based database optimized for operational simplicity. It has a huge data volumes. And Redis is an in-memory, persistent  data Structure store that is popular for performing operations with maximum effectiveness. Caching Caching is a process where the data are stored in caches so that those can be retrieved faster. It is for making user experience better. Redis Cache As Redis is an in-memory data structure store  where data access operations are performed faster. That’s why it’s the perfect method for caching. The ...

Some Important learning sites for Dynamic programming

Now, We will discuss some of the most useful sites for learning Dynamic Programming. Here, I have mentioned some of those . :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: medium.freecodecamp.org -  Demystifying Dynamic Programming iarcs.org.in -  Dynamic Programming - Tiling topcoder.com -  Dynamic Programming – From Novice to Advanced illinois.edu -  Dynamic Programming  ;(Exercises are recommended) codechef.com -  Dynamic Programming geeksforgeeks.org -  Dynamic Programming  ;(Contains a lot of practice sessions) MIT OCW (Contains some Advanced topics as well) Dynamic Programming I Dynamic Programming II Dynamic Programming III Dynamic Programming IV Happy Learning...Thanks 😉😉😊😊  

Params in C#

C# HAS A FEATURE. IT CAN SPECIFY A METHOD PARAMETER WHICH WILL TAKE A VARIABLE NUMBER OF  ARGUMENTS. WE USED TO DO THOSE THINGS BY USING POINTER IN C++ C# MAKES OUR LIFE EASIER. ALL WE NEED IS A KEYWORD. AND THAT IS "PARAMS" LET'S HAVE THE FUN REMEMBER, THE PARAMS PARAMETER MUST BE A SINGLE ARRAY.   public class MyClass         {                          public static void amarmethod(params int[] amararrayerkichuelements)             {                                 for (int i = 0; i < amararrayerkichuelements.Length; i++)                 {                     Console.Write(amararrayerkichuelements[i] + " ");                 } ...