创建类、实现类接口方法、实现类
类属性和接口方法定义在.h文件Stock.h
#ifndef PRO1_STOCK_H
#define PRO1_STOCK_H
class Stock {
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_toto(){total_val = shares * share_val;}
public:
void buy(int num, double price);
void sell(int num, double price);
void update(double price);
void show()const;
};
#endif //PRO1_STOCK_H
类接口方法的实现定义在.cpp文件-Stock.cpp
#include "Stock.h"
#include <iostream>
#include <cstring>
using namespace std;
void Stocks::acquire(const char *co, int n, double pr) {
std::strncpy(company, co, 29);
company[29] = '\0';
if (n < 0){
cerr << "Number of shares cant's be negative." << company << "shares set to 0.\n";
shares = 0;
} else
shares = n;
share_val = pr;
set_tot();
}
void Stocks::buy(int num, double price) {
if (num < 0){
cerr << "Number of shares purchased can't be negative." << "Transaction is aborted.\n";
} else{
shares += num;
share_val = price;
set_tot();
}
}
void Stocks::sell(int num, double price) {
if(num < 0){
cerr << "Nummber of shares sold can't be negative." << "Transaction is aborted.\n";
} else if (num > shares){
cerr << "You can't sell more than you have!" << "Transaction is aborted.\n";
} else {
shares += num;
share_val = price;
set_tot();
}
}
void Stocks::update(double price) {
share_val = price;
set_tot();
}
void Stocks::show() {
cout << "Company:" << company << " Shares: "<< shares << endl
<< "Share Price:$" << share_val << " Total Worth:$" << total_val << endl;
}
// 在外部定义内联函数
//inline void Stocks::set_tot(){
// total_val = shares * share_val;
//}
// 声明类变量
Stocks kate, joe;
类文件的引入
#include "module1/Mouh.h"
#include "module2/Stock.h"
类对象实例化
Stock stock = Stock{};
类的默认构造函数和折构函数
Stock.cpp
#include "Stock.h"
#include <iostream>
#include <cstring>
using namespace std;
class Stocks{
private:
char company[30];
int shares;
double share_val;
double total_val;
// 在内部定义内联函数
void set_tot(){
total_val = shares * share_val;
}
public:
// 默认构造函数
Stocks();
// 折构函数——用来释放内存
~Stocks();
};
Stocks::~Stocks() {
cout << "折构函数调用结束" << endl;
}
创建对象数组-类必须有默认构造函数
Stock.h
#ifndef PRO1_STOCK_H
#define PRO1_STOCK_H
class Stock {
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_toto(){total_val = shares * share_val;}
public:
Stock();
Stock(const char* co, int n = 0, double pr = 0.0);
~Stock(){};
void buy(int num, double price);
void sell(int num, double price);
void update(double price);
void show()const;
const Stock & topval(const Stock & s)const;
};
#endif //PRO1_STOCK_H
main.cpp
#include "module2/Stock.h"
// 对象数组-要创建对象数组,则这个类必须有默认构造函数
Stock stuff[4] = {
Stock("NanoSmart", 12.5, 20),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Obelisks", 130, 3.24),
Stock("Fleep EnterPrices", 60, 6.5)
};
stuff[0].update(23.0);
stuff[3].show();
Stock tops = stuff[2].topval(stuff[1]);
友元函数的定义及使用
FcTime.h
#ifndef PRO1_FCTIME_H
#define PRO1_FCTIME_H
#include <iostream>
using namespace std;
class FcTime {
private:
int hours;
int minutes;
public:
FcTime();
FcTime(int h, int m = 0);
void AddMin(int m);
void AddHou(int h);
void Reset(int h = 0, int m = 0);
FcTime operator+(const FcTime & t)const;
FcTime operator-(const FcTime & t)const;
FcTime operator*(double n)const;
// 友元函数的定义
friend FcTime operator*(double m, const FcTime & t){
return t * m;
}
friend ostream &operator << (ostream & os, const FcTime & t);
};
#endif //PRO1_FCTIME_H
FcTime.cpp
#include <iostream>
#include "FcTime.h"
FcTime::FcTime() {
hours = minutes = 0;
}
FcTime::FcTime(int h, int m) {
hours = h;
minutes = m;
}
void FcTime::AddMin(int m) {
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void FcTime::AddHou(int h) {
hours += h;
}
void FcTime::Reset(int h, int m) {
hours = h;
minutes = m;
}
FcTime FcTime::operator+(const FcTime &t) const {
FcTime sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
FcTime FcTime::operator-(const FcTime &t) const {
FcTime diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
FcTime FcTime::operator*(double n) const {
FcTime result;
long totalminutes = hours * n * 60 + minutes * n;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
ostream & operator<< (ostream & os, const FcTime & t){
os << t.hours << "hours, " << t.minutes << "minutes";
return os;
}
main.cpp-友元函数的调用
#include <iostream>
#include "module3/FcTime.h"
using namespace std;
// 类的使用以及友元函数的使用
void friendFunction(){
FcTime aida(3, 25);
FcTime tosca(2, 48);
FcTime temp;
cout << "Aida and Tosca:\n";
cout << aida << "; " << tosca << endl;
temp = aida + tosca;
cout << "Aida + Tosca" << endl;
temp = aida * 1.17;
cout << "Aida * 1.17: " << temp << endl;
cout << "Tosca * 10: " << 10 * tosca << endl;
}
int main() {
friendFunction();
return 0;
}