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

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

Related

Why isnt my .cpp class using my .h variables?

So I am using Visual Studio to make a simple program for intro to Object Oriented Programming. I am using C++ language to do this, in Netbeans with JAVA OPP isn't so complicated but I am having trouble here. I have to make a simple Object, I chose to make my object called Movie. I made a Movie.h and Movie.cpp file. I included .h's extensions to my Movie.cpp and main.cpp but when I create the object in my main or try to i keep getting errors and underlines because my .cpp file is not recognizing my variables declared in .h, it keeps saying the variable is undefined.
So visual studio's wants to help me out and I followed their method so in my .h files they said they will define my methods for me, meaning they will set it up for me and when I clicked it i get this format
string Movie::getName()
{
return string();
}
While I am using this format for this function
string getName(){
return name;
}
My variables keeps getting red underlined saying they are undefined.
My Movie.h file
#pragma once
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z );
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
My Movie.cpp File
#include "Movie.h"
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void setLength(int x) {
length = x;
}
void setRating(double y) {
rating = y;
}
void setName(string z) {
name = z;
}
//Get functions
int getLength() {
return length;
}
double getRating() {
return rating;
}
string getName() {
return name;
}
My .main
#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1();
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Now I havent worked on my toString method yet but I can't because I cant figure out what the real problem is, why is my .cpp not recognizing my variables? Is my format wrong? Is the visual studio's format right because I ran with their format and got bunch of errors as well or am I declaring them wrong or something? Thank you!
Main.cpp
//#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1;
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Movie.h
#pragma once
#ifndef Movie_H
#define Movie_H
#include <string>
#include <iostream>
using namespace std;
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z);
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
#endif
Movie.cpp
#include "Movie.h"
//#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void Movie::setLength(int x) {
length = x;
}
void Movie::setRating(double y) {
rating = y;
}
void Movie::setName(string z) {
name = z;
}
//Get functions
int Movie::getLength() {
return length;
}
double Movie::getRating() {
return rating;
}
string Movie::getName() {
return name;
}

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

Inheritance in 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.

C++ Object inaccessible

I'm trying to reference an object in a function and it is giving me an "Object is inaccessible" error. Here is the header and cpp file in question.
customer header file below. I've put the object declaration at the bottom.
#pragma once
#include "bankAccount.h"
#include "checkingAccount.h"
#include "savingsAccount.h"
#include "address.h"
using namespace std;
class customer {
public:
customer(void);
customer(string,string);
~customer(void);
void setName(string n);
string getName();
void withdrawChecking(double);
void wihdrawSavings(double);
double depositSavings(double);
string print();
private:
string name
checkingAccount myChecking;
savingsAccount mySavings;
};
Here is the cpp file. I've bolded the problem statement.
#include "customer.h"
#include "checkingAccount.h"
customer::customer(void){
}
customer::customer(string n, string ac){
name = n;
mySavings.setAccount(ac);
myChecking.setAccount(ac);
}
void customer::setName(string n){
name = n;
}
string customer::getName(){
return name;
}
void withdrawChecking(double w){
myChecking.withdrawChecking(w);
}
So what is wrong with this last statement and my header?
Sorry for bad styling... first time posting a question.
You're missing a customer on the front of withdrawChecking. It should be:
void customer::withdrawChecking(double w)