More Issues with classes:

This is a discussion on More Issues with classes: within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; class HeartBeats { public: explicit HeartBeats(string first, string last, int how_old) : ...

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

    More Issues with classes:

    Code:
     #include <iostream>
    #include <string>
    
    using namespace std;
    
    class HeartBeats {
        
    public:
        explicit HeartBeats(string first, string last, int how_old)
        : firstNam(first), lastNam(last), age(how_old)
        {
        
        }
        
        void firstName(string first)
        {
            first=firstNam;
            
        }
        void lastName(string last)
        {
            last=lastNam;
        }
        void Age(string how_old)
        {
            how_old=age;
        }
        string get_First() const
        {
            return firstNam;
        }
        string getLastName() const
        {
            return lastNam;
        }
        int getAge() const
        {
            return age;
        }
        void CalculateBPM()
        {
            int BPM;
            
            BPM = 220-age;
            
            cout<<"Hello" <<firstNam<<", Your max Heart Rate should not be over"<<BPM<<"BPM"<<endl;
        }
       
    private:
        string firstNam;
        string lastNam;
        int age;
        
    };
    
    int main(int argc, const char * argv[])
    
    {
        string firstname,lastname;
        int PersonsAge;
        
        cout<<"Enter the persons first name"<<endl;
        getline(cin, firstname);
        
        cout<<"Enter the persons last name"<<endl;
        getline(cin, lastname);
        
        cout<<"Enter the persons age"<<endl;
        cin << PersonsAge;
        
        HeartBeats heartBeats(firstname ,lastname, PersonsAge);
        
        cout<< heartBeats.get_First()<<endl;
        cout<< heartBeats.getLastName()<<endl;
        cout<< heartBeats.getAge()<<endl;
        cout<< heartBeats.CalculateBPM()<<endl;
    
       
        return 0;
    }
    I the int PersonAge is not being sent to the class. Everything up to that point was fine.

    I want to separate everything into .cpp file and .h files once I see it all working in one program. cin is simply giving me an error and the syntax is correct, what the heck?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Code:
        void firstName(string first)
        {
            first=firstNam;
             
        }
        void lastName(string last)
        {
            last=lastNam;
        }
        void Age(string how_old)
        {
            how_old=age;
        }
    You've got these backwards. Variable "first" is the argument passed into the function. You want to store this into firstNam. Oh, and it's spelled "Name", not "Nam". Same for the others.

    >>cin << PersonsAge;
    Wrong way. You read FROM cin TO a variable, so it should be
    cin >> PersonsAge;

    >>cout<< heartBeats.CalculateBPM()<<endl;
    CalculateBPM returns void (ie nothing). You can't print void. You probably want to return the BPM from this function instead (use return statement and an appropriate return type).
    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.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    Code:
     #include <iostream>
    #include <string>
    
    using namespace std;
    
    class HeartBeats {
        
    public:
        explicit HeartBeats(string first, string last, int how_old)
        : firstNam(first), lastNam(last), age(how_old)
        {
        
        }
        
        void firstName(string first)
        {
            firstNam=first;
            
        }
        void lastName(string last)
        {
            lastNam=last;
        }
        void Age(int how_old)
        {
            age = how_old;
        }
        string get_First() const
        {
            return firstNam;
        }
        string getLastName() const
        {
            return lastNam;
        }
        int getAge() const
        {
            return age;
        }
        int CalculateBPM()
        {
            int BPM;
            
            BPM = 220-age;
            
            return BPM;
        }
       
    private:
        string firstNam;
        string lastNam;
        int age;
        
    };
    
    int main(int argc, const char * argv[])
    
    {
        string firstname,lastname;
        int PersonsAge;
        
        cout<<"Enter the persons first name"<<endl;
        getline(cin, firstname);
        
        cout<<"Enter the persons last name"<<endl;
        getline(cin, lastname);
        
        cout<<"Enter the persons age"<<endl;
        cin >> PersonsAge;
        
        HeartBeats heartBeats(firstname ,lastname, PersonsAge);
        
        cout<< heartBeats.get_First()<<endl;
        cout<< heartBeats.getLastName()<<endl;
        cout<< heartBeats.getAge()<<endl;
        cout<< heartBeats.CalculateBPM()<<endl;
        cout<<"Hello " <<heartBeats.get_First()<<", Your max Heart Rate should not be over "<<heartBeats.CalculateBPM()<<" BPM"<<endl;
        
        return 0;
    }
    I got it working thanks again. I think I got a basic understanding now and think I am ready to move to the next chapter. I'll working on these too. Seems like a lot of work though for something so basic. I am sure I will find good use for it in time.

    The cin mistake can show you how new I really am. I knew I misspelled lastNam and firstNam, can't give you a good reason why, just did it.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    It might be fun to make this class give the Target Heart Rate, the BPM that is good for exercise.

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 being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 10:19 AM
  4. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  5. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM

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