CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Join Date
    Jun 2007
    Location
    Vercelli, Italy
    Posts
    87

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by STLDude
    Just an observation, but I'm always surprise to see code from students learning programming (specially C++) to use char/char[] rather then std::string. Why do professors still teach that in Universities?

    Last question was more rhetorical one.
    I can tell you why. Usually, the academic programming courses are about C, and OOP courses teach Java. When one starts using C++, nobody teaches him about the STL and the containers, so he goes on using null-terminated char arrays and pointers to arrays.
    I owe Paul McKenzie a pizza.

    I am no expert; but they say I can make concepts easy to understand. That's why newbies questions are mine!!! XD

  2. #17
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C++ Newbie - Derived Class Issue

    1. private is spelt P-R-I-V-A-T-E.

    2. In C++ strings are stored using std::string not arrays of char. They are also often passed around as const std::string & although they may be passed by value.

    3. Methods that do not change the state of the class are declared const.


    so

    Code:
    #include <string>
    
    class Employee
    {
    
    private:
      std::string name;
      std::string ssn;
      std::string empId;
      std::string hireDate;
    
    public:
      Employee();
      Employee
       (
            const std::string & social, const std::string & EmpId, 
            const std::string & HireDate, const std::string & Name
      ); 
    
      bool setSocial( const std::string & ]);
      void printSocial() const;
      bool setEmpId(const std::string &);
      void printEmpId() const;
      bool setHireDate( const std::string &);
      void printHireDate() const;
      bool setName( const std::string & );
      void printName() const;
    };
    Even now all you have is a few strings grouped together. Also printName() etc is not a good idea, preferable is getName() and then the caller can print the string if they wish.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured