添加class的demo

This commit is contained in:
zhuyijun 2021-08-17 20:03:20 +08:00
parent 39fe55348c
commit 077f94aed8
13 changed files with 234 additions and 188 deletions

1
base/.gitignore vendored
View File

@ -4,4 +4,5 @@
.vscode
/cmake-build-debug
*.o
*.exe

View File

@ -47,6 +47,14 @@ add_executable(external namespacetest/external.cpp)
add_executable(support namespacetest/support.cpp)
add_executable(ststic namespacetest/ststic.cpp)
add_executable(newplace namespacetest/newplace.cpp)
<<<<<<< HEAD
add_executable(namessp namespacetest/namessp.cpp)
add_executable(usestock0 class/usestock0.cpp)
add_executable(stock20 class/stock20.cpp)
add_executable(useStock20 class/useStock20.cpp)
add_executable(useStock20 class/useStock20.cpp)
=======
add_executable(namessp namespacetest/namessp.cpp)
>>>>>>> parent of 39fe553 (添加对象)
=======
add_executable(namessp namespacetest/namessp.cpp)
>>>>>>> parent of 39fe553 (添加对象)

View File

@ -1,34 +0,0 @@
//
// Created by zhuyijun on 2021/8/17.
//
#include "stack.h"
Stack::Stack() {
top = 0;
}
bool Stack::isempty() const {
return top == 0;
}
bool Stack::isfull() const {
return top == MAX;
}
bool Stack::push(const Item &item) {
if (top < MAX) {
items[top++] = item;
return true;
}
return false;
}
bool Stack::pop(Item &item) {
if (top > 0) {
item = items[--top];
return true;
}
return false;
}

View File

@ -1,29 +0,0 @@
//
// Created by zhuyijun on 2021/8/17.
//
#ifndef BASE_STACK_H
#define BASE_STACK_H
typedef unsigned long Item;
class Stack {
private:
enum {MAX=10};
Item items[MAX];
int top;
public:
//构造函数
Stack();
//判断栈是否为空
bool isempty() const;
//判断栈是否满了
bool isfull() const;
//入栈
bool push(const Item & item);
//出栈
bool pop(Item & item);
};
#endif //BASE_STACK_H

View File

@ -1,50 +0,0 @@
//
// Created by zhuyijun on 2021/8/17.
//
#include <iostream>
#include <cctype>
#include "stack.h"
using namespace std;
int main() {
Stack st;
char ch;
unsigned long po;
cout << "Please enter A to add a purchase order,\n Pto process a PO,or Q to quit.\n";
while (cin >> ch && toupper(ch) != 'Q') {
while (cin.get() != '\n') {
continue;
}
if (!isalpha(ch)) {
cout << '\a';
continue;
}
switch (ch) {
case 'A':
case 'a':
cout << "Enter a PO number to add: ";
cin >> po;
if (st.isfull())
cout << "stack already full";
else
st.push(po);
break;
case 'P':
case 'p':
if (st.isempty())
cout << "stack already empty \n";
else {
st.pop(po);
cout << "PO #" << po << " popped\n";
}
break;
}
cout << "Please enter A to add a purchase order,\n "
"P to process a PO, or Q to quit,\n";
}
cout << "Bye\n";
return 0;
}

View File

@ -1,21 +1,35 @@
//
// Created by zhuyijun on 2021/8/17.
//
#include <iostream>
#include "stock20.h"
#include "stock00.h"
//构造函数
Stock::Stock() {
company = "no name";
shares = 0;
share_val = 0.0;
total_val = -0.0;
total_val = 0.0;
}
Stock::Stock(const std::string &co, long n, double pr) {
company = co;
if (n < 0) {
std::cout << "Number of shares can't be nagetive; " << company << " shares set to 0." << std::endl;
std::cout << "Number of shares can't be negative; " << company << " shares set to 0.\n";
} else {
shares = n;
}
share_val = pr;
set_tot();
}
//使用inline限定符 使其成为内联函数
inline void Stock::set_tot() {
total_val = shares * share_val;
}
void Stock::acquire(const std::string &co, long n, double pr) {
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;
@ -24,17 +38,9 @@ Stock::Stock(const std::string &co, long n, double pr) {
set_tot();
}
inline void Stock::set_tot() {
total_val = shares * share_val;
}
Stock::~Stock() {
std::cout << "run " << company << " ~Stock" << std::endl;
}
void Stock::buy(long num, double price) {
if (num < 0) {
std::cout << "Number of shares purchased can't be negative. Transaction is aborted." << std::endl;
std::cout << "Number of shares purchased can't be negative." << "Transaction is aborted.\n";
} else {
shares += num;
share_val = price;
@ -45,11 +51,11 @@ void Stock::buy(long num, double price) {
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." << std::endl;
std::cout << "Number of shares purchased can't be negative." << "Transaction is aborted.\n";
} else if (num > shares) {
std::cout << "You can't sell more than you have! Transaction is aborted." << std::endl;
cout << "You can't sell more than you have! " << "Transaction is aborted.\n";
} else {
shares -= num;
shares += num;
share_val = price;
set_tot();
}
@ -60,24 +66,16 @@ void Stock::update(double price) {
set_tot();
}
void Stock::show() const {
void Stock::show() {
using std::cout;
using std::endl;
using std::ios_base;
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = cout.precision(3);
cout << "Company: " << company << endl;
cout << "shares: " << shares << endl;
cout << "Share Price: $" << share_val << endl;
cout << "Company: " << company
<< " Shares: " << shares << std::endl;
cout << " Share Price: $" << share_val;
cout.precision(2);
cout << "Total Worth: $" << total_val << endl;
cout << " Total Worth: $" << total_val << std::endl;
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
const Stock &Stock::topval(const Stock &s) const {
if (s.total_val > total_val) {
return s;
}
return *this;
}

38
base/class/stock00.h Normal file
View File

@ -0,0 +1,38 @@
//
// Created by nicemoe on 2021/8/16.
//
#ifndef BASE_STOCK00_H
#define BASE_STOCK00_H
#include <string>
class Stock {
//私有
private:
std::string company;
long shares;
double share_val;
double total_val;
// void set_tot() {
// total_val = shares * share_val;
// }
void set_tot();
//公有
public:
Stock();
Stock(const std::string &co, long n = 0, double pr = 0.0);
void acquire(const std::string &co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
};
#endif //BASE_STOCK00_H

85
base/class/stock10.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <iostream>
#include "stock10.h"
//构造函数
Stock::Stock() {
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string &co, long n, double pr) {
company = co;
if (n < 0) {
std::cout << "Number of shares can't be negative; " << company << " shares set to 0.\n";
} else {
shares = n;
}
share_val = pr;
set_tot();
}
//使用inline限定符 使其成为内联函数
inline void Stock::set_tot() {
total_val = shares * share_val;
}
Stock::~Stock() {
std::cout << "Bye, " << company << " !\n";
}
void Stock::acquire(const std::string &co, long n, double pr) {
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();
}
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();
}
//后面加const保证对象不被修改
void Stock::show() const {
using std::cout;
using std::ios_base;
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = cout.precision(3);
cout << "Company: " << company
<< " Shares: " << shares << std::endl;
cout << " Share Price: $" << share_val;
cout.precision(2);
cout << " Total Worth: $" << total_val << std::endl;
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}

View File

@ -1,28 +1,31 @@
//
// Created by zhuyijun on 2021/8/17.
// Created by nicemoe on 2021/8/16.
//
#ifndef BASE_STOCK20_H
#define BASE_STOCK20_H
#ifndef BASE_STOCK10_H
#define BASE_STOCK10_H
#include <string>
class Stock {
//私有
private:
std::string company;
int shares;
long shares;
double share_val;
double total_val{};
double total_val;
// void set_tot() {
// total_val = shares * share_val;
// }
void set_tot();
//公有
public:
Stock();
~Stock();
Stock(const std::string &co, long n = 0, double pr = 0.0);
~Stock();
void acquire(const std::string &co, long n, double pr);
void buy(long num, double price);
@ -31,9 +34,6 @@ public:
void update(double price);
void show() const;
const Stock &topval(const Stock &s) const;
};
#endif //BASE_STOCK20_H
#endif //BASE_STOCK10_H

View File

@ -1,30 +0,0 @@
//
// Created by zhuyijun on 2021/8/17.
//
#include <iostream>
#include "stock20.h"
const int STKS =4;
int main() {
Stock stock[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) {
stock[st].show();
}
const Stock * top = &stock[0];
for (st = 1; st < STKS; ++st) {
top = &top ->topval(stock[st]);
}
std::cout<< "\n the most valuble holding:\n";
top->show();
return 0;
}

25
base/class/usestock0.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by nicemoe on 2021/8/16.
//
#include <iostream>
#include "stock00.h"
/**
* g++ .\stock00.cpp .\usestock0.cpp -o usestock
* @return
*/
int main() {
Stock fluffy_the_cat;
fluffy_the_cat.acquire("NanoSmart",20,12.50);
fluffy_the_cat.show();
fluffy_the_cat.buy(15,18.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(400,20.00);
fluffy_the_cat.show();
fluffy_the_cat.buy(300000,40.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(300000,0.125);
fluffy_the_cat.show();
return 0;
}

34
base/class/usestock2.cpp Normal file
View File

@ -0,0 +1,34 @@
//
// Created by nicemoe on 2021/8/16.
//
#include <iostream>
#include "stock10.h"
/**
* g++ .\stock10.cpp .\usestock2.cpp -o usestock
* @return
*/
int main() {
{
using std::cout;
cout << "Using constructors to create new Objects \n";
Stock stock1("NanoSmart", 12, 20.0);
stock1.show();
Stock stock2 = Stock("Boffo Objects", 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 a constructor to reset an object\n";
stock1 = Stock("Nifty Foods", 10, 50.0);
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
}
return 0;
}

Binary file not shown.