derived class giving error when accessing private variables of base class - c++

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;
}

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.

Related

How to reuse variables from the base class to the derived class

I am working on a project to show statistics for a generic vehicle, then for a car and a truck.
All of the objects have a Manufacturer and Model Year, but the Car has a number of doors, and the truck has a towing capacity.
I have my vehicle class and class constructors all working properly in the code.
Now I want to call the same method of data storage from the base class Vehicle, and use it for the Car Class.
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
private:
string manu;
int year;
public:
// Default constructor
Vehicle() {
manu = "";
year = 0;
}
// Constructor
Vehicle(string autoManu, int autoYear) {
manu = autoManu;
year = autoYear;
}
// Accessors
string getManu() const { return manu; }
int getModel() const { return year; }
void storeInfo(string autoManu, int autoYear);
void displayInfo();
};
This is my Vehicle.h file ^
#include "Vehicle.h"
void Vehicle::storeInfo(string autoManu, int autoYear) {
manu = autoManu;
year = autoYear;
}
void Vehicle::displayInfo() {
cout << "Manufacturer- " << manu << endl;
cout << "Make Year- " << year << endl;
}
This is my Vehicle.cpp file ^
These both work perfectly, now I want to use the same kind of displayInfo for the Car class.
#pragma once
#include <iostream>
#include "Vehicle.h"
#include <string>
using namespace std;
// The Car class represents a car.
class Car : public Vehicle {
private:
int doors;
public:
// Default constructor
Car() : Vehicle() { doors = 0; }
// Constructor #2
Car(string carManu, int carYear, int carDoors) : Vehicle(carManu, carYear) {
doors = carDoors;
}
// Accessor for doors attribute
int getDoors() { return doors; }
void storeInfo(string autoManu, int autoYear, int carDoors);
void displayInfo();
};
This is my Car.h file ^
#include "Car.h"
void Car::storeInfo(string autoManu, int autoYear, int carDoors) {
manu = autoManu;
year = autoYear;
doors = carDoors;
}
void Car::displayInfo() {
cout << "Manufacturer- " << manu << endl;
cout << "Make Year- " << year << endl;
cout << "Doors on the Car" << doors << endl;
}
This is the Car.cpp file.
The issue I am encountering is that the menu and year variables, the variables defined in the base class, are saying "Vehicle::manu is inaccessible"
What would be causing this, and what is the fix?
Thanks!

I dont understand error "definition of implicity-declared 'Clothing::Clothing()'

The question:
Why is the following error happening?
definition of implicity-declared 'Clothing::Clothing()
The context:
As an assignment I have to do constructors, destructors and methods in a class Clothing. I'm having a problem when I try to define the constructor in clothing.cpp. I have read that the problem is because I did not declare the constructor in clothing.h, but I think how I have done it, it's declared. I have no clue where the problem lies.
My code:
clothing.h:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing();
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
clothing.cpp:
#include <iostream>
#include "clothing.h"
#include <string>
#include <sstream>
using namespace std;
Clothing::Clothing() :
gender(1),
size(1),
name("outofstock") {
}
Clothing::Clothing(const Clothing& t) :
gender(t.gender),
size(t.size),
name(t.name) {
}
Clothing::Clothing(int gender, int size, string name) {
}
int Clothing::getGender() {
return gender;
}
int Clothing::getSize() {
return size;
}
string Clothing::getName() {
return name;
}
void Clothing::setGender(int gender1) {
gender = gender1;
}
void Clothing::setSize(int size1) {
size = size1;
}
void Clothing::setName(string name1) {
name = name1;
}
void Clothing::print() {
cout << name << " " << gender << " " << size << endl;
}
void Clothing::toString() {
stringstream ss;
ss << name << " " << gender << " " << size;
cout << ss.str();
}
Errors: \src\clothing.cpp:7:21: error: definition of implicitly-declared 'Clothing::Clothing()'
\src\clothing.cpp:14:37: error: definition of implicitly-declared 'Clothing::Clothing(const Clothing&)'
The error is: you declared a destructor but you didn't define it. Add a definition for the destructor or define it as default:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing() = default; // <-- add a default destructor
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
After fixing this your code snippet works: tio.run
If you have more problems with your code, the problems are outside of your provided code snippet.

C++ clang linker command failed with exit code 1 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
My professor gave me two class header and .cpp files to build on. When I include these in main, they work fine. Whenever I just use his files, I get linker errors with clang and xcode.
Here's the error:
shannigan#mbp-007100 inheritance (master) $ make main
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"SavitchEmployees::SalariedEmployee::SalariedEmployee()", referenced from:
_main in main-0d7e27.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Here's my main:
#include "employee.h"
#include "salariedemployee.h"
#include <string>
#include <cstdlib>
#include <iostream>
using namespace SavitchEmployees;
using namespace std;
int main() {
cout << "Do I run?" << endl;
SalariedEmployee sam;
return 0;
};
The header file for Employee:
//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;
namespace SavitchEmployees
{
class Employee
{
public:
Employee( );
Employee(const string& theName, const string& theSsn);
string getName( ) const;
string getSsn( ) const;
double getNetPay( ) const;
void setName(const string& newName);
void setSsn(const string& newSsn);
void setNetPay(double newNetPay);
void printCheck( ) const;
protected:
string name;
string ssn;
double netPay;
};
}//SavitchEmployees
#endif //EMPLOYEE_H
The CPP file for main:
//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h.
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::string;
using std::cout;
namespace SavitchEmployees
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), netPay(0)
{
//deliberately empty
}
Employee::Employee(const string& theName, const string& theNumber)
: name(theName), ssn(theNumber), netPay(0)
{
//deliberately empty
}
string Employee::getName( ) const
{
return name;
}
string Employee::getSsn( ) const
{
return ssn;
}
double Employee::getNetPay( ) const
{
return netPay;
}
void Employee::setName(const string& newName)
{
name = newName;
}
void Employee::setSsn(const string& newSsn)
{
ssn = newSsn;
}
void Employee::setNetPay (double newNetPay)
{
netPay = newNetPay;
}
void Employee::printCheck( ) const
{
cout << "\nERROR: printCheck FUNCTION CALLED FOR AN \n"
<< "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
<< "Check with the author of the program about this bug.\n";
exit(1);
}
}//SavitchEmployees
SalariedEmployees header:
//This is the header file salariedemployee.h.
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using std::string;
namespace SavitchEmployees
{
class SalariedEmployee : public Employee
{
protected:
double salary;//weekly
public:
SalariedEmployee( );
SalariedEmployee (const string& theName, const string& theSsn,
double theWeeklySalary);
double getSalary( ) const;
void setSalary(double newSalary);
void printCheck( );
};
}//SavitchEmployees
#endif //SALARIEDEMPLOYEE_H
SalariedEmployee.cpp:
//This is the file salariedemployee.cpp
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the header file salariedemployee.h.
#include <iostream>
#include <string>
#include "salariedemployee.h"
using std::string;
using std::cout;
using std::endl;
namespace SavitchEmployees
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(const string& newName, const string& newNumber,
double newWeeklyPay)
: Employee(newName, newNumber), salary(newWeeklyPay)
{
//deliberately empty
}
double SalariedEmployee::getSalary( ) const
{
return salary;
}
void SalariedEmployee::setSalary(double newSalary)
{
salary = newSalary;
}
void SalariedEmployee::printCheck( )
{
setNetPay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << getName( ) << endl;
cout << "The sum of " << getNetPay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << getSsn( ) << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
}//SavitchEmployees
How can I get rid of these linker errors so I can focus on my actual code? Is there anything obvious wrong? The only thing I've changed was making the "private" variables protected.
I can't see the class named SalariedEmployee.
I think the main function should look like this.
int main() {
cout << "Do I run?" << endl;
Employee sam;
return 0;
};
You have to use Employee instead of SalariedEmployee

Printing out the wrong value

I'm trying to write a program that takes the grades and prints out the following:
ID:123 NAME:John GRADE:78
but instead I'm getting:
ID:-842150451 NAME: GRADE: 78
Can you guys help me and give me some extra tips to make my code cleaner since I'm fairly new to C++.
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(int num, string text);
int getID();
void setExamGrade(int a, int b);
int getOverallGrade();
void display();
string getName();
string name;
int id;
int exams[3];
int sum;
int average;
};
#endif
Student.cpp
#ifndef STUDENT_CPP
#define STUDENT_CPP
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student(int num, string text)
{
num = id;
text = name;
exams[0, 1, 2] = 0;
}
int Student::getID() {
return id;
}
string Student::getName() {
return name;
}
void Student::setExamGrade(int a, int b) {
exams[a] = b;
}
int Student::getOverallGrade() {
sum = exams[0] + exams[1] + exams[2];
average = sum / 3;
return average;
}
void Student::display() {
cout << "ID: " << getID();
cout << " NAME: " << getName();
cout << " GRADE: " << getOverallGrade() << endl;
}
#endif
gradebook.cpp
#ifndef GRADEBOOK_CPP
#define GRADEBOOK_CPP
#include "Student.h"
#include <iostream>
using namespace std;
int main() {
Student *s = new Student(123, "John");
s->setExamGrade(0, 80);
s->setExamGrade(1, 60);
s->setExamGrade(2, 95);
s->display();
delete s;
return 0;
}
#endif
You never assign to id in the constructor, hence it's uninitialized and you will have undefined behavior when you print it.
Change
num = id;
to
id = num;
Same with the name.
Also, the statement
exams[0, 1, 2] = 0;
doesn't do what you expect it to do, it only initializes exams[2] to sero, and leaves the rest uninitialized. The expression 0, 1, 2 uses the comma operator.
Either assign to all members of the array separately, or use a constructor member initializer list (which I recommend for all the initialization).

Setting up linked list class with templates?

I am trying to implement a linked list using a class template, however each of my classes that I want in the list all inherit from an Account class. I have attempted to template the linked list class as account however Ive come into errors I can't resolve. Any ideas as to how I can go about this?
The error exists within the customer.cpp class where it says
14 IntelliSense: a value of type "Account *" cannot be assigned to an entity of type "CurrentAccount *" f:\Further C++\Assignment with Templates\Assignment\Customer.cpp 22 9 Assignment
Customer class:
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include <string>
#include "Account.h"
#include "AccountLinkedList.h"
using namespace std;
class Customer
{
private:
string name;
string telNo;
int doorNum;
string street;
string postcode;
string sex;
string dob;
AccountLinkedList <Account> accountList; //Here is the linkedList templated as Account class
Account * head;
Account * aNode;
public:
int customerID;
Customer * next;
//Customers personal details
Customer(int id, string customerName, string gender, int doorNumber, string customerPostcode, string dateOfBirth)
: customerID(id), name(customerName), sex(gender), doorNum(doorNumber), postcode(customerPostcode), dob(dateOfBirth)
{
};
string getName();
void addAccount(int choice);
void showPersonDetails();
void updatePersonDetails();
};
#endif
Here is the Customer.cpp file:
#include "Customer.h"
#include "Account.h"
#include "CurrentAccount.h"
#include "JuniorCurrentAccount.h"
#include "SavingsAccount.h"
#include "CorporateSavingsAccount.h"
#include "StudentSavingsAccount.h"
#include <string>
using namespace std;
void Customer::addAccount(int choice)
{
int id; // temp account ID
switch(choice)
{
/*Current account + JuniourCurrentAccount both inherit from Account class*/
case 0: CurrentAccount * aNode;
CurrentAccount * head;
/*the two lines below give the error on the = sign*/
aNode = accountList.CreateNode(id/*newaccountID*/);
head = accountList.InsertFirst(head, aNode);
break;
case 1: JuniorCurrentAccount * aNode;
JuniorCurrentAccount * head;
aNode = accountList.CreateNode(id);
head = accountList.InsertFirst(head, aNode);
}
}
string Customer::getName()
{
return name;
}
void Customer::showPersonDetails()
{
cout << name << " details" << endl;
cout << "===============================" << endl;
cout << "sex: " << sex << endl;
cout << "dob: " << dob << endl;
cout << "doorNum: " << doorNum << endl;
cout << "postcode: " << postcode << endl;
cout << "===============================" << endl;
}
Here is the current Account class that inherits from account:
#ifndef CURRENTACCOUNT_H
#define CURRENTACCOUNT_H
#include <iostream>
#include <string>
#include "Account.h"
using namespace std;
class CurrentAccount : Account
{
private:
double intRate;
double balance;
protected:
public:
CurrentAccount * next;
CurrentAccount(int accountNumber, double interestRate, double setBalance) : Account(accountNumber)
{
accountType = "Current Account";
intRate = interestRate;
balance = setBalance;
}
};
#endif