Classes and .h files

This is a discussion on Classes and .h files within the C++ Programming forums, part of the General Programming Boards category; Here is Main Code: #include <iostream> #include <string> #include "warrents.h" using namespace std; class addressBook { public: explicit addressBook(string streetaddy, ...

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

    Classes and .h files

    Here is Main
    Code:
    #include <iostream>
    #include <string>
    #include "warrents.h"
    
    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;
        }
      
    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:"<<address1.getAddress()<<"\n "<< address1.getPhoneNum()<<endl;
         cout << "The second address is:"<<address2.getAddress()<<"\n "<< address2.getPhoneNum()<<endl;
        
        warrents recordChecker1("Boy are are good" "Girls are bad");
        warrents recordChecker2("Girls are good" "Boys are bad");
        
        cout << "The header file 1 says:"<<recordChecker1.getRecordBoy()<<endl;
        cout << "The header file 2 says:"<<recordChecker2.getRecordGirl()<<endl;
        
        return 0;
    }
    Here is the header file

    Code:
    //
    //  warrents.h
    
    #ifndef Classes_project_warrents_h
    #define Classes_project_warrents_h
    
    class warrents {
        
    public:
        explicit checkRecord::checkRecord std::string (badBoy), std::string (badGirl)
        : boy (badBoy),girl (badGirl)
        {
        
        }
        void recordBoy()
        {
            boy = badBoy;
        }
        void recordGirl()
        {
            girl= badGirl;
        }
        std::string getRecordBoy()
        {
            return girl;
        }
        std::string getRecordGirl()
        {
            return boy;
        }
        
    private:
        std::string boy;
        std::string girl;
    };
    
    
    #endif

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    They are not talking... I am trying to hide the files and practice good programming by not reveling the whole code. Granted this is very basic but once I can get this to work I'll step it up a little.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Your addressBook class is correct (on first inspection), but your warrents class, which looks like it's supposed to be identical in function, isn't written the same way.

    As for the two not talking, that's likely a problem with your IDE settings. IDE's can sometimes compile a single file, but generally they compile projects. You want all files related to your program to be in the same project.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You could have a main and for every class a .h and a .cpp, where the first would hold the declarations, etc. and the last one, the implementation of the class (definitions of functions).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

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

    Code:
    #include <iostream>
    #include <string>
    #include "checkFile.h"
    #include "check.cpp"
    
    using namespace std;
    
    int main(int argc, const char * argv[])
    {
        addressBook address1("New York Way", "555-555-5554");
        addressBook address2("New Jersey Bld","678-979-9999");
        
        std::cout << "The first address is:"<<address1.getAddress()<<"\n"<< address1.getPhoneNum()<<std::endl;
        std::cout << "The second address is:"<<address2.getAddress()<<"\n"<< address2.getPhoneNum()<<std::endl; 
    
        return 0;
    }
    .cpp file
    Code:
    #include <iostream>
    #include <string>
    #include "checkfile.h"
    
    explicit addressBook::addressBook(string streetaddy, sting phoneNumber)
    {
        street=streetaddy;
        phone=phoneNumber;
    }
    void addressBook::Address(std::string streetAddy)
    {
        street = streetAddy;
    }
    
    void addressBook::PhoneNum(std::string phoneNumber)
    {
        phone = phoneNumber;
    }
    string addressBook::getPhoneNum() const
    {
        return phone;
    }
    std::string addressBook::getAddress() const
    {
        return street;
    }
    .h file
    Code:
    //checkFile.h
    
    #ifndef Classes_project_checkFile_h
    #define Classes_project_checkFile_h
    #include <iostream>
    #include <string>
    #include "checkfile.h"
    
    class  addressBook{
        
    public:
        explicit addressBook(std::string streetaddy, std::string phoneNumber)
        : street(streetaddy), phone (phoneNumber);
        
        void Address(std::string streetAddy)
        {
            street = streetAddy;
        }
        
        void PhoneNum(std::string phoneNumber)
        {
            phone = phoneNumber;
        }
        std::string getPhoneNum() const
        {
            return phone;
        }
        std::string getAddress() const
        {
            return street;
        }
        
    private:
        std::string street;
        std::string phone;
    };
    
    #endif
    Code:
    Errors:
    line 13 on checkFile.h Expected { or ','
    line 6 .cpp file Expected ';' after top level declatator
    If I fix these error a lot more show up. This is frustrating!!!

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you need to remove everything on line 13 except ; and move what is removed to the cpp where the constructor body is
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Code:
     
    #include <iostream>
    #include <string>
    #include "checkfile.h"
    
    explicit addressBook::addressBook(string streetaddy, sting phoneNumber)
    :street(streetaddy), phone (phoneNumber)
    
    {
        street=streetaddy;
        phone=phoneNumber;
    }
    void addressBook::Address(std::string streetAddy)
    {
        street = streetAddy;
    }
    
    void addressBook::PhoneNum(std::string phoneNumber)
    {
        phone = phoneNumber;
    }
    string addressBook::getPhoneNum() const
    {
        return phone;
    }
    std::string addressBook::getAddress() const
    {
        return street;
    }
    now the only error is on 6 and 7.

    line 6 addressBook cannot be name of a variable or data member
    line 7 ; after top level declarator

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Remove the explicit keyword from the source file.
    You don't need to initialize and assign. Remove the assignment in the constructor body.
    Don't include .cpp files. Compile separately and link. If you're using an IDE, create a new project and make sure both .cpp files are in it and build it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    I do not understand....

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    I do not understand what you do not understand. Be more specific.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Remove the explicit keyword from the source file. (So I remove explicit, got it and did it)

    You don't need to initialize and assign. Remove the assignment in the constructor body. (LOST)

    Don't include .cpp files. Compile separately and link. If you're using an IDE, create a new project and make sure both .cpp files are in it and build it. (So remove my .cpp file? My book said it was safe programing practice?)

    This just started C++, I got excited at first when I seen it was just like C but was soon let down. I am trying not to just write a program and move on but really understand what is going on. I sadly don't.




  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    This just started C++, I got excited at first when I seen it was just like C but was soon let down. I am trying not to just write a program and move on but really understand what is going on. I sadly don't.
    That's okay. This is how it is in the beginning for everyone. I can elaborate.

    Quote Originally Posted by jocdrew21 View Post
    You don't need to initialize and assign. Remove the assignment in the constructor body. (LOST)
    Code:
    addressBook::addressBook(string streetaddy, sting phoneNumber)
    :street(streetaddy), phone (phoneNumber) // <--- Here you initialize your variables
    {
        // Here you assign your variables
        street=streetaddy;
        phone=phoneNumber;
    }
    You be familiar with initialize vs assign. Basically, if you assign on the row where you declare, you initialize. You can also initialize using the initializer list (as you've done).
    But what I meant is that your code is essentially the same as

    Code:
    std::string a = "a", b = "b";
    a = "a";
    b = "b";
    You already initialize street and phone, so there is no need to assign them later as in this example.

    Don't include .cpp files. Compile separately and link. If you're using an IDE, create a new project and make sure both .cpp files are in it and build it. (So remove my .cpp file? My book said it was safe programing practice?)
    That depends on what you mean by "remove". If you mean remove the #include "check.cpp", then yes. Otherwise, no.
    You should know that in C and C++, a program can consist of multiple files (called translation units). To make a program, you basically do my_compiler a.cpp b.cpp -o my_program.exe or the like. Note that I pass all .cpp files to the compiler. You should not include .cpp files from another .cpp file or header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Thank you very much. I still had issues so I thought I would try something else. Who what of thought it gave me more issues. I post the question in a new thread. I'll figure this out. You were a big help thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files,sub classes
    By sigur47 in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2012, 08:29 AM
  2. Classes and header files
    By sigur47 in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2012, 04:56 PM
  3. Header files and classes
    By disruptivetech in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2008, 09:02 AM
  4. Classes and header files
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 10:42 AM
  5. Sharing Classes using .DLL files?
    By Laos in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2001, 04:58 AM

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