面向對象編程是一種特殊的設計程序的概念性方法,C++通過一些特性改進了C語言,使得應用這種方法更容易。下面是最重要的OPP特性: 抽象 封裝和數據隱藏 多態 繼承 代碼的可重用性
為了實現這些特性并將它們組合在一起,C++最重要的改進是提供了類。
在類聲明中一般包括兩部分,一般情況下,數據(PRivite),成員函數(public)。成員函數可以調用私有數據,是接口。
// stock00.h -- Stock class interface(用戶接口)// version 00#ifndef STOCK00_H_#define STOCK00_H_#include <string> class Stock // class declaration{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對于類的成員函數在源文件中實現,如果成員函數很短,可以在類聲明里實現,這樣的話就會變成內聯函數,如void set_tot()。
// stock00.cpp -- implementing the Stock class(實現類成員函數)// version 00#include <iostream>#include "stock00.h"http://使用類作用域符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) { cout << "Number of shares sold 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: " << company << " Shares: " << shares << '/n' << " Share Price: $" << share_val << " Total Worth: $" << total_val << '/n';}
使用C++類的目標之一是讓使用類對象就像使用標準類型一樣。然而以上并不能像初始化int類型那樣初始化Stock對象。 不能初始化的原因在于,數據的狀態是私有的,意味著程序不能直接訪問數據成員。 因此,C++提供了也特殊的成員函數,類構造函數。它專門用于構造新對象,將值賦給它們的數據成員。
用構造函數創建對象后,程序負責跟蹤該對象,直到過期為止,此時程序自動調用析構函數完成清理工作,因此很有必要。 如果構造函數用new來分配內存,則析構函數將用delete來釋放這些內存。Stock的構造函數沒有使用new,則析構函數也沒什么任務。
析構函數的名稱:在類名前加~,沒有返回值和聲明類型。 增加構造函數析構函數后的三個文件如下:
1
// stock10.h ?Stock class declaration with constructors, destructor added#ifndef STOCK1_H_#define STOCK1_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; }public: Stock(); // default constructor Stock(const std::string & co, long n = 0, double pr = 0.0); ~Stock(); // noisy destructor void buy(long num, double price); void sell(long num, double price); void update(double price); void show();};#endif2
// stock1.cpp ?Stock class implementation with constructors, destructor added#include <iostream>#include "stock10.h"http:// constructors (verbose versions)Stock::Stock() // default constructor{ std::cout << "Default constructor called/n"; company = "no name"; shares = 0; share_val = 0.0; total_val = 0.0;}Stock::Stock(const std::string & co, long n, double pr){ std::cout << "Constructor using " << co << " called/n"; 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();}// class destructorStock::~Stock() // verbose class destructor{ std::cout << "Bye, " << company << "!/n";}// other methodsvoid 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 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(){ using std::cout; using std::ios_base; // set format to #.### ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); std::streamsize prec = cout.precision(3); cout << "Company: " << company << " Shares: " << shares << '/n'; cout << " Share Price: $" << share_val; // set format to #.## cout.precision(2); cout << " Total Worth: $" << total_val << '/n'; // restore original format cout.setf(orig, ios_base::floatfield); cout.precision(prec);}3
// usestok1.cpp -- using the Stock class// compile with stock10.cpp#include <iostream>#include "stock10.h"int main(){ { using std::cout; cout << "Using constructors to create new objects/n"; Stock stock1("NanoSmart", 12, 20.0); // syntax 1 stock1.show(); Stock stock2 = Stock ("Boffo Objects", 2, 2.0); // syntax 2 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); // temp object cout << "Revised stock1:/n"; stock1.show(); cout << "Done/n"; } // std::cin.get(); return 0; }當成員函數設計兩個指針的時候可能會用到this指針,如比較兩個對象某個數據成員的大小時。 1
//stock20.h...public: // Stock(); // default constructor Stock(const std::string & co, long n = 0, double pr = 0.0); ~Stock(); // do-nothing destructor void buy(long num, double price); void sell(long num, double price); void update(double price); void show()const; //添加比較兩個對象大小的聲明 const Stock & topval(const Stock & s) const;2
//stock20.cpp...const Stock & Stock::topval(const Stock & s) const{ if (s.total_val > total_val) return s; else return *this; //解引用,返回對象}新聞熱點
疑難解答
圖片精選