首页 > 试题广场 >

下列程序编译时有语法错误的语句是() #includeio

[单选题]
下列程序编译时有语法错误的语句是()
#include<iostream>
#include<string>

using namespace std; 

class Pet {
     string name;
public:
     Pet(string p=" ") { name = p; }
     string getName() const { return name; }
     virtual void call() const=0;
};

class Dog : public Pet{
public:
    Dog(string n) : Pet(n) {}
    void call() const { cout<< "##" << " "; }
};

class Cat : public Pet{
public:
    Cat(string n) : Pet(n) {}
    void call() const { cout << "**"; }
};

void f(Pet *p) {
    p->call();
}
int main() { 
    Pet pet0("#");    //(1)
    Dog pet1("*");    //(2)
    Cat pet2("$");    //(3)
    f(&pet1);         //(4)
    f(&pet2);         //(5)
    return 0;
}
  • (1)
  • (2) (3)
  • (4) (5)
  • (5)

C++ 接口是使用抽象类来实现的,抽象类与数据抽象互不混淆,数据抽象是一个把实现细节与相关的数据分离开的概念。

如果类中至少有一个函数被声明为纯虚函数,则这个类就是抽象类。纯虚函数是通过在声明中使用 "= 0" 来指定的:

class Pet
{
     string name;
public:
     Pet(string p=" "){name=p;}
      string getName()const {return name;}
     virtual void call()const=0; // 在这里指定
}; 

Pet 是一个抽象类,所以不能用来实例化对象,只能作为接口被调用

发表于 2019-05-27 16:21:40 回复(1)

抽象类不允许实例化表示

发表于 2019-09-26 00:31:17 回复(0)
抽象类,不能用来实例化
发表于 2019-09-03 15:51:08 回复(0)
抽象类不能被实例化
发表于 2022-03-02 23:51:14 回复(0)
基类为抽象类,不能实例化只能被继承
发表于 2023-05-23 17:01:52 回复(0)