Classes:

This is a discussion on Classes: within the C++ Programming forums, part of the General Programming Boards category; I am having a hard time with classes. What is going on with this code. It has errors all over ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468

    Classes:

    I am having a hard time with classes. What is going on with this code. It has errors all over it.
    Code:
     #include <iostream>
    #include <string>
    
    using namespace std;
    
    class addressBook {
        
    public:
       explicit addressBook(string streetaddy, string phoneNumber)
        : street(streetaddy) 
        : phone (phoneNumber)
        {
        
        }
        
        void Address(string streetAddy)
        {
            street = streetAddy;
        }
        
        void PhoneNum(string phoneNumber)
        {
            phone = phoneNumber;
        }
        string getPhoneNum() const
        {
            return phone;
        }
        string getAddress() const
        {
            return street;
        }
        void displayAddressBook()
        {
        
        }
    private:
        string street;
        string phone;
    };
    
    
    
    int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        cout << "The first address is:"<<addressBook1.getAddress<<"\n"<< addressBook1.getPhoneNum<<endl;
        
        return 0;
    }
    Last edited by jocdrew21; 09-08-2013 at 11:17 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    What might the error messages be?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    line 10,11 - you need to remove string -
    Code:
    : street(streetaddy),phone (phoneNumber)
    and use : only once, after that only ,

    line 49 - there is function call but missing ()
    Code:
    addressBook1.getAddress()
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class addressBook {
        
    public:
       explicit addressBook(string streetaddy, string phoneNumber)
        : street(streetaddy), phone (phoneNumber)
         
        {
        
        }
        
        void Address(string streetAddy)
        {
            street = streetAddy;
        }
        
        void PhoneNum(string phoneNumber)
        {
            phone = phoneNumber;
        }
        string getPhoneNum() const
        {
            return phone;
        }
        string getAddress() const
        {
            return street;
        }
        void displayAddressBook()
        {
        
        }
    private:
        string street;
        string phone;
    };
    
    
    
    int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        cout << "The first address is:"<<addressBook1.getAddress()<<"\n"<< addressBook1.getPhoneNum()<<endl;
         cout << "The second address is:"<<addressBook2.getAddress()<<"\n"<< addressBook2.getPhoneNum()<<endl;
        return 0;
    }
    Error- Use of undeclared identifier (lines 49 and 50)

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    guess it would help if displayAddressBook was not blank!!!

    O man....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    address1 and addressBook1 are different names.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Code:
     int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        cout << "The first address is:"<<address1.getAddress()<<"\n "<< address1.getPhoneNum()<<endl;
         cout << "The second address is:"<<address2.getAddress()<<"\n "<< address2.getPhoneNum()<<endl;
        return 0;
    }
    GOT IT THANK YOU!!!!

    I'll make a few more programs up to make sure I got it. Just started to learn about .h files and classes. I think I got a grip of the concept now, just have to practice.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,500
    why do you have a class called "addressBook" that only stores one address? it may be a minor point, but on this forum, we like to emphasize the idea of naming things in ways that properly represent them. often this gets applied to things like single-letter variable names, but I think this case should also be addressed. in the real world, an address book can contain many addresses. if you plan to do this as a career someday, it would be good to keep this sort of thing in mind.
    Code:
    namespace life
    {
        const bool change = true;
    }

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Will do thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 03:36 AM
  3. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Classes inside Classes
    By LostGeneration in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2003, 11:02 AM

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