CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2015
    Posts
    2

    Classes not being Inherited and help with virtual Functions?

    Im trying to make 4 classes ( 1 base and 3 derived classes) for different types of Workers for a project.

    Here's what it looks like when it runs:

    Walter made 0
    made 2000
    made 2403.85
    made 2692.31
    ___________________________________________
    Walter made 0
    made 0
    made 0
    made 0

    None of the getName() functions work even though I know I inherited them properly. Also, what am I doing wrong with the compute_pay function in worker?
    I know your supposed to use virtual functions but I just don't seem to get it.

    For Comparison, heres what I want it to print out when it is working. Nothing is wrong with the worker.cpp,worker.h or driver.cpp


    Walter made 0
    Horatio made 2000
    Sally made 2403.85
    Nico made 2692.31
    ___________________________________________
    Walter made 0
    Horatio made 2000
    Sally made 2403.85
    Nico made 2692.31


    Code:
    #ifndef WORKER_H
    #define WORKER_H
    
    #include<string>
    #include<cstdio>
    #include<iostream>
    
    using namespace std;
    
    class Worker
    {
     public:
       Worker();
       Worker(string, int, string, int);
       double compute_pay() const;
       string getName() const;
       int getCompID() const;
       string getDepartment() const;
       int getSocialSecurityNumber() const;
     private:
       string name;
       int compid;
       string dept;
       int ssnum;
    };
           
    #endif
    #include "Worker.h"

    Worker::Worker()
    {}


    Worker::Worker(string name, int compid, string dept, int ssnum)
    {
    this->name = name;
    this->compid = compid;
    this->dept = dept;
    this->ssnum =ssnum;
    }


    double Worker::compute_pay() const
    {
    return 0.0;
    }


    string Worker::getName() const
    {
    return name;
    }

    int Worker::getCompID() const
    {
    return compid;
    }

    string Worker::getDepartment() const
    {
    return dept;
    }

    int Worker::getSocialSecurityNumber() const
    {
    return ssnum;
    }
    Code:
    #ifndef HOURLYWORKER_H
    #define HOURLYWORKER_H
    
    #include <string>
    #include <iostream>
    #include <cstdio>
    #include "Worker.h"
    
    using namespace std;
    
    class HourlyWorker : public Worker
    
    /*
    An HourlyWorker represents the specific case of a person who is compensated for
    his/her work based on the amount of time (measured in hours) devoted to a work activity
    multiplied by the hourly rate of pay (measured in dollars per hour).
    For the purposes of this assignment we will compute the weekly pay amount as Weekly
    Pay = Hours * HourlyPayRate
    */
    {
    
    public:
    	HourlyWorker();
    	//HourlyWorker(string, int, string, int,int,double,double);
    		HourlyWorker(string, int, string, int,int,double,double);
    	int getHoursWorked() const;
    	float getHourlyPayRate() const;
    	virtual double compute_pay() const;
    
    
    
    private:
        int hoursWorked;
       float hourlyPayRate;
       double weeklyPay;
     
    };
    
    #endif
    Code:
    #include "HourlyWorker.h"
    #include "Worker.h"
    
    HourlyWorker::HourlyWorker()
    {
    	
    }
    
     
    HourlyWorker::HourlyWorker(string name, int compid, string dept, int ssnum, int time, double rate, double getPay) 
    {
      hoursWorked = time;
      hourlyPayRate = rate;
      weeklyPay = (hourlyPayRate * hoursWorked);
    }
    
    
    //Accessor Functions
    int HourlyWorker::getHoursWorked() const
    {
      return hoursWorked;    
    }    
    
    float HourlyWorker::getHourlyPayRate() const
    {
      return hourlyPayRate;    
    }    
    
    double HourlyWorker::compute_pay() const
    {
    	return weeklyPay;
    }
    
    //float HourlyWorker::getPay(int,float) const
    //{
    //  return getPay;   
    //}
    Code:
    #ifndef SALARIEDWORKER_H
    #define SALARIEDWORKER_H
    
    #include<string>
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include "Worker.h"
    
    
    using namespace std;
    
    class SalariedWorker: public Worker
    
    
    /*A SalariedWorker is a worker that is paid a fixed annual amount known as a salary.
    Depending on the payroll system used by an organization, a salaried worker is paid
    periodically on a weekly, bi-monthly, or other basis. For purposes of this assignment, we
    will compute the weekly pay amount as Weekly Pay = AnnualSalary / 52 . 
    */
    
    
    {
     public:
     	
     	SalariedWorker();
     	SalariedWorker(string, int, string, int, double);
    	virtual double compute_pay() const;
      
     private:
    	
    	double annualSalary; 
    	double weeklyPay;
     
    
     
     
    };
    #endif
    Code:
    #include "SalariedWorker.h"
    #include "Worker.h"
    
    
    SalariedWorker::SalariedWorker()
    {
    	
    }
    
    SalariedWorker::SalariedWorker(string name, int compid, string dept, int ssnum, double annualSalary)
    {
    
    
    weeklyPay = annualSalary/52;
    
    	
    }
    
    
    double SalariedWorker::compute_pay() const
    {
    	return weeklyPay;
    }
    Code:
    #ifndef MANAGER_H
    #define MANAGER_H
    
    #include<string>
    #include<cstdio>
    #include<iostream>
    
    #include "Worker.h"
    #include "SalariedWorker.h"
    
    
    using namespace std;
    
    
    class Manager : public SalariedWorker
    
    /*
    A Manager is a specific case of a salaried worker that manages a department. A
    manager receives a salary, and may also receive a large monetary bonus at the end
    of the year if the manager’s department meets or exceeds certain departmental
    goals. For purposes of this assignment, we will compute the weekly pay amount as
    Weekly Pay = (AnnualSalary + Bonus from last year) / 52.
    */
    
    {
     public:
     	
    Manager();
    Manager(string, int, string, int, double,double);
    double getBonus();
    	virtual double compute_pay() const;
    
       
     private:
     	double bonus;
     	double weeklyPay;
     
    };
           
    #endif
    Code:
    #include "SalariedWorker.h"
    #include "Manager.h"
    #include "Worker.h"
    
    
    Manager::Manager()
    {
    	
    }
    
    Manager::Manager(string name, int compid, string dept, int ssnum, double annualSalary, double bonus)
    {
    
    weeklyPay = (annualSalary + bonus)/52;
    	
    }
    
    
    
    
    
    double Manager::compute_pay() const
    {
    	return weeklyPay;
    }
    Code:
    //THIS IS THE DRIVER!
    #include "Worker.h"
    #include "HourlyWorker.h"
    #include "SalariedWorker.h"
    #include "Manager.h"
    
    using namespace std;
    
    int main()
    
    {
      Worker         w("Walter", 70113, "sales", 123456789);
      HourlyWorker   h("Horatio", 70114, "accounting", 999999999,50.0, 40.0, 10.0);
      SalariedWorker s("Sally", 72000, "IT", 333333333, 125000.00);
      Manager        m("Nico", 71111, "marketing", 414414141, 125000.00, 15000.00);
      
      cout << endl;
      cout << w.getName() << " made " << w.compute_pay() << endl;
      cout << h.getName() << " made " << h.compute_pay() << endl;
      cout << s.getName() << " made " << s.compute_pay() << endl;
      cout << m.getName() << " made " << m.compute_pay() << endl;
      cout << endl;
    
    
     
      Worker workers[] = {w,h,s,m};
      for (int i = 0; i < 4; i++)
      cout << workers[i].getName() << " made " << workers[i].compute_pay() << endl;
    
    
      cout << endl;
    
      system("pause");
      return 0;    
        
        
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Classes not being Inherited and help with virtual Functions?


  3. #3
    Join Date
    Nov 2015
    Posts
    2

    Re: Classes not being Inherited and help with virtual Functions?


    ok Ive changed the array to an array of pointers and set the base class compute_pay to virtual, but my
    Code:
    getName()
    and other inherited functions still dont work.

    my output looks like this:

    Walter made 0
    made 2000
    made 2403.85
    made 2692.31
    ___________________________________________
    Walter made 0
    made 2000
    made 2403.85
    made 2692.31

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Classes not being Inherited and help with virtual Functions?

    What is your current code and how exactly does it not work?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured