Inheritance in C++? - c++

I'm trying to learn about inheritance. I've tried to make a class called ProductionWorker that is derived from Employee. I'm trying to follow the model given by this website. However, it seems like the inheritance isn't working because I get errors in the main function saying that name, number, and date are not set in this scope. What is wrong with the code?
Program.cpp:
#include <iostream>
#include <string>
#include "employee.h"
using namespace std;
int main() {
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
worker.setName(name);
cout<<"What is the employee number?"<<endl;
cin>>number;
worker.setNumber(number);
cout<<"What is the employee hire date?"<<endl;
cin>>date;
worker.setDate(date);
cout<<"Employee Information:"<<endl;
cout<<worker.getName()<<endl;
cout<<worker.getNumber()<<endl;
cout<<worker.getDate()<<endl;
cout<<worker.getShift()<<endl;
cout<<worker.getPayRate()<<endl;
return 0;
}
employee.h:
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <string>
#include <iostream>
using namespace std;
class Employee{
protected:
string name;
int number;
string date;
public:
Employee(string a="", int b=0, string c=""){
name=a;
number=b;
date=c;
}
void setName(string);
void setNumber(int);
void setDate(string);
string getName();
int getNumber();
string getDate();
};
class ProductionWorker: public Employee{
private:
int shift;
double pay;
public:
ProductionWorker(int d=1, double e=10.0, string a="", int b=0, string c=""):Employee(a, b, c){
shift=d;
pay=e;
}
int getShift();
double getPayRate();
};
employee.cpp:
#include <string>
#include <iostream>
#include "employee.h"
using namespace std;
//EMPLOYEE
void Employee::setName(string a){
name=a;
}
void Employee::setNumber(int b){
number=b;
}
void Employee::setDate(string c){
date=c;
}
string Employee::getName(){
return name;
}
int Employee::getNumber(){
return number;
}
string Employee::getDate(){
return date;
}
//PRODUCTION WORKER
int ProductionWorker::getShift(){
return shift;
}
double ProductionWorker::getPayRate(){
return pay;
}

I think that you should declare name, number and date before you use it, in Program.cpp. You try to get input into symbol that compiler doesn't know yet.

Your error is nothing to do with inheritance:
int main() {
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
You're reading into a variable called name but no such variable is in scope (i.e. declared in the main() function itself or globally). Though name is a member of the Employee class, you cannot use it as such in this way. (Consider: how would the compiler know which instance of Employee that you were wanting to set the name of? And, of course, if you could read directly into the instance variable, there would be no need to call worker.setName(...) afterwards).
You should just declare name as a local variable, by specifying its type:
int main() {
string name;
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
Now cin>>name; reads into the local variable name that is declared within the main() function. (I took the liberty of fixing your indentation). In a similar way, you need declarations for date and number.

You haven't declared the variables that cin needs to read into.
Just add
string name;
int number;
string date;
At the top of function main.

Related

C++ Best way to allow user to declare a struct object?

#include <iostream>
#include <string>
using namespace std;
struct player
{
string name;
int money = 100;
int wins = 0;
};
int main()
{
string user;
cin >> user;
player user;
user.name = user;
}
What is the proper syntax for this in C++? I'm trying to declare the object name as the one given by the user. How would you do this? Would a class be easier to do this? Any tips/advice is much appreciated!
variables names are meaningless at runtime in compiled langages like c++. If you want to refer to a player using his or her provided username at runtime you need a datastructure that will remember the name as as string, like a hashtable or a map (I would recommend a map, those are easier to use) http://www.cplusplus.com/reference/map/map/
best is classes because of security
as by default ,
classes are private in nature but struct are public in nature
#include <iostream>
#include <string>
using namespace std;
class player
{
public:
string name;
int money;
int wins;
public(string name){
this->name = name;
this->money = 100;
this->wins = 0;
}
};
int main()
{
string user;
cin >> user;
player *user = new player(user);
}

Undefined reference to Class::Class/Function (Beginner in OOP)

I have this annoying error;
Undefined Reference to Shape::Shape(...), Shape::getName(...), Shape::getAge(...)
My Main.cpp is this
#include <iostream>
#include <string>
#include "Bit.h"
using namespace std;
int main()
{
//simple assignment
string name;
int age;
cout<<"enter name: ";
cin>>name;
cout<<"enter age: ";
cin>>age;
Shape sh(name,age); //class declaration (i think here is the problem)
cout<<endl<<"name: "<<sh.getName();
cout<<endl<<"age: "<<sh.getAge();
return 0;
}
This is the Bit.h header
#include <iostream>
#include <string>
using namespace std;
#ifndef BIT_H
#define BIT_H
//creating class
class Shape{
string newName;
int newAge;
public:
//Default Constructor
Shape();
//Overload Constructor
Shape(string n,int a);
//Destructor
~Shape();
//Accessor Functions
string getName();
int getAge();
};
And finally, this is the Bit.cpp
#include "Bit.h"
//constructors and destructor
Shape::Shape(){
newName="";
newAge=0;
}
Shape::Shape(string n, int a){
newName=name;
newAge=age;
}
Shape::~Shape(){
}
string Shape::getName(){
return newName;
}
//getters
int Shape::getAge(){
return newAge;
}
I understand, that this might be a very simple problem/error, but I have been struggling for about 2 hours.
I suppose that the error is in the declaration od "sh" object, even if I declare it like this "Shape sh();" or "Shape sh;", there are still errors.
Thanks
EDIT. GNU GCC Compiler
EDIT2. Using Code Blocks (sorry for forgetting all these)
You're probably not compiling Bit.cpp, but only Main.cpp.
Considering that Bit.h, Bit.cpp and Main.cpp are in the same folder, here is how you should compile it :
g++ *.cpp
It will still not compile, as in the default constructor, you're trying to initialize name, and age which both don't exist.
You probably meant newAge, and newName?
In the other Constructor, name and age also don't exist, you probably meant n, and a?

How to return a value in one method to another method of the same class

I have this two code samples Employee.h and Employee.cpp
Employee.h is as follows:
#include<iostream>
using namespace std;
#define SIZE 20
class Employee{
private:
double othrs, otrate,salary;
protected:
int Empno;
char name[SIZE];
int telephone;
double basicSalary;
public:
Employee();
~Employee();
double calcNetSalary(double basicSalary,double othrs, double otrate);
void displayNetSalary();
};
And here's my Employee.cpp
#include<iostream>
#include "Employee.h"
using namespace std;
Employee::Employee(){
}
Employee::~Employee(){
}
double Employee::calcNetSalary(double basicSalary,double othrs, double otrate){
double salary=(basicSalary+othrs*otrate);
return salary;
}
void Employee::displayNetSalary(){
cout<<"Net Salary : "<<salary; //This line doesn't print the correct value but some other values (**6.01347e-154**)
}
And here's my Main.cpp
#include<iostream>
#include "Employee.h"
using namespace std;
int main(){
Employee *emp=new Employee();
emp->calcNetSalary(10,20,30);
emp->displayNetSalary();
return 0;
};
And is there a way I could use the returning value(salary) in calcNetSalary(double basicSalary,double othrs, double otrate) in displayNetSalary() method?
The answer is to use this which assigns the value to the instance variable not a temporary variable:
double Employee::calcNetSalary(double basicSalary,double othrs, double otrate){
salary=(basicSalary+othrs*otrate);
return salary;
}
You are assigning value to a local variable within calcNetSalary function. Assign the value to your private member variable salary
Another option is to call calcNetSalary within displayNetSalary function

How to do you call a function inside separated class?

So, I tried to make this code :
#include <iostream>
using namespace std;
class BuckysClass{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main()
{
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
BECOMING TO A SEPARATED CLASS like this :
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
int main (){
BuckysClass bo;
bo.setName("Buckingham Palace");
cout << bo.getName();
return 0;
}
==============
#ifndef BUCKYSCLASS_H
#define BUCKYSCLASS_H
class BuckysClass
{
public:
void setName(string x);
string getName();
private:
string name;
};
#endif // BUCKYSCLASS_H
=============
#include "BuckysClass.h"
#include <iostream>
#include <string>
using namespace std;
BuckysClass::BuckysClass()
{
}
void setName(string x){
name = x;
}
string getName(){
return name;
}
When I run the first code, I succeed,
but I got error when running the separated class code,
help me find out what's wrong ???
I tried to use different code,
but it seems I can't find the reason,
the closest reason I believe, is the main doesnt call the function on the separated class properly.
If you write this code:
void setName(string x){
name = x;
}
string getName(){
return name;
}
The compiler interprete it as two functions called setName and getName, it has no idea they are member functions of your BuckysClass class.
You have to precise it with the following syntax:
void BuckysClass::setName(string x){
name = x;
}
string BuckysClass::getName(){
return name;
}
Additionally, here you are defining a default constructor:
BuckysClass::BuckysClass()
{
}
But you didn't put it in the class prototype. You have to add it somewhere in the class prototype definition in your .h file, or your compiler won't recognize it:
class BuckysClass
{
public:
BuckysClass(); // Default constructor.
void setName(string x);
string getName();
private:
string name;
};

C++ Variable Value Not Passing From Header to main {}

I'm trying to make a "grade book".
I have a header file (GradeBook.h), and a main.cpp file.
I'm having trouble getting the value of a "midtermExamGrade" value to pass given the follow parameters:
GradeBook.h file:
#include <string>
#include <iostream>
using namespace std;
class GradeBook
{
public:
void setMidtermExamGrade(double grade)
{
double midterm_exam_grade = grade;
}
double getMidtermExamGrade()
{
return midterm_exam_grade;
}
private:
double grade;
} //end class GradeBook
Your private member is not being initialized correctly because your setter and getter are wrong. Try this:
#include <string>
#include <iostream>
using namespace std;
class GradeBook
{
public:
void setMidtermExamGrade(double grade)
{
midterm_exam_grade = grade; // use the private member
}
double getMidtermExamGrade()
{
return midterm_exam_grade;
}
private:
double midterm_exam_grade; // change here to match tha names in setter and getter
} //end class GradeBook