Fully understanding classes ...

This is a discussion on Fully understanding classes ... within the C++ Programming forums, part of the General Programming Boards category; I am working thru a C++ text (coming from VB ) and the following class is generating an error. I ...

  1. #1
    Unregistered
    Guest

    Unhappy Fully understanding classes ...

    I am working thru a C++ text (coming from VB) and the following class is generating an error. I have looked thru three different books and online tutorials and am still confused.

    #include <iostream.h>

    class Employee
    {
    public:
    int GetAge() const {return itsAge;}
    void SetAge(int age) {itsAge = age;}

    int GetYearsOfService() const {return YearsOfService;}
    void SetYearsOfService(int years) {YearsOfService = years;}

    int GetSalary() {return Salary;}
    void SetSalary(int salary) {Salary = salary;}

    private:
    int itsAge;
    int YearsOfService;
    int Salary;
    };

    int main()
    {
    Employee John;
    Employee Sally;

    John.SetAge(32);
    John.SetYearsOfService(5);
    John.SetSalary(50000);

    Sally.SetAge(32);
    Sally.SetYearsOfService(8);
    Sally.SetSalary(40000);

    cout << "John is " << John.GetAge << " years old.\n";
    cout << "He has been with the company for " << John.GetYearsOfService << " years.\n";
    cout << "John earns $" << John.GetSalary << " per year.\n\n";

    cout << "Sally is " << Sally.GetAge << " years old.\n";
    cout << "She has been with the company for " << Sally.GetYearsOfService << " years.\n";
    cout << "Sally earns $" << Sally.GetSalary << " per year.\n\n";

    return 0;
    }
    ----------------------------------------------------------------------
    Error (lines 34 thru 40):

    ex6-3.cpp(34) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
    ----------------------------------------------------------------------
    I've tried for two days to get it to work and I'm going nuts. Thanks for any help at all.

    Minh

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Your VB is shining through


    C++ does not explicitely specify a properties member (though you can implement them)

    Try John.GetAge()

  3. #3
    Unregistered
    Guest

    Thumbs up

    Thanks Ford ... I shoulda caught that one. The VC++ auto-complete doesn't put them in and I lost sight of the fact that they were methods.

    Minh

  4. #4
    jdinger
    Guest
    I wasn't aware that VC++ had auto-complete...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I wasn't aware that VC++ had auto-complete...
    It doesn't to my knowledge. VC++ has tooltips that give you semi-useful information about function parameters and the like. If it did have auto-complete then I would turn it off, the compiler and I often have different ideas about how things should be done.

    -Prelude
    My best code is written with the delete key.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The autocomplete just gives a dropdown with the relevant class members........

    And Minh is right.....member functions dont hve the ( & ) put on the end.....

  7. #7
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Fordy
    The autocomplete just gives a dropdown with the relevant class members........
    OK, that I knew about, just didn't realize it was called autocomplete. In VB it will add "()" to the end of a function, correct capitilization, etc.

    And I agree, Prelude, to turning it off if VC++ did have it.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    7

    Cool Now registered ...

    Thanks for the help, guys.

    I'm a manager in a small firm and I've programmed a little (professionally) in VB (desktop), ASP, and Cold Fusion over the years but felt I never really understood the concepts well. I think learning VB as a first language was a mistake. I'm sure most of you would agree

    So, even though I will rarely use it professionally, I wanted to take a stab at C++ to exercise my brain a little. While doing so I am attempting to ensure I REALLY understand each section/subject of the text I'm using before I go forward. I'm working on classes this week and I wanted to create and use a class with methods inside and outside the class definition, with and without constructors, inline and not, and split up the functionality across .cpp and .h files. To essentially define, declare, implement and instantiate elementary classes every way they can be used. And later, as new concepts are introduced in the book - inhertance, polymorphism, etc. - to add these variations, as well.

    Thanks again!

    Thich Minh Thong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes partial understanding
    By Boomba in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2004, 07:44 AM
  2. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  3. i need help understanding classes
    By jmprich in forum C++ Programming
    Replies: 1
    Last Post: 06-10-2003, 07:10 PM
  4. I'm having a really hard time understanding classes.
    By imortal in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2003, 01:02 PM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 01:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21