Need help with code
Nov 20, 2018 at 3:47pm UTC
Hello everyone I am in need of assistance for this program that I am writing. When I run the program I receive 8 errors 4 of which are claiming lines 35,37,57, and 69 are inaccessible and the other 4 for lines 83,85,87,99 are saying cannot access private member. Any help would be greatly appreciated.
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 85 86 87 88 89 90
#include "pch.h"
#include <iostream>
using namespace std;
class Animal
{
public :
Animal() {
cout << "Animal constructor executing...\n" ;
}
~Animal() {
cout << "Animal destructor...\n" ;
}
void communicate() {
cout << "Speak." << endl;
}
};
class Dog : public Animal {
Dog() {
cout << "Dog constructor executing...\n" ;
}
~Dog() {
cout << "Dog destructor executing...\n" ;
}
void communicate() {
cout << "Woof!" << endl;
}
};
class Cat : public Animal {
Cat() {
cout << "Cat constructor executing...\n" ;
}
~Cat() {
cout << "Cat destructor executing...\n" ;
}
void communicate() {
cout << "Meow!" << endl;
}
};
int main() {
Animal genericAnimal;
genericAnimal.communicate();
Dog ralph;
ralph.communicate();
Cat fluffy;
fluffy.communicate();
return 0;
}
Nov 20, 2018 at 4:03pm UTC
Default access for a class in c++ is private - so it won't even be able to access the constructors.
Put
public :
as the first statement in each of your dog and cat classes and you get this witty conversation:
Animal constructor executing...
Speak.
Animal constructor executing...
Dog constructor executing...
Woof!
Animal constructor executing...
Cat constructor executing...
Meow!
Cat destructor executing...
Animal destructor...
Dog destructor executing...
Animal destructor...
Animal destructor...
Nov 21, 2018 at 12:00am UTC
Thank you very much felt like bagging my head against the wall for something this simple thank you again!
Topic archived. No new replies allowed.