3 Replies - 303 Views - Last Post: 01 August 2014 - 12:26 PM Rate Topic: ***** 1 Votes

#1 dombavetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-July 14

What is making this an abstract class?

Posted 01 August 2014 - 11:57 AM

I'm working on a project that deal a lot with inheritance, I have this base class "Employee" and a derived class "Manager" and whenever I try to create a new manager in the main class it tells I cant because its an abstract class. I ran into the same problem earlier in this program when I was creating the The different kinds of sale objects but I fixed it after I figured out I didn't have all my pure virtual functions defined in the derived classed of Sale. In this case though I have all the virtual function defined in the derived classes so I'm not sure what is making "Manager" abstract.. Any help clearing this up would be greatly appreciated :helpsmilie:/>/>

Here's the base class:
class Employee{
protected:
    string first_name;
    string last_name;
    float base_salary;
    float commission;
    float total_salary;
public:
    vector<Insurance *> sale_list; // holders pointers to the abstract Insurance class objects
    Employee():first_name(""), last_name(""), base_salary(0), commission(getCommission()),total_salary(getTotalSalary()){ };
    Employee(string fn, string ln, float bs):first_name(fn), last_name(ln), base_salary(bs), commission(getCommission()),total_salary(getTotalSalary()){ };
    
    // Print Functions
    virtual ostream &print_info( ostream & ) = 0;
    virtual ostream &print_employee_sales( ostream & ) = 0;
    virtual ostream &print_staff_sales( ostream & ) = 0;
    
    //Commission Functions
    virtual void setCommission();
    float getCommission();
    
    // Calculates total salary
    float getTotalSalary();
    
    // Adds sale object to a list of all that empoyees sales
    void addSale( Insurance );
};

namespace printInfo {
    ostream &operator<<( ostream &, Employee & );
}

namespace printSales {
    ostream &operator<<( ostream &, Employee & );
}

namespace printStaffSales {
    ostream &operator<<( ostream &, Employee & );
}



Derived employee class:
class Manager: public Employee{
private:
    float staff_commission;
    vector<Salesperson *> staff_list;
public:
    // Constructors
    Manager(): Employee(){ };
    Manager(string fn, string ln, float bs): Employee(fn, ln, bs){
        Manager::setCommission();
        commission = Manager::getCommission();
        total_salary = getTotalSalary();
    };
    
    virtual ostream &print_info( ostream & ) = 0;
    virtual ostream &print_employee_sales( ostream & ) = 0;
    virtual ostream &print_staff_sales( ostream & ) = 0;
    
    // Sets managers commission based off of individual sales and their staffs individual sales
    virtual void setCommission();
    
    // Adds a saleperson to a list of employees this manager is in charge of.
    void addSalesperson( Salesperson );
};




Managers defined virtual functions:
void Manager::setCommission(){
    //Gets commission from all of managers staff
    Salesperson *s_temp;
    for (int i = 0; i < staff_list.size(); ++i){
        s_temp = staff_list.at(i);
        
        Insurance *i_temp;
        for (int i = 0; i < s_temp->sale_list.size(); ++i){
            i_temp = sale_list.at(i);
            this->staff_commission += i_temp->getCommission();
        }
    }
    
    // Gets commission from all of mangers individual sales
    Insurance *temp;
    for(int i = 0; i < sale_list.size(); ++i){
        temp = sale_list.at(i);
        this->commission += temp->getCommission();
    }
    commission = commission + (commission * 0.3) + (staff_commission * 0.05);
}

void Manager::addSalesperson(Salesperson s){
    staff_list.push_back(&s);
}

//Prints manager info and a list of all their sales
ostream &Manager::print_info( ostream &strm ){
    strm << "-Manager information- " << endl;
    strm << "  Name: " << first_name << " " << last_name << endl;
    strm << "  Base Salary : " << base_salary << endl;
    strm << "  Commission : " << getCommission() << endl;
    strm << "  Total Salary : " << getTotalSalary() << endl;
    
    return strm;
}

ostream &Manager::print_employee_sales( ostream &strm ){
    strm << "-Sale information- " << endl;
    strm << "  List of Sales : " << endl;
        // Prints the stored list of sales for that employee
        Insurance *temp;
        for(int i = 0; i < sale_list.size(); ++i){
            temp = sale_list.at(i);
            strm<< temp << endl;
        }
    
    return strm;
}

ostream &Manager::print_staff_sales( ostream &strm ){
    strm << "Manager Name: " << first_name << " " << last_name << endl;
    
    Salesperson *s_temp;
    for (int i = 0; i < staff_list.size(); ++i){
        s_temp = staff_list.at(i);
        
        Insurance *i_temp;
        for (int i = 0; i < s_temp->sale_list.size(); ++i){
            i_temp = sale_list.at(i);
            strm<< i_temp;
        }
    }
    
    return strm;
}



And as a side note, all 3 of the virtual print methods are called in the overloaded output operator in the namespaces.

Is This A Good Question/Topic? 0
  • +

Replies To: What is making this an abstract class?

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: What is making this an abstract class?

Posted 01 August 2014 - 12:18 PM

At a guess I'd say it would be lines 14-17 in your manager class. This says is has 3 pure virtual functions. It shouldn't as you given implementations later on.

I think you need to understand the difference between a virtual function and a pure virtual function. Have a look at this.
Was This Post Helpful? 1
  • +
  • -

#3 dombavetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-July 14

Re: What is making this an abstract class?

Posted 01 August 2014 - 12:21 PM

Well I figured it out, and since there's no way to delete this post (as far as I know) I'll just post the solution. I just had to remove the zeros in the Manger class, I missed that the first time since I copied and pasted them out of the Employee class!

View Postsimeesta, on 01 August 2014 - 12:18 PM, said:

At a guess I'd say it would be lines 14-17 in your manager class. This says is has 3 pure virtual functions. It shouldn't as you given implementations later on.

I think you need to understand the difference between a virtual function and a pure virtual function. Have a look at this.


Yeah I missed those when I copied them from the employee class! This is the first time i've worked with pure/virtual functions so I miss little things like that, Ill check out the link and thanks for the help :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: What is making this an abstract class?

Posted 01 August 2014 - 12:26 PM

You have defined the Manager class to contain three pure virtual functions. That right there makes it an abstract class. Any class which defines a pure virtual function becomes an abstract class. You are going to have to remove teh "= 0" part if you are creating definitions for them in Manager. The =0 part is saying "Hey this class doesn't have an implementation of this method, but its derived classes will so stay tuned".

:)

Edit: looks like I was beaten to the punch.

This post has been edited by Martyr2: 01 August 2014 - 12:27 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1