Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses.

Similar presentations


Presentation on theme: "1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses."— Presentation transcript:

1 1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses the class. This means that several files of the same project may all #include the header file. However, the compiler should only process it once. Conditional compilation allows code to be included or omitted based on certain conditions. We can use the following directives for conditional compilation: #ifdef, #ifndef, #else, #endif, #define

2 2 Classes Conditional compilation example #ifndef CONSTANT_NAME #define CONSTANT_NAME // code #endif In english: if CONSTANT_NAME has not been defined, define it compile code If CONSTANT_NAME has been defined, skip everything up to #endif and compile whatever follows (if anything follows). How does this help us? Initially, CONSTANT_NAME will not be defined, so the code will be compiled. After that, whenever this file is #included in another one, the constant will have been defined so the code will not be recompiled.

3 3 The bank account class #ifndef BANKACCOUNT_H #define BANKACCOUNT_H class BankAccount { private: double balance; // in dollars public: BankAccount(); BankAccount(double initbalance); double deposit(double amount); double withdraw(double amount); double getBalance(); } ; #endif bankaccount.h

4 4 The bank account class #include "bankaccount.h" #include using namespace std; BankAccount::BankAccount() { balance = 0.0; } bankaccount.cpp, continued on next slide use " ", not for user-defined header files. :: is the scope resolution operator. If it's missing, your compiler won't know that this is the implementation of the constructor for the BankAccount class.

5 5 The bank account class #include "bankaccount.h" #include using namespace std; BankAccount::BankAccount() { balance = 0.0; } BankAccount::BankAccount(double initbalance) { balance = initbalance; } double BankAccount::getBalance () { return balance; } bankaccount.cpp, continued on next slide

6 6 The bank account class BankAccount::BankAccount(double initbalance) { balance = initbalance; } double BankAccount::getBalance () { return balance; } double BankAccount::deposit (double amount) { balance += amount; return balance; } bankaccount.cpp, continued on next slide

7 7 The bank account class double BankAccount::deposit (double amount) { balance += amount; return balance; } double BankAccount::withdraw (double amount) { if (balance < amount) cerr << "Insufficient funds\n"; else balance –= amount; return balance; } bankaccount.cpp

8 8 Using the bank account class #include "bankaccount.h" #include using namespace std; int main () { BankAccount myaccount; myaccount.deposit(1000); myaccount.withdraw(333.33); cout << "I have " << myaccount.getBalance() << " dollars" << endl; myaccount.balance = 10000000; return 0; } Example 1: Create a BankAccount object. The default constructor is executed automatically. Use the dot operator to access the member functions This line will give a compilation ERROR because balance is private.

9 9 Using the bank account class #include "bankaccount.h" #include using namespace std; int main () { BankAccount myaccount(1000); cout << "I have " << myaccount.getBalance() << " dollars" << endl; return 0; } Example 1: Create a BankAccount object, using the constructor that takes an argument. The balance will be initialized to 1000 immediately.

10 10 Using the bank account class #include "bankaccount.h" #include using namespace std; int main () { BankAccount *myaccount, *youraccount; myaccount = new BankAccount; youraccount = new BankAccount(100.00); (*myaccount).deposit(15.00); youraccount –> deposit(25.00); return 0; } Example 1: Pointers to BankAccount. Allocate space. This statement uses the default constructor. And this one uses the other constructor. the arrow operator (hyphen followed by > ) is shorthand for the asterisk-dot combination.

11 11 Using the bank account class #include "bankaccount.h" #include using namespace std; int main () { BankAccount *familyaccounts[4]; for (int i = 0; i < 4; i++) familyaccounts[i] = new BankAccount; familyaccounts[0] –> deposit(1000.00); return 0; } Example 1: An array of pointers to BankAccount. allocate space and initialize each element of the array


Download ppt "1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses."

Similar presentations


Ads by Google