Help with program

This is a discussion on Help with program within the C++ Programming forums, part of the General Programming Boards category; Hey people, I have a program here which is giving me a lot of problems. Im just learn c++ and ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    Angry HELP! with program

    Hey people,

    I have a program here which is giving me a lot of problems. Im just learn c++ and I'm following a book, the excercise is spearation of interface and implementation.

    Here's the code (3 files) :
    * Main Program *
    Code:
    #include <string.h>
    #include <iostream.h>
    #include "pair.h"
    
    class Date : public Pair {
          public:
                 int getYear () {
                     return aYear;
                 }
                 Date (int day, int month, int year) {
                 // set attributes of pair
                 setNum1 (day);
                 setNum2 (month);
                 // set new attribute
                 aYear = year;
                 }
                 void output () {
                      cout << getNum1 () << "." << getNum2 () << "."
                      << aYear << endl;
                      }
                 void output2 () {
                      cout << "the value of day is " << day << endl; }
                 private:
                         int aYear; // the year
                 };
    
    main () {
         Date HeidiKlum (4,8,1976); // the constructor
         HeidiKlum.output2 ();
         cout << "the contents of aYear is for Heidi is" << " "
              << HeidiKlum.getYear () << endl;
    };
    * Second file - "pair.h" *
    Code:
    // pair.h file
    #ifndef PAIR_H__
    #define PAIR_H__
    
    class Pair {
          public:
    		Pair (int num1, int num2);
    		Pair ();
    		void output ();
    		void setNum1 (int num);
    		void setNum2 (int num);
    
          private:
                  	int aNum1;
                  	int aNum2;
          };
    
    #endif
    * Third file - "pair.cpp" *
    Code:
    // Pair.cpp file
    #include <iostream.h>
    #include "pair.h"
    
    Pair :: Pair (int num1, int num2) {
    aNum1 = num1; 
    aNum2 = num2; 
    } 
    Pair :: Pair () {
    aNum1 = 0; 
    aNum2 = 0; 
    } 
    void Pair :: output () {
    cout << aNum1 << " " << aNum2 << endl; 
    } 
    void Pair :: setNum1 (int num) {
    aNum1 = num; 
    } 
    void Pair :: setNum2 (int num) {
    aNum2 = num; 
    }

    Thanks for the help!
    Last edited by Gamma; 04-17-2002 at 09:22 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Without even looking at the rest of your code, check out your main function. It should be int main(), and return 0. Also, unless I'm missing something, how does output2() know what variable day is? It's not a class variable, and not passed in to output2().
    It doesn't matter that it's passed into the constructor; that's a different scope.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    30
    exactly what sould i change, i made it int main () and return 0;, i actually did that many times before and it still did not work. Then what should i do with the output2, remove it completely? When i do remove it completely, i get down to two errors on line 18 regarding implicit declaration of getNum1 and getNum2. But, i want to include the output2, how should i do this?
    Last edited by Gamma; 04-17-2002 at 08:05 PM.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You need int main(), return 0 regardless of any other errors.
    You can have output2(), you just have to give it a variable called day. output2() in class Date can't access private variables aNum1 and aNum2 in class Pair, and even if it could, they're not called day or month. When you use inheritance, private functions and variables in base classes are not accessible in derived classes.
    There are different ways you could approach this. It seems odd that the variable aYear is declared in one class, while aNum1 and aNum2 are declared in another class. One class ought to handle this ok. You can separate interface and implementation with one class - you do it in your Pair class.
    If you're going to have set methods for your variables, aYear should have one too.

  5. #5
    Unregistered
    Guest
    since Date is inherited from Pair everything in Pair will be available in Date as well. That is each Date object will have a private variable called aNum1 and aNum2, and each Date object will have public methods called setNum1() and setNum2(). Therefore, you can access aNum1 and aNum2 for a given Date object just like you would for a given Pair object.

    However, each Date object will also get the public method output() available in Pair. Unfortunately, you have a public method called output() in Date as well. Both methods called output() have the return value and the same arguments, so the compiler can't tell whether to call the version inherited from Pair, or the version provided in Date. I believe that is the most likely cause for the crash. To fix this try placing the keyword virtual before the declaration output() in Pair, and it wouldn't hurt to use it in front of the declaration of output() in Date, either, although that is not absolutely necessary. Now, if output() is called by a Date object the output defined in the Date interface will be called, and if output() is called by a Pair object, then the version of output() defined in Pair will be called. If you want to call the version of output() from Pair by a Date object then you can try placing the class name Pair and the scope operator before the function call. You should probably look up the use of the keyword virtual in your favorite textbook to confirm my discussion.


    Since aNum1 and aNum2 are actually part of Date, I don't believe you need to call the setNum1() and setNum2() methods in Date, although it isn't wrong to do so.

    day is a variable passed to Date's constructor, but it is not a variable that can be used outside the constructor. The value of day is assigned to aNum1, so output that instead.

    getNum1() and getNum2() are not declared, defined, or needed. output() acts as the public accessor function for the private member variables of Date and Pair.
    Code:
    #include <string.h>
    #include <iostream.h>
    #include "pair.h"
    
    class Date : public Pair {
          public:
                 int getYear () {
                     return aYear;
                 }
                 Date (int day, int month, int year) {
                 // set attributes of pair
                 aNum1 = day;
                 aNum2 = month;
                 // set new attribute
                 aYear = year;
                 }
                 virtual void output () {
                      cout << aNum1 << "." << aNum2 << "."
                      << aYear << endl;
                      }
                 void output2 () {
                      cout << "the value of day is " << aNum1 << endl; }
                 private:
                         int aYear; // the year
                 };
    
    main () {
         Date HeidiKlum (4,8,1976); // the constructor
         HeidiKlum.output2 ();
         cout << "the contents of aYear is for Heidi is" << " "
              << HeidiKlum.getYear () << endl;
    };
    * Second file - "pair.h" *

    Code:
    // pair.h file
    #ifndef  _PAIR_H__
    #define  _PAIR_H__
    
    class Pair {
          public:
    		Pair (int num1, int num2);
    		Pair ();
    		virtual void output ();
    		void setNum1 (int num);
    		void setNum2 (int num);
    
          private:
                  	int aNum1;
                  	int aNum2;
          };
    
    #endif
    * Third file - "pair.cpp" *

    Code:
    // Pair.cpp file
    #include <iostream.h>
    #include "pair.h"
    
    Pair::Pair (int num1, int num2) {
    aNum1 = num1; 
    aNum2 = num2; 
    } 
    
    Pair :: Pair () {
    aNum1 = 0; 
    aNum2 = 0; 
    } 
    
    void Pair::output () {
    cout << aNum1 << " " << aNum2 << endl; 
    } 
    void Pair::setNum1 (int num) {
    aNum1 = num; 
    } 
    void Pair::setNum2 (int num) {
    aNum2 = num; 
    }

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Originally posted by Unregistered
    since Date is inherited from Pair everything in Pair will be available in Date as well. That is each Date object will have a private variable called aNum1 and aNum2, and each Date object will have public methods called setNum1() and setNum2
    True except for the available part. Date does inherit aNum1 and aNum2, but since those are declared private in Pair, they're not directly accessible to Date. Date would have to use a public or protected function of Pair to do anything with them. aNum1 and 2 are directly available only to Pair.
    I still don't see why there's a Pair class in the first place, though.

  7. #7
    Unregistered
    Guest
    admit one's mistakes. Salvelinus is correct, private members in a base class are not available to a derived class withought public accessor functions.

    Also in reviewing my reference, the output() method in Date overrides and hides the output() method in Pair, but it shouldn't cause the problem I indicated. Still, I think it is best to use virtual functions in a base class if you think you will be overriding it in a derived class.

    Sorry.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    30
    can someone try this program out to see if it works maybe its a problem with my compiler, i made the changes and i got more errors than i did the last time, HELP!

    ps the reason why the classes are dived teh way they are is because the book im using divides it that way
    Last edited by Gamma; 04-18-2002 at 08:17 PM.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Here's some partially cleaned up code. There's a linking error on the constructor that I can't figure out right now, but this is one track to follow w/o recoding the whole thing.
    PHP Code:
    #ifndef PAIR_H__
    #define PAIR_H__

    class Pair 
    {
        public:
            
    Pair(intint);
            
    int GetNum1(); 
            
    int GetNum2(); 
        protected:
            
    int aNum1;
            
    int aNum2;
    };

    #endif 
    PHP Code:
    #include <iostream.h>
    #include "pair.h"

    Pair::Pair(int num1int num2)  
    {
        
    aNum1 num1;
        
    aNum2 num2;
    }

    int Pair::GetNum1() {return aNum1;}
    int Pair::GetNum2() {return aNum2;} 
    and:
    PHP Code:
    #include <iostream.h>
    #include "pair.h"

    class Date : public Pair 
    {
        private:
            
    int aYear// the year

        
    public:
            
    Date (int dayint monthint year) : Pair(daymonth
            {
                
    aYear year;        
            }

            
    int getYear() { return aYear; }

            
    void output2() { cout << "the value of day is " << Pair::GetNum1() << endl;}          
    };


    int main() 
    {
        
    Date HeidiKlum(4,8,1976); // the constructor
        
    HeidiKlum.output2();
        
    cout << "the contents of aYear is for Heidi is " 
            
    << HeidiKlum.getYear() << endl;

        return 
    0;


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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