New blog location
New blog location
CheckingAccount
class as a running example.CheckingAccount
class:checkingaccount.h
:#ifndef CHECKING_H #define CHECKING_H class CheckingAccount { public: CheckingAccount(double balance); ~CheckingAccount(); void deposit(double amount); bool withdraw(double amount); double getBalance() const; private: double mBalance; } #endif
checkingaccount.cpp
: Note that the ::
operator is the scope resolution operator.CheckingAccount::CheckingAccount(double balance) //constructor { mBalance = balance; } void CheckingAccount::deposit(double amount) { mBalance += amount; } bool CheckingAccount::withdraw(double amount) { if((mBalance - amount) > 0) { mBalance -= amount; return true; } return false; } double getBalance() const { return mBalance; } //Destructor not implemented as there is no dynamic memory allocated.
public
: Any code can call a public
member function or access a public
data member.protected
: Only the class and its derived classes can access the member function or data memberprivate
: Only the class can access the member function or data member.class CheckingAccount //.... private : double mBalance = 0.0;
this
pointerthis
which points to the current object. It can be used to disambiguate parameters from data members. It can also be used to pass a reference to the current object to a function that takes a reference or const reference to the object.CheckingAccount chkAccount(1000.00); cout << "Balance is " << chkAccount.getBalance() << endl;
auto myChkAccount = std::make_unique<CheckingAccount>(10000.00); //smart pointer cout << myChkAccount->getBalance() << endl; CheckingAccount * chkaccount = new CheckingAccount(10000.00); //prefer smart pointer over this cout << chkaccount->getBalance() << endl; delete chkaccount;
#include <string> class Foo { private: std::string mAddress; } int main() { Foo fooObj; return 0; }
string
object is created when fooObj
is created and is destructed when fooObj
is destructed (in this case, goes out of scope).CheckingAccount account(5000.00);
auto chkAccount = std::make_unique<CheckingAccount>(5000.00); CheckingAccount anotherAccount = nullptr; anotherAccount = new CheckingAccount(100.00);
CheckingAccount accounts[5]; CheckingAccount* accounts = new CheckingAccount[5];
CheckingAccount::CheckingAccount() { mBalance = 0.0; }
CheckingAccount chkAccount; chkAccount.deposit(100.00);
class CheckingAccount { public: CheckingAccount() = delete; }
CheckingAccount:: CheckingAccount(double balance) : mBalance(balance) { }
const
data members: can only be created and assigned once.class CheckingAccount { public: CheckingAccount(const CheckingAccount &src); }
CheckingAccount::CheckingAccount(const CheckingAccount &src) : mBalance(src.mBalance) { }
n1
,n2
,nM
, compiler generates a copy constructor that can be visualized as follows:cname::cname(const cname& src) : n1(src.n1), n2(src.n2),...,nM(src.nM) {}
void foo(std::string name) { cout << "Name is "<<name<<endl; } int main() { string name = "John"; foo(name); //copy constructor }
CheckingAccount account(5000.00); CheckingAccount accountCopy(account);
string_view
by value since it is just a pointer and lengthCheckingAccount(const CheckingAccount &src) = delete;
std::initializer_list<T>
as the first parameter, with no additional params or with additional params given default values.class Sequence { public : Sequence(initializer_list<int> params) { for(const auto& value: params) { values.push_back(value); //note push_back takes const reference parameter and makes a copy of it internally } } private: vector<int> values; }
CheckingAccount& operator=(const CheckingAccount &rhs);
CheckingAccount& CheckingAccount::operator=(const CheckingAccount& rhs) { if(this == &rhs) { return *this; //have to do this as self assignment is allowed in C++ } mBalance = rhs.mBalance; return *this; }
CheckingAccount acct1(10.0); CheckingAccount acct2(acct1); CheckingAccount acct3 = acct2;
acct3 = acct1;
std::string
is returned from a function and assigned as follows, the copy constructor is called and then the assignment operator is called:string s1; s1 = getString();
string s1 = getString(); //getString calls copy constructor and s1's copy constructor is called.
CheckingAccount::CheckingAccount(const CheckingAccount &src) : mBalance(src.mBalance) {}
CheckingAccount::CheckingAccount(const CheckingAccount &src) { mBalance = src.mBalance; //assignment operator }
Comments
Post a Comment