classes problem

This is a discussion on classes problem within the C++ Programming forums, part of the General Programming Boards category; I have written this program to test out my newly gained knowledge on classes, but my compiler (Dev-C++... soon getting ...

classes problem

I have written this program to test out my newly gained knowledge on classes, but my compiler (Dev-C++... soon getting MSVC++) says that I havn't declared to variables inside the class. To help you understand my problem, here are the errors....

Code:

c:\mydocu~1\people.cpp: In function `void outPutCiv()':
c:\mydocu~1\people.cpp:38: `name' undeclared (first use this function)
c:\mydocu~1\people.cpp:38: (Each undeclared identifier is reported only once
c:\mydocu~1\people.cpp:38: for each function it appears in.)
c:\mydocu~1\people.cpp:40: `creditCard' undeclared (first use this function)
c:\mydocu~1\people.cpp:42: `extra' undeclared (first use this function)

I didn't examine it closely, but
name.person
should be
person.name
You declare the object person of class civillian, so the class data a/o functions are on the right of the dot. Same with creditcard.person, etc.
I don't know if it's required, but you might want to include a default constructor as well.

First of all your BIGGEST mistake is by not defining a default constructor. This can and will lead to hard to find problems. Also you could make a class Person and derive the others classes from it. Anytime you have classes with similarities such as this it is a good practice to use inheritance. Will check your code in a second but this is what was really standing out.

OK I just ran your code and seem some room for inprovement.
1.)make your member variables protected. This gives you control over what is allowed to changeyour data and how.

2.)use your classes to their ful extent. Like I stated before use inheritance to simplify your code.

3.)the output function in main should be a member function of class Person(see note 2 above). By using the this pointer your class will take care of it's own input and output.

4.)the info functions is an excellent canidate for a member function. Same reason as above.

5.)Your function calls to info are all wrong(no offense). The proper way would be to(if not made a member function of person) pass the function the address of a Person and let it do the operations from there.

If you lookl at your code, you can see that person, person1 and person2 are defined in the funcion main(), this means that the methods civInfo(), govInfo() & milInfo() knows nothing about them.
But, since they are methods of an object, they can simply access its memebers, so in these methods, you should simply use "name" instead of "person.name". That's the idea of OO programming.

two more notes:
1. you might want to derive those three classes from one common class that holds common data members. This base class would then have a virtual Info)( method, that would be implemented differently by each of the derived classes, and then main() function would call the same function for different objects of different classes.
2. in the switch statement in the main function, since you declare variables in the various cases, you might want to enclose the entire block after each case with braces - {} - to avoid compiler errors and warnings.