c++c++11inheritancecompiler-errorsprivate-members

derived class giving error when accessing private variables of base class


I am making a c++ program to store employee's data like name, id, saleamount, commission amount and calculate earning (sales*commission). I am using the concept of inheritance. My base class is 'Employee' and my derived class is 'SeniorEmployee'. When I run the program, the compiler gives me an error that I cannot access the private members of base class. The errors are like this

error: 'std::__cxx11::string Employee::name' is private.

Another error on 2nd line is

error: 'int Employee::id' is private

Again the same error on 3rd line

error: 'double Employee::sales' is private

Following is my code. I have included all files.

File Employee.h

#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

#include <iostream>
#include <string> 
using namespace std;


class Employee {

public:

    Employee(string EmpName, int EmpID, double EmpSales, double EmpComm);

    void setName(string EmpName);
    string getName();

    void setID(int EmpID);
    int getID();

    void setSales(double EmpSales);
    double getSales();

    void setComm(double EmpComm);
    double getComm();

    double earning();


private:

     string name;
     int id;
     double sales;
     double commission;

};


#endif

File Employee.cpp

#include <iostream>
#include <string>
#include "Employee.h"

using namespace std;

Employee::Employee(string EmpName, int EmpID, double EmpSales, double EmpComm): name(EmpName), id(EmpID), sales(EmpSales), commission(EmpComm)
{

}

void Employee::setName(string EmpName) {
    name=EmpName;
}

string Employee::getName() {
     return name;
}

void Employee::setID(int EmpID) {
    id=EmpID;
}

int Employee::getID() {
    return id;
}

void Employee::setSales(double EmpSales) {
    sales=EmpSales;
}

double Employee::getSales() {
    return sales;
}

void Employee::setComm(double EmpComm) {
    commission=EmpComm;
}

double Employee::getComm() {
    return commission;
}

double Employee::earning() {
    return sales*commission;
}

File SeniorEmployee.h

#ifndef SENIOREMPLOYEE_H_
#define SENIOREMPLOYEE_H_
#include "Employee.h"

#include <iostream>
#include <string> 

using namespace std;

class SeniorEmployee: public Employee {

public:

     SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary);

     void setBaseSalary(double BaseSalary);

     double getBaseSalary();

private:
    double bsalary;
};


#endif

File SeniorEmployee.cpp

#include <iostream>
#include <string>
#include "SeniorEmployee.h"

using namespace std;

SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(name,id,sales,commission)
{
}

void SeniorEmployee::setBaseSalary(double BaseSalary) {
    bsalary=BaseSalary;
}

double SeniorEmployee::getBaseSalary() {
    return bsalary;
}

File main.cpp

#include <iostream>
#include "SeniorEmployee.h"

using namespace std;

int main() {

    string empname = "Fareed Shuja";
    int empid = 3978;
    double empsales = 30.0;
    double empcomm = 0.99;
    double basesalary = 50.0;

    SeniorEmployee emp(empname,empid,empsales,empcomm,basesalary);

    cout << "Name of the Employee is : " << emp.getName() << endl;

    cout << "Employee ID is : " << emp.getID() << endl;

    cout << "Sale Amount is : " << emp.getSales() << endl;

    cout << "Commission is : " << emp.getComm() << endl;

    cout << "Earning is : " << emp.earning() << endl;

    cout << "Employee's base salary is " << emp.getBaseSalary() << endl;


    return 0;
}

Solution

  • The following line in SeniorEmployee.cpp is incorrect:

    SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(name,id,sales,commission)
    

    It's attempting to access the private variables 'name', 'id', etc... instead of passing your constructor's arguments to the base class constructor. It should instead be:

    SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(EmpName,EmpID,EmpSales,EmpComm)
    

    Also if you want to access variables from a derived class but not make them visible outside of the class they must be declared protected instead of private.