File stock00.h
#ifndef STOCH00_H_
#define STOCK00_H_
#include <iostream>
#include <string>
class Stock
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot() {total_val = shares * share_val;}
public:
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show(void);
Stock(const std::string & co, long n = 0, double pr = 0.0)
{
using namespace std;
cout << "constructor using " << co << " called\n";
company = co;
if (n < 0)
{
cout << "number of shares can't be negative" << ' ' << company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
Stock()
{
using namespace std;
cout << "default constructor called\n";
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
~Stock()
{
std::cout << "Bye, " << company << "!\n";
}
};
#endif
file stock00.cpp
#include <iostream>
#include "stock00.h"
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
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)
{
std::cout << "number of shares purchased 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();
}
void Stock::show()
{
std::cout << company << " " << shares << ' ' << share_val << ' ' << total_val << '\n';
}
File userofstock.cpp
#include "stock00.h"
#include <iostream>
int main()
{
using namespace std;
cout << "using constructors to create new object\n";
Stock stock1("hello", 12, 20.0);
stock1.show();
Stock stock2 = Stock("gay", 2, 2.0);
stock2.show();
cout << "assigning stock1 to stock2: \n";
stock2 = stock1;
cout << "listing stock1 and stock2:\n";
stock1.show();
stock2.show();
cout << "using constructor to reset an object\n";
stock1 = Stock("nifty", 10, 50.0);
cout << "revised stock1:\n";
stock1.show();
cout << "done\n";
return 0;
}
ошибки:
C:\Users\A-TECH~1\AppData\Local\Temp\ccIxlHhs.o userofstcok.cpp:(.text+0x98): undefined reference to `Stock::show()'
C:\Users\A-TECH~1\AppData\Local\Temp\ccIxlHhs.o userofstcok.cpp:(.text+0x108): undefined reference to `Stock::show()'
C:\Users\A-TECH~1\AppData\Local\Temp\ccIxlHhs.o userofstcok.cpp:(.text+0x14a): undefined reference to `Stock::show()'
C:\Users\A-TECH~1\AppData\Local\Temp\ccIxlHhs.o userofstcok.cpp:(.text+0x156): undefined reference to `Stock::show()'
C:\Users\A-TECH~1\AppData\Local\Temp\ccIxlHhs.o userofstcok.cpp:(.text+0x20b): undefined reference to `Stock::show()'
C:\Users\A-technics\Desktop\collect2.exe [Error] ld returned 1 exit status