Download presentation
Presentation is loading. Please wait.
Published byWhitney Dean Modified over 6 years ago
1
Classes Object-oriented programming: Example: Bank transactions
Model the problem as a collection of objects that have certain attributes and interact with one another and/or the world through specific operations. Example: Bank transactions object: bank account attribute: balance operations: initialize, deposit, withdraw. We need to design a blueprint to describe the bank account.
2
Classes Class = a blueprint for an object.
It describes the object's attributes (member data) and any operations that are allowed on the object (member functions) Object = an instance of a class e.g. a specific bank account. The attributes will now have specific values.
3
The bank account class, take 1
The name of the class. We will use it to create BankAccount objects. class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; NEVER forget the semicolon
4
The bank account class, take 1
class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; data member private and public are member access specifiers. Data members or member functions that are declared private cannot be directly accessed outside of the class. Instead, they can only be accessed by member functions. In this example, it is necessary to make the balance private since then it can only be modified in a controlled way through the withdraw and deposit functions.
5
The bank account class, take 1
class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; Usually, data are private and functions are public. Data members or member functions that are declared public can be accessed directly from outside the class. Privacy allows us to separate the interface from the implementation. public members : the interface private members : part of the implementation This allows us to change the implementation without changing the interface.
6
The bank account class, take 1
class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; default constructor. ALWAYS define one When a class object is created, its members are initialized by a special function called a "constructor". The default constructor specifies default initial values, so that the object is always guaranteed to be initialized to a consistent state. If the programmer does not define a default constructor, the compiler creates one and calls it automatically every time an instance of the class is created, but this does not guarantee correct initialization of member data.
7
The bank account class, take 1
class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; various public member functions Note the naming convention: class names usually start with an uppercase letter member names usually start with a lowercase letter.
8
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
9
Classes Conditional compilation example 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). #ifndef CONSTANT_NAME #define CONSTANT_NAME // code #endif 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.
10
The bank account class, take 1
#ifndef BANKACCOUNT_H #define BANKACCOUNT_H class BankAccount { private: int balance; // in dollars public: BankAccount(); void deposit(int amount); void withdraw(int amount); int getBalance(); } ; #endif bankaccount.h
11
The bank account class, take 1
#include "bankaccount.h" using std::cerr; using std::endl; BankAccount::BankAccount() { balance = 0; } use " ", not < > for user-defined header files. bankaccount.cpp, continued on next slide :: 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.
12
The bank account class, take 1
BankAccount::BankAccount() { balance = 0; } int BankAccount::getBalance () { return balance; bankaccount.cpp, continued on next slide
13
The bank account class, take 1
void BankAccount::deposit (int amount) { balance += amount; } void BankAccount::withdraw (int amount) { if (balance < amount) cerr << "Insufficient funds\n"; else balance –= amount;
14
Using the bank account class
Example 1: #include "bankaccount.h" #include<iostream> using std::cout; using std::endl; int main () { BankAccount myaccount; myaccount.deposit(1000); myaccount.withdraw(333.33); cout << "I have " << myaccount.getBalance() << " dollars" << endl; myaccount.balance = ; return 0; } 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.