#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0

    Need desperate help - trying to learn classes!


    Hey guys,

    I am just starting a class (Data Strucures using C++ w/STL) and I am so lost here. I made it thru Introduction to C++ with a 4.0, but had a very difficult (in an unhelpful way) teacher and I left feeling like I really knew nothing and now its showing.

    Anyway, I would love to have some help if some of you guys could spare it. I have an assignment that I will list below - I have got most (maybe all) of the header files complete, but need help to develop the actual .cpp. I am just so lost right now and the teacher is so little help (enter bad memories from C++). On top of this there are no real examples and the chapter was just an introductory type thing - no explanation towards something like the assignment.

    Here is the assignment --------

    Create a C++ class called Employee using the external code which will implement the Employee constructor. Remember that once the Name of the Employee is entered, the Address of the Employee is entered, then it is a matter of adding a SSN to create an employee.

    Here what the Name Header file (Name.h) will have:
    1- a default constructor
    2- 3 private string instance variables for :
    First Name, Middle Name, and a last Name
    3- getFirstLast function: it returns the first
    and middle name and last name in order
    4- print function: it prints the first, middle, and last name.

    Here what the Address Header File (Address.h) will have:
    1- a default constructor
    2- 4 private string instance variables for :Street, City, State, Zip
    3- getCity: it returns the City
    4- getState: It returns the State
    5- getStreet: It returns the street
    6- getZip: it returns the zip
    7- print function: it prints the Street, City, State, and Zip.

    Here what the Employee Header File (Employee.h) will have:
    1- 3 private instance variables:
    One for Name (Use the Header Name from above),
    One for The Address (Use the Address Header from above),
    a string variable for the SSN.
    2- a default constructor which initializes the
    SSN to "999-99-9999", Name to "John H. Doe",
    Address to "99999 Sunset Boulevard",
    "Beverly Hills", "CA", "99999"
    2- a constructor with 3 parameters
    3- getName function: it returns the Name of the Employee
    4- getAddress function : it returns the address of the Employee.
    5- getSSN function: it returns the SSN as a string
    6- print function: Prints the name, prints the Address,
    prints the SSN.

    Implement the Employee class using the constructor as an external function:
    1- Remember that the default constructor for Employee
    has the following initial values:
    SSN to "999-99-9999", Name to "John H. Doe",
    Address to "99999 Sunset Boulevard" ,
    Beverly Hills, CA, 99999
    2- Remember that the default constructor for Address
    has the following initial values:
    Address to "99999 Sunset Boulevard" ,
    "Beverly Hills", "CA", "99999"
    3- Remember that the default constructor for Name has
    the following initial values:
    Name to "John H. Doe",

    In the void main() function you would declare:
    a Name n;
    an Address A;
    and an Employee e;
    and print the values of each.
    Remember to email using the internal email of the class the followings:

    Name.h
    Address.h
    Employee.h
    Employee.cpp

    Remember to compile the Employee.cpp file and execute it to verify the output:

    Name:

    John H. Doe

    Address:

    9999 Sunset Boulevard" , Beverly Hills, CA, 99999 (Address)

    Employee:

    John H. Doe

    99999 Sunset Boulevard" , Beverly Hills, CA, 99999

    xxx-xx-xxxx

    -----------------

    Here are my header files -----------


    // Name.h - Mike Oliver

    #ifndef NAME_H
    #define NAME_H

    #if _MSC_VER > 1000
    #pragma once
    #endif

    #include "NAME.H"
    #include "ADDRESS.H"
    #include "EMPLOYEE.H"
    #include <iostream>
    #include <iomanip>
    #include <string>

    using namespace std;

    class NAME

    {

    public:
    NAME()
    {
    Name_First = "John";
    Name_Middle = "H.";
    Name_Last = "Doe";
    }

    string NAME::getFirstLast();

    void NAME::prtName();

    ~NAME(){;}

    string NAME::getFirstLast();
    void NAME::prtName();


    private:
    string Name_First;
    string Name_Last;
    string Name_Middle;
    };

    //************** NAME CLASS **********************

    #endif

    -----------------

    //Address.h

    #ifndef ADDRESS_H
    #define ADDRESS_H

    #if _MSC_VER > 1000
    #pragma once
    #endif


    #include "ADDRESS.H"
    #include "EMPLOYEE.H"
    #include "NAME.H"
    #include <iostream>
    #include <iomanip>
    #include <string>

    using namespace std;

    class ADDRESS

    {

    public:
    ADDRESS()
    {
    Street = "99999 Sunset Boulevard";
    City = "Beverly Hills";
    State = "CA";
    Zip = "99999";
    }
    ~ADDRESS(){;}

    string ADDRESS::getCity();
    string ADDRESS::getState();
    string ADDRESS::getStreet();
    string ADDRESS::getZip();
    void ADDRESS::prtAddress();

    private:
    string Street;
    string City;
    string State;
    string Zip ;


    };

    //************** ADDRESS CLASS **********************

    #endif

    -----------------

    //Employee.h

    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H

    #if _MSC_VER > 1000
    #pragma once
    #endif

    #include "ADDRESS.H"
    #include "NAME.H"
    #include "EMPLOYEE.H"
    #include <iostream>
    #include <iomanip>
    #include <string>

    using namespace std;

    class EMPLOYEE

    {

    public:

    //default constructor to initialize variables
    EMPLOYEE()
    {
    SSN = "999-99-9999";
    Name = "John H. Doe";
    Address = "99999 Sunset Boulevard, ";
    Address += "Beverly Hills, ";
    Address += "CA, ";
    Address += "99999";
    }

    //parameter constructor
    EMPLOYEE(string SS, string n, string a)
    {
    SSN = SS;
    Name = n;
    Address = a;
    }

    ~EMPLOYEE(){;}
    string EMPLOYEE::getName();
    string EMPLOYEE::getAddress();
    string EMPLOYEE::getSSN();
    void EMPLOYEE::prtEmployee();

    private:
    string Name;
    string Address;
    string SSN;

    };

    //************** EMPLOYEE CLASS **********************

    #endif

    -----------

    PLEASE HELP!!!!

    Mike
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    USA East Coast
    Posts
    0
    Rep Power
    0

    Bruce Eckel has written some really nice books online


    http://www.mindview.net/

    C++ book
    http://www.mindview.net/Books/TICPP/...ngInCPP2e.html


    I wouldn't really worry too much about C++, You should be learning JAVA. Nothing needs to be programmed in C++ besides Video Games (speed purposes). If it cant be done in JAVA, it should be done in C, and if it cant be done in C use assembly. Otherwise one should wonder if what your trying to do is really worth doing.

    If your looking for an easy way out, drop the class. Set a personal goal to program something in C++ on your own time so that you actually retain what you learn. Without repetition you wont retain anything your learning. Then take the course when you are more experienced with C++.

    If this is college they should be there to help you learn. Go to the teacher and ignore your feelings that your are stupid and keep bugging him until you understand. Camp outside his door if need be. You are only truly stupid if you believe bugging the teacher until you learn is stupid. Ask the Dean if there are any support programs for learning, and complain that the teacher inst helping you learn and that you wont pass unless you can figure out what it is that your lacking.

    We could help you through the assignment, but in the end you are wasting your time. You would be better off spending it on something that will benefit your learning in a productive manner. I am guessing your taking this class to learn?

    -S. Bob
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0
    Thanks for the reply. Actually I am taking Java right now as well. I don't have a choice on whether or not to take C++, its required for the degree. I took Intro to C++ last semester and this is data structures using C++.

    I have emailed the teacher and he is seeming to help. We'll just see how it goes.

    Thanks again!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13

    Re: Bruce Eckel has written some really nice books online


    Originally posted by Soldier Bob
    I wouldn't really worry too much about C++, You should be learning JAVA. Nothing needs to be programmed in C++ besides Video Games (speed purposes). If it cant be done in JAVA, it should be done in C, and if it cant be done in C use assembly. Otherwise one should wonder if what your trying to do is really worth doing.
    First off, if you have a chance to learn a new language, go all out. You never know - you might need it some day.

    Secondly, Java is just too slow for the desktop. Any GUI written in Java is mind-numbingly slow. On Web pages, applets take forever to load. IMHO, Java has failed thus far in the desktop environment and should be confined to server applications.

    Thirdly, do you know how much legacy C++ code is out there? C++ will be a desired skill for a long time, so failing to learn it now will only hurt him.

    The Java programming language is great in concept (write once, run anywhere), but mediocre at best in implementation. Many enterprise-level applications have been, and will continue to be, written with C++. As machine speeds get faster, hopefully the lag time created by JITting will become negligable, and the gap will close between C++ and Java. Only then will Java (and Java-pretenders like C# and Visual J#) become a true force in the marketplace.
    Jon Sagara

    "Me fail English? That's unpossible!"
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    USA East Coast
    Posts
    0
    Rep Power
    0

    is JAVA really that slow??


    jonsagara: Well, keeping up with the processor speeds, they are talking about making 64 bit processors main stream in the near future. Not to mention the current speed war with AMD and Intel. Speed is irrelevant, if you have to spend 10 hours a week maintaining code that has bugs because it wasnt programmed to good design specs. (which happens more often in the real world then not)

    The money you save on maintenance is far more then the cost of buying a new server to keep up the speeds. IMO.

    But in any event, the whole "JAVA is slow" is more of a anti JAVA idea that keeps floating around. Have you ever looked into native compilers? From what I understsand there is negligable difference afterwards. If you mean JAVA running on windows... well that in and of it self is a interesting topic. MS is in competition against Sun, so I wouldnt expect any good performance on that OS, ever. MS tried to kill JAVA before it ever began if my computer history is correct.

    Dont get me wrong, I think that learning C++ is a great. And that knowing every programming language will make you a better programmer. However, one really has to know what they like, and personally, I wouldn't code in COBOL if you paid me. Maintaining another persons code is about the most annoying and frustrating task period. Unless your the lucky person who had a programmer who actually stuck to the proper coding guidelines and actually wrote "documentation", that IS useable afterwards.

    What you write in JAVA today can be used 10 years if not 30 years from now, without loosing any of its usefulness. A code language shouldnt be defined by its optimizations, but by its ability to allow one to write code that is code. Not optimizations to make something run faster. or interface with another system.

    -S. Bob
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0
    Ok guys, timeout, I appreciate the comments and all, but I have to take both C++ and Java because the degree requires it, and secondly because I just want to know them both. I have no clue as to which is more powerful, faster, or flexible. Right now I just want to learn them - which brings me to my original request.

    Could someone please look at one of the classes that I posted above and tell me what is missing. I am just having a little trouble understanding this whole concept.

    For instance in the following:

    class NAME

    {

    public:
    NAME()
    {
    Name_First = "John";
    Name_Middle = "H.";
    Name_Last = "Doe";
    }
    ~NAME(){;}

    string NAME::getFirstLast();
    void NAME::printName();

    private:
    string Name_First;
    string Name_Middle;
    string Name_Last;
    };

    --------------------------------

    I believe I should have something like this:

    --------------------------------

    public:
    NAME(string Name_First, Name_Middle, Name_Last)
    {
    Name_First = "John";
    Name_Middle = "H.";
    Name_Last = "Doe";
    }

    --------------------------------

    Is this right? And secondly we declared a couple of functions - whre are they to be coded - after the private variable declarations or in the public section or somewhere else entirely. It seems I have seen it every which way.

    Thanks guys!
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0
    Ok guys, me again! I have worked through all the syntax errors - some of this code was provided from different sources. I have it down to these two compile errors:

    Compiling...
    Employee.cpp
    employee.cpp(16) : error C2661: 'NAME::NAME' : no overloaded function takes 3 parameters
    employee.cpp(17) : error C2661: 'ADDRESS::ADDRESS' : no overloaded function takes 4 parameters
    Error executing cl.exe.

    Employee.obj - 2 error(s), 0 warning(s)

    -----------------------

    Any comments would be greatly appreciated! Secondly, I still don't know where to code the functions themselves. - Thanks!
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13

    Re: is JAVA really that slow??


    jonsagara: Well, keeping up with the processor speeds, they are talking about making 64 bit processors main stream in the near future. Not to mention the current speed war with AMD and Intel. Speed is irrelevant, if you have to spend 10 hours a week maintaining code that has bugs because it wasnt programmed to good design specs. (which happens more often in the real world then not)

    The money you save on maintenance is far more then the cost of buying a new server to keep up the speeds. IMO.
    64-bit processors are coming out real soon. Sure. I've heard that for the past 3 years. Anyway, you're missing the point of the "slow" argument. From a usability standpoint, Java sucks. Even on my Dual 1.7 GHz Xeon system, any GUI written in Java is painfully slow compared to a C/C++/MFC GUI. This is not even to mention the fact that the swing components look like some cheap knock off of standard windows UI components.

    But in any event, the whole "JAVA is slow" is more of a anti JAVA idea that keeps floating around. Have you ever looked into native compilers? From what I understsand there is negligable difference afterwards. If you mean JAVA running on windows... well that in and of it self is a interesting topic. MS is in competition against Sun, so I wouldnt expect any good performance on that OS, ever. MS tried to kill JAVA before it ever began if my computer history is correct.
    It keeps floating around because Java *is* slow. :D You can't deny the facts, my friend. As far as Java not running well on Windows, well, Sun really shot themselves in the foot there. MS wrote a great JRE fully optimized for Windows. This scared the sh*t out of Sun, so they sued Windows to halt anymore development of the Java language on Windows and fully restricted them to using an old JRE that sun wrote. Even if they wanted to they couldn't ship a newer JRE. Why do you think they opted not to include it in Windows XP? (As I understand it, this will not be the case in XP sp1 and onward) Their hands are tied - Sun actually stunted the development of Java on Windows. It's their own bloody fault.

    Dont get me wrong, I think that learning C++ is a great. And that knowing every programming language will make you a better programmer. However, one really has to know what they like, and personally, I wouldn't code in COBOL if you paid me. Maintaining another persons code is about the most annoying and frustrating task period. Unless your the lucky person who had a programmer who actually stuck to the proper coding guidelines and actually wrote "documentation", that IS useable afterwards.
    Yes, you are correct, maintenance programming *sucks.* I have done it before and I hate it. But please realize that new programs are written in C++ all over the industry, not just in Java.

    What you write in JAVA today can be used 10 years if not 30 years from now, without loosing any of its usefulness. A code language shouldnt be defined by its optimizations, but by its ability to allow one to write code that is code. Not optimizations to make something run faster. or interface with another system.
    That is a very optimistic attitude toward language design. :)
    Jon Sagara

    "Me fail English? That's unpossible!"
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13
    Originally posted by moliver
    Compiling...
    Employee.cpp
    employee.cpp(16) : error C2661: 'NAME::NAME' : no overloaded function takes 3 parameters
    employee.cpp(17) : error C2661: 'ADDRESS::ADDRESS' : no overloaded function takes 4 parameters
    Error executing cl.exe.

    Employee.obj - 2 error(s), 0 warning(s)
    Those errors mean that you're passing in too many arguments or too few arguments to those respective functions. Look at where you use those functions and fix them to use the correct # of arguments.
    Jon Sagara

    "Me fail English? That's unpossible!"
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Jackson, TN, USA
    Posts
    18
    Rep Power
    0
    Well, according to my instructor I don't have the proper constructor. I have a default constructor in the public declaration (as you can see from the Name.h code above), but he says I need a general constructor after the public/private sections. Well, I have tried several ways to code one using a very large example header file he sent me to look at and can't get anywhere with it?

    I tried this:

    NAME::NAME(string Name_First, string Name_Middle, string Name_Last);

    it gives me:
    error C2244: 'NAME::NAME::NAME' : unable to resolve function overload

    I tried a few other things, but apparently can't figure out the syntax.

    Thanks for the help!
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13
    Instead of

    Code:
    NAME::NAME(string Name_First, string Name_Middle, string Name_Last);
    try

    Code:
    NAME(string Name_First, string Name_Middle, string Name_Last);
    IOW,

    Code:
    class NAME
    {
    // your other declarations...
    NAME(string Name_First, string Name_Middle, string Name_Last);
    // other stuff ...
    };
    Don't forget that if you don't define the constructor inline, you have to define this constructor elsewhere. It would look something like this:

    Code:
    NAME::NAME(string Name_First, string Name_Middle, string Name_Last)
    {
    // Your function code here
    }
    Jon Sagara

    "Me fail English? That's unpossible!"

IMN logo majestic logo threadwatch logo seochat tools logo