How do I write the copy Constructor for Composition? [closed] - c++

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

Related

Why can't I update my class properties in C++?

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

taking string input as parameter in constructor in OOP

This code is showing error when I make an instance of the class website. The instance has to use the explicit constructor which I have defined in the class definition. So I am passing a string value in it. This value is being received by an array of character type in the constructor which then initializes a pointer pointing to that array. Please avoid giving complex answers.
#include<iostream>
#include<conio.h>
using namespace std;
class Links
{
private:
char *linkname;
public:
Links()
{
cout << "Links default constructor called." << endl;
};
Links(char n[]):linkname(n)
{
cout << "Links parameterized constructor called." << endl;
};
char getlinkname()
{
return *linkname;
}
void setlinkname(char n[])
{
linkname = n;
}
};
class Webpage
{
private:
double width;
double height;
Links link1;
Links link2;
public:
Webpage()
{
cout << "Webpage default constructor called." << endl;
};
Webpage(double w, double h) :width(w), height(h)
{
cout << "Webpage parameterized constructor called." << endl;
};
double getheight()
{
return height;
}
double getwidth()
{
return width;
}
void setheight(double h)
{
height = h;
}
void setwidth(double w)
{
width = w;
}
};
class Website
{
private:
char *name;
Webpage webpage1{24.5,37.2};
Webpage webpage2;
Webpage webpage3{10,18.7};
Webpage webpage4;
public:
Website()
{
cout << "Website default constructor called." << endl;
};
Website(char n[]):name(n)
{
cout << "Website parameterized constructor called." << endl;
};
char getname()
{
return *name;
}
void setname(char n[])
{
name = n;
}
};
int main()
{
Website w1("www.google.com");
_getch();
return 0;
}
String literals are 'const', try this:
class Website
{
private:
const char *name;
Webpage webpage1{24.5,37.2};
Webpage webpage2;
Webpage webpage3{10,18.7};
Webpage webpage4;
public:
Website()
{
cout << "Website default constructor called." << endl;
};
Website(const char* n):name(n)
{
cout << "Website parameterized constructor called." << endl;
};
const char* getname()
{
return name;
}
void setname(const char* n)
{
name = n;
}
};

How to check which objects in vector in C++

Well, Hospital is the class which has vector of patients.
FemaleIn, FemaleOut, MaleIn, MaleOut are derived classes from patients(Base class). Those classes have toString function(method). What I am trying to do is, in display method in Hospital class, I just want to display only in case of Outpatient which is parent class of FemaleOut and Maleout or Inpatient which is parent class of FemaleIn and MaleIn. From what I am thinking to call only specific method from, for example, Outpatient, I will have to know which objects in which index of vector for automatically. Is there any idea to call only toString for specific class which is, for example, FemaleIn and MaleIn where parent class is Inpatient. Thank you for your any help or suggestion.
void Hospital::determinePatientType()
{
int selection;
cout << endl;
cout << "What is the patient type?" << endl;
cout << "1. Female Inpatient" << endl;
cout << "2. Female Outpatient" << endl;
cout << "3. Male Inpatient" << endl;
cout << "4. Male Outpatient" << endl;
cout << endl;
cin >> selection;
switch(selection)
{
case 1:
patients.push_back(new FemaleIn());
cout << patients.back() << endl;
patients[totalPatients]->enterPatientData();
totalPatients++;
break;
case 2:
patients.push_back(new FemaleOut());
cout << patients.back() << endl;
patients[totalPatients]->enterPatientData();
totalPatients++;
break;
case 3:
patients.push_back(new MaleIn());
cout << patients.back() << endl;
patients[totalPatients]->enterPatientData();
totalPatients++;
break;
case 4:
patients.push_back(new MaleOut());
cout << patients.back() << endl;
patients[totalPatients]->enterPatientData();
totalPatients++;
break;
default:
return;
}
}
void Hospital::display(string type)
{
cout << "Patient Name Spouse Name Sex Patient Type Unit/Appt. Date Diagnosis" << endl;
cout << "===================================================================================" << endl;
if(type=="All")
{
for(int i=0;i<patients.size();i++)
{
patients[i]->toString();
}
}
else if(type=="Outpatient")
{
for(int i=0;i<patients.size();i++)
{
patients[i]->toString();
}
}
else
{
for(int i=0;i<patients.size();i++)
{
patients[i]->toString();
}
}
}
I think this question might be similar to How do I check if an object's type is a particular subclass in C++? .
I would propose something like:
Class Patient{
virtual bool display(string filter);
};
Class OutPatient : Patient {
bool display(string filter) override;
};
bool OutPatient::display(string filter) {
if (filter != "OutPatient")
return false;
//Do stuff
return true;
}
Class InPatient : Patient {
bool display(string filter) override;
};
// You could just make this the default definition on display on Patient
bool InPatient::display(string filter) {
return false;
}
And then:
void Hospital::display(string type)
{
for(auto& patient: patients)
patient->display(type);
}
As quick and dirty way you can use dynamic_cast to certain class pointer to detect whether given instance is of that class:
if (type == "Outpatient")
{
for(int i=0; i<patients.size(); ++i)
{
// try to cast parent class pointer to one of child class
// if one is pointer to that child class p is not nullptr
// otherwise p is nullptr
Outpatient * p = dynamic_cast<Outpatient *>(patients[i]);
if (p) {
p->toString();
}
}
}
For clean way you could use Visitor pattern
Visitor pattern implementation:
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <utility>
class AbstractDirectionPatientsDispatcher;
class AbstractGenderPatientDispatcher;
class Patient
{
public:
virtual void accept(AbstractDirectionPatientsDispatcher &dispatcher) = 0;
virtual void accept(AbstractGenderPatientDispatcher &dispatcher) = 0;
std::string name;
};
class InPatient;
class OutPatient;
class AbstractDirectionPatientsDispatcher {
public:
virtual void dispatch(InPatient &patient) = 0;
virtual void dispatch(OutPatient &patient) = 0;
};
class FemalePatient;
class MalePatient;
class AbstractGenderPatientDispatcher {
public:
virtual void dispatch(FemalePatient &patient) = 0;
virtual void dispatch(MalePatient &patient) = 0;
};
template <typename PatientClass, typename Dispatcher>
class CRTPDispatchApplier : virtual public Patient
{
public:
void accept(Dispatcher &dispatcher) override {
dispatcher.dispatch(static_cast<PatientClass &>(*this));
}
};
class InPatient : public CRTPDispatchApplier<InPatient, AbstractDirectionPatientsDispatcher>
{
};
class OutPatient : public CRTPDispatchApplier<OutPatient, AbstractDirectionPatientsDispatcher>
{
};
class FemalePatient : public CRTPDispatchApplier<FemalePatient, AbstractGenderPatientDispatcher>
{
};
class MalePatient : public CRTPDispatchApplier<MalePatient, AbstractGenderPatientDispatcher>
{
};
class InFemale : public FemalePatient, public InPatient
{
};
class OutFemale : public FemalePatient, public OutPatient
{
};
class InMale : public MalePatient, public InPatient
{
};
class OutMale : public MalePatient, public OutPatient
{
};
class DummyDirectionDispatecher : public AbstractDirectionPatientsDispatcher
{
public:
void dispatch(InPatient & ) override {
}
void dispatch(OutPatient & ) override {
}
};
class DummyGenderDispatcher : public AbstractGenderPatientDispatcher
{
public:
void dispatch(FemalePatient &) override {
}
void dispatch(MalePatient &) override {
}
};
template <typename Direction>
class DispatchByDirection : public DummyDirectionDispatecher
{
public:
DispatchByDirection(std::function<void(Direction &)> action) :
m_action(std::move(action))
{}
void dispatch(Direction & p) override {
m_action(p);
}
private:
std::function<void(Direction &)> m_action;
};
template <typename Gender>
class DispatchByGender : public DummyGenderDispatcher
{
public:
DispatchByGender(std::function<void(Gender &)> action) :
m_action(std::move(action))
{}
void dispatch(Gender & p) override {
m_action(p);
}
private:
std::function<void(Gender &)> m_action;
};
int main() {
InFemale f1;
OutFemale f2;
InMale m1;
OutMale m2;
f1.name = "Eve";
f2.name = "Alice";
m1.name = "Bob";
m2.name = "Charlie";
std::vector<Patient *> patients;
patients.push_back(&f1);
patients.push_back(&f2);
patients.push_back(&m1);
patients.push_back(&m2);
DispatchByDirection<InPatient> print_in_patients{[](InPatient &patient){
std::cout << "in: " << patient.name << std::endl;
}};
for (auto p : patients) {
p->accept(print_in_patients);
}
std::cout << std::endl;
DispatchByDirection<OutPatient> print_out_patients{[](OutPatient &patient) {
std::cout << "out: " << patient.name << std::endl;
}};
for (auto p : patients) {
p->accept(print_out_patients);
}
std::cout << std::endl;
DispatchByGender<FemalePatient> print_female{[](FemalePatient &patient) {
std::cout << "female: " << patient.name << std::endl;
}};
for (auto p : patients) {
p->accept(print_female);
}
std::cout << std::endl;
DispatchByGender<MalePatient> print_male{[](MalePatient &patient) {
std::cout << "male: " << patient.name << std::endl;
}};
for (auto p : patients) {
p->accept(print_male);
}
std::cout << std::endl;
}

How to get only one shared array in all my classes?

This is my code of a example program. The problem im getting is that when I add a new item to the shoppingCart array, it makes a new copy of the shoppingCart array with the present shoppingindex. How can I add all these items into the same shoppingCart array.
#include <iostream>
#include <iomanip>
using namespace std;
class Product{
private:
protected:
static int shoppingindex;
string shoppingcart[10];
public:
Product()
{
}
void displayShoppingCart() const
{
cout << endl<<endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << " Shopping Cart \n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
for(int i=0;i<10;i++)
{
// if(shoppingcart[i].length()<=1)
// {
//
// }
// else
{
cout << "\nShopping Item " <<"("<< i+1 << ")" << " - " << shoppingcart[i] << endl;
//cout << shoppingindex << endl;
}
}
}
};
class DairyProduct:public Product{
private:
protected:
public:
DairyProduct()
{
}
void AddDairyProduct(string productName)
{
shoppingcart[shoppingindex]=productName;
shoppingindex++;
}
};
class Beverages:public Product{
private:
protected:
public:
Beverage()
{
}
void AddBeverage(string productName)
{
shoppingcart[shoppingindex]=productName;
shoppingindex++;
}
};
class PersonalCare:public Product{
private:
protected:
public:
PersonalCare()
{
}
void AddPersonalCare(string productName)
{
//if conditions in all of them to match with the given dataset
shoppingcart[shoppingindex]=productName;
shoppingindex++;
}
};
class ShoppingCart{
public:
Product *object;
DairyProduct *dairy;
Beverages *bevg;
PersonalCare *personal;
ShoppingCart()
{
object = new Product; //deep copy
dairy=new DairyProduct;
bevg=new Beverages;
personal = new PersonalCare;
}
~ShoppingCart()
{
}
};
int Product::shoppingindex=0;
int main()
{
ShoppingCart user;
user.bevg->AddBeverage("Tea");
user.bevg->displayShoppingCart();
user.personal->AddPersonalCare("Shampoo");
user.personal->displayShoppingCart();
user.dairy->AddDairyProduct("Milk");
user.dairy->displayShoppingCart();
}

I cant print the name of my book but can print everything else

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