F
F
Fango2020-12-05 19:08:18
C++ / C#
Fango, 2020-12-05 19:08:18

Why won't the program compile?

#include <iostream>
using namespace std;

class A {
public:
  virtual void func(char a, char b) = 0;
};
class B : public A {
public:
  void func(char a, char b) {
    cout << a << b;
  }
  virtual void func1(char a, char b) = 0;
};
class D : public B {
public:
  void func(char a, char b) {
    cout << a;
  }
  void func1(char a, char b) {
    cout << b << a;
  }
  void func2(char a, char b) {
    cout << b;
  }
};
int main() {
  A a; B b; D d;
  char i = 'o', j = 'k';
  d.func1(i, j);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-12-06
@Fango

A pure virtual class is a class that has at least one pure virtual method ( = 0; the syntax is stupid, of course). This method has no body and therefore cannot be called.
Therefore, a purely virtual class cannot be instantiated, that is, an instance of this class cannot be created. А а;directly calls the constructor without parameters, in this place an instance of the class is created (the syntax is no less dibile). There is a contradiction. And so the program does not compile.
Therefore, variables cannot be of the type of purely virtual classes. But there can be links А&and pointers А*. The reference and pointer will refer to (point to) a child instance that can be created.

U
User700, 2020-12-05
@User700

Because at least A is a pure virtual class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question