abstract base class and constructor

I know that using abstract base class to virtual member functions. When there are variable data in the abstract base class, I do not know how to write the constructor for derived constructor.

I have an exercise question that related to my problem.

output (should be like this):

I am an excellent dog and you may refer to me as Fido
I say woof
I am a cat and my name is Garfield
I say meow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

using namespace std;

class Pet
{
protected:
    string type;
    string name;
public:
    Pet(const string& arg1, const string& arg2);
    virtual void describe() const;
    virtual string speak() const = 0;
};
Pet::Pet(const string& arg1, const string& arg2): type(arg1), name(arg2)
{
}


class Dog : public Pet
{
public:
    Dog();
    void describe() const;  // override the describe() function
    string speak() const;
};

Dog::Dog():Pet(arg1, arg2)
{
}
void Dog::describe() const
{
    cout << "I am an excellent " << type << " and you may refer to me as " << name << endl;
}

string Dog::speak() const
{
    string a = "woof\n";
    return a;
}




class Cat : public Pet
{
public:
    Cat();
    void describe() const;
    string speak() const;
};

Cat::Cat():Pet(arg1, arg2)
{
}

void Cat:: describe() const
{
    cout << "I am a " << type << " and my name is " << name << endl;
}

string Cat::speak() const
{
    string a = "meow\n";
    return a;
}

ostream& operator<<(ostream& out, const Pet& p)
{
    p.describe();
    out << "I say " << p.speak();
    return out;
}

int main()
{
    Dog fido("dog","Fido");
    Cat garfield("cat","Garfield");
    Pet* ptr = &fido;
    cout << *ptr << endl;
    ptr = &garfield;
    cout << *ptr << endl;
}
Last edited on
77:26: error: no matching function for call to 'Dog::Dog(const char [4], const char [5])'
77:26: note: candidates are:
  28:1: note: Dog::Dog()
  28:1: note: candidate expects 0 arguments, 2 provided 

If you want to pass two arguments to a Dog's constructor, then Dog should have a constructor that accepts two arguments.
Line28: I change it to

Dog::Dog(string a, string b):Pet(a,b)
{}

The dog class is derived from pet class, and pet class has two data member which are type and name.

How can this not work?
"can not work"? I don't understand your question. Please explain.


I want to question an another detail of your code too:
Why do you create objects in main() like this?
Dog foo( "squid", "Eeyore");
If all Doq objects are dogs, then why the user can give them arbitrary type?
Thank you.

This is correct:
Dog::Dog(string a, string b):Pet(a,b)
{}

The problem is that I did not define the Pet::describe() function.
And I found my error in line12 that I need to have either {} or =0
Last edited on
Topic archived. No new replies allowed.