-1

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; } 
3
  • 2
    Why do you think you should be able to access the private members of a base class? There's a reason why they're private. Commented Apr 21, 2016 at 23:27
  • I have some tutorials in which they do access the private members. Here the error is caused by this line in SeniorEmployee.cpp 'SeniorEmployee::SeniorEmployee(string EmpName, int EmpID, double EmpSales, double EmpComm, double BaseSalary) : Employee(name,id,sales,commission) { }' Commented Apr 21, 2016 at 23:31
  • "I have some tutorials in which they do access the private members" - Either the tutorial is seriously flawed and wrong, you misunderstood it, or it's not for C++. Commented Apr 22, 2016 at 0:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.