I'm making a Wizard game prototype and I came across an issue:
I created a class called Wizard and a class called Goblin:
class Wizard
{
public:
int damage;
int health;
int stamina;
Wizard(int d, int h, int s)
{
d = damage;
h = health;
s = stamina;
}
float getDamage() { return damage; }
void setDamage(int v) { v = damage; }
float getHealth() { return health; }
void setHealth(int v) { v = health; }
float getStamina() { return stamina; }
void setStamina(float v) { v = stamina; }
};
class Goblin
{
public:
int health;
int damage;
Goblin(int h, int d)
{
h = health;
d = damage;
}
float getHealth() { return health; }
void setHealth(int v) { v = health; }
float getDamage() { return damage; }
void setDamage(int v) { v = damage; }
};
Then, I declared and initialized variables for them:
Wizard wizard(50, 150, 10);
Goblin goblin(150, 25);
Then, I created an attack() function:
void attack(Wizard attacker, Goblin target)
{
if(target.getDamage() > 0.0)
{
attacker.setStamina(attacker.getStamina() - 1);
}
}
So, in the main function, I called attack and printed out the results:
int main()
{
attack(wizard, goblin);
printInfo();
return 0;
}
But this is the result:
> clang++-7 -pthread -std=c++17 -o main main.cpp
> ./main
Wizard Health: 0
Wizard Damage: 0
Wizard Stamina: 0
Goblin Damage: 0
Goblin Health: 0
I also created a printInfo() function:
void printInfo()
{
std::cout << "Wizard Health: " << wizard.getHealth() << std::endl;
std::cout << "Wizard Damage: " << wizard.getDamage() << std::endl;
std::cout << "Wizard Stamina: " << wizard.getStamina() << std::endl;
std::cout << std::endl;
std::cout << "Goblin Damage: " << goblin.getHealth() << std::endl;
std::cout << "Goblin Health: " << goblin.getDamage() << std::endl;
}
Here is the full file if you need it:
#include <iostream>
class Wizard
{
public:
int damage;
int health;
int stamina;
Wizard(int d, int h, int s)
{
d = damage;
h = health;
s = stamina;
}
float getDamage() { return damage; }
void setDamage(int v) { v = damage; }
float getHealth() { return health; }
void setHealth(int v) { v = health; }
float getStamina() { return stamina; }
void setStamina(float v) { v = stamina; }
};
class Goblin
{
public:
int health;
int damage;
Goblin(int h, int d)
{
h = health;
d = damage;
}
float getHealth() { return health; }
void setHealth(int v) { v = health; }
float getDamage() { return damage; }
void setDamage(int v) { v = damage; }
};
Wizard wizard(50, 150, 10);
Goblin goblin(150, 25);
void attack(Wizard attacker, Goblin target);
void printInfo();
int main()
{
attack(wizard, goblin);
printInfo();
return 0;
}
void attack(Wizard attacker, Goblin target)
{
if(target.getDamage() > 0.0)
{
attacker.setStamina(attacker.getStamina() - 1);
}
}
void printInfo()
{
std::cout << "Wizard Health: " << wizard.getHealth() << std::endl;
std::cout << "Wizard Damage: " << wizard.getDamage() << std::endl;
std::cout << "Wizard Stamina: " << wizard.getStamina() << std::endl;
std::cout << std::endl;
std::cout << "Goblin Damage: " << goblin.getHealth() << std::endl;
std::cout << "Goblin Health: " << goblin.getDamage() << std::endl;
}
Sorry if this is very long, but help would be appreciated.
Your attack() function takes parameters by value. As such it modifies a copy of your global variables.
Use references:
void attack(Wizard& attacker, Goblin& target)
{
...
}
PS: see also https://en.cppreference.com/w/cpp/language/constructor to learn about member initializer lists
In your constructors and set* methods you need to assign input parameter values to the member variables of classes. Not vice versa.
#include <iostream>
class Wizard
{
public:
int damage;
int health;
int stamina;
Wizard(int d, int h, int s)
{
damage = d;
health = h;
stamina = s;
}
float getDamage() { return damage; }
void setDamage(int v) { damage = v; }
float getHealth() { return health; }
void setHealth(int v) { health = v; }
float getStamina() { return stamina; }
void setStamina(float v) { stamina = v; }
};
class Goblin
{
public:
int health;
int damage;
Goblin(int h, int d)
{
health = h;
damage = d;
}
float getHealth() { return health; }
void setHealth(int v) { health = v; }
float getDamage() { return damage; }
void setDamage(int v) { damage = v; }
};
Wizard wizard(50, 150, 10);
Goblin goblin(150, 25);
void attack(Wizard attacker, Goblin target);
void printInfo();
int main()
{
attack(wizard, goblin);
printInfo();
return 0;
}
void attack(Wizard attacker, Goblin target)
{
if(target.getDamage() > 0.0)
{
attacker.setStamina(attacker.getStamina() - 1);
}
}
void printInfo()
{
std::cout << "Wizard Health: " << wizard.getHealth() << std::endl;
std::cout << "Wizard Damage: " << wizard.getDamage() << std::endl;
std::cout << "Wizard Stamina: " << wizard.getStamina() << std::endl;
std::cout << std::endl;
std::cout << "Goblin Damage: " << goblin.getHealth() << std::endl;
std::cout << "Goblin Health: " << goblin.getDamage() << std::endl;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I have tried to run the program with more than one inheritance i got many errors
For this program ,but when i use it for Source File, it will looks good
I don't know why but I think because i update my version or does make any sense
please help me with my project
int main
#include"Shapes.h"
#include"Cylinder.h"
#include"Sphere.h"
#include"Triangle.h"
#include"Square.h"
#define MAX_SHAPES 100
int main() {
ofstream myfile;
myfile.open("Shapes.dat");
Shape* shapes[MAX_SHAPES];
int currentShapes = 0;
int n = 0;
cout << "Select any Number to calculate : \n";
cout << "1. Square\n";
cout << "2. Triangle\n";
cout << "3. Sphere\n";
cout << "4. Cylinder\n";
cout << "Enter your choice: ";
cin >> n;
cin.ignore();
if (n == 1) {
Square* obj2 = new Square();
obj2->readData();
shapes[currentShapes] = obj2;
}
if (n == 2) {
Triangle* obj3 = new Triangle();
obj3->readData();
shapes[currentShapes] = obj3;
}
if (n == 3) {
Sphere* obj5 = new Sphere();
obj5->readData();
shapes[currentShapes] = obj5;
}
if (n == 4) {
Cylinder* obj7 = new Cylinder();
obj7->readData();
obj7->computeSurfaceArea();
obj7->computeVolume();
obj7->print();
obj7->printToFile();
shapes[currentShapes] = obj7;
}
return 0;
}
shape.h
class Shape {
private:
string color, name;
public:
// default constructor
Shape()
{
color = 1.0;
name = "";
cout << "Base Class is Shape for this element . \n";
}
// parameterized constructor
Shape(string color, string name) {
this->color = color;
this->name = name;
}
// read data
void readData() {
cout << "Enter color: ";
getline(cin, color);
cout << "Enter name: ";
getline(cin, name);
}
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
shape2d.h
class Shape2D : public Shape {
public:
double area;
double perimeter;
Shape2D() :Shape()
{
cout << "Parent Class is Shape2D for this element .\n";
}
// parameterized constructor
Shape2D(string color, string name) :Shape(color, name) {
}
// read data
void readData() {
Shape::readData();
}
virtual void computeArea() = 0;
virtual void computePerimeter() = 0;
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
Shape3d
class Shape3D : public Shape {
public:
double surfaceArea;
double volume;
Shape3D() :Shape()
{
surfaceArea = 1.0;
volume = 1.0;
cout << "Parent Class is Shape3D for this element .\n";
}
// parameterized constructor
Shape3D(string color, string name) :Shape(color, name) {
}
void readData() {
Shape::readData();
}
virtual void computeSurfaceArea() = 0;
virtual void computeVolume() = 0;
virtual void print() = 0;
virtual void printToFile() = 0;
virtual void readFromFile(ifstream& input) = 0;
};
Square.h
class Square : public Shape2D {
private:
double side;
public:
Square() :Shape2D()
{
side = 1.0;
cout << "Calculating square area and perimeter \n";
}
// parameterized constructor
Square(string color, string name, double side) :Shape2D(color, name) {
this->side = side;
}
void readData() {
Shape2D::readData();
cout << "Square Side: ";
cin >> side;
cin.ignore();
computeArea();
computePerimeter();
}
void computeArea()
{
area = side * side;
}
void computePerimeter() {
perimeter = 4 * side;
}
void print(string x, double y)
{
cout << x << " of Square = " << y << "\n";
}
void print() {
print("Area: ", area);
print("Perimeter", perimeter);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Square = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Area: ", area);
printToFile("Perimeter", perimeter);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Triangle.h
class Triangle : public Shape2D {
private:
double base;
double height;
public:
Triangle() :Shape2D()
{
cout << "This is a Triangle\n";
}
// parameterized constructor
Triangle(string color, string name, double base, double height) :Shape2D(color, name) {
this->base = base;
this->height = height;
}
void readData() {
Shape2D::readData();
cout << "Triangle Base: ";
cin >> base;
cout << "Triangle Height: ";
cin >> height;
cin.ignore();
computeArea();
computePerimeter();
}
void computeArea() {
area = (height * base) / 2;
}
void computePerimeter() {
perimeter = 2 * height + base;
}
void print(string x, double y)
{
cout << x << " of Triangle = " << y << "\n";
}
void print() {
print("Area: ", area);
print("Perimeter: ", perimeter);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Triangle = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Area: ", area);
printToFile("Perimeter", perimeter);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Sphere.h
class Sphere : public Shape3D {
private:
double radius;
public:
Sphere() :Shape3D()
{
radius = 1.0;
cout << "This is a Sphere\n";
}
// parameterized constructor
Sphere(string color, string name, double side) :Shape3D(color, name) {
this->radius = radius;
}
void readData() {
Shape3D::readData();
cout << "Sphere Radius: ";
cin >> radius;
cin.ignore();
computeSurfaceArea();
computeVolume();
}
void computeSurfaceArea() {
surfaceArea = 4 * 3.14 * radius * radius;
}
void computeVolume() {
volume = (4 / 3) * (3.14 * radius * radius * radius);
}
void print(string x, double y)
{
cout << x << " of Sphere = " << y << "\n";
}
void print() {
print("Surface Area: ", surfaceArea);
print("Volume: ", volume);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Sphere = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Surface Area: ", surfaceArea);
printToFile("Volume", volume);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
Cylinder .h
class Cylinder : public Shape3D {
private:
double radius;
double height;
public:
Cylinder() :Shape3D()
{
radius = 1.0;
height = 1.0;
cout << "This is a Cylinder\n";
}
// parameterized constructor
Cylinder(string color, string name, double radius, double height) :Shape3D(color, name) {
this->radius = radius;
this->height = height;
}
void readData() {
Shape3D::readData();
cout << "Cylinder Radius: ";
cin >> radius;
cout << "Cylinder Height: ";
cin >> height;
cin.ignore();
//computeSurfaceArea();
//computeVolume();
}
void computeSurfaceArea() {
surfaceArea = (2 * 3.14 * radius) * (radius + height);
}
void computeVolume() {
volume = 3.14 * radius * radius * height;
}
void print(string x, double y)
{
cout << x << " of Cylinder = " << y << "\n";
}
void print() {
print("Surface Area: ", surfaceArea);
print("Volume: ", volume);
}
void printToFile(string x, double y)
{
ofstream ofs;
ofs.open("shapes.dat", ios_base::app);
if (!ofs) {
cout << "Error opening file" << endl;
}
cout << "Shapes.dat file updated .";
ofs << x << " of Cylinder = " << y << "\n";
ofs.close();
}
void printToFile() {
printToFile("Surface Area: ", surfaceArea);
printToFile("Volume", volume);
}
void readFromFile(ifstream& input)
{
string data = "";
while (std::getline(input, data))
{
std::cout << data << endl;
}
}
};
In shape2d.h and shape3d.h with have to add
#include "shape.h"
In square.h and triangle.h with have to add
#include "shape2d.h"
In sphere.h and cylinder.h you have to add
#include "shape3d.h"
And to avoid multiple inclusions add at the beginning of each .h file
#pragma once
or if your compiler doesn't support it enclose the hole code inside any .h in something like
#ifndef YOUR_H_FILE_NAME_H
#define YOUR_H_FILE_NAME_H
// ... your .h code here
#endif
Further in main.cpp you include shapes.h which doesn't exist but you can remove it because you already include all the specilized .h file
The code does not reduce the default health value, just prints default value out. It does not call GetName() and GetHealth() functions that are inside the TakeDamage() function. The code can run without problem. Screen out put is below the code. Where is the problem here? How do I fix?
#include<iostream>
using namespace std;
class Creature
{
public:
Creature();
void setName(string name);
void TakeDamage(float Damage);
string GetName();
float GetHealth();
private:
string Name;
float Health;
};
class Voldemort : public Creature
{
private:
void AvadaKedavra();
public:
void MakeMagic();
};
void Creature::setName(string name)
{
Name = name;
cout << name << endl;
}
int main()
{
Voldemort Anger;
Anger.MakeMagic();
Anger.setName("Harry Potter");
return 0;
}
Creature::Creature()
{
cout << "A creature has been created" << endl;
Health=100;
}
string Creature::GetName()
{
return Name;
}
float Creature::GetHealth()
{
return Health;
}
void Creature::TakeDamage(float Damage)
{
float Total;
Total = Health - Damage;
if (Total <= 0.f)
{
cout << GetName() << " has died" << endl;
}
else
{
Health -= Damage;
}
cout << "Health: " << Health << endl;
cout << "Health: " << GetHealth() << endl;
}
void Voldemort::MakeMagic()
{
AvadaKedavra();
}
void Voldemort::AvadaKedavra()
{
TakeDamage(100);
}
Screen:
A creature has been created
has died
Health: 100
Health: 100
Harry Potter
if (Total <= 0.f)
{
cout << GetName() << " has died" << endl;
}
else
{
Health -= Damage;
}
You don't change the health when Total is less or equal 0, which is the case when you call TakeDamage(100)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
below is my code:
class Pulley{
private:
int noOfTeeth;
public:
Pulley();
Pulley(int teeth);
~Pulley();
void show();
};
Pulley::Pulley()
{
cout << "Pulley constructor called!";
noOfTeeth = 0;
}
Pulley::Pulley(int teeth)
{
cout << "Pulley constructor called!";
noOfTeeth = teeth;
}
void Pulley::show()
{
cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
Pulley::~Pulley()
{
}
class GearBox{
private:
char *transmission;
Pulley p;
public:
GearBox();
GearBox(char *trans, int pTeeth);
~GearBox();
void show();
};
GearBox::GearBox(): p()
{
cout << "Gearbox constructor called!";
transmission = NULL;
}
GearBox::GearBox(char *trans, int pTeeth): p(pTeeth)
{
cout << "Gearbox constructor called!";
if(trans != NULL)
{
transmission = new char[strlen(trans)+1];
strcpy(transmission, trans);
}
else
{
transmission = NULL;
}
}
void GearBox::show()
{
cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
GearBox::~GearBox()
{
}
class Vehicle{
private:
char *model;
char *color;
GearBox g;
public:
Vehicle();
Vehicle(char *mod, char *col, char *gr);
Vehicle(const Vehicle &vh);
~Vehicle();
void show();
};
Vehicle::Vehicle(): g()
{
cout << "Vehicle constructor called!";
model = NULL;
color = NULL;
}
Vehicle::Vehicle(char *mod, char *col, char *gr): g(gr)
{
cout << "Vehicle constructor called!";
if(mod != NULL)
{
model = new char[strlen(mod)+1];
strcpy(model, mod);
}
else
{
model = NULL;
}
if(col != NULL)
{
color = new char[strlen(col)+1];
strcpy(color, col);
}
else
{
color = NULL;
}
}
void Vehicle::show()
{
cout << "\n\nModel of Vehicle: " << model;
cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
system("PAUSE");
}
Now, when I run this code I get a lots of error, I don't know what are those and how to resolve them. The one error I understood is of Copy Constructor, so can anyone explain me that how can I write the copy constructor for pointer to characters transmission, model and color. Also tell me how do I resolve other errors. Thanks a lot.
Try like this:
#include <string>
#include <iostream>
class Pulley{
private:
int noOfTeeth;
public:
Pulley(int teeth = 0);
void show();
};
Pulley::Pulley(int teeth)
: noOfTeeth(teeth)
{
std::cout << "Pulley constructor called!";
}
void Pulley::show()
{
std::cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
class GearBox{
private:
std::string transmission;
Pulley p;
public:
GearBox(std::string trans = std::string(), Pulley p = Pulley());
void show();
};
GearBox::GearBox(std::string trans, Pulley p)
: transmission(std::move(trans))
, p(std::move(p))
{
std::cout << "Gearbox constructor called!";
}
void GearBox::show()
{
std::cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
class Vehicle{
private:
std::string model;
std::string color;
GearBox g;
public:
Vehicle();
Vehicle(std::string mod = "", std::string col = "", GearBox gr = GearBox());
void show();
};
Vehicle::Vehicle(std::string mod,std::string col, GearBox gr)
: model(std::move(mod))
, color(std::move(col))
, g(std::move(gr))
{
std::cout << "Vehicle constructor called!";
}
void Vehicle::show()
{
std::cout << "\n\nModel of Vehicle: " << model;
std::cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
// system("PAUSE");
}
At the moment my program is crashing after displaying one line out of the 250 it is supposed to display. Here is my code:
string MovieList::PrintAll() {
for (int i = 0; i < last_movie_index; i++) {
movies[last_movie_index].Movie::PrintMovie();
}
}
string Movie::PrintMovie() {
cout << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
year_made << ")" << "\t" << name << endl;
}
Full Movie and MovieList class:
class Movie {
public:
Movie();
Movie(string n, int y, int v, double r);
string get_name();
void set_name(string n);
int get_year();
void set_year(int y);
int get_votes();
void set_votes(int v);
double get_rating();
void set_rating(double r);
string PrintMovie();
private:
string name;
int year_made;
int votes;
double rating;
};
Movie::Movie() {
name = "null";
year_made = 0;
votes = 0;
rating = 0.0;
}
Movie::Movie(string n, int y, int v, double r) {
name = n;
year_made = y;
votes = v;
rating = r;
}
string Movie::get_name() {
return name;
}
void Movie::set_name(string n) {
name = n;
}
int Movie::get_year() {
return year_made;
}
void Movie::set_year(int y) {
year_made = y;
}
int Movie::get_votes() {
return votes;
}
void Movie::set_votes(int v) {
votes = v;
}
double Movie::get_rating() {
return rating;
}
void Movie::set_rating(double r) {
rating = r;
}
string Movie::PrintMovie() {
cout << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
year_made << ")" << "\t" << name << endl;
}
class MovieList {
public:
MovieList(int size);
~MovieList();
int Length();
bool IsFull();
void Add(Movie const& m);
string PrintAll();
private:
Movie* movies;
int last_movie_index;
int movies_size;
int movie_count = 0;
};
MovieList::MovieList(int size) {
movies_size = size;
movies = new Movie[movies_size];
last_movie_index = -1;
}
MovieList::~MovieList() {
delete [] movies;
}
int MovieList::Length() {
return last_movie_index;
}
bool MovieList::IsFull() {
return last_movie_index == movies_size;
}
void MovieList::Add(Movie const& m)
{
if (IsFull()) {
cout << "Cannot add movie, list is full" << endl;
return;
}
last_movie_index++;
movies[last_movie_index] = m;
}
string MovieList::PrintAll() {
for (int i = 0; i < last_movie_index; i++) {
movies[i].Movie::PrintMovie();
}
}
My array movies is dynamically allocated (i.e movies = new Movie[movies_size];). I noticed that using cout << movies[1] << endl will not work in the PrintAll function. Is this why it is crashing possibly? And what can I do to fix it?
This won't solve your problem, but if you want to make cout << movies[i] << endl work, then you need to define the following function in the Movie class:
friend ostream& operator<<(ostream& ostr, const Movie& movie){
ostr << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
year_made << ")" << "\t" << name ;
}
My cpp file, for some reason I can print everything else beside the name of the book i think its my main file. I don't get any errors when I build it but when I run it the little window comes ups and goes away
#include "BookRecord.h"
BookRecord::BookRecord()
{
m_sName[128]=NULL;
m_lStockNum=0;
m_iClassification = 0;
m_dCost = 0.0;
m_iCount = 0;
}
BookRecord::BookRecord(char *name, long sn, int cl, double cost)
{
m_iCount=1;
m_sName[128]=*name;
m_lStockNum=sn;
m_iClassification=cl;
m_dCost=cost;
}
BookRecord::~BookRecord()
{
}
void BookRecord::getName(char *name)
{
strcpy(name, m_sName);
}
void BookRecord::setName(char *name)
{
strcpy(m_sName, name);
}
long BookRecord::getStockNum()
{
return m_lStockNum;
}
void BookRecord::setStockNum(long sn)
{
m_lStockNum = sn;
}
void BookRecord::getClassification(int& cl)
{
cl=m_iClassification;
}
void BookRecord::setClassification(int cl)
{
m_iClassification= cl;
}
void BookRecord::getCost(double *c)
{
*c = m_dCost;
}
void BookRecord::setCost(double c)
{
m_dCost = c;
}
int BookRecord::getNumberInStock()
{
return m_iCount;
}
void BookRecord::setNumberInStock(int count)
{
count = m_iCount;
}
void BookRecord::printRecord()
{
//cout << "Name: " <<m_sName <<"\n";
printf("Name: %s", m_sName);
cout<<endl;
cout<<"Stock Number: "<<m_lStockNum<<"\n";
cout<<"Class: " <<m_iClassification<<"\n";
cout<<"Cost: $"<<m_dCost<<"\n";
cout<<"In Stock: "<<m_iCount<<"\n";
}
My main file
#include "BookRecord.h"
int main()
{
char *bName="C Plus Plus";
BookRecord *Ana = new BookRecord(bName, 1, 1, 50.9);
/*char * bookName="";
int clNo;
double c;
Ana->getClassification(clNo);
Ana->getCost(&c);
Ana->getName(bookName);*/
cout << "Printing using printRecord() Function" << endl;
Ana->printRecord();
/*cout << endl << "Printing using getter properties" <<endl;
cout << bookName << endl;
cout<< clNo << endl;
cout << c << endl;
cout << Ana->getNumberInStock() << endl;*/
return 0;
}
Hi I would have done this a bit different, not using char m_sName[128], I would rather use std::string or a pointer and just destroyed it in the desctructor. I base my code here on what you have already requested above, and I'm making it as simple as possible. I know things could be done better here, but here you go:
EDIT: Refactored a bit and question grew.
BookRecord.h
#ifndef __BOOKRECORD_H__
#define __BOOKRECORD_H__
#ifndef MAX_NAME_SIZE
#define MAX_NAME_SIZE 128
#endif
class BookRecord
{
private:
char m_sName[MAX_NAME_SIZE] = {}; // I would have used a pointer or std:string and not char
long m_lStockNum = 0;
int m_iClassification = 0;
double m_dCost = 0.0;
int m_iCount = 0;
public:
BookRecord();
BookRecord(char* name, long sn, int cl, double cost);
~BookRecord();
// EDIT: Your question grew
char* GetName();
void SetName(char *name);
long GetStockNum();
void SetStockNum(long sn);
int GetClassification();
void SetClassification(int cl);
double GetCost();
void SetCost(double c);
int GetNumberInStock();
void SetNumberInStock(int count);
void PrintRecord();
};
#endif
BookRecord.cpp
#include <stdlib.h>
#include <iostream>
#include <stdexcept>
#include "BookRecord.h"
using namespace std;
BookRecord::BookRecord()
{
}
BookRecord::BookRecord(char* name, long sn, int cl, double cost) :
m_iCount(1), m_lStockNum(sn), m_iClassification(cl), m_dCost(cost)
{
if (name == NULL)
throw std::invalid_argument("Name of book is null");
if (strlen(name)>MAX_NAME_SIZE)
throw std::invalid_argument("Name of book is to long");
memset(&m_sName, 0, sizeof(char)*MAX_NAME_SIZE);
memcpy(&m_sName, name, strlen(name)*sizeof(char));
}
BookRecord::~BookRecord()
{
}
// Edit: Question grew
char* BookRecord::GetName()
{
return static_cast<char*>(m_sName);
}
void BookRecord::SetName(char *name)
{
strcpy(m_sName, name); // TODO: handle null, and sizechecks here to your likings
}
long BookRecord::GetStockNum()
{
return m_lStockNum;
}
void BookRecord::SetStockNum(long sn)
{
m_lStockNum = sn;
}
int BookRecord::GetClassification()
{
return m_iClassification;
}
void BookRecord::SetClassification(int cl)
{
m_iClassification = cl;
}
double BookRecord::GetCost()
{
return m_dCost;
}
void BookRecord::SetCost(double c)
{
m_dCost = c;
}
int BookRecord::GetNumberInStock()
{
return m_iCount;
}
void BookRecord::SetNumberInStock(int count)
{
m_iCount = count;
}
void BookRecord::PrintRecord()
{
cout << static_cast<const char*>(m_sName) << endl;
cout << "Stock Number: " << m_lStockNum << endl;
cout << "Class: " << m_iClassification << endl;
cout << "Cost: $" << m_dCost << endl;
cout << "In Stock: " << m_iCount << endl;
}
If you are wondering about the exception throwing have a look at this post.
Hope it helps