C++ 类

创建类、实现类接口方法、实现类

类属性和接口方法定义在.h文件Stock.h

  1. #ifndef PRO1_STOCK_H
  2. #define PRO1_STOCK_H
  3. class Stock {
  4. private:
  5. char company[30];
  6. int shares;
  7. double share_val;
  8. double total_val;
  9. void set_toto(){total_val = shares * share_val;}
  10. public:
  11. void buy(int num, double price);
  12. void sell(int num, double price);
  13. void update(double price);
  14. void show()const;
  15. };
  16. #endif //PRO1_STOCK_H

类接口方法的实现定义在.cpp文件-Stock.cpp

  1. #include "Stock.h"
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5. void Stocks::acquire(const char *co, int n, double pr) {
  6. std::strncpy(company, co, 29);
  7. company[29] = '\0';
  8. if (n < 0){
  9. cerr << "Number of shares cant's be negative." << company << "shares set to 0.\n";
  10. shares = 0;
  11. } else
  12. shares = n;
  13. share_val = pr;
  14. set_tot();
  15. }
  16. void Stocks::buy(int num, double price) {
  17. if (num < 0){
  18. cerr << "Number of shares purchased can't be negative." << "Transaction is aborted.\n";
  19. } else{
  20. shares += num;
  21. share_val = price;
  22. set_tot();
  23. }
  24. }
  25. void Stocks::sell(int num, double price) {
  26. if(num < 0){
  27. cerr << "Nummber of shares sold can't be negative." << "Transaction is aborted.\n";
  28. } else if (num > shares){
  29. cerr << "You can't sell more than you have!" << "Transaction is aborted.\n";
  30. } else {
  31. shares += num;
  32. share_val = price;
  33. set_tot();
  34. }
  35. }
  36. void Stocks::update(double price) {
  37. share_val = price;
  38. set_tot();
  39. }
  40. void Stocks::show() {
  41. cout << "Company:" << company << " Shares: "<< shares << endl
  42. << "Share Price:$" << share_val << " Total Worth:$" << total_val << endl;
  43. }
  44. // 在外部定义内联函数
  45. //inline void Stocks::set_tot(){
  46. // total_val = shares * share_val;
  47. //}
  48. // 声明类变量
  49. Stocks kate, joe;

类文件的引入

  1. #include "module1/Mouh.h"
  2. #include "module2/Stock.h"

类对象实例化

  1. Stock stock = Stock{};

类的默认构造函数和折构函数

Stock.cpp

  1. #include "Stock.h"
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5. class Stocks{
  6. private:
  7. char company[30];
  8. int shares;
  9. double share_val;
  10. double total_val;
  11. // 在内部定义内联函数
  12. void set_tot(){
  13. total_val = shares * share_val;
  14. }
  15. public:
  16. // 默认构造函数
  17. Stocks();
  18. // 折构函数——用来释放内存
  19. ~Stocks();
  20. };
  21. Stocks::~Stocks() {
  22. cout << "折构函数调用结束" << endl;
  23. }

创建对象数组-类必须有默认构造函数

Stock.h

  1. #ifndef PRO1_STOCK_H
  2. #define PRO1_STOCK_H
  3. class Stock {
  4. private:
  5. char company[30];
  6. int shares;
  7. double share_val;
  8. double total_val;
  9. void set_toto(){total_val = shares * share_val;}
  10. public:
  11. Stock();
  12. Stock(const char* co, int n = 0, double pr = 0.0);
  13. ~Stock(){};
  14. void buy(int num, double price);
  15. void sell(int num, double price);
  16. void update(double price);
  17. void show()const;
  18. const Stock & topval(const Stock & s)const;
  19. };
  20. #endif //PRO1_STOCK_H

main.cpp

  1. #include "module2/Stock.h"
  2. // 对象数组-要创建对象数组,则这个类必须有默认构造函数
  3. Stock stuff[4] = {
  4. Stock("NanoSmart", 12.5, 20),
  5. Stock("Boffo Objects", 200, 2.0),
  6. Stock("Monolithic Obelisks", 130, 3.24),
  7. Stock("Fleep EnterPrices", 60, 6.5)
  8. };
  9. stuff[0].update(23.0);
  10. stuff[3].show();
  11. Stock tops = stuff[2].topval(stuff[1]);

友元函数的定义及使用

FcTime.h

  1. #ifndef PRO1_FCTIME_H
  2. #define PRO1_FCTIME_H
  3. #include <iostream>
  4. using namespace std;
  5. class FcTime {
  6. private:
  7. int hours;
  8. int minutes;
  9. public:
  10. FcTime();
  11. FcTime(int h, int m = 0);
  12. void AddMin(int m);
  13. void AddHou(int h);
  14. void Reset(int h = 0, int m = 0);
  15. FcTime operator+(const FcTime & t)const;
  16. FcTime operator-(const FcTime & t)const;
  17. FcTime operator*(double n)const;
  18. // 友元函数的定义
  19. friend FcTime operator*(double m, const FcTime & t){
  20. return t * m;
  21. }
  22. friend ostream &operator << (ostream & os, const FcTime & t);
  23. };
  24. #endif //PRO1_FCTIME_H

FcTime.cpp

  1. #include <iostream>
  2. #include "FcTime.h"
  3. FcTime::FcTime() {
  4. hours = minutes = 0;
  5. }
  6. FcTime::FcTime(int h, int m) {
  7. hours = h;
  8. minutes = m;
  9. }
  10. void FcTime::AddMin(int m) {
  11. minutes += m;
  12. hours += minutes / 60;
  13. minutes %= 60;
  14. }
  15. void FcTime::AddHou(int h) {
  16. hours += h;
  17. }
  18. void FcTime::Reset(int h, int m) {
  19. hours = h;
  20. minutes = m;
  21. }
  22. FcTime FcTime::operator+(const FcTime &t) const {
  23. FcTime sum;
  24. sum.minutes = minutes + t.minutes;
  25. sum.hours = hours + t.hours + sum.minutes / 60;
  26. sum.minutes %= 60;
  27. return sum;
  28. }
  29. FcTime FcTime::operator-(const FcTime &t) const {
  30. FcTime diff;
  31. int tot1, tot2;
  32. tot1 = t.minutes + 60 * t.hours;
  33. tot2 = minutes + 60 * hours;
  34. diff.minutes = (tot2 - tot1) % 60;
  35. diff.hours = (tot2 - tot1) / 60;
  36. return diff;
  37. }
  38. FcTime FcTime::operator*(double n) const {
  39. FcTime result;
  40. long totalminutes = hours * n * 60 + minutes * n;
  41. result.hours = totalminutes / 60;
  42. result.minutes = totalminutes % 60;
  43. return result;
  44. }
  45. ostream & operator<< (ostream & os, const FcTime & t){
  46. os << t.hours << "hours, " << t.minutes << "minutes";
  47. return os;
  48. }

main.cpp-友元函数的调用

  1. #include <iostream>
  2. #include "module3/FcTime.h"
  3. using namespace std;
  4. // 类的使用以及友元函数的使用
  5. void friendFunction(){
  6. FcTime aida(3, 25);
  7. FcTime tosca(2, 48);
  8. FcTime temp;
  9. cout << "Aida and Tosca:\n";
  10. cout << aida << "; " << tosca << endl;
  11. temp = aida + tosca;
  12. cout << "Aida + Tosca" << endl;
  13. temp = aida * 1.17;
  14. cout << "Aida * 1.17: " << temp << endl;
  15. cout << "Tosca * 10: " << 10 * tosca << endl;
  16. }
  17. int main() {
  18. friendFunction();
  19. return 0;
  20. }