Showing posts with label virtual. Show all posts
Showing posts with label virtual. Show all posts

Monday, February 18, 2013

Interview question on Virtual Function

Que. Can a methode in base class which is declared as virtual be called  using the base class pointer which is pointing to a derived  class object?
 


#include <iostream>
using namespace std;

class B
{
    public:
        virtual void foo()
        {
            cout << "\n its from B" << endl;
        }
};

class D: public B
{
    public:
         void foo()
        {
            cout << "\n its from D" << endl;
        }
};

int main()
{
    B * b = new D();
    b->B::foo();
    b->foo();   //here parent class access the function of child class
//  b->D::foo();  //error -->'D' is not a base of 'B'
    delete b;
    b = 0;

//    D * d = new B;  //error --> invalid conversion from 'B*' to 'D*'
    return 0;
}


OUTPUT:

See in C# also CLICK HERE

Sunday, January 13, 2013

Virtual Inheritance

Virtual inheritance:
                        Inheritance can be private (where even the public fields of the base class are treated as private), public (where the base class determines visibility), or virtual. With virtual inheritance there is only one copy of each object even if (because of multiple inheritance) the object appears more than once in the hierarchy. In the following example, derived1a and derived1b each contain a base object, butderived2 contains just one field.

#include <iostream>
using namespace std;

class base{
    public:
        int i;
};

class derived1a: virtual public base {
    public:
        int j;
};

class derived1b: virtual public base {
    public:
        int k;
};

class derived2: public derived1a, public derived1b{
    public:
        int product(){
            return i*j*k;
        }
};

int main()
{
    derived2 obj;
    obj.i = 10;
    obj.j = 3;
    obj.k = 5;
   
    cout << "\n product is " << obj.product() << '\n';
    return 0;
}

OUTPUT:

Tuesday, August 14, 2012

virtual inheritance and runtime polymorphism

// Using dot net and C#
using System;

public class a
{
    public void Hockey()
    {
        Console.WriteLine("A play hockey");
    }

    public virtual void education()
    {
        Console.WriteLine("MBBS");
    }

}
public class b:a
{
    public void cricket()
    {
        Console.WriteLine("B play cricket");
    }
    public override void education()
    {
        Console.WriteLine("MCA");
    }
}

class demo
{
    public static void Main()
    {
        a a1=new b();            //run time polymorphism
        a1.education();
        Console.ReadLine();
    }
}


OUTPUT:

To see in c++ CLICK HERE

Tuesday, July 17, 2012

code to understand Pure Virtual Function

#include <iostream>
using namespace std;

class Cat{
    public:
        int mAge;
        int mWeight;
       
        virtual void innoculate()=0;
};

class Persian: public Cat
{
    public:
        virtual void innoculate()

        {
            cout << "successful innoculated" << endl;
        }
};

int main()
{
        //Cat c2; gives error: because Cat is a abstract class
    Persian c1;
    c1.innoculate();
    return 0;
}


OUTPUT: