Thread: problem in inherting class in dev c++

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    problem in inherting class in dev c++

    i made a program in dev c++ to calculate salary of regular based employee and hourly based employee but when i compile the program i got some errors. i want to inherit person class in both re and hbe class but the problem is that the function definitions for input();calsalary() and display() are different for the child classes.

    error- no `void re::input()' member function declared in class `re'
    same for other member functions also

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class person
    {
       protected:
        char ch[20];
     
       public:
         void input();
         float calsalary();
         void display();
    };
    
    class re:public person
    {
       int empid;
       float basic,da,hra,pf; 
    };
    
    class hbe:public person
    {
       int empid,hpm;
       float pph;
    };
    
    void re::input()
    {
         cout<<"\n Name      = ";
         cin>>ch;
         cout<<"\n Emp ID    = ";
         cin>>empid;
         cout<<"\n Basic pay = ";
         cin>>basic;
         cout<<"\n DA        = ";
         cin>>da;
         cout<<"\n HRA       = ";
         cin>>hra;
         cout<<"\n PF        = ";
         cin>>pf;
    }
    
    float re::calsalary()
    {
          return(basic+da+hra+pf);
    }
    
    void re::display()
    {
         cout<<"\n Name       = "<<ch;
         cout<<"\n Emp ID     = "<<empid;
         cout<<"\n DA         = "<<da;
         cout<<"\n HRA        = "<<hra;
         cout<<"\n PF         = "<<pf;
         cout<<"\n Net Salary = "<<calsalary();
    }    
    
    void hbe::input()
    {
         cout<<"\n Name            = ";
         cin>>ch; 
         cout<<"\n Emp ID          = ";
         cin>>empid;
         cout<<"\n Pay per hour    = ";
         cin>>pph;
         cout<<"\n Hours per month = ";
         cin>>hpm;
    }
    
    float hbe::calsalary()
    {
          return(pph*hpm);
    }
    
    void hbe::display()
    {
         cout<<"\n Name       = "<<ch;
         cout<<"\n Emp ID     = "<<empid;
         cout<<"\n Hr per mon = "<<hpm;
         cout<<"\n Pay per Hr = "<<pph;
         cout<<"\n Net Salary = "<<calsalary();
    }    
    
    int main()
    {
        re  o1;
        hbe o2;
        
        cout<<"\n\t\t Regular Employee  \n";
        o1.input();
        o1.display();
        
        cout<<"\n\t\t Hourly Based Employee \n";
        o2.input();
        o2.display();
        
        getch();
        return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You class definition needs to include a declaration for each subsequently defined method:

    Code:
    class re:public person
    {
       public:
          void input();
          int empid;
          float basic,da,hra,pf;
    };
    Last edited by MK27; 10-27-2011 at 08:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Also, you need a new IDE!
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    to MK27: i don't understand why there is a need of writing function declarations in child classes because they are inheriting features of base class-parent.so in my opinion, while inheriting; the function declarations which i wrote in the base class should automatically comes inside sub classes in public section and so they should be visible like char ch[20] is visible.but,then why compiler gives the error :-(

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shikhardeep
    i don't understand why there is a need of writing function declarations in child classes because they are inheriting features of base class-parent.so in my opinion, while inheriting; the function declarations which i wrote in the base class should automatically comes inside sub classes in public section and so they should be visible like char ch[20] is visible.but,then why compiler gives the error :-(
    If you intend to use the base class version of the member functions, then yes, that is correct: you just use them without re-declaring them in the definition of the derived class. However, if you wish to override, or in this case re-define, certain member functions, then you need to declare those member functions in the definition of the derived class.

    Note that I wrote "in this case re-define". All the member functions of person are non-virtual. Therefore, person looks like it is intended to be a concrete class that is not a base class. If it really is supposed to be a polymorphic base class, then at least one of its member functions (or at least the destructor) would be declared virtual. Furthermore, I note that you did not define the person member functions, despite declaring them. Perhaps they should be declared pure virtual instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by shikhardeep View Post
    to MK27: i don't understand why there is a need of writing function declarations in child classes because they are inheriting features of base class-parent.so in my opinion, while inheriting; the function declarations which i wrote in the base class should automatically comes inside sub classes in public section and so they should be visible like char ch[20] is visible.but,then why compiler gives the error :-(
    Yes, the derived classes will inherit the functions. But note the word "inherit."
    The derived classed will have the same functions as the base class, not different ones.
    When inheriting, you do not inherit function declarations, but the function itself. If you wish to redefine that function to something different, you are going to have to create a new function.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    so it means that i have to give all the 3 member functions(input(),calsalary()and display()) definitions separately for re class and hbe class and there is no point of using concept of inheritance as the function definitions are different for both re and hbe classes.The only thing which is therefore common in both the child classes is the char ch[20](used for entry of name of the employee).

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Any code savings is worth it.
    Besides that, inheritance enables you to use polymorphism.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shikhardeep
    so it means that i have to give all the 3 member functions(input(),calsalary()and display()) definitions separately for re class and hbe class and there is no point of using concept of inheritance as the function definitions are different for both re and hbe classes.The only thing which is therefore common in both the child classes is the char ch[20](used for entry of name of the employee).
    No, the interface of the member functions are common, so maybe you should declare them as virtual member functions and override them in the derived classes.

    There are two aspects in the use of inheritance. We might use inheritance to inherit implementation, which is what you have in mind. More interestingly, we might use inheritance to implement an interface, thereby taking advantage of supertype/subtype polymorphism at runtime: this way, we can create new derived classes that can be used by existing code that only uses the interface of the base class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another c++ class problem,
    By program in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2011, 08:46 AM
  2. A little class problem
    By bijan311 in forum C++ Programming
    Replies: 12
    Last Post: 05-23-2010, 11:42 AM
  3. Problem with class/derived class
    By ammochck21 in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2007, 12:40 AM
  4. A class problem
    By Luminost in forum C++ Programming
    Replies: 10
    Last Post: 03-25-2007, 04:12 PM
  5. Replies: 27
    Last Post: 10-11-2006, 04:27 AM