C ++和Java中的虚拟函数 [英] Virtual Functions in C++ and Java

查看:154
本文介绍了C ++和Java中的虚拟函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于虚函数,发现,

VF用于继承类的多态性。



因此,如果一个类和派生类都有相同的函数名,VF将适当的函数绑定到函数调用。



如果所讨论的函数在基类中被指定为virtual,那么将调用派生类的函数。如果它不是virtual,基类的函数将被调用。



在Java中,默认情况下:所有函数都是虚拟C ++:非虚拟的,通过使用final,私人访问修饰符,而在C + +中使用虚拟关键字使一个功能虚拟。



根据上述理论,我写了代码:

  #include< iostream> 

class base {
public:
virtual void function1(){
std :: cout<BaseVirtual<< std :: endl;
}

void function2(){
std :: cout<<Base NonVirtual<< std :: endl;
}
};


class derieved:public base
{
public:
void function1(){
std :: cout<虚拟<< std :: endl;
}

void function2(){
std :: cout<<Derieved NonVirtual<< std :: endl;
}
};



int main()
{
base b1;
derieved d1;

b1.function1();
b1.function2();

d1.function1();
d1.function2();

}



现在基于这个事实,如果它的一个虚函数只有派生类函数被调用,我的上述程序的输出应该是:

  BaseVirtual 
Base NonVirtual
Derieved Virtual
Base NonVirtual

但是,它是:

  BaseVirtual 
基本非虚拟
衍生虚拟
衍生非虚拟

这当然是正确的。所以我的问题是输出完全违反语句如果有问题的函数在基类中被指定为virtual,那么派生类的函数将被调用。如果它不是虚拟的,基类的函数将被调用

  d1.function2 ); 


解决方案

并且只有当你有试图访问带有基类指针的派生类对象。



用你的例子: -

  #include< iostream> 

class base {
public:
virtual void function1(){
std :: cout<BaseVirtual<< std :: endl;
}

void function2(){
std :: cout<<Base NonVirtual<< std :: endl;
}
};


class derieved:public base
{
public:
void function1(){
std :: cout<虚拟<< std :: endl;
}

void function2(){
std :: cout<<Derieved NonVirtual<< std :: endl;
}
};



int main()
{
base * b1;
derieved d1;

b1 =& d1;

b1-> function1();
b1-> function2();
return 0;
}

输出: -

  Derieved Virtual 
Base NonVirtual


I have been reading about Virtual functions and found,

VF are used in polymorphism of an inherited class.

So , if a class and derived class both have the same function name, VF binds the appropriate function to the function call.

i.e. If the function in question is designated virtual in the base class then the derived class's function would be called. If it is not virtual, the base class's function would be called.

In Java by default: all functions are Virtual C++: Non-virtual and can be made Virtual in Java by using final, private access modifier while in C++ using Virtual keyword to make a function virtual.

Based on the above theory, i wrote the code:

#include <iostream>

class base{
    public : 
        virtual void function1(){
            std::cout<<"BaseVirtual"<<std::endl;
        }

        void function2(){
            std::cout<<"Base NonVirtual"<<std::endl;
        }
};


class derieved: public base
{
    public : 
        void function1(){
            std::cout<<"Derieved Virtual"<<std::endl;
        }

        void function2(){
            std::cout<<"Derieved NonVirtual"<<std::endl;
        }
};



int main()
{
    base b1;
    derieved d1;

    b1.function1();
    b1.function2();

    d1.function1();
    d1.function2();    

}

Now based on the fact, if its a virtual function then only derived class function is called, my output for the above program should be:

BaseVirtual
Base NonVirtual
Derieved Virtual
Base NonVirtual

however, it came out to be:

BaseVirtual
Base NonVirtual
Derieved Virtual
Derieved NonVirtual

which must be right of course. So my question is the output totally violates the statement If the function in question is designated virtual in the base class then the derived class's function would be called. If it is not virtual, the base class's function would be called. for the call:

  d1.function2();    

解决方案

Yes.. the role of virtual comes into picture if and only if you have are trying to access the derived class object with a base class pointer.

With you example:-

#include <iostream>

class base{
    public : 
        virtual void function1(){
            std::cout<<"BaseVirtual"<<std::endl;
        }

        void function2(){
            std::cout<<"Base NonVirtual"<<std::endl;
        }
};


class derieved: public base
{
    public : 
        void function1(){
            std::cout<<"Derieved Virtual"<<std::endl;
        }

        void function2(){
            std::cout<<"Derieved NonVirtual"<<std::endl;
        }
};



int main()
{
    base *b1;
    derieved d1;

    b1=&d1;

    b1->function1();
    b1->function2();    
    return 0;
}

output:-

Derieved Virtual
Base NonVirtual

这篇关于C ++和Java中的虚拟函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆