|
-
July 31st, 2007, 09:14 AM
#16
Re: C++ Newbie - Derived Class Issue
 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
-
August 1st, 2007, 08:54 AM
#17
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|