1: // Demonstrates compiler errors
2:
3:
4: #include <iostream.h> // for cout
5:
6: class Cat
7: {
8: public:
9: Cat(int initialAge);
10: ~Cat();
11: int GetAge() const; // const accessor function
12: void SetAge (int age);
13: void Meow();
14: private:
15: int itsAge;
16: };
17:
18: // constructor of Cat,
19: Cat::Cat(int initialAge)
20: {
21: itsAge = initialAge;
21: cout << "Cat Constructor\n";
22: }
23:
24: Cat::~Cat() // destructor, takes no action
25: {
26: cout << "Cat Destructor\n";
27: }
28: // GetAge, const function
29: // but we violate const!
30: int Cat::GetAge() const
31: {
32: return (itsAge++); // violates const!
33: }
34:
35: // definition of SetAge, public
36: // accessor function
37:
38: void Cat::SetAge(int age)
39: {
40: // set member variable its age to
41: // value passed in by parameter age
42: itsAge = age;
43: }
44:
45: // definition of Meow method
46: // returns: void
47: // parameters: None
48: // action: Prints "meow" to screen
49: void Cat::Meow()
50: {
51: cout << "Meow.\n";
52: }
53:
54: // demonstrate various violations of the
55 // interface, and resulting compiler errors
56: int main()
57: {
58: Cat Frisky; // doesn't match declaration
59: Frisky.Meow();
60: Frisky.Bark(); // No, silly, cat's can't bark.
61: Frisky.itsAge = 7; // itsAge is private
62: return 0;
63: }
Analysis: As it is written, this program doesn't compile. Therefore, there is no output.
This program was fun to write because there are so many errors in it.