Problem with classes

This is a discussion on Problem with classes within the C++ Programming forums, part of the General Programming Boards category; I am new to programming and have to admit that I am lost on a simple assignment that can be ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Question Problem with classes

    I am new to programming and have to admit that I am lost on a simple assignment that can be fixed quickly but I am stumped and over looking the problem.
    Classes are definitely not my favorite, yet I know I must get use to them since they are in fact everywhere.
    Here is my problem:
    I have to create a Person class that includes fields for last name, first name, and zip code. Then I must include a default constructor that initializes last name, first name, and zip code to "X" if there are no arguements supplied as well as a display function. The main program has to instantiate and display two Person objects; one for default values and the other one for which you supply your own values.
    I am having the problem with the char.

    Here is the code:
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    class Person
    {
    private:
    	char lastName[15];
    	char firstName[15];
    	char zip;
    public:
    	Person();
    	void setValues(char lastName2[], char firstName2[], int zip2);
    	void displayValues();
    };
    Person::Person()
    {
    	lastName = "Jefferson";
    	firstName = "Thomas";
    	zip = "X";
    };
    void Person::displayValues()
    {
    	cout<<"The first name is "<<firstName<<"and the last name is "<<lastName<<endl;
    	cout<<firstName<<lastName<< " 's zip code is "<<zip<<endl;
    };
    void Person::setValues(char lastName2[], char firstName2[], int zip2)
    {
    	lastName = lastName2;
    	firstName = firstName2;
    	zip = zip2;
    };
    void main()
    {
    	Person adult;
    	cout<<"Before setting values with setValues()"<<endl;
    	adult.displayValues();
    	adult.setValues("Miller", "Bobby", 92807);
    	cout<<"After setting values with setValues()"<<endl;
    	adult.displayValues();
    	getch();
    };
    The errors do have to do with the char and again I am completely lost. Can someone help? Please recommend something to get this thing running error free...I'm lost

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    what are the errors that your compiler is flaggin??

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    zip should be char zip[6]. The way you have it declared it is a
    single char. Since a zipcode is 5 numbers you need a 6 char array
    to hold it and the null terminator at the end.

    Also, you mentioned that your default ctor needs to initiliaze the
    member vars to X if nothing is input, that would mean that you
    need to overload your constructor. One that takes no params
    (the default one) and one that accepts 3 const char arrays. You
    also mentioned that the default ctor would set all vars to X yet
    you only set the zip to X.

    Maybe something like this:

    Code:
    class Person
    {
      public:
        Person(); //default
        Person(const char *FName, const char *LName, const char *ZipCode);
    };
    
    //the ctor bodys
    Person::Person()
    {
        strcpy(lastname,"X");
        strcpy(firstname,"X");
        strcpy(zip,"X");
    }
    
    Person::Person(const char *FName, const char *LName, const char *ZipCode)
    {
       strcpy(lastname,LName);
       strcpy(firstname(FName);
       strcpy(zip,ZipCode);
    }
    Hope this helps.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    All of the errors are on the lines with char and are basically the same:

    cpp(17) : error C2440: '=' : cannot convert from 'char [7]' to 'char [15]'

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by crzy1
    All of the errors are on the lines with char and are basically the same:

    cpp(17) : error C2440: '=' : cannot convert from 'char [7]' to 'char [15]'
    Sorry I didn't even notice that you were trying to use the = operator. You can't do that with char arrays like that. Use strcpy(). Look it up here if you've never used it.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    also, Classes are a huge part of OOP. You will need to learn them to be successful not only at C++, but also other OOP languages. If you don't like them, either start, or learn C and/or other procedural langauges. Classes offer a lot of power.

    you do not need the ; after every fxn. only after classes, structs, and enumerated types.

    also, use int main. if you want to know why, do a search.

    edit: for classes and structs, fxn and constructor definitions are do not need ; after also.

  7. #7
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    fixed it..but i somehow freaked up on the destructor..sorry i just got home..i just went over your code like really fast...

    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    class Person
    {
    	private:
    		char* m_lastName;
    		char* m_firstName;
    		char* m_zip; //zip codes are usually 5 digits
    	public:
    		Person(char* last = "x", char* first = "x", char* zip = "x");
    		void setValues(char* lastName, char* firstName, char* zip);
    		void displayValues();
    		//~Person(); // destructor
    };
    
    Person::Person(char* last, char* first, char* zip)
    {
    	m_lastName = new char[15];
    	m_firstName = new char[15];
    	m_zip = new char[5];
    
    	strcpy(m_lastName, last);
    	strcpy(m_firstName, first);
    	strcpy(m_zip, zip);
    
    }
    void Person::displayValues()
    {
    	cout<<"The first name is "<<m_firstName<<" and the last name is "<<m_lastName<<endl;
    	cout<<m_firstName<< ' ' << m_lastName<< "'s zip code is "<<m_zip<<endl;
    };
    void Person::setValues(char* last, char* first, char* zip)
    {
    	delete [] m_lastName;
    	delete [] m_firstName;
    	delete [] m_zip;
    
    	m_lastName = new char[15];
    	m_firstName = new char[15];
    	m_zip = new char[5];
    
    	strcpy(m_lastName, last);
    	strcpy(m_firstName, first);
    	strcpy(m_zip, zip);
    };
    
    /*Person::~Person()
    {
    	delete [] m_lastName;
    	delete [] m_firstName;
    	delete [] m_zip;
    }*/
    int main() // int MAIN!
    {
    	Person adult;
    	cout<<"Before setting values with setValues()"<<endl;
    	adult.displayValues();
    	adult.setValues("Miller", "Bobby", "92807");
    	cout<<"After setting values with setValues()"<<endl;
    	adult.displayValues();
    
    	return 0;
    }
    fix the destructor...i somehow mess that up..also i didnt touched your int main() stuff..
    nextus, the samurai warrior

  8. #8
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by alpha
    also, Classes are a huge part of OOP. You will need to learn them to be successful not only at C++, but also other OOP languages. If you don't like them, either start, or learn C and/or other procedural langauges. Classes offer a lot of power.

    you do not need the ; after every fxn. only after classes, structs, and enumerated types.

    also, use int main. if you want to know why, do a search.
    Good catch, alpha. Man, I must be out of it tonight! I missed too
    many simple ones there.... **gives self 20 lashes across the back**..hehe.

    Seriously, crzy1, everything alpha mentioned is correct. void main() is not and never has been standard.

  9. #9
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    haha..i fixed it..my bad..i forget to dynamically allocated 6 characters for m_zip..forgot abou the '\0'...stupid me..here is the working source code

    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    class Person
    {
    	private:
    		char* m_lastName;
    		char* m_firstName;
    		char* m_zip; //zip codes are usually 5 digits
    	public:
    		Person(char* last = "x", char* first = "x", char* zip = "x");
    		void setValues(char* lastName, char* firstName, char* zip);
    		void displayValues();
    		~Person(); // destructor
    };
    
    Person::Person(char* last, char* first, char* zip)
    {
    	m_lastName = new char[15];
    	m_firstName = new char[15];
    	m_zip = new char[6];
    
    	strcpy(m_lastName, last);
    	strcpy(m_firstName, first);
    	strcpy(m_zip, zip);
    
    }
    void Person::displayValues()
    {
    	cout<<"The first name is "<<m_firstName<<" and the last name is "<<m_lastName<<endl;
    	cout<<m_firstName<< ' ' << m_lastName<< "'s zip code is "<<m_zip<<endl;
    };
    void Person::setValues(char* last, char* first, char* zip)
    {
    	delete [] m_lastName;
    	delete [] m_firstName;
    	delete [] m_zip;
    
    	m_lastName = new char[15];
    	m_firstName = new char[15];
    	m_zip = new char[6];
    
    	strcpy(m_lastName, last);
    	strcpy(m_firstName, first);
    	strcpy(m_zip, zip);
    };
    
    Person::~Person()
    {
    	delete [] m_lastName;
    	delete [] m_firstName;
    	delete [] m_zip;
    }
    int main() // int MAIN!
    {
    	Person adult;
    	cout<<"Before setting values with setValues()"<<endl;
    	adult.displayValues();
    	adult.setValues("Miller", "Bobby", "92807");
    	cout<<"After setting values with setValues()"<<endl;
    	adult.displayValues();
    
    	return 0;
    }
    nextus, the samurai warrior

  10. #10
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    No sweat, nextus. I think we're all slipping up a little bit tonight.

  11. #11
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    haha..i got a good reason..its 8 pm and i just got home..very tired...need my darn dinner..!!!
    nextus, the samurai warrior

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by jdinger
    No sweat, nextus. I think we're all slipping up a little bit tonight.
    I agree. I slip a bit and make mistakes too. That is what the forum is for.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    I don't know what the problem is with C and classees with me, but with any other languages, such as VB. net I have no problems.

    Thanks for the help!

    I have been told before not to use the void main but that is what I am taught in this class, to use the void main. So I can understand why not to but at the same time when your Professor is telling you what he wants, kind of hard not to use it out of habit.

    Thank you again.

  14. #14
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    It's unfortunate but there are a lot of C/C++ instructors who are clueless. I can understand if you have to use it to pass his class, but I'd at least recommend that you (respectfully) try to explain to him that it's not accepted (granted if and how you approach him is based on the particulars of the situation (like is he an egotistical jerk?)). Also, outside of class, I would use the proper int main() returning 0 on success.

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    C is procedural, so C does not have classes. The wonderful aspects of C++

    As for class, unless he is completely strict, I would just start using it. If he tells you not to, then don't; but start anyways.

    My teacher teaches void main; she knows it is supposed to be int, and she writes/wrote int main for other software, but for the purposes of class, I don't know why she teaches it other than saying that main doesn't return anything so you could write void.

    I just started writing int main in class, she doesn't really care.

    If you can't, then just start practicing it outside of class. Also, start practicing standards, i.e. like std::cout or using std::cout; or using namespace std;

    edit: She is saved because java is starting to be taught; since AP is in java next year.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 05:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21