有关虚拟函数和抽象类的C++面试问题

2021年4月19日12:00:49 发表评论 722 次浏览

1.什么是纯虚函数?

回答:C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数, 我们仅对其进行声明。通过在声明中分配0来声明纯虚函数。请参见以下示例。

//An abstract class
class Test {
     //Data members of class
public :
     //Pure Virtual Function
     virtual void show() = 0;
  
     /* Other members */
};

2.什么是抽象类

回答:包含至少一个纯虚函数的类称为抽象类。看下面的例子

//An abstract class
class Test {
     //Data members of class
public :
     //Pure Virtual Function
     virtual void show() = 0;
  
     /* Other members */
};

在上面的示例中, Test是一个抽象类, 因为它具有纯虚函数。

有关抽象类的一些有趣事实

1)我们无法创建抽象类的对象。

//pure virtual functions make a class abstract
#include <iostream>
using namespace std;
  
class Test {
     int x;
  
public :
     virtual void show() = 0;
     int getX() { return x; }
};
  
int main( void )
{
     Test t;
     return 0;
}

输出:

Compiler Error: cannot declare variable 't' to be of abstract
 type 'Test' because the following virtual functions are pure 
within 'Test': note:     virtual void Test::show()

2.我们可以有抽象类类型的指针和引用。

例如, 以下程序可以正常运行。

#include <iostream>
using namespace std;
  
class Base {
public :
     virtual void show() = 0;
};
  
class Derived : public Base {
public :
     void show() { cout <<"In Derived \n" ; }
};
  
int main( void )
{
     Base* bp = new Derived();
     bp->show();
     return 0;
}

输出如下:

In Derived

3.如果我们不重写派生类中的纯虚函数, 则派生类也将成为抽象类。

下面的示例演示了相同的内容。

#include <iostream>
using namespace std;
class Base {
public :
     virtual void show() = 0;
};
  
class Derived : public Base {
};
  
int main( void )
{
     Derived d;
     return 0;
}

输出如下:

Compiler Error: cannot declare variable 'd' to be of abstract type 
'Derived'  because the following virtual functions are pure within
'Derived': virtual void Base::show()

3.该程序的输出是什么?

#include <iostream>
using namespace std;
class Test {
protected :
     int width, height;
  
public :
     void set_values( int a, int b)
     {
         width = a;
         height = b;
     }
     virtual int area( void ) = 0;
};
class r : public Test {
public :
     int area( void )
     {
         return (width * height);
     }
};
class t : public Test {
public :
     int area( void )
     {
         return (width * height /2);
     }
};
int main()
{
     r rect;
     t trgl;
     Test* ppoly1 = ▭
     Test* ppoly2 = &trgl;
     ppoly1->set_values(4, 5);
     ppoly2->set_values(4, 5);
     cout <<ppoly1->area();
     cout <<ppoly2->area();
     return 0;
}

输出如下:

2010

说明:

在此程序中, 我们正在计算矩形和

三角形通过使用抽象类。

4.该程序的输出是什么?

#include <iostream>
using namespace std;
class Base {
public :
     virtual void print() const = 0;
};
class DerivedOne : virtual public Base {
public :
     void print() const
     {
         cout <<"1" ;
     }
};
class DerivedTwo : virtual public Base {
public :
     void print() const
     {
         cout <<"2" ;
     }
};
class Multiple : public DerivedOne, DerivedTwo {
public :
     void print() const
     {
         DerivedTwo::print();
     }
};
int main()
{
     Multiple both;
     DerivedOne one;
     DerivedTwo two;
     Base* array[3];
     array[0] = &both;
     array[1] = &one;
     array[2] = &two;
     for ( int i = 0; i <3; i++)
         array[i]->print();
     return 0;
}

输出如下

212

说明:在此程序中, 我们将根据数组中给出的条件执行这些操作。因此它打印为212。

5.该程序的输出是什么?

#include <iostream>
using namespace std;
class sample {
public :
     virtual void example() = 0;
};
class Ex1 : public sample {
public :
     void example()
     {
         cout <<"lsbin" ;
     }
};
class Ex2 : public sample {
public :
     void example()
     {
         cout <<" is awesome" ;
     }
};
int main()
{
     sample* arra[2];
     Ex1 e1;
     Ex2 e2;
     arra[0] = &e1;
     arra[1] = &e2;
     arra[0]->example();
     arra[1]->example();
}

输出如下:

lsbin is awesome

说明:在此程序中, 我们将合并两个类中的两个语句, 并使用抽象类进行打印。

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: