若是你依然在編程的世界裏迷茫,不知道本身的將來規劃,小編給你們推薦一個iOS高級交流羣:458839238 裏面能夠與大神一塊兒交流並走出迷茫。小白可進羣免費領取學習資料,看看前輩們是如何在編程的世界裏傲然前行! 羣內提供數據結構與算法、底層進階、swift、逆向、整合面試題等免費資料 附上一份收集的各大廠面試題(附答案) ! 羣文件直接獲取ios
在頭文件中定義類程序員
// version 00
#ifndef STOCK00_H_
#define STOCK00_H_
#include <string>
//private 聲明的成員只有用成員函數才能訪問
//public 聲明的成員,使用類的對象的程序均可以訪問
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();
}; // note semicolon at the end
#endif
複製代碼
// version 00
#include <iostream>
#include "stock00.h"
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';
}
複製代碼
1.定義於類聲明中的函數都將自動稱爲內聯函數,所以Stock::set_tot()是一個內聯函數。類聲明常將短小的成員函數做爲內聯函數。面試
2.在類的聲明以外將函數聲明爲內聯函數須要使用inline限定符進行限定。 使用類算法
// compile with stock.cpp
#include <iostream>
#include "stock00.h"
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();
// std::cin.get();
return 0;
}
複製代碼
類中的數據部分是私有的,所以不能經過直接訪問類中的數據成員對其進行初始化,這時就須要使用構造函數和析構函數了編程
爲Stock類增長構造函數和析構函數swift
#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();
};
#endif
複製代碼
// stock1.cpp � Stock class implementation with constructors, destructor added
#include <iostream>
#include "stock10.h"
// 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 destructor
//< 析構函數
Stock::~Stock() // verbose class destructor
{
std::cout << "Bye, " << company << "!\n";
}
// other methods
//< 方法定義
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()
{
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);
}
複製代碼
// 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
//< 賦值結束以後程序調用析構函數,刪除該臨時對象
//< 所以此處有析構函數的調用
stock1 = Stock("Nifty Foods", 10, 50.0); // temp object
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
}
// std::cin.get();
return 0;
}
複製代碼
void show() const; //< 確保該函數不修改調用對象
複製代碼
頭文件數組
// stock20.h -- augmented version
#ifndef STOCK20_H_
#define STOCK20_H_
#include <string>
class Stock
{
private:
std::string company;
int 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(); // 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;
};
#endif
複製代碼
#include <iostream>
#include "stock20.h"
using namespace std;
// constructors
Stock::Stock() // default constructor
{
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";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
// class destructor
Stock::~Stock() // quiet class destructor
{
}
// other methods
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() const
{
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);
}
const Stock & Stock::topval(const Stock & s) const
{
if (s.total_val > total_val)
return s;
else
//< 如果調用方法的對象比較大就直接返回,該對象的引用
//< this指向調用方法對象的地址, *this則是對象的地址
return *this;
}
複製代碼
####對象數組bash
用戶一般常見一個類的多個對象,能夠建立獨立的對象變量,可是闖將對象數組將更加合適。數據結構
初始化對象數組的方案是,首先使用默認構造函數建立數組元素,而後花括號中的構造函數將建立臨時對象,而後臨時對象內容複製到對應的元素中。所以、要建立對象數組,則這個類必須由默認構造函數。函數
// compile with stock20.cpp
#include <iostream>
#include "stock20.h"
const int STKS = 4;
int main()
{
{//< 使用大括號括起來是爲了可以在主函數退出前就調用構造函數
// create an array of initialized objects
Stock stocks[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++)
stocks[st].show();
// set pointer to first element
const Stock * top = &stocks[0];
for (st = 1; st < STKS; st++)
top = &top->topval(stocks[st]);
// now top points to the most valuable holding
std::cout << "\nMost valuable holding:\n";
top->show();}
// std::cin.get();
return 0;
}
複製代碼
####類的做用域
在類中定義的名稱做用域都爲整個類,類的做用域意味着不能在外部直接訪問類的成員,公有成員也是如此,也就是說調用公有成員函數,也必須經過對象。
在類聲明或成員函數定義中,可使用未修飾的成員名稱(未限定的名稱),
####抽象數據類型
// stack.h -- class definition for the stack ADT
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack
{
private:
//< 無名的枚舉型並不實際建立枚舉型類成員
enum {MAX = 10}; // constant specific to class
//< 使用數組實現棧,可是在公有部分隱藏了棧的實現細節
//< 所以可使用動態數組實現,也不須要更改代碼的藉口實現的細節
Item items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop top into item
};
#endif
複製代碼
// stack.cpp -- Stack member functions
#include "stack.h"
Stack::Stack() // create an empty 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;
}
else
return false;
}
bool Stack::pop(Item & item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
yhhgfik
複製代碼
// stacker.cpp -- testing the Stack class
#include <iostream>
#include <cctype> // or ctype.h
#include "stack.h"
int main()
{
using namespace std;
Stack st; // create an empty stack
char ch;
unsigned long po;
cout << "Please enter A to add a purchase order,\n"
<< "P to 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\n";
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;
}
複製代碼