c++ - derived class giving error when accessing private variables of base class -
i making c++ program store employee's data name, id, saleamount, commission amount , calculate earning (sales*commission). using concept of inheritance. base class 'employee' , derived class 'senioremployee'. when run program, compiler gives me error cannot access private members of base class. errors
error: 'std::__cxx11::string employee::name' private.
another error on 2nd line
error: 'int employee::id' private
again same error on 3rd line
error: 'double employee::sales' private
following code. have included 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 employee : " << emp.getname() << endl; cout << "employee id : " << emp.getid() << endl; cout << "sale amount : " << emp.getsales() << endl; cout << "commission : " << emp.getcomm() << endl; cout << "earning : " << emp.earning() << endl; cout << "employee's base salary " << emp.getbasesalary() << endl; return 0; }
the following line in senioremployee.cpp incorrect:
senioremployee::senioremployee(string empname, int empid, double empsales, double empcomm, double basesalary) : employee(name,id,sales,commission)
it's attempting access private variables 'name', 'id', etc... instead of passing constructor's arguments base class constructor. should instead be:
senioremployee::senioremployee(string empname, int empid, double empsales, double empcomm, double basesalary) : employee(empname,empid,empsales,empcomm)
also if want access variables derived class not make them visible outside of class must declared protected
instead of private
.
Comments
Post a Comment