kod C++, zasięg ::

0

Cześć :)

//: C15:AddingVirtuals.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Adding virtuals in derivation
#include <iostream>
#include <string>
using namespace std;

class Pet {
  string pname;
public:
  Pet(const string& petName) : pname(petName) {}
  virtual string name() const { return pname; }
  virtual string speak() const { return ""; }
};

class Dog : public Pet {
  string name;
public:
  Dog(const string& petName) : Pet(petName) {
      cout << Pet::name() << endl;
  }
  // New virtual function in the Dog class:
  virtual string sit() const {
    return Pet::name() + " sits";
  }
  string speak() const { // Override
    return Pet::name() + " says 'Bark!'";
  }
};

int main() {
  Pet* p[] = {new Pet("generic"),new Dog("bob")};
  cout << "p[0]->speak() = "
       << p[0]->speak() << endl;
  cout << "p[1]->speak() = "
       << p[1]->speak() << endl;
//! cout << "p[1]->sit() = "
//!      << p[1]->sit() << endl; // Illegal
} ///:~

Dlaczego literalnie musimy mu wskazać, którą funkcję wywołać, skoro dziediczymy po Pet?
Chodzi mi o to: Pet::name()

1

Dlatego, że jest to funkcja z klasy Pet. Jeżeli usuniesz

Pet::

to dostaniesz błąd: " no match for call to..."

1

tylko dlatego, że mamy atrybut o nazwie name?

1 użytkowników online, w tym zalogowanych: 0, gości: 1