Classes%20with%20dynamic%20members - PowerPoint PPT Presentation

About This Presentation
Title:

Classes%20with%20dynamic%20members

Description:

Classes with dynamic members – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 21
Provided by: ust108
Category:

less

Transcript and Presenter's Notes

Title: Classes%20with%20dynamic%20members


1
  • Classes with dynamic members

2
Class matters!
local
global
public
class A public int x void f() main()
A a a.x 0 a.f()
int x void f() int x x10 main()
x0 f()
int x void f() x10 main() x0 f()
Unstructured with everything global
Structured with local variables, global
functions
Object-oriented with global objects
3
Public or private?
int main() A a a.f() cout ltlt a.x ltlt
endl cout ltlt a.y ltlt endl // no!!! a.x
1000 a.y 10000 // no!!!
class A public void f() int
x private int y void Af()
x10 y100
Global objects ? member functions (global? or
public to objects) ? member variables (local or
private to member functions)
4
Abstract Data Type public function, private data
class A public A() int f() private
int x AA() x0 int Af() return
x
int main() A a cout ltlt a.f() // not a.x!!!
A b(a) A c c a // member-wise copy
Objects are calling member funtions (public
interface) Member functions are using member
variables (private data) Objects are not directly
dependent on implementation (private data)
5
Static and dynamic
Static variables (objects)
Dynamic variables (objects)
A (direct) named memory location
A static part (pointer) (indirect) nameless
memory location (dynamic part)
int a a 20
int pa pa new int pa 20
20
20
a
pa
static
dynamic
static
6
classes
At least one pointer member (I.e. dynamic
variable)
Only static member variables
class B public B() B(const B b)
B() private int px
class A public A() private int
x
(Must) a defaut value constructor A() for copy
constructor automatically privided No
destructor assignment automatically provided
(Must) a defaut value constructor for (Must)
redefine copy constructor (Must) the
destructor (Must) redefine assignment (do
later)
Creation by new initialiation
initialisation
7
BB() px new int px 0 BB(const
B b) px new int (px)
(b.px) BB() delete px
class B public B() B(const B b)
B() private int px
8
Automatic behavior of constructors/destructor
All constructors/destructor have automatic
behavior, they are called implicitly!!! (thats
why they have standard names!)
Constructors are called when creating variables
and other occasions
A a // implicitly call the default value
constructor AA() A b(a) // implicitly call
the copy constructor AA(const A a)
Destructors are called when the variables go out
of scope
void somefunction() B b
9
Make an Abstract Data Type with dynamic class
  • One more example of ADT
  • integer linked list using class
  • A class with dynamic objects
  • Copy constructor
  • Destructor

10
linked lists definition
struct Node int data Node
next typedef Node NodePtr NodePtr head
  • bool listEmpty(NodePtr head)
  • NodePtr addHead(NodePtr head, int newdata)
  • int getHead(NodePtr head)
  • NodePtr getRest(NodePtr head)
  • // void delHead(NodePtr Head)
  • //

11
Usage in procedural way
void main() NodePtr Head1NULL, Head2 NULL,
Head addHead(Head1, 50) addHead(Head1,
40) addHead(Head1, 30) addHead(Head1,
20) cout ltlt "List 1 " ltlt endl DisplayList(Hea
d1) cout ltlt "Length of Head1 list " ltlt
length(Head1) ltlt endl cout ltlt "Recursive length
of Head1 list " ltlt lengthRec(Head1) ltlt
endl if(isPalindrome(Head1)) cout ltlt "Head1
list is palindrome" ltlt endl else cout ltlt
"Head1 list is not palindrome" ltlt
endl addHead(Head2, 25) addHead(Head2,
35) addHead(Head2, 45) addHead(Head2,
35) addHead(Head2, 25) cout ltlt "List 2 " ltlt
endl DisplayList(Head2) cout ltlt "Length of
Head2 list " ltlt length(Head2) ltlt endl cout ltlt
"Recursive length of Head2 list " ltlt
lengthRec(Head2) ltlt endl if(isPalindrome(Head2))
cout ltlt "Head2 list is palindrome" ltlt
endl else cout ltlt "Head2 list is not
palindrome" ltlt endl Head mergeLists(Head1,
Head2) cout ltlt "Merged List " ltlt
endl DisplayList(Head) cout ltlt "Length of
Merged list " ltlt length(Head) ltlt endl cout ltlt
"Recursive length of Merged list " ltlt
lengthRec(Head) ltlt endl if(isPalindromeRec(Head)
) cout ltlt "Merged list is palindrome" ltlt
endl else cout ltlt "Merged list is not
palindrome" ltlt endl cout ltlt "check the list
again" ltlt endl DisplayList(Head)
12
  • struct Node
  • public
  • int data
  • Node next
  • typedef Node Nodeptr
  • class listClass
  • public
  • listClass() //
    constructor
  • listClass(const listClass list1) // copy
    constructor
  • listClass() //
    destructor
  • bool empty() const // boolean
    function
  • void addHead(int newdata) // add to
    the head
  • void delHead() // delete
    the head
  • int headElement() const // access
    functions
  • int length() const // utility
    function

new member functions
old operations
13
Usage in object way
  • void main()
  • listClass L // constructor called
    automatically here for L
  • L.print()
  • L.addHead(30)
  • L.print() 30
  • L.addHead(13)
  • L.print() 13 30
  • L.addHead(40)
  • L.print() 40 13 30
  • L.addHead(50)
  • L.print() 50 40 13 30
  • listClass N(L)
  • N.print() 30 13 40 50
  • listClass R
  • R.print()
  • if(R.empty())
  • cout ltlt "List R empty" ltlt endl
  • L.delHead()

14
Implementation
Some simple member functions
  • listClasslistClass()
  • head NULL
  • bool listClassempty() const
  • if(headNULL)
  • return true
  • else
  • return false
  • int listClassheadElement() const
  • if(head ! NULL)
  • return head-gtdata
  • else
  • cout ltlt "error trying to find head of empty
    list" ltlt endl
  • exit(1)

15
(explicitly defined) copy constructor
  • listClasslistClass(const listClass list1)
  • head NULL
  • Nodeptr cur list1.head
  • while(cur ! NULL)
  • // addEnd(cur-gtdata)
  • addHead(cur-gtdata) // inverse list order
  • cur cur-gtnext

Call other member function!, so within class,
still procedural.
16
Destructor deallocation function
listClasslistClass() Nodeptr
cur while(head!NULL) cur head head
head-gtnext delete cur
17
Adding an element to the head
  • void listClassaddHead(int newdata)
  • Nodeptr newPtr new Node
  • newPtr-gtdata newdata
  • newPtr-gtnext head
  • head newPtr

18
Deleting the head
  • void listClassdelHead()
  • if(head ! NULL)
  • Nodeptr cur head
  • head head-gtnext
  • delete cur

19
Print the list
  • void listClassprint() const
  • cout ltlt ""
  • Nodeptr cur head
  • while(cur ! NULL)
  • cout ltlt cur-gtdata ltlt " "
  • cur cur-gtnext
  • cout ltlt "" ltlt endl

20
Computing the number of elements of a given list
  • int listClasslength() const
  • int n0
  • Nodeptr cur head
  • while(cur ! NULL)
  • n
  • cur cur-gtnext
  • return n
Write a Comment
User Comments (0)
About PowerShow.com