Problems with classes... just starting out w/ them.

This is a discussion on Problems with classes... just starting out w/ them. within the C++ Programming forums, part of the General Programming Boards category; I cannot figure out the proper way to implement classes, I have just begun learning about them and I am ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Unhappy Problems with classes... just starting out w/ them.

    I cannot figure out the proper way to implement classes, I have just begun learning about them and I am trying to wrtie a program to take a first name, last name, social security number, birth year, birth month, and birth day, and return them... here's what I have..

    Code:
     
    // person.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include "person.h"
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void Get_Input (char* name, char* lname, char* soc){
    	cout <<"Input a name ";
    	cin >> name;
    	cout <<"Input a last name ";
    	cin >> lname;
    	cout <<"Input a SSN (9 digits) ";
    	cin >> soc;
    }
    
    int main (void)
    {
    	char* name;
    	char* lname;
    	char* soc;
    
    	Person::SetfName(name);
    	name = Person::GetfName;
    	cout << name << endl;
    }

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Code:
    //person_functs.cpp
    
    #include "stdafx.h"
    #include "person.h"
    
    Person::Person (char* fName, char* lName, char* SSN, int Birth_Year, 
    				int Birth_Month, int Birth_Day){
    	fName = "";
    	lName = "";
    	SSN = "000000000";
    	Birth_Year = 0000;
    	Birth_Month = 00;
    	Birth_Day = 00;
    	}
    
    
    
    
    int Person::findLength (){
    	int len = 0;
    	char *s=value;
    	for (;s!='\0'; s++, len++);
    	return len;
    	}
    
    bool Person::validateSSN (char* soc){
    		value = soc;
    		Length = findLength();
    		if (Length < 9 && Length > 9)
    			return false;
    		else if (Length == 9)
    			return true;
    	}
    void Person::SetfName (char* first){
    	fName = first;
    }
    
    void Person::SetlName (char* last){
    	lName = last;
    }
    void Person::SetSSN (char* soc){
    	SSN = soc;
    }
    void Person::SetBirth_Year (int byr){
    	Birth_Year = byr;
    }
    void Person::SetBirth_Month (int bmo){
    	Birth_Month = bmo;
    }
    void Person::SetBirth_Day (int bdy){
    	Birth_Day = bdy;
    }
    char* Person::GetfName (){
    	return fName;
    }
    char* Person::GetlName (){
    	return lName;
    }
    char* Person::GetSSN (){
    	return SSN;
    }
    int  Person::GetBirth_Year (){
    	return Birth_Year;
    }
    int Person::GetBirth_Month (){
    	return Birth_Month;
    }
    int Person::GetBirth_Day (){
    	return Birth_Day;
    }

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Code:
     
    #ifndef Person_H
    #define Person_H
    class Person{
    private:
    	char *fName;
    	char *lName;
    	char ........N;
    	int Birth_Year;
    	int Birth_Month;
    	int Birth_Day;
    	int Length;
    	char* value;
    	
    	int findLength ();
        Person (char *fName, char *lName, char ........N, int Birth_Year, 
    				int Birth_Month, int Birth_Day);
    public:
    
    	
    bool validateSSN (char *soc);
    void SetfName (char* first);
    void SetlName (char* last);
    void SetSSN (char* soc);
    void SetBirth_Year (int byr);
    void SetBirth_Month (int bmo);
    void SetBirth_Day (int bdy);
    
    char* GetfName ();
    char* GetlName ();
    char* GetSSN ();
    int GetBirth_Year ();
    int GetBirth_Month ();
    int GetBirth_Day ();
    };
    #endif;
    The errors I am getting are:
    d:\Visual Studio Projects\person\person.cpp(28): error C2352: 'Person::SetfName' : illegal call of non-static member function

    d:\Visual Studio Projects\person\person.cpp(29): error C2440: '=' : cannot convert from 'char *(__thiscall Person::* )(void)' to 'char *'
    There is no context in which this conversion is possible

    d:\visual studio projects\person\person_functs.cpp(31): warning C4715: 'Person::validateSSN' : not all control paths return a value

    any suggestions / help will be greatly appreciated, as I really want to master this concept!

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    First, your GetInput function could be a lot simpler:

    Code:
    void Get_Input (Person &tmp){
    	char name[25];
    	char lname[25];
    	char soc[25];
    	cout <<"Input a name ";
    	cin >> name;
                    tmp.SetfName(name);
    	cout <<"Input a last name ";
    	cin >> lname;
                    tmp.SetlName(lname);
    	cout <<"Input a SSN (9 digits) ";
    	cin >> soc;
                    tmp.SetSSN(soc);
    }
    At least I'm sure that's how it should go. You might need to change the '.' to '->' so tmp->SetfName(name); etc.

    The first error you are getting is because you aren't using objects correctly. Once you make a class of Person, it's like another variable. So you need lines of code like these:

    Code:
    Person someFirstPerson;
    //using the function from above:
    Get_Input(someFirstPerson);
    And that should make up most of your main function.

    Code:
    name = Person::GetfName;
    This is invalid, Person::GetfName(). You are trying to use the declaration of the function. After declaring, do this:
    Code:
    Person someFirstPerson;
    Get_Input(someFirstPerson);
    tmpName = someFirstPerson.GetfName();
    To fix the warning, change this:
    Code:
    bool Person::validateSSN (char* soc){
    		value = soc;
    		Length = findLength();
    		if (Length < 9 && Length > 9)
    			return false;
    		else if (Length == 9)
    			return true;
    	}
    to
    Code:
    bool Person::validateSSN (char* soc){
    		value = soc;
    		Length = findLength();
    		if (Length == 9)
    			return true;
    		else
    			return false;
    	}
    When using if/else checks, the first if should check what is most likely to return true (this actually doesn't effect most computers because they're so fast now), and you need to learn to make your checks more efficient. Also, you need to double check your if statement. You are trying to see if Length is less than 9 AND if Length is greater than 9. You should use the OR ( two pipes, |||), or just use the code I gave.
    Last edited by neandrake; 03-03-2004 at 07:06 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    how do you make a "default" constructor? I am sooo lost right now... I tried this:
    Code:
     
    int main (void)
    {
    	Person name;
    	Person lname;
    	Person soc;
      
    	Person GetfName();
    	name = GetfName();
        Get_Input (name);
    	
    }

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    No, no, no.

    You create one object of Person. Each Person object you make (like variables) each has their own lname, ssn, fname, etc. and each has their own functions to access them. An example, say you have two people and you want their information. You can also create an array of objects just like any other variable.

    Code:
    int main() {
        Person firstPerson, secondPerson;
        Person workers[15];
        Get_Input(firstPerson);
        Get_Input(secondPerson);
        for (int i=0; i<=15; i++)
            GetInput(workers[i]);
        return 0;
    }
    Now firstPerson, secondPerson, and workers[0-15] each have their own separate lname, fname, etc.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    I have this for the consturctor section. using your example i am still getting errors:
    d:\Visual Studio Projects\person\person.cpp(30): error C2512: 'Person' : no appropriate default constructor available


    Code:
     
    Person::Person (char* fName, char* lName, char* SSN,
     int Birth_Year, int Birth_Month, int Birth_Day){
    	fName = "";
    	lName = "";
    	SSN = "000000000";
    	Birth_Year = 0000;
    	Birth_Month = 00;
    	Birth_Day = 00;
    	}

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I completely forgot to address the constructor problem. You need to prefix the constructor function/prototype with ~. You don't need all the parameters you have listed. In fact, you don't need any. All the variables in the class are local to the functions of the class. The purpose of constructors is to initialize values, which is what this does:
    Code:
    ~Person::Person();
    ~Person::Person(){
    	fName = "";
    	lName = "";
    	SSN = "000000000";
    	Birth_Year = 0000;
    	Birth_Month = 00;
    	Birth_Day = 00;
    	}
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    A default constructor

    A default constructor is just empty
    Code:
    Person::Person()
    {}
    It is called if you don't supply initialized data for your constructor
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    I thought the ~ was for Destructors?

    Correct me if I am wrong but I thought destructors used the ~.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  11. #11
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Example class

    Code:
    class MyClass
    {
    public:
    	int classvariable1;
    	int classvariable2;
    
    
    	//A constructor supplied by you that initializes 
                    //data no need for a default constructor
    
    	MyClass(int a=1, int b=1)
    	{
    		classvariable1=a;
    		classvariable2=b;
    	}
    
    };
    	// if you didn't supply the 1 if you just left it 
                    //MyClass(int a, int b) then you would
    	//need to include a default constructor in Addition to  
                    //your constructor 
    
    class MyClass
    {
    public:
    	int classvariable1;
    	int classvariable2;
    
    	MyClass(int a, int b)
    	{
    		classvariable1=a;
    		classvariable2=b;
    	}
    
    
    
    	//a default constructor
    	MyClass()
    	{}
    
    
    
    };
    Last edited by manofsteel972; 03-03-2004 at 08:56 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    using
    Code:
    Person (); //(in the header)
    and


    Code:
     
    Person::Person (){
    	fName = "";
    	lName = "";
    	SSN = "000000000";
    	Birth_Year = 0000;
    	Birth_Month = 00;
    	Birth_Day = 00;
    	} // (in the person_functs.cpp)
    I get the following error
    d:\Visual Studio Projects\person\person.cpp(30): error C2248: 'Person::Person' : cannot access private member declared in class 'Person'

  13. #13
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    nevermind that I realized that I didn't have the Person(); in the Public section...

  14. #14
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    now... how do I use a "get" to get data stored in the class to display out on the screen what was input for each person?

  15. #15
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ~ is for desconstructors (sorry).

    I thought you had Get functions to do that, which return the value

    Code:
    int Person::GetAge() { return age; }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

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

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. Just starting to learn C++ Question about classes
    By uraliss in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:31 AM
  3. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  4. Starting to get aggrivated with classes....
    By imortal in forum C++ Programming
    Replies: 21
    Last Post: 06-01-2003, 04:21 PM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 03:57 PM

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