In this book im reading about C++ it has gave me a task to create a small program using a class CAT and constructor and a destructor in right.
so it says meow
frisky is a cat who is 5 years old
meow
now frisky is 7 years old
but its giving me an error and i just cannot see what wrong with it.
if anyone could just look at it for me and try to tell me whats wrong with it id greatly appreciate it.
thanks in advance.
Code:
// Demonstrates declaration of constructors and
// destructor for the cat class
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int initialAge);
~Cat();
int GetAge();
int SetAge(int age);
void Meow();
private:
int ItsAge;
};
// constructor of Cat,
Cat::Cat(int initialAge)
{
ItsAge = initialAge;
}
Cat::~Cat()
{ // do nothing
}
// GetAge public accessor function
// returns value of ItsAge member
int Cat::GetAge()
{
return ItsAge;
}
// Definition of SetAge, public
// accessor function
void Cat::SetAge(int age)
{
// set member variable ItsAge to
// value passed in by parameter age
ItsAge = age;
}
// Definition of Meow method
// returns: void
// parameters: none
// action: prints "MEOW" on screen
void Cat::Meow()
{
cout <<"MEOW\n";
}
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat that is";
cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
cout << "Frisky is now";
cout << Frisky.GetAge() << " years old.\n";
system("PAUSE");
return 0;
}
the errors are
Code:
------ Build started: Project: constructor and destructor, Configuration: Debug Win32 ------
Compiling...
constructor and destructor.cpp
.\constructor and destructor.cpp(27) : error C2143: syntax error : missing ';' before '}'
.\constructor and destructor.cpp(27) : error C2761: 'Cat::~Cat(void)' : member function redeclaration not allowed
.\constructor and destructor.cpp(27) : error C2143: syntax error : missing ';' before '}'
.\constructor and destructor.cpp(27) : error C2059: syntax error : '}'
.\constructor and destructor.cpp(28) : error C2143: syntax error : missing ';' before '}'
.\constructor and destructor.cpp(28) : error C2059: syntax error : '}'
.\constructor and destructor.cpp(34) : error C2143: syntax error : missing ';' before '{'
.\constructor and destructor.cpp(34) : error C2447: '{' : missing function header (old-style formal list?)
.\constructor and destructor.cpp(42) : error C2556: 'void Cat::SetAge(int)' : overloaded function differs only by return type from 'int Cat::SetAge(int)'
.\constructor and destructor.cpp(14) : see declaration of 'Cat::SetAge'
.\constructor and destructor.cpp(42) : error C2371: 'Cat::SetAge' : redefinition; different basic types
.\constructor and destructor.cpp(14) : see declaration of 'Cat::SetAge'
.\constructor and destructor.cpp(66) : error C2264: 'Cat::SetAge' : error in function definition or declaration; function not called
Build log was saved at "file://c:\Documents and Settings\Reece.YOUR-E0367A1424\My Documents\Visual Studio 2005\Projects\constructor and destructor\constructor and destructor\Debug\BuildLog.htm"
constructor and destructor - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hugo.