0% found this document useful (0 votes)
11 views

Chapter2 Classesandobjects Updated

This is class and object pdf

Uploaded by

ashwinh2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chapter2 Classesandobjects Updated

This is class and object pdf

Uploaded by

ashwinh2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Chapter 2: Classes and Objects

• A simple class : (Recap definitions and examples)


• C++ objects as Data types
• Constructors
• Objects as function arguments
• The default copy constructor
• Returning objects from Functions
• Classes, objects and memory
• Static class data
• const and classes
Simple class-Scenario: BankAccount

• 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.
Data and functions of class

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

Draw the object visualization ba2


for the customer object accnumber: 0
created in this line accbalance: 0
Summary:
1. Implemented classes (private, public, data,
constructor, functions)
2. Implemented object as function argument
and return values
3. Implemented static data members , const:
functions and objects
Scenario:
• Consider a Debit Card issued to a customer by a
Bank. The debit card is of type ‘Rupay’. The
customer can swipe the card for paying his
purchases and he is allowed to change the pin.
Identify the relevant attributes and functions the
customer can perform using debit card. Draw a
class diagram using proper UML notations.
Scenario:
• Consider a student of 4th semester, he register for
multiple courses. The student is allowed to
change his address and email. Identify relevant
attributes for student object and functions
performed by him. Draw a class diagram using
UML notations. Print the student details before
and after modifications.
Copy Constructor
• A copy constructor is a member function which
initializes an object using another object of the same
class. A copy constructor has the following general
function prototype:
• ClassName (const ClassName &oldObject);
Copy Constructor
• In C++, a Copy Constructor may be called in following
cases:
1. When an object of the class is returned by value.
2. When an object of the class is passed (to a
function) by value as an argument.
3. When an object is constructed based on another
object of the same class.
4. When the compiler generates a temporary object.
Copy Constructor
• Copy constructor vs Assignment Operator
Which of the following two statements call copy
constructor and which one calls assignment
operator?

BankAccount ba1, ba2(1001,5000);


BankAccount ba3=ba1; // calls copy constructor
ba2=ba1; // calls assignment operator
Copy Constructor
• When is user-defined copy constructor needed?
– The C++ compiler creates a default copy constructor for
each class which does a member-wise copy between
objects.
– The compiler created copy constructor works fine in
general.
– We need to define our own copy constructor only if an
object has pointers or any runtime allocation of the
resource like file handle, a network connection..etc.
Copy Constructor

You might also like