14
1 Classes Object-oriented programming: 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.

1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

Embed Size (px)

Citation preview

Page 1: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

1

Classes

Object-oriented programming: 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.

Page 2: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

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.

Page 3: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

3

The bank account class, take 1

class BankAccount {private:

int balance; // in dollarspublic:

BankAccount(); void deposit(int amount);void withdraw(int amount);int getBalance();

} ;

NEVER forget the semicolon

The name of the class. We will use itto create BankAccount objects.

Page 4: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

4

The bank account class, take 1

class BankAccount {private:

int balance; // in dollarspublic:

BankAccount(); void deposit(int amount);void withdraw(int amount);int getBalance();

} ;

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.

data member

Page 5: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

5

The bank account class, take 1

class BankAccount {private:

int balance; // in dollarspublic:

BankAccount(); void deposit(int amount);void withdraw(int amount);int getBalance();

} ;

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 implementationThis allows us to change the implementation without changing the interface.

Usually, data are private and functions are public.

Page 6: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

6

The bank account class, take 1class BankAccount {private:

int balance; // in dollarspublic:

BankAccount(); void deposit(int amount);void withdraw(int amount);int getBalance();

} ;

When a class object is created, its members are initialized by a specialfunction 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.

default constructor.ALWAYS define one

Page 7: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

7

The bank account class, take 1

class BankAccount {private:

int balance; // in dollarspublic:

BankAccount(); void deposit(int amount);void withdraw(int amount);int getBalance();

} ;

Note the naming convention:class names usually start with an uppercase lettermember names usually start with a lowercase letter.

various public member functions

Page 8: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

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

Page 9: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

9

Classes

Conditional compilation example

#ifndef CONSTANT_NAME#define CONSTANT_NAME// code #endif

In english:

if CONSTANT_NAME has not been defined, define itcompile 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, sothe code will be compiled. After that, whenever this file is #included in anotherone, the constant will have been defined so the code will not be recompiled.

Page 10: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

10

The bank account class, take 1

#ifndef BANKACCOUNT_H#define BANKACCOUNT_H

class BankAccount {private:

int balance; // in dollarspublic:

BankAccount(); void deposit(int amount);void withdraw(int amount);int getBalance();

} ;

#endif

bankaccount.h

Page 11: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

11

The bank account class, take 1

#include "bankaccount.h"using std::cerr;using std::endl;

BankAccount::BankAccount() {balance = 0;

}

bankaccount.cpp, continued on next slide

use " ", not < > for user-definedheader files.

:: is the scope resolution operator. If it's missing, your compiler won't knowthat this is the implementation of the constructor for the BankAccount class.

Page 12: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

12

The bank account class, take 1

BankAccount::BankAccount() {balance = 0;

}

int BankAccount::getBalance () {return balance;

}

bankaccount.cpp, continued on next slide

Page 13: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

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;}

Page 14: 1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the

14

Using the bank account class

#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 = 10000000;

return 0;}

Example 1:

Create a BankAccountobject. The defaultconstructor is executedautomatically.

Use the dot operatorto access the memberfunctions

This line will give a compilation ERRORbecause balance isprivate.