Chapter2 Classesandobjects Updated
Chapter2 Classesandobjects Updated
BankAccount
-accountnumber:int
-accbalance
+setaccountnumber(int):void
+setaccbalance(float):void
+getaccountnumber():int
+getaccountbalance():int
General syntax of class definition
Class:BankAccount
BankAccount : main function, object as a data type
Class (Object) specification and object of BankAccount
Constructor: Initializing Object data members
• The data members of object acc1 (instance of BankAccount)
– accnumber and accbalance
– The member functions setaccnumber(int) and setaccbalance(float) are
used to initialize
– A set function is required for every data member initialization, if forget to
initialize may lead to error
– The data members are to be initialized as and when the object is created.
– It is implemented using a special function called ‘constructor’, which
automatically invoked when object is created.
– The constraint of creating constructor
• The name of the constructor is same as class name
• No return type for the conctructor
Constructor: Default, Parameterized, copy
Object of BankAccount
acc1
accnumber=
0
accbalance=
0
Object of BankAccount
acc2
accnumber=1
001
Object of BankAccount
accbalance=
5000 acc3
accnumber= 1001
accbalance=
5000
Object as Function argument
• Consider a bank account, the attributes are accnumber and
accbalance. Write a CPP to create a class for bank account,
instantiate account object, initialize and print attributes of bank
account using object as a function argument.
Returning objects from Functions
Enhance the previous example to return (create) BankAccount object using functions.
Returning objects from Functions
Classes, Objects and Memory
• OO concept emphasizes that, objects are complete, self
contained entities designed using the class definitions.
• The impression is, each object created from a class
contains separate copies of that class’s data and
member functions.
• The actual storage structure if different for object data,
as all the objects of a classes use the same function.
• The member functions are created and placed in
memory only once.
Classes, Objects and Memory
Static class data
• Each object created has its own copy of data.
• If a “static” modifier use with data member of a class, then
that data is visible across objects of that class.
• The scope of the static data member is within the class, it is
a class level variable.
• A static data item is useful when all objects of the same
class must share a common item of information.
• Key word: “static” is used to create static data members
• Two step process to create static data member: declare and
define
static class data
static class data: count of objects created
const and classes:
• const: on member function and on objects
• The const member function guarantees that it will
never modify any of its class’s data member.
Const: function
Const: objects
Member function defined out side the class
Array
• C++ uses c-style array: declaration and definition
• Two types: 1D and 2D
General syntax:
type array_name[size];
Type: primitive type/ user defined types (objects)
Array syntax and example
Array of objects
• Enhance the previous BankAccount class and use array
of objects and call methods.
Redefine the BankAccount class to include the additional attributes: name and
mobile The account holder has two mobiles.
Scenario:
Consider a customer has bank accounts, they are of type
savings accounts. He can credit, debit money to his
accounts and transfer amount from one account to
another. Both the accounts shall have minimum
balance of Rs. 500. Write a cpp program to simulate the
above scenario.
Relationship: Customer has BankAccount
Object of Customer
c1
custid: 0
name: 0
Object of BankAccount Note:
ba1 This is
accnumber: 0
not
Class
accbalance: 0 Diagra
m
Object of BankAccount