Friend Function in C++

Image
Friend Function in C++ Welcome to C++ Programming. In this article, we will talk about friend Function and friend Class trying to understand what do they mean and why are they useful in C++ design process. These will be the module outline we will take examples of Matrix Vector Multiplication and Linked List, and finally will end with somenotes. As you know as usual the outline will be available on the left of your slide So, let us first introduce the basic notion of friend function. On the left you have the view of ordinary function. So the situation is like this, that I have a class myclass which as some private data, it has a constructor, and it has; I am sorry if just ignore this line. I have this function written outside of this class which is trying to do something with the class. What is it doing? It takes an object of the class by reference parameter call by reference, and then it tries to print the data element component of that objective. Now, what do we know, this is pr...

Multiple Inheritance Problem In C++


Ambiguity in Multiple Inheritance

                            Suppose we have 3 classes first class is A and second class is B and the last class is C which is derived from A and B.In A class we have one method void display() same for class B.

Code

                                class A
                                {
                                    public : 
                                                void display(){count<<"This Is Class A "<<endl;}
                                 };

                                 class B
                                {
                                    public : 
                                                void display(){count<<"This Is Class B "<<endl;}
                                 };

                                 class C :public A, public B
                                {
                                 };


Main Method

                                In main method make an object of class C and call the method display().This is the actually problem in multiple inheritance in c++ because the system will not know which display() method should execute.The execution of class A or class B.


 Code

                                   int main()
                                    {
                                         C obj;
                                          obj.display()   // here is the problem 
                                       }



Solution

                                            This problem is solved by scoperesolution operator with class name.


 Code Solution

                                   int main()
                                    {
                                         C obj;
                                          // obj.display()   // here is the problem 
                                         obj.A::display();
                                         obj.B::display();
                                         return 0;
                                       }

Learn Inheritance in C++




Comments

Popular posts from this blog

Friend Function in C++

Programming language in C++

How To Use Class and Object In C++