I have an issue with a class i have created. When i compiling this so occures error message. Its basically all about char* to string. Here is my class
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Stock //klassdekleration
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot() {total_val = shares * share_val;}
public:
Stock();
Stock(const char * co, int n = 0, double pr = 0.0);
void acquire(const char * co, int n, double pr);
void buy(int num,double price);
void sell(int num, double price);
void update(double price);
void show();
void compare( Stock );
const char * returncompany();
int returnshares();
};
#endif
// class function
//------------------------------------------------------------------------------------------------------
// constructor
Stock::Stock()
{
cout << "pre constructor is called \n";
strcpy(company, "namnlöst");
shares = 0;
share_val = 0;
total_val = 0;
}
Stock::Stock(const char * co, int n, double pr)
{
cout << " Constructor that use " << co << " is called \n";
strcpy("company", co);
company[29] = '\0';
shares = n;
share_val=pr;
set_tot();
}
//---------------------------------------------------------------------------------------------------------
// andra metoddefintioner
void Stock::acquire(const char * co, int n, double pr)
{
strcpy(company, co); //trunkera co om det behövs
company[29]='\0';
if (n<0)
{
cerr << "amount of shares cant be negative "
<< "share put to 0. \n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(int num, double price)
{
if (num < 0)
{
cerr << "Amount of bought shares cant be negative. "
<< "transaction cancelled. ";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(int num, double price)
{
if (num < 0)
{
cerr << "amount of sold shares cant be negative. "
<< "Transaction cancelled. ";
}
else if (num > shares)
{
cerr << "cant sell more shares than you posses"
<< "Transaction cancelled. ";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
cout << "Company: " << company
<< " Shares: " << shares << " \n"
<< " share value: $" << share_val
<< " Total value: $ " << total_val << " \n";
}
int Stock::returnshares()
{
return shares;
}
const char * Stock::returncompany()
{
return company;
}
void Stock::compare(Stock stock2)
{
if (shares < stock2.returnshares())
{
cout << "Company" << stock2.returncompany() << "have higher share value than" << company;
}
else
{
cout << "Company" << company << "have higher share value than" << stock2.returncompany();
}
}
I get this error message.
In constructor ‘Stock::Stock(const char*, int, double)’:
Stock_class.cc:60:23: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
strcpy("company", co);
any idea how i can fix this issue ?
kind regards
Hampus hahne
It seems fairly obvious, you wrote
strcpy("company", co);
when you meant
strcpy(company, co);
Attention to detail is required.
Also
company[29]='\0';
is unnecessary because strcpy always adds a '\0' character, so you don't need to add one as well.
i think in your code strcpy(company,co) will fix you problem.
usage of strcpy was not proper please follow below link for more details:
https://www.geeksforgeeks.org/strcpy-in-c-cpp/
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 1 year ago.
Improve this question
I am writing this program for my class and have run into an issue. There are no errors but when I try to compile (compiling with g++), the program stops outputting to console after the line "Stuart placing order.\n". I know \n doesn't flush the output, so I tried using flush but that didn't work (maybe I did it wrong?). What do I need to do to allow the program to show the full output in console? I'm not really sure where I'm going wrong as I've been trying to figure out the problem for a while now. I'm still relatively new so I apologize if this is a simple fix. Thank you in advance.
output:
Kevin placing order.
Invalid input [Iron Goddess].
Not serving requested drinks. Drink order ignored.
Order Detail:
Kevin
Date: 3/2/2021
Phone: 123-456-0000
Total Balance: $21.75
Ordered Drinks: 4
Balance: $39.279
Discounted Balance: $37.315
Stuart placing order.
main
#include <string>
#include "Order.h"
#include <iostream>
using namespace std;
int main()
{
const Account Stuart("Stuart", "Owner");
Account Kevin("Kevin", "VIP");
Account Bob("Bob", "");
cout << "Kevin placing order.\n";
BobaOrder K("Kevin", 3, 2, 2021, "123-456-0000", 10.4, "Bar Pa Tea", 0.0);
try
{
K.addDrink("Matcha Lemonade", 1, true);
K.addDrink("Lemon Green Tea", 1, false);
K.addDrink("Brown Sugar Oolong Tea", 2, false);
K.addDrink("Iron Goddess", 1, false);
}
catch(InvalidInput& WrongDrink)
{
WrongDrink.cause();
cout << "Not serving requested drinks. Drink order ignored.\n";
}
cout.precision(5);
K.printReceipt();
cout << "Balance: $" << K.calcBalance() << endl;
cout << "Discounted Balance: $" << applyDiscount(&K, &Kevin) << endl << endl;
cout << "Stuart placing order.\n";
FoodOrder S("Stuart", 3, 2, 2021, "123-456-1111", 25.5, "Trauts Steak House", 0.0);
try
{
S.addFood("Bone-in Ribeye", 2, true);
S.addFood("Grilled Salmon", 1, false);
S.addFood("Beyond Meat Burger", 3, true);
}
catch(InvalidInput& WrongFood)
{
WrongFood.cause();
cout << "Not serving requested food. Food order ignored.\n";
}
S.printReceipt();
cout << "Balance: $" << S.calcBalance() << endl;
cout << "Discounted Balance: $" << applyDiscount(&S, &Stuart) << endl << endl;
return 0;
}
cpp file
#include "Order.h"
#include <iostream>
using namespace std;
int DeliveryOrder::orderCount;
const float DeliveryOrder::taxRate = 0.0887;
const float DeliveryOrder::deliveryRate = 1.5;
int BobaOrder::drinksCount;
int FoodOrder::foodCount;
DeliveryOrder::DeliveryOrder(string name, int month, int day, int year, string phone, float miles, float balance = 0.0)
{
DeliveryOrder::name = name;
DeliveryOrder::month = month;
DeliveryOrder::day = day;
DeliveryOrder::year = year;
DeliveryOrder::phone = phone;
DeliveryOrder::miles = miles;
DeliveryOrder::balance = balance;
orderCount++;
}
DeliveryOrder::~DeliveryOrder()
{
cout << "DeliveryOrder destroyed.\n";
}
void DeliveryOrder::printReceipt() const
{
cout << "Order Detail:" << "\n";
cout << "\t" << name << "\n";
cout << "\tDate: " << month << "/" << day << "/" << year << "\n";
cout << "\tPhone: " << phone << "\n";
cout << "\tTotal Balance: $" << balance << "\n";
}
float DeliveryOrder::calcBalance()
{
balance = balance * (1 + taxRate) + miles * deliveryRate;
return balance;
}
float DeliveryOrder::getBalance() const
{
return balance;
}
int DeliveryOrder::getOrderCount()
{
return orderCount;
}
BobaOrder::BobaOrder(string name, int month, int day, int year, string phone, float miles, string shopName, float balance)
:DeliveryOrder(name, month, day, year, phone, miles, balance)
{
BobaOrder::shopName = shopName;
}
BobaOrder::~BobaOrder()
{
cout << "BobaOrder destroyed.\n";
}
void BobaOrder::printReceipt() const
{
DeliveryOrder::printReceipt();
cout << "\tOrdered Drinks: " << drinksCount << endl;
}
float BobaOrder::VIPdiscount() const
{
float discount;
if (drinksCount > 10){discount = 0.85;}
else
{
if (drinksCount > 5){discount = 0.9;}
else
{
if (drinksCount > 2){discount = 0.95;}
else
{
if(drinksCount <= 2){discount = 1;}
}
}
}
return discount;
}
void BobaOrder::addDrink(string drinkName, int sameDrink = 1, bool boba=true)
{
if (drinkName == "Matcha Lemonade"){balance = balance + 5.5 * sameDrink;}
else
{
if (drinkName == "Brown Sugar Oolong Tea"){balance = balance + 5 * sameDrink;}
else
{
if (drinkName == "Lemon Green Tea"){balance = balance + 5.25 * sameDrink;}
else
{
throw InvalidInput(drinkName);
}
}
}
if (boba==true){balance = balance + 1 * sameDrink;}
drinksCount = drinksCount + sameDrink;
}
FoodOrder::FoodOrder(string name, int month, int day, int year, string phone, float miles, string restaurantName, float balance)
:DeliveryOrder(name, month, day, year, phone, miles, balance)
{
FoodOrder::restaurantName = restaurantName;
}
FoodOrder::~FoodOrder()
{
cout << "FoodOrder destroyed.\n";
}
void FoodOrder::printReceipt() const
{
FoodOrder::printReceipt();
cout << "\tOrdered Foods: " << foodCount << endl;
}
float FoodOrder::VIPdiscount() const
{
float discount;
if (balance > 50){discount = 0.85;}
else
{
if (balance > 30){discount = 0.9;}
else
{
if (balance > 20){discount = 0.95;}
else
{
if(balance <= 20){discount = 1;}
}
}
}
return discount;
}
void FoodOrder::addFood(string foodName, int sides = 0, bool soup=false)
{
if (foodName == "Bone-in Ribeye"){balance = 32 + balance;}
else
{
if (foodName == "Rack of Lamb"){balance = 28 + balance;}
else
{
if (foodName == "Grilled Salmon"){balance = 24 + balance;}
else
{
if (foodName == "Beyond Meat Burger"){balance = 22 + balance;}
else
{
throw InvalidInput(foodName);
}
}
}
}
if (soup==true){balance = balance + 0.5;}
balance = balance + sides * 1;
foodCount++;
}
Account::Account(string username, string status)
{
Account::username = username;
Account::status = status;
}
Account::~Account()
{
cout << "Account Removed.\n";
}
string Account::getStatus() const
{
return status;
}
float applyDiscount(DeliveryOrder *o, Account const *a)
{
float b = o->getBalance();
float v = o->VIPdiscount();
if (a->getStatus() == "Owner"){b = b * 0.1;}
else
{
if(a->getStatus() == "VIP"){b = b * v;}
}
return b;
}
h file
#ifndef ORDER_H
#define ORDER_H
#include <string>
#include <iostream>
using namespace std;
class DeliveryOrder
{
private:
string name;
int month;
int day;
int year;
string phone;
float miles;
static int orderCount;
protected:
float balance;
public:
const static float taxRate;
const static float deliveryRate;
DeliveryOrder(string, int, int, int, string, float, float);
~DeliveryOrder();
void printReceipt() const;
float calcBalance();
float getBalance() const;
static int getOrderCount();
virtual float VIPdiscount() const = 0;
};
class BobaOrder : public DeliveryOrder
{
private:
string shopName;
static int drinksCount;
public:
BobaOrder(string, int, int, int, string, float, string, float);
~BobaOrder();
void printReceipt() const;
virtual float VIPdiscount() const;
void addDrink(string, int, bool);
};
class FoodOrder : public DeliveryOrder
{
private:
string restaurantName;
static int foodCount;
public:
FoodOrder(string, int, int, int, string, float, string, float);
~FoodOrder();
void printReceipt() const;
virtual float VIPdiscount() const;
void addFood(string, int, bool);
};
class Account
{
private:
string username;
string status;
public:
Account(string username, string status);
~Account();
string getStatus() const;
};
class InvalidInput
{
private:
string message;
public:
InvalidInput(string s) : message("Invalid input [" + s + "].\n")
{};
void cause() {cout << message;}
};
float applyDiscount(DeliveryOrder*, Account const*);
#endif
Using a debugger may be new to you, but it will save you LOTS of time.
Running your program in a debugger would have shown you that your problem is here.
void FoodOrder::printReceipt() const
{
FoodOrder::printReceipt();
cout << "\tOrdered Foods: " << foodCount << endl;
}
Calling FoodOrder::printReceipt() will call FoodOrder::printReceipt(). Infinitely.
I'm doing self-study C++. I tried a program from the book, that would normally allocate a few objects of two derived classes dynamically using an array of pointers. I'm however preparing for an assignment where I'm not allowed to use pointers, so I made an alternative version without pointers.
The only error it gives me is C2259 "cannot instantiate abstract class", but I'm pretty sure I have overriden all the virtual functions.
Here is the header:
#ifndef ACCTBAC_H_
#define ACCTBAC_H_
#include <iostream>
#include <string>
// Abstract Base Class
class AcctABC
{
private:
std::string fullName;
long acctNum;
double balance;
protected:
struct Formatting
{
std::ios_base::fmtflags flag;
std::streamsize pr;
};
const std::string& FullName() const { return fullName; }
long AcctNum() const { return acctNum; }
Formatting SetFormat() const;
void Restore(Formatting& f) const;
public:
AcctABC(const std::string& s = "Nullbody", long an = -1, double bal = 0.0);
void Deposit(double amt);
virtual void Withdraw(double amt) = 0; // pure virtual function
double Balance() const { return balance; };
virtual void ViewAcct() const = 0; // pure virtual function
virtual ~AcctABC() {}
};
// Brass Account Class
class Brass : public AcctABC
{
public:
Brass(const std::string& s = "Nullbody", long an = -1, double bal = 0.0) : AcctABC(s, an, bal) {}
virtual void Withdraw(double amt);
virtual void ViewAcct() const;
virtual ~Brass() {}
};
// Brass Plus Account Class
class BrassPlus : public AcctABC
{
private:
double maxLoan;
double rate;
double owesBank;
public:
BrassPlus(const std::string& s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.10);
BrassPlus(const Brass& ba, double ml = 500, double r = 0.1);
virtual void ViewAcct() const;
virtual void Withdraw(double amt);
void ResetMax(double m) { maxLoan = m; }
void ResetRate(double r) { rate = r; }
void ResetOwes() { owesBank = 0; }
};
#endif
the class functions:
// acctabc.cpp -- bank account class methods
#include <iostream>
#include "acctabc.h"
using std::cout;
using std::ios_base;
using std::string;
// Abstract Base Class
AcctABC::AcctABC(const string& s, long an, double bal)
{
fullName = s;
acctNum = an;
balance = bal;
}
void AcctABC::Deposit(double amt)
{
if (amt < 0)
cout << "Negative deposit not allowed; "
<< "deposit is cancelled.\n";
else
balance += amt;
}
void AcctABC::Withdraw(double amt)
{
balance -= amt;
}
// protected methods for formatting
AcctABC::Formatting AcctABC::SetFormat() const
{
// set up ###.## format
Formatting f;
f.flag = cout.setf(ios_base::fixed, ios_base::floatfield);
f.pr = cout.precision(2);
return f;
}
void AcctABC::Restore(Formatting& f) const
{
cout.setf(f.flag, ios_base::floatfield);
cout.precision(f.pr);
}
// Brass methods
void Brass::Withdraw(double amt)
{
if (amt < 0)
cout << "Withdrawal amount must be positive; "
<< "withdrawal cancelled.\n";
else if (amt <= Balance())
AcctABC::Withdraw(amt);
else
cout << "Withdrawal amount of $" << amt
<< " exceeds your balance.\n"
<< "Withdrawal cancelled.\n";
}
void Brass::ViewAcct() const
{
Formatting f = SetFormat();
cout << "Brass Client: " << FullName() << "\n";
cout << "Account Number: " << AcctNum() << "\n";
cout << "Balance: $" << Balance() << "\n";
Restore(f);
}
// BrassPlus methods
BrassPlus::BrassPlus(const string& s, long an, double bal, double ml, double r) : AcctABC(s, an, bal)
{
maxLoan = ml;
owesBank = 0.0;
rate = r;
}
void BrassPlus::ViewAcct() const
{
Formatting f = SetFormat();
cout << "BrassPlus Client: " << FullName() << "\n";
cout << "Account Number: " << AcctNum() << "\n";
cout << "Balance: $" << Balance() << "\n";
cout << "Maximum loan: $" << maxLoan << "\n";
cout << "Owed to bank: $" << owesBank << "\n";
cout.precision(3);
cout << "Loan Rate: " << 100 * rate << "%\n";
Restore(f);
}
void BrassPlus::Withdraw(double amt)
{
Formatting f = SetFormat();
double bal = Balance();
if (amt <= bal)
AcctABC::Withdraw(amt);
else if (amt <= bal + maxLoan - owesBank)
{
double advance = amt - bal;
owesBank += advance * (1.0 + rate);
cout << "Bank Advance: $" << advance << "\n";
cout << "Finance charge: $" << advance * rate << "\n";
Deposit(advance);
AcctABC::Withdraw(amt);
}
else
cout << "Credit limit exceeded. Transaction cancelled.\n";
Restore(f);
}
and the main program:
// usebrass3.cpp -- polymorphic example using an abstract base class
#include <iostream>
#include <string>
#include "acctabc.h"
#include <vector>
const int CLIENTS = 4;
int main()
{
using std::cin;
using std::cout;
using std::vector;
using std::string;
vector<AcctABC> accounts(CLIENTS);
string temp;
long tempnum;
double tempbal;
char kind;
for (int i = 0; i < CLIENTS; i++)
{
cout << "Enter client's name: ";
getline(cin, temp);
cout << "Enter client's account number: ";
cin >> tempnum;
cout << "Enter opening balance: $";
cin >> tempbal;
cout << "Enter 1 for Brass Account: ";
while (cin >> kind && (kind != '1' && kind != '2'))
cout << "Enter either 1 or 2: ";
if (kind == 1)
accounts.push_back(Brass(temp, tempnum, tempbal));
else
{
double tmax, trate;
cout << "Enter the overdraft limit: $";
cin >> tmax;
cout << "Enter the interest rate "
<< "as a decimal fraction: ";
cin >> trate;
accounts.push_back(BrassPlus(temp, tempnum, tempbal, tmax, trate));
}
while (cin.get() != '\n')
continue;
}
cout << "\n";
for (int i = 0; i < CLIENTS; i++)
{
accounts[i].ViewAcct();
cout << "\n";
}
cout << "Done.\n";
return 0;
}
Here:
vector<AcctABC> accounts(CLIENTS);
you are trying to create a vector of AcctABC with CLIENTS default constructed AcctABC elements. But you cannot have AcctABC elements when the class is abstract. You also cannot have Brass elements in a vector of AcctABCs. You need pointers when you want to store a polymorphic type in a vector.
You cannot do this
vector<AcctABC> accounts(CLIENTS);
because it will make CLIENTS number of default constructed abstract base classes. You will also lose polymorphism and induce object slicing. Instead
vector<std::unique_ptr<AcctABC>> accounts;
then for example
accounts.push_back(std::make_unique<Brass>(temp, tempnum, tempbal));
So I was able to fix it, however, the operator doesn't seem to be comparing them both since I always get false. There seems to be an error with the pLoan where it is not comparing both of them.
My code is
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
//Vehicle Class
class Vehicle {
public:
Vehicle();
void setPrice(double a);
void setMpg(int a);
double getPrice() const;
int getMpg() const;
void printVehicle() const;
Vehicle(double price, int mpg);
private:
double price;
int mpg;
};
//Loan Class
class Loan {
public:
void setBank(string a);
void setLoan(double a);
string getBank() const;
double getLoan() const;
void printLoan() const;
Loan(string bank = "", double loan = 0);
private:
string bank;
double loan;
};
//Car Class
class Car : public Vehicle {
public:
Car(double price = 0, int mpg = 0, string bank = "", double loan = 0, string name = "", int element = 0);
void setName(string a);
void setLoan(string b, double l, int element);
string getName() const;
void printFull() const;
void setNbrOfLoans(int a);
int getNbrOfLoans() const;
Loan* getpLoan() const;
~Car();
private:
string name;
Loan* pLoan;
int nbrOfLoans;
};
bool operator==(const Car &car1, const Car &car2) {
Loan* pLoan1 = car1.getpLoan();
Loan* pLoan2 = car2.getpLoan();
return ((car1.getPrice() == car2.getPrice()) && (car1.getMpg() == car2.getMpg()) && (car1.getName() == car2.getName())
&& (car1.getNbrOfLoans() == car2.getNbrOfLoans()) &&
(pLoan1[0].getBank() == pLoan2[0].getBank()) && (pLoan1[0].getLoan() == pLoan2[0].getLoan()));
}
//Main
int main() {
Car car1(24800, 22, "Citi", 21600, "Mustang", 1);
Car* pCar1 = &car1;
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
pCar1->setNbrOfLoans(1);
Car car2;
Car* pCar2 = &car2;
cout << boolalpha;
cout << "Enter the price of the car: ";
double price;
cin >> price;
pCar2->setPrice(price);
cout << "Enter the mpg: ";
int mpg;
cin >> mpg;
pCar2->setMpg(mpg);
cout << "Enter the name of the car: ";
string name;
cin >> name;
pCar2->setName(name);
string bank;
double loan;
int index;
cout << "Enter the amount of loans you obtained: ";
cin >> index;
pCar2->setNbrOfLoans(index);
for (int i = 0; i < index; i++)
{
cout << "Enter the name of bank " << i + 1 << ": ";
cin >> bank;
cout << "Enter the amount of loan " << i + 1 << ": ";
cin >> loan;
pCar2->setLoan(bank, loan, i);
}
if (pCar1 == pCar2)
cout << "Cars are the same. ";
else
cout << "Cars are not the same. ";
cout << endl;
pCar2->printFull();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
//Vehicle class function definitions
////////////////////////////////////////////////////////////////////////////////////////
Vehicle::Vehicle() {
price = 0;
mpg = 0;
}
Vehicle::Vehicle(double price, int mpg) {
price = price;
mpg = mpg;
}
void Vehicle::setPrice(double a) {
price = a;
}
void Vehicle::setMpg(int a) {
mpg = a;
}
double Vehicle::getPrice() const {
return price;
}
int Vehicle::getMpg() const {
return mpg;
}
void Vehicle::printVehicle() const {
cout << "Price: " << price << endl;
cout << "MPG: " << mpg << endl;
}
////////////////////////////////////////////////////////////////////////////////////////
//Loan Class function definitions
///////////////////////////////////////////////////////////////////////////////////////
Loan::Loan(string bank, double loan) {
Loan::bank = bank;
Loan::loan = loan;
}
void Loan::setBank(string a) {
bank = a;
}
void Loan::setLoan(double a) {
loan = a;
}
string Loan::getBank() const {
return bank;
}
double Loan::getLoan() const {
return loan;
}
void Loan::printLoan() const {
cout << "Bank: " << bank << endl;
cout << "Loan: " << loan << endl;
}
////////////////////////////////////////////////////////////////////////////////////
//Car Class function definitions
////////////////////////////////////////////////////////////////////////////////////
Car::Car(double price, int mpg, string bank, double loan, string name, int element) : Vehicle(price, mpg)
{
nbrOfLoans = element;
Car::name = name;
setMpg(mpg);
setPrice(price);
pLoan = new Loan[nbrOfLoans];
}
Loan* Car::getpLoan() const{
return pLoan;
}
void Car::setName(string a) {
name = a;
}
void Car::setLoan(string b, double l, int element) {
pLoan[element].setBank(b);
pLoan[element].setLoan(l);
}
string Car::getName() const {
return name;
}
int Car::getNbrOfLoans() const {
return nbrOfLoans;
}
void Car::setNbrOfLoans(int a) {
nbrOfLoans = a;
if (pLoan != NULL)
delete[] pLoan;
pLoan = new Loan[nbrOfLoans];
}
void Car::printFull() const {
cout << endl << "Car name: " << name << endl;
cout << "Price: " << getPrice() << endl;
cout << "MPG: " << getMpg() << endl;
for (int i = 0; i < nbrOfLoans; i++)
{
cout << "Loan #" << i + 1 << "." << endl;
cout << "Bank: " << pLoan[i].getBank();
cout << endl;
cout << "Loan amount: " << pLoan[i].getLoan();
cout << endl;
}
}
Car::~Car() {
delete[] pLoan;
}
Output:
IS always cars are not the same even when they are
Your main code is not calling your operator ==:
if (pCar1 == pCar2)
cout << "Cars are the same. ";
else
cout << "Cars are not the same. ";
here you're comparing the two pointers. To compare the two pointed-to objects you need
if (*pCar1 == *pCar2) ...
One more error:
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
pCar1->setNbrOfLoans(1);
setNbrOfLoans must be located before setLoan:
pCar1->setNbrOfLoans(1);
pCar1->setLoan("Citi", 21600, 0);
pCar1->printFull();
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
My code compiles fine. But when i try to use my overloaded operator<< - app crashes. I didn't find any decisions by myself.If i don't use this operator everything works just fine. For example, i can pass negative argument to constructor and it will show a message using class data pointer 'company'. Debugger shows that programm crashes at this line:
os << "Company: " << s.company
in function:
std::ostream& operator<<(std::ostream& os, const Stock& s)
{
using std::ios_base;
// set format to #.###
ios_base::fmtflags orig =
os.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = os.precision(3);
os << "Company: " << s.company
<< " Shares: " << s.shares << '\n';
os << " Share Price: $" << s.share_val;
// set format to #.##
os.precision(2);
os << " Total Worth: $" << s.total_val << '\n';
// restore original format
os.setf(orig, ios_base::floatfield);
os.precision(prec);
return os;
}
Here's the code:
// stock20.h -- augmented version
#ifndef STOCK20_H_
#define STOCK20_H_
#include <iostream>
class Stock
{
private:
char* company;
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock(); // default constructor
Stock(const char* co, long n = 0, double pr = 0.0);
~Stock(); // do-nothing destructor
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
const Stock & topval(const Stock & s) const;
friend std::ostream& operator<<(std::ostream& os, const Stock& s);
};
#endif
Realisation:
// stock20.cpp -- augmented version
#include "stock20.h"
#include <cstring>
#define my_delete(x){ delete[] x; x = NULL; }
using namespace std;
// constructors
Stock::Stock() // default constructor
{
company = new char[1];
company[0] = '\0';
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const char* co, long n, double pr)
{
company = new char[strlen(co)+1];
strcpy(company,co);
if (n < 0)
{
std::cout << "Number of shares can't be negative; "
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
// class destructor
Stock::~Stock() // quiet class destructor
{
my_delete(company);
}
// other methods
void Stock::buy(long num, double price)
{
if (num < 0)
{
std::cout << "Number of shares purchased can't be negative. "
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
using std::cout;
if (num < 0)
{
cout << "Number of shares sold can't be negative. "
<< "Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can't sell more than you have! "
<< "Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
std::ostream& operator<<(std::ostream& os, const Stock& s)
{
using std::ios_base;
// set format to #.###
ios_base::fmtflags orig =
os.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = os.precision(3);
os << "Company: " << s.company
<< " Shares: " << s.shares << '\n';
os << " Share Price: $" << s.share_val;
// set format to #.##
os.precision(2);
os << " Total Worth: $" << s.total_val << '\n';
// restore original format
os.setf(orig, ios_base::floatfield);
os.precision(prec);
return os;
}
const Stock & Stock::topval(const Stock & s) const
{
if (s.total_val > total_val)
return s;
else
return *this;
}
And the code usage:
// usestok2.cpp -- using the Stock class
// compile with stock20.cpp
#include "stock20.h"
const int STKS = 4;
int main()
{{
//create an array of initialized objects
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.0),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Obelisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};
std::cout << "Stock holdings:\n";
int st;
for (st = 0; st < STKS; st++)
std::cout<<stocks[STKS]; //here we got an error
// set pointer to first element
const Stock * top = &stocks[0];
for (st = 1; st < STKS; st++)
top = &top->topval(stocks[st]);
// now top points to the most valuable holding
std::cout << "\nMost valuable holding:\n";
std::cout<<*top;}
// std::cin.get();
return 0;
}
If since i asked 1 question, i hope you don't mind me asking another one. How can i avoid using includes in headers. For example for overloaded operator<< i need to include iostream cause it has return value of ostream& type and a parameter of the same type. Thanks in advance.
When you declare an array:
Stock stocks[STKS] = ...
the array elements are indexed 0 through STKS - 1. When you access stocks[STKS] in this line:
std::cout<<stocks[STKS]; // Out-of-bounds: STKS > STKS - 1
you're accessing a non-existent array element, which is causing the crash. Given the context, you probably want std::cout<<stocks[st]; instead (hat tip: #gx_).