c++学习记录
面向对象编程(oop)是一种特殊的、设计程序的概念性方法,c++通过一些特性改进c语言,使得应用这种方法更容易
下面是最重要的opp特性
- 抽象
- 封装和数据隐藏
- 多态
- 继承
- 代码可重用性
1.过程性编程和面向对象编程
-
类的定义,保存在stock00.h
#ifndef STOCK00_H_ #define STOCK00_H_ #include<string> //using namespace std; class Stock { private : std::string company; long shares; double share_val; double total_val; void set_tot() { total_val=shares*share_val; } public: 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 -
实现类成员函数
// stockD0 cpp--implementing the Stock class // version 00 #include <iostream> #include "stock00.h" //using namespace std; 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 0.\n"; shares = 0; } else shares = n; share_val = n; 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) { cout<<"Number of shares sold can't be negatives."<<"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:"<<company<<"Shares:"<<shares<<'\n' <<"Share Price : $"<<share_val <<"Total Wotth: $" << total_val <<"\n"; } -
使用类
#include<iostream> #include "stock00.h" int main() { Stock fluffy_the_cat; fluffy_the_cat.acquire("NanSmart",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; } -
说明
-
定义类的时候,为了避免重复使用头文件,使用头文件,,当程序为未定义STOCK00_H_时,执行#ifndef~#endif的内容,当已经执行,则跳到#endif的位置,具体如下:
#ifndef STOCK00_H_ #define STOCK00_H_ ... #endif -
该程序使用了多文件编译,将stock00.cpp和usestock00.cpp同时编译,g++ stock00.cpp usestock00.cpp -o stock11,.\stock11,结果如下:
Company:NanSmartShares:20 Share Price : $20Total Wotth: $400 Company:NanSmartShares:35 Share Price : $18.125Total Wotth: $634.375 You can't sell more than you have!Transaction is aborted. Company:NanSmartShares:35 Share Price : $18.125Total Wotth: $634.375 Company:NanSmartShares:300035 Share Price : $40.125Total Wotth: $1.20389e+07 Company:NanSmartShares:35 Share Price : $0.125Total Wotth: $4.3752.
-
2.类的构造函数和析构函数
-
声明和定义构造函数
现在需要创建Stock的构造函数,具体定义如下
由于*#include “stock00.h”中定义Stock的私有数据,因此无法采用非成员函数访问这些数据,若未定义构造函数就采用默认函数,例如Stock::Stock()*
Stock::Stock(const string &co,long n,double pr)//虽然没有返回值,但不声明为void,实际上构造函数没有声明类型 { company = co; if (n < 0) { std::cerr << "Number of shares can't be negative ;" << company << "shares set to 0.\n"; shares = 0; } else shares = n; share _val = pr; set_tot(); } -
使用构造函数
-
显示调用
Stock food =Stock("World Cabbage",250,1.25); -
隐式调用
Stock food ("Furry Mason" , 50 , 2.5); -
构造函数和new一起使用
Stock *food = new Stock("Electroshock", 18, 19.0);
-
-
析构函数
-
构造函数使用new 来分配内存,则析构函数将使用delete 来释放这些内存。和构造函数一样,析构函数的名称也很特殊:在类名前加~。因此Stock类的析构函数为~Stock(),同时析构函数也可以没有返回值和声明类型,与构造函数不同的是析构函数没有参数。
-
由于Stock的析构函数不承担任何重要工作,因此可以编写为不执行任何操作的函数
Stock::~Stock() { } 如果程序员没有提供析构函数,编译器将隐式声明一个默认析构函数,一般来说,析构函数的调用由编译器决定,通常不会在程序中显示调用析构函数。
1.如果创建静态存储对象,则其析构函数回在程序结束时自动调用.
2.而如果创建自动存储对象,则其析构函数.
3.如果通过new创建的,则将驻留在栈内存或自由存储区中,当使用delete来释放内存,析构函数自动调用
-
3.改进Stock类
相关源码如下:
/* file : stock01.h date : 2021\2\19 */ #ifndef STOCK00_H_ #define STOCK00_H_ #include<string> //using namespace std; class Stock { private : std::string company; long shares; double share_val; double total_val; void set_tot() { total_val=shares*share_val; } public: Stock(); Stock(const std::string & co,long n,double pr); ~Stock(); //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
/* file : stock01.cpp date : 2021\2\19 */ // stockD0 cpp--implementing the Stock class // version 00 #include <iostream> #include "stock01.h" //using namespace std; Stock::Stock() { std::cout<< "Default constructor called\n"; company = "noname"; shares = 0; share_val = 0.0; total_val = 0.0; } Stock::~Stock() { std::cout<< "Bye!\n"; } 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 0.\n"; shares = 0; } else shares = n; share_val = n; 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) { cout<<"Number of shares sold can't be negatives."<<"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:"<<company<<"Shares:"<<shares<<'\n' <<"Share Price : $"<<share_val <<"Total Worth: $" << total_val <<"\n"; } #include<iostream> #include "stock01.h"
/* file : stock01.cpp date : 2021\2\19 */ #include<iostream> #include "stock01.h" int main() { Stock fluffy_the_cat("NanoSmart", 12, 20.0); 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; }
运行结果
Company:NanSmartShares:20 Share Price : $20Total Wotth: $400 Company:NanSmartShares:35 Share Price : $18.125Total Wotth: $634.375 You can't sell more than you have!Transaction is aborted. Company:NanSmartShares:35 Share Price : $18.125Total Wotth: $634.375 Company:NanSmartShares:300035 Share Price : $40.125Total Wotth: $1.20389e+07 Company:NanSmartShares:35 Share Price : $0.125Total Wotth: $4.375