You are here:

C++/structclasses c++

Advertisement


Question
hi Sandra:



i am attempting to put together a code that for c++. here is my problem, i can figure out the struct code, but the class is a problem. i am not clear on public(classes). i clear on private. this is the code i have so far. i just don't what to code when it comes to public. pls help!  

class employee
}

private:
//data members(attributes)
int Age;
int zipCode;
double salary;
char name[20];
char city[20];
char state[25];

public: ?  

Answer
The public aspect of a class is just like a struct, anyone and everyone can access it:

class test {
public:
   int i;
};

int main()
{
   test t;

   t.i = 10; // This works because i is public
}

A struct is public by default, so if you know how a struct works then the public members of a class are simple:

struct test {
   int i;
};

int main()
{
   test t;

   t.i = 10; // This works because i is public by default
}

As for what to put in the public section of a class, you want to stick with member functions that work as an interface for the class. For example, class employee would probably have some form of print function to pretty print the private data without allowing clients to wantonly access it and possibly do bad things:

class employee {
private:
   // Data members(attributes)
   ...
public:
   void print();
};

void employee::print()
{
   // Print the employee data neatly
}

Most of the time you will want nontrivial constructors to be placed in the public section, as well as overloaded operators. What type of interface you use depends very much on how you intend to use objects of the class. Judging from your example, I would start with something like this and build on it (or trim from it in the case of the get/set functions):

class employee {
private:
   //data members(attributes)
   int Age;
   int zipCode;
   double salary;
   char name[20];
   char city[20];
   char state[25];
protected:
   int getAge();
   int getZip();
   double getSalary();
   char *getName();
   char *getCity();
   char *getState();
   int setAge();
   int setZip();
   double setSalary();
   char *setName();
   char *setCity();
   char *setState();
public:
   employee(char *name, int age, double salary);
   virtual ~employee();
   void setAddress(char *city, char *state, int zip);
   void prettyPrint();
};

Without knowing more about your class, I have to assume you will be deriving from it. Thus, a virtual destructor since child classes may have nontrivial destructors and an employee class is likely to be used polymorphically. Also, since I don't know how you intend to use the class, derived classes have restricted access to all of the data members through protected member functions. You should avoid get/set member functions whenever possible, I'm just covering all of the bases by doing it. Notice that derived classes and the outside world cannot access the private members directly and that the public interface is sparse.

I hope that helped answer your question.

-Sandra

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sandra Guth

Expertise

I can help with any questions concerning ISO standard C++, object oriented programming and design as well as algorithms and data structures.

Experience

I have nine years of programming experience with C++.

©2016 About.com. All rights reserved.