"this" is a pointer to the currrent object class BankAccount { public : BankAccount deposit(float amount); BankAccount withdrawl(float amount); BankAccount(float amount = 0.0); private: float balance; }; BankAccount::BankAccount(float amount) { balance = amount; } BankAccount& BankAccount::deposit(float amount) { this->balance = this->balance + amount; } BankAccount& BankAccount::withdrawl(float amount) { balance = balance - amount; }
class BankAccount { public : BankAccount& deposit(float amount); BankAccount& withdrawl(float amount); BankAccount(float amount = 0.0); private: float balance; }; BankAccount::BankAccount(float amount) { balance = amount; } BankAccount& BankAccount::deposit(float amount) { balance = balance + amount; return *this; } BankAccount& BankAccount::withdrawl(float amount) { balance = balance - amount; return *this; } main() { BankAccount customer; customer.deposit(50).deposit(150).withdrawl(217.0); }
#include <iostream.h> class BankAccount { friend void friendFunction(BankAccount& change); public: void setBalance(float amount) {balance = amount;} float getBalance() {return balance;} private: float balance; }; void friendFunction(BankAccount& change) { change.balance = 20; // friends can access private members } main() { BankAccount customer; customer.setBalance(2.0); friendFunction(customer); cout << customer.getBalance() << "\n"; // prints 20 }
#include <iostream.h> class BankAccount { friend class Legislature; public: void setBalance(float amount) {balance = amount;} float getBalance() {return balance;} private: float balance; void transaction(float amount) {balance += amount;} }; class Legislature // all members of a class can access { // a friends private members public: void tax(BankAccount &yourMoney) { yourMoney.transaction(-20);} }; main() { BankAccount taxPayer; Legislature Congress ; taxPayer.setBalance(200.0); Congress.tax(taxPayer); cout << taxPayer.getBalance() << "\n"; // prints 180 }
#include <iostream.h> class BankAccount { friend ostream& operator<<(ostream& , BankAccount&); friend istream& operator>>(istream&, BankAccount&); private: float balance; char name[20]; }; ostream& operator<<(ostream& output, BankAccount& account) { output << account.name << '\t' << account.balance; return output; } istream& operator>>(istream& input, BankAccount& account) { input >> account.name >> account.balance; return input; } main() { BankAccount richCustomer, poorCustomer; cin >> poorCustomer>> richCustomer; cout << poorCustomer<< richCustomer; }
cin >> a is translated to >>(cin, a) cin >> a >> b is translated to >>( >>(cin,a), b) cout << a is translated to <<(cout, a) cout << a << b is translated to <<( <<(cout,a), b) Must return the stream when we define >> or << ostream& operator<<(ostream& output, BankAccount& account) { output << account.name << '\t' << account.balance; return output; } istream& operator>>(istream& input, BankAccount& account) { input >> account.name >> account.balance; return input; }
class Stack { friend ostream& operator<<(ostream& , Stack&); public: Stack(); int isEmpty(); int isFull(); void push(int item); float pop(); private: float stack[100]; int nextFreeElement; }; ostream& operator<<(ostream& output, Stack& aStack) { output << "Stack(" ; output << aStack.stack[aStack.nextFreeElement - 1]; for (int K = aStack.nextFreeElement - 2; K>= 0; K--) output << "," << aStack.stack[K]; output << ")"; return output; } void main() { Stack customerRecords; customerRecords.push(5); customerRecords.push(4); customerRecords.push(2); cout << customerRecords << endl; }
class Stack; ostream& operator<<(ostream& os, Stack& aStack) { aStack.print(os); return os; } class Stack { public: Stack(); void print(ostream&); int isEmpty(); int isFull(); void push(int item); float pop(); private: float stack[100]; int nextFreeElement; }; void Stack::print(ostream& output) { output << "Stack(" ; output << stack[nextFreeElement - 1]; for (int K = nextFreeElement - 2; K>= 0; K--) output << "," << stack[K]; output << ")"; }