Vehicle class is an abstract base class for ElectricVehicle and GasolineVehicle classes, and HybridVehicle class inherits from ElectricVehicle and GasolineVehicle.
I have two 'Vehicle':base class undefined errors in ElectricVehicle.h and GasolineVehicle.h. I know I am messing up the #include files somehow, but I'm having trouble figuring that out.
I tried removing the Vehicle.cpp in ElectricVehicle.cpp and GasolineVehicle.cpp and adding the Vehicle header file, but that came up with a bunch of other errors.
Vehicle.h
#pragma once
#ifndef Vehicle_h
#define Vehicle_h
class Vehicle {
public:
// check here // 4 lines
float engineEfficiency;
virtual float calculateRange() = 0;
virtual float percentEnergyRemaining() = 0;
virtual void drive(float) = 0;
virtual ~Vehicle();
};
#endif
Vehicle.cpp
#pragma once
#include "Vehicle.h"
#include <iostream>
using namespace std;
Vehicle::~Vehicle() {
cout << "In vehicle destructor" << endl;
}
ElectricVehicle.h
#pragma once
#include <iostream>
#ifndef ElectricVehicle_h
#define ElectricVehicle_h
class ElectricVehicle : virtual public Vehicle {
public:
float maximumCharge;
float currentCharge;
float engineEfficiency;
public:
ElectricVehicle(float max, float eff);
float calculateRange();
float percentEnergyRemaining();
void drive(float km);
~ElectricVehicle();
};
#endif
ElecticVehicle.cpp
#pragma once
#include "Vehicle.cpp"
#include "ElectricVehicle.h"
using namespace std;
ElectricVehicle::ElectricVehicle(float maxEnergy, float rating) {
engineEfficiency = rating;
maximumCharge = maxEnergy;
currentCharge = maxEnergy;
}
ElectricVehicle::~ElectricVehicle() {
cout << "In Electric vehicle destructor" << endl;
}
float ElectricVehicle::calculateRange() {
return (currentCharge * 100 / engineEfficiency);
}
float ElectricVehicle::percentEnergyRemaining() {
return (currentCharge * 100.0f / maximumCharge);
}
void ElectricVehicle::drive(float km) {
if (currentCharge < 0) {
cout << "Your Car is out of energy";
return;
}
currentCharge -= (km / 100) * engineEfficiency;
}
GasolineVehicle.cpp
#include "Vehicle.cpp"
#include "GasolineVehicle.h"
using namespace std;
GasolineVehicle::GasolineVehicle(float maxEnergy, float rating) {
engineEfficiency = rating;
maximumGasoline = maxEnergy;
currentGasoline = maxEnergy;
}
GasolineVehicle::~GasolineVehicle() {
cout << "In Gasoline vehicle destructor" << endl;
}
float GasolineVehicle::calculateRange() {
return currentGasoline * 100 / engineEfficiency;
}
float GasolineVehicle::percentEnergyRemaining() {
return (currentGasoline * 100.0f / maximumGasoline);
}
void GasolineVehicle::drive(float km) {
if (currentGasoline < 0){
cout << "Your Car is out of energy";
return;
}
currentGasoline -= (km / 100) * engineEfficiency;
}
Gasoline.h
#pragma once
#include <iostream>
#ifndef GasolineVehicle_h
#define GasolineVehicle_h
class GasolineVehicle : virtual public Vehicle {
public:
float maximumGasoline;
float currentGasoline;
float engineEfficiency;
GasolineVehicle(float max, float eff);
~GasolineVehicle();
float calculateRange();
float percentEnergyRemaining();
void drive(float km);
};
#endif
HybridVehicle.cpp
#pragma once
#include "ElectricVehicle.h"
#include "GasolineVehicle.h"
#include "HybridVehicle.h"
#include<iostream>
using namespace std;
// hybridvehicle constructor is calling its parent class constructors;
HybridVehicle::HybridVehicle(float maxGasoline, float gasefficiency, float maxcharge, float electricefficiency) :GasolineVehicle(maxGasoline, gasefficiency), ElectricVehicle(maxcharge, electricefficiency) {}
HybridVehicle::~HybridVehicle() {
cout << "In Hybrid vehicle destructor" << endl;
}
// :: is scope resolution operator as both gasoline and electric vehicle classes having engine efficiency the compiler
// will be confused of which variable to take.. so we :: to remove ambuiguity
float HybridVehicle::calculateRange() {
return (currentCharge / ElectricVehicle::engineEfficiency) * 100 + (currentGasoline / GasolineVehicle::engineEfficiency) * 100;
}
float HybridVehicle::percentEnergyRemaining() {
return ((currentCharge + currentGasoline) / (maximumCharge + maximumGasoline)) * 100.0f;
}
void HybridVehicle::drive(float km) {
if (currentGasoline + currentCharge < 0) {
cout << "Your Car is out of energy";
return;
}
else if (currentCharge > 0) {
currentCharge -= (km / 100) * ElectricVehicle::engineEfficiency;
}
else
{
currentGasoline -= (km / 100) * GasolineVehicle::engineEfficiency;
}
}
HybridVehicle.h
#pragma once
#ifndef HybridVehicle_h
#define HybridVehicle_h
class HybridVehicle: public GasolineVehicle, public ElectricVehicle {
public:
HybridVehicle(float gMax, float gEff, float eMax, float eEff);
~HybridVehicle();
float calculateRange();
float percentEnergyRemaining();
void drive(float km);
};
#endif
Week2.cpp
#pragma once
#include "Vehicle.cpp"
#include "HybridVehicle.cpp"
using namespace std;
Vehicle* testVehicle(Vehicle* pVehicle, const char* vehicleName) {
cout << vehicleName << " s range is: " << pVehicle->calculateRange() << endl;
pVehicle->drive(150);
cout << vehicleName << " s energy left is: " << pVehicle->percentEnergyRemaining() << endl;
cout << vehicleName << " s range is now: " << pVehicle->calculateRange() << endl;
return pVehicle;
}
int main(int argc, char** argv)
{
//50L of gas, 7.1 L/100km
delete testVehicle(new GasolineVehicle(50, 7.1), "Corolla");
//42 L of gas, 4.3 L/100km, 8.8kWh, 22 kWh/100km
delete testVehicle((GasolineVehicle*)new HybridVehicle(42, 4.3, 8.8, 22.0), "Prius");
//75 kWh, 16 kWh/100km
delete testVehicle(new ElectricVehicle(75, 16), "Tesla 3");
return 0;
}
Related
I'm doing the following problem :
Add on to your Rectangle class from last time:
1)Create a new class TestRect which is a friend class of Rectangle. Inside this class, implement a member function called TestRect::tester, which takes a rectangle as a parameter and tests that both length and width are > 0.
2)Create a main() function which makes three rectangles r1, r2, and r3, each of which has length and width 20. Then call your TestRect::tester function on each of these rectangles and print out the result
I think I did part 1) correctly but part 2) does not give me the output I'm looking for. I do not know how to fix my code from the following output I got :
cppcompile rectEnhanceStaticFriends
Undefined symbols for architecture x86_64:
"TestRect::tester(Rectangle&)", referenced from:
_main in rectEnhanceStaticFriends.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
cppcompile:15: no such file or directory: ./rectEnhanceStaticFriends
Can you help me how to fix it? Seems a linker issue, that I did not find how to fix it. Thank you for reading through. (Ps: I compile with VSC)
Here is my code :
rectEnhanceStaticFriends.cpp
#include <iomanip>
#include <cstring>
#include <string>
#include "rect.h"
using namespace std;
int main()
{
Rectangle::setYards(100);
Rectangle r1(20, 20, "Kitchen");
Rectangle r2(20, 20, "Bathroom");
Rectangle r3(20, 20, "Office");
TestRect tr;
cout << "Test on r1: " << tr.tester(r1) << endl;
cout << "Test on r2: " << tr.tester(r2) << endl;
cout << "Test on r3: " << tr.tester(r3) << endl;
Rectangle house[] = {Rectangle(10, 12, "Kitchen"),
Rectangle(20, 20, "Bedroom"),
Rectangle(8, 12, "Offce")};
for (int i = 0; i < 3; i++)
{
if (strcmp(house[i].printName(), "Offce") == 0)
{
// cout << "oui\n";
house[i].setName("Office");
};
cout << "Area for " << house[i].printName() << " is : " << house[i].getArea() << endl;
}
if (house[1].getArea() > house[2].getArea() && house[1].getArea() > house[3].getArea())
{
cout << house[1].printName() << " has the biggest area.\n";
}
else if (house[2].getArea() > house[1].getArea() && house[2].getArea() > house[3].getArea())
{
cout << house[2].printName() << " has the biggest area\n";
}
else
{
cout << house[3].printName() << " has the biggest area\n";
}
//there is an error house[3] go beyond the array..
return 0;
}
testRect.h
#ifndef TESTRECT_H
#define TESTRECT_H
class Rectangle; //forward declaration of class Rectangle
class TestRect
{
public:
bool tester(Rectangle &);
};
#endif
testRect.cpp
#include <iostream> //
#include <iomanip>
#include <string>
#include "testRect.h"
#include "rect.h"
bool TestRect::tester(Rectangle &r)
{
bool testResult = false;
if (r.width > 0 && r.length > 0)
testResult = true;
return testResult;
}
rect.h
// Rec header file
#ifndef RECT_H
#define RECT_H
#include "testRect.h"
class Rectangle
{
private:
double width;
double length;
char *name;
static double yardsAvail; //indicate how many yards of perimeter are available to make rectangle
void initName(const char *n);
void initName(const char *n, int size);
public:
//constructors
Rectangle();
Rectangle(double, double,
const char *);
//destructor
~Rectangle() { delete[] name; };
void setWidth(double);
void setLength(double);
void setWidth(char *);
void setLength(char *);
void setName(const char *);
int getWidth() const;
int getLength() const;
double getArea() const;
char *printName() const
{
return name;
}
//added parts
static void setYards(double);
friend class TestRect;
friend bool TestRect::tester(Rectangle &);
};
double Rectangle::yardsAvail = 0; //added parts
#endif
rect.cpp
#include <iostream> //
#include <iomanip>
#include <string>
#include "rect.h"
#include "testRect.h"
using namespace std;
Rectangle::Rectangle()
{
width = 0;
length = 0;
initName("Default");
}
Rectangle::Rectangle(double x, double y, const char *z)
{
width = x;
length = y;
initName(z);
double yardsReqd = 2 * x + 2 * y;
if (yardsAvail - yardsReqd < 0)
{
cout << "Not enough yard..\n";
width = 0;
length = 0;
}
yardsAvail -= yardsReqd;
}
void Rectangle::initName(const char *n)
{
name = new char[258];
strcpy(name, n);
};
void Rectangle::initName(const char *n, int size)
{
name = new char[size];
strcpy(name, n);
};
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
void Rectangle::setName(const char *newname)
{
//newname.newName = "Office";
strcpy(name, newname);
}
double Rectangle::getArea() const
{
return width * length;
}
//added part
void Rectangle::setYards(double y)
{
yardsAvail = y;
}
Here is the class
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;
class Point {
protected:
int x, y;
double operator-(const Point &def){
return sqrt(pow((x-def.x),2.0)+
pow((y-def.y),2.0));
}
};
class Circle: public Point {
private:
int radius;
public:
Circle(){
this->x=x;
this->y=y;
this->radius=radius;
}
Circle(int x, int y, int radius){
this->x=x;
this->y=y;
this->radius=radius;
}
void printCircleInfo() {
cout << x << " " << y << " " << radius << " " ;
}
This is the operator I want to be the condition in my if statement.
bool operator==(const Circle &def){
return (x==def.x) & (y==def.y) & (radius==def.radius);
}
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
if (anotherCircle.radius + radius >= *this - anotherCircle )
return true;
return false;
}
};
Here is main
int main(){
int x,y,radius;
const int SIZE = 13;
Circle myCircleArry[SIZE];
myCircleArry[0] = Circle(5,3,9);
cout << endl;
myCircleArry[0].printCircleInfo(); cout << " ; ";
ifstream Lab6DataFileHandle;
Lab6DataFileHandle.open("Lab6Data.txt");
while (!Lab6DataFileHandle.eof( )) {
for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>x;
Lab6DataFileHandle>>y;
Lab6DataFileHandle>>radius;
myCircleArry[i] = Circle(x,y,radius);
if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
myCircleArry[i].printCircleInfo(); cout << " ; ";
Here is the If statement
if ( operator==( Circle &def))
{cout <<"*";
}
}
}
}
Lab6DataFileHandle.close();
}
How do I use the overloaded operator as the condition of the if statement? If you need any clarification just ask other wise please leave an example in your answer.
Thank you for your time.
A == needs two arguments (even if the overload is a member), you would write the if as any other if statement:
if(circle1 == circle2) { ... }
and if there's a matching overload the compiler would transform that into something like:
if(circle1.operator ==(circle2)) { ... }
I'm working on a multiple inheritance exercise and I'm running into a strange error. My code is not returning the correct information to the console. It is just outputting zero, I have checked multiple times in my code and I can't seem to find anything obviously wrong. I'm fairly new to C++ so any help I would appreciate it, in addition to any other critique.
The console will output
Maverick
South station
50 - passengers, is ok
40 - speed
0 - it is supposed to take distance/mph and output 2.6 in this case, but is returning nothing.
MBTA.cpp
#include "MBTA.h"
//objects
transportation Dest;
MBTA::MBTA()
{
}
MBTA::MBTA(string strIn, string strInTransport, int iIn, int distIn, int eIn)
{
setTrain(strIn);
//Destination
Dest.setTransport(strInTransport);
//set passengers
setPass(iIn);
Dest.setMilesToDest(distIn);
engine.setMPH(eIn);
//outputs train information
printTrainDestinationHours();
//used printf as I was running into issue with using cout here
//cout << " I am going to ";
printf("I am going to %s\n", Dest.getTransport().c_str());
//uses engine stats function
cout << "I go " << engine.getMPH() << endl;
printf("It will take me %.2f hours to arrive", redline.getTotal());
}
void MBTA::setTravelDist(int iIn)
{
double destdistance = Dest.getDist();
double trainMPH = engine.getMPH();
//this divides miles by MPH, this might return a float
redline.setTotal(50, 10);
}
MBTA::~MBTA()
{
}
MBTA.H
#pragma once
#include "train.h"
class MBTA :
public train
{
public:
engine engine;
train redline;
MBTA();
//train, destination, passengers, traveldist, speed
MBTA(string, string, int, int, int);
void setTravelDist(int);
//double getTotal();
//uses engine stats function
//double total = 0;
~MBTA();
};
Train.cpp
#include "train.h"
#include <iostream>
#include <cstdio>
#include <cmath>
using std::string;
using std::cout;
using std::endl;
//object member for transport
transportation tTrain;
train::train()
{
}
void train::printTrainDestinationHours()
{
printf("\n\nTrain type: %s\n", getTrain().c_str());
//passengers
printf("I have %d passengers\n", getPass());
}
void train::setPass(int iIn)
{
passengers = iIn;
}
int train::getPass()
{
return passengers;
}
void train::setTrain(string strIn)
{
trainName = strIn;
}
string train::getTrain()
{
return trainName;
}
void train::setTotal(int aIn, int bIn)
{
//dist / mph
total = aIn / bIn;
}
double train::getTotal()
{
return total;
}
train::~train()
{
}
Train Header
#pragma once
#include <iostream>
#include <cstdio>
using std::string;
using std::cout;
using std::endl;
#include "engine.h"
#include "transportation.h"
class train : public transportation
{
public:
train();
void printTrainDestinationHours();
//set and get destination
//num of pass
void setPass(int);
int getPass();
//train
void setTrain(string);
string getTrain();
//distance
void setTotal(int, int);
double getTotal();
~train();
private:
engine engineStats;
int total = 0;
string trainName = "";
string destination = "";
int passengers = 0;
};
engine.cpp
#include "engine.h"
engine::engine()
{
}
void engine::setMPH(int iIn)
{
MPH = iIn;
}
int engine::getMPH()
{
return MPH;
}
engine::~engine()
{
}
engine header
#pragma once
class engine
{
public:
engine();
//return
void setMPH(int);
int getMPH();
~engine();
protected:
int MPH = 0;
};
'''
transportation cpp
'''
#pragma once
class engine
{
public:
engine();
//return
void setMPH(int);
int getMPH();
~engine();
protected:
int MPH = 0;
};
transportation header
#include "transportation.h"
transportation::transportation()
{
}
void transportation::setTransport(string strIn)
{
destination = strIn;
}
string transportation::getTransport()
{
return destination;
}
void transportation::setMilesToDest(int iIn)
{
MilesToDestination = iIn;
}
int transportation::getDist()
{
return MilesToDestination;
}
transportation::~transportation()
{
}
main file
#include <iostream>
using std::string;
using std::cout;
using std::endl;
using std::cin; //for ignore
#include "challenger.h"
#include "MBTA.h"
#include "plane.h"
int main()
{
//object composition of vehicle type
// vehicle type location, passengers, MPH , distance
challenger SRT8707("Boston", 2, 100, 200);
plane boeing("boeing", "houston", 50, 500, 300);
MBTA redline("Maverick", "South station", 50, 100, 40);
//pause and blank line
cout << endl << endl;
cin.ignore();
}
I've been working on this for several hours and I can't figure out which class is giving me problems. I get a bunch of 'VendSlot' does not name a type" errors. I appreciate any help you can provide. I am new to C++ and extremely frustrated.
//class Snack
#ifndef SNACK_HPP
#define SNACK_HPP
#include <iostream>
#include <string>
using namespace std;
class Snack
{
private:
string name;
double price;
int calories;
public:
Snack();
Snack(string name, double price, int calories);
string getName();
double getPrice();
int getNumCalories();
};
#endif
Snack.cpp
#include "Snack.hpp"
#include <iostream>
#include <string>
using namespace std;
Snack::Snack()
{
name = "bottled water";
price = 1.75;
calories = 0;
}
Snack::Snack(string n, double p, int c)
{
name = n;
price = p;
calories = c;
}
string Snack::getName()
{
return name;
}
double Snack::getPrice()
{
return price;
}
int Snack::getNumCalories()
{
return calories;
}
VendSlot.hpp
#ifndef VENDSLOT_HPP
#define VENDSLOT_HPP
#include "Snack.hpp"
class VendSlot
{
private:
Snack s;
int amount;
public:
VendSlot();
VendSlot(Snack s, int a);
Snack getSnack();
int getAmount();
int decrementAmount();
};
#endif
VendSlot.cpp
#include "VendSlot.hpp"
#include <iostream>
#include <string>
using namespace std;
VendSlot();
VendSlot::VendSlot()
{
Snack s1;
s = s1;
amount = 5;
}
VendSlot::VendSlot(Snack s1, int a)
{
s = s1;
amount = 5;
}
Snack VendSlot::getSnack()
{
Snack s1;
s = s1;
return s;
}
int VendSlot::getAmount()
{
return amount;
}
int VendSlot::decrementAmount()
{
amount--;
return amount;
}
MiniVend.hpp
#ifndef MINIVEND_HPP
#define MINIVEND_HPP
#include "VendSlot.hpp"
class MiniVend
{
private:
VendSlot vs1;
VendSlot vs2;
VendSlot vs3;
VendSlot vs4;
double money;
public:
MiniVend();
MiniVend(VendSlot v1, VendSlot v2, VendSlot v3, VendSlot v4, double money);
int numEmptySlots();
double getMoney();
double valueOfSnacks();
void buySnack();
};
#endif
MiniVend.cpp
#include "VendSlot.hpp"
using namespace std;
MiniVend::MiniVend(VendSlot v1, VendSlot v2, VendSlot v3, VendSlot v4, double m)
{
vs1 = v1;
vs2 = v2;
vs3 = v3;
vs4 = v4;
money = m;
}
double MiniVend::getMoney()
{
return money;
}
int MiniVend::numEmptySlots()
{
int numSlots = 0;
if (vs0.getAmount() == 0)
slots++;
if (vs1.getAmount() == 0)
slots++;
if (vs2.getAmount() == 0)
slots++;
if (vs3.getAmount() == 0)
numSlots++;
return numSlots;
}
double MiniVend::valueOfSnacks()
{
double value = 0;
value = (vs0.getSnack().getPrice() * vs0.getAmount() +
(vs1.getSnack().getPrice() * vs1.getAmount()) +
(vs2.getSnack().getPrice() * vs2.getAmount()) +
(vs3.getSnack().getPrice() * vs3.getAmount()))
return value;
}
void MiniVend::buySnack(int slot);
{
if (slot == 0 && vs0.getAmount >= 1)
{
money = money + vs0.getSnack.getPrice();
vs0.decrementAmount();
}
else if (slot == 1 && vs1.getAmount >= 1)
{
money = money + vs1.getSnack.getPrice();
vs1.decrementAmount();
}
else if (slot == 2 && vs2.getAmount >= 1)
{
money = money + vs2.getSnack.getPrice();
vs2.decrementAmount();
}
else if (slot == 3 && vs3.getAmount >= 1)
{
money = money + vs3.getSnack.getPrice();
vs3.decrementAmount();#include "Snack.hpp"
}
else
cout << "Sold out. Please select another Snack." << endl;
}
Main
#include "MiniVend.hpp"
#include "Snack.hpp"
#include "VendSlot.hpp"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Snack s2("candy bar", 1.25, 300);
Snack s3("root beer", 2.00, 450);
VendSlot groucho(s1, 2);
VendSlot harpo(s2, 1);
VendSlot chico(s3, 0);
VendSlot zeppo; // five bottles of water
MiniVend machine(groucho, harpo, chico, zeppo, 0);
cout << machine.numEmptySlots() << endl;
cout << machine.valueOfSnacks() << endl;
cout << machine.getMoney() << endl;
machine.buySnack(1);
cout << machine.numEmptySlots() << endl;
cout << machine.valueOfSnacks() << endl;
cout << machine.getMoney() << endl;
return 0;
}
At MiniVend.hpp your buySnack does not take any parameters void buySnack();, but at Minivend.cpp you are defining an argument at your function header
void MiniVend::buySnack(int slot);, and you shouldn't be using an semicolon too.
Your MiniVend.cpp file should also include the "MiniVend.hpp" header as well.
At MiniVend.cpp in the declaration of function int MiniVend::numEmptySlots() vs0 is not defined anywhere in the code as well as slots
At you main function you are trying to declare this VendSlot groucho(s1, 2);, but you have not defined s1 anywhere in your main function
That's what I have found so far. Hope this helps and please use comments when posting a question so that everyone can understand the purpose of the code.
I'm a noobie at C++ and I was making a game for practice on Visual Studios and I just couldn't figure out how to update stats when exp was added. I tried changing the player level by adding exp but when I add 55 exp the player remained at level 1 still.
Main:
#include <iostream>
#include <windows.h>
#include "Game.h"
using namespace std;
void FalseLoad();
int main() {
//Cool load intro
FalseLoad();
cout << "\n \n";
Game::Game();
system("PAUSE");
return 0;
}
void FalseLoad() {
int i = 0;
int start;
cout << "***Freelancer*** \n \n";
system("PAUSE");
while (i <= 100){
cout << "Loading game... " << i << "% \n";
i++;
Sleep(110 - i);
if (i == 100) {
start = 0;
}
}
}
Game.cpp:
#include <iostream>
#include "Game.h"
using namespace std;
Game::Game() {
Player Player;
Player.Init();
cout << Player.exp << " " << Player.level;
Player.exp += 55;
cout << " " << Player.exp << " " << Player.level << " ";
}
Game.h:
#pragma once
#include "Player.h"
class Game {
public:
Game();
};
Player.cpp:
#include "Player.h"
Player::Player() {
}
void Player::Init() {
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
}
Player.h:
#pragma once
class Player
{
public:
Player();
void Init();
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
};
If you add 55 to exp, only exp will be changed.
You can write getters and setters and declare the member variables private:
Player.h
#pragma once
class Player
{
public:
Player();
void Init();
void addExp(const int additionalExp);
int getExp();
//... TODO add similar get/set methods for the other members...
private:
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
};
and add the method definitions:
#include "Player.h"
Player::Player() {
}
void Player::Init() {
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
}
void Player::addExp(const int additionalExp) {
if ( additionalExp < 0 ) return; // think about error handling or use
// unsigned for exp
exp += additionalExp;
level = exp / 50; // or something else, as you like.
}
int Player::getExp(){ return exp; }
// ... TODO add definitions for the other get/set methods...
And use the addExp() method in your main.cpp.
One benefit of having the member variables private is that you get more control in how they get manipulated. E.g. if you add exp, you can set level accordingly simultaneously.