Re: Classes



"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:15874187-02B3-4914-B4A2-E13D3343D1C3@xxxxxxxxxxxxx
Hi there,

I am reviewing some C++ examples and trying them out in Visual C++
console application. Here is an example straight out of the book I am
reading. I feel a little discouraged because I am getting a
catastrophic amount of errors. Can someone please take look at this
code fragment:

#include <iostream>
using namespace std;

Class Cat
{
public:
Cat(int initialAge);
~Cat();
int getAge();
void setAge(int Age);
void Meow();
private:
int itsAge;
};

Cat::Cat(int initialAge)
{
itsAge = initialAge;
}

Cat::~Cat()
{
}

int Cat::getAge()
{
return itsAge;
}

void Cat::setAge(int age)
{
itsAge = age;
}
void Cat::Meow()
{
cout << "Meow.\n";
}

int main()
{
cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat who is ";
cout << Frisky.getAge() << " years old.\n";
Frisky.Meow();
Frisky.setAge(7);
cout << "Now Frisky is " ;
cout << Frisky.getAge(); >> " years old. \n";
return 0;
}


One error can spawn many error messages.

1. C++ is case sensitive. It is class Cat, not Class Cat. And, having
defined class Cat, you must declare objects of class Cat using the idenifier
Cat, not the identifier cat (i.e., it is Cat Frisky(5), not cat Frisky(5)).

2. Your second last line

cout << Frisky.getAge(); >> " years old. \n";

has a spurious semi-colon followed by a >> that should be the other way
around, i.e., <<.

When you get an error message, go to the line of the first error (and
possibly the line before). Solve one error at a time and you will often find
that there are less errors than may appear. You might also find that the
number of errors reported by the compiler increases at some points during
this process. This is not because there are more errors; it is because the
compiler is able to proceed further before giving up, so *finds* more
errors.


-- John Carson

.



Relevant Pages

  • Re: how to design an testing example
    ... int main ... void filecopy ... when I input command lines,for example:cat f:\test\1.cpp (windows xp ... The angrument which is f:\test\1.cpp donot have work,and output is cat ...
    (comp.lang.c)
  • Re: preprocessor/linker c++ error
    ... $ cat file1.cc ... void f1(int x,int y, int z) ... Novell's Directory Services is a competitive product to Microsoft's ...
    (Debian-User)
  • Re: how to design an testing example
    ... int main ... void filecopy ... The angrument which is f:\test\1.cpp donot have work,and output is cat ... A backslash character is ...
    (comp.lang.c)
  • Re: how to design an testing example
    ... int main ... void filecopy ... expected: cat f:\test\1.cpp ... Nick Keighley ...
    (comp.lang.c)
  • Re: Classes
    ... "Missing identifier before 'Cat' " should make you look carefully at what's ... > int getAge(); ... > void setAge; ... > int itsAge; ...
    (microsoft.public.vc.language)

Quantcast