Member Wise Initialization of Objects - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Member Wise Initialization of Objects

Description:

Member-wise initialization can cause problem, if the class ... void String::display()const { cout str; Dr. M. Moussavi. 5. Member-wise Initialization ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 36
Provided by: mous1
Category:

less

Transcript and Presenter's Notes

Title: Member Wise Initialization of Objects


1
Member Wise Initialization of Objects
2
Member-wise Initialization
  • An instance of a class can be initialized with
    another instance of the same class
  • Aclass a1(40)
  • Aclass a2 a1
  • Every data member of instance a1 will be copied
    into instance a2.
  • An important point
  • Declaration of a2 does not invoke the constructor
    of class A.

3
Member-wise Initialization
  • Member-wise initialization can cause problem, if
    the class definition contains a pointer (see the
    following example)
  • class String
  • public
  • String(char s)
  • String()
  • void display()const
  • private
  • char str
  • int len

4
Member-wise Initialization
  • StringString(char s)
  • len strlen(s)
  • str new char len1
  • assert(str ! 0)
  • strcpy(str,s)
  • StringString()
  • delete str
  • void Stringdisplay()const
  • cout ltlt str

5
Member-wise Initialization
  • void main()
  • String s1(World)
  • String s2 s1
  • // Point 1


6
Member-wise Initialization Problems
Point 1
5
Len
s1
Free Store
str
W
o
Len
5
r
s2
l
str
d
\0
No Arguments
7
Member-wise Initialization Problems
  • Another Problem!!
  • Destructor of String will be called twice

8
How to solve the Problem
  • The designer of the class must
  • Define a special constructor similar to the the
    following function prototype
  • X (const X)
  • Define an assignment operator function for class
    String
  • String Stringoperator (String S)

9
Member-wise Initialization
  • class String
  • char str
  • int len
  • public
  • String(char s)
  • String(const String) // special constructor
  • String Stringoperator (String S)
  • String()
  • void display()cost

10
Definition of special constructor
  • StringString(const String s)
  • //point 1
  • len s.len.
  • str new char len1
  • assert (str !0)
  • strcpy(str, s.str)

11
Define Assignment Operator
  • String Stringoperator (String s)
  • //point Three
  • if(this ! s)
  • delete str
  • len S.len
  • strnew char len1
  • strcpy(str,S.str)
  • return this

12
Using String Class
  • void main()
  • String s1(World)
  • String s2 s1
  • // Point 2
  • String s3 ABC
  • s1 s3

13
AR at point 1 inside the copy constructor
AR StringString(const String )
No locals
this
s
Free Store
len
5
W
s1
o
str
r
l
d
\0
len
??
s2
str
14
Point 2 in main
AR StringString(const String )
No locals
Free Store
this
s
len
5
W
W
s1
o
str
o
r
r
l
l
d
d
\0
len
\0
5
s2
str
15
AR at point 3 inside the assignment operator
String Stringoperator (String s)
// Point 3 if(this ! s) delete
str len S.len strnew char len1
strcpy(str,S.str) return this
AR Stringoperator
No locals
this
s
len
5
W
s1
o
str
r
l
d
\0
len
3
s3
str
A
B
C
\0
Free Storage
16
  • A Simple Class String with Copy Constructor and
    Overloaded Assignment Assignment Operator.

17
Interface for Class String
  • class String
  • public
  • String(char s)
  • String() // default constructor
  • String() // destructor
  • String(const String) // Special (copy)
    constructor
  • String operator (String S) // assignment
    operator
  • void display()const
  • private
  • char str
  • int len

18
Implementation of Class String
  • StringString(char s)
  • len strlen(s)
  • str new charlen1
  • assert(str ! 0)
  • strcpy(str,s)
  • StringString()
  • len(0), str(new char1)
  • str0 \0
  • void Stringdisplay()const
  • cout ltlt str
  • StringString()
  • delete str

19
Definition of Copy Constructor
  • StringString(const String s)
  • //point 1
  • len s.len
  • str new char len1
  • assert (str !0)
  • strcpy(str, s.str)

20
Define Assignment Operator
  • String Stringoperator (String s)
  • //point Three
  • if(this ! s)
  • delete str
  • len s.len
  • strnew char len1
  • strcpy(str, s.str)
  • return this

21
Using String Class
  • include ltiostream.hgt
  • include ltstring.hgt
  • include ltassert.hgt
  • void main()
  • String p NULL
  • p fun()
  • // point 3
  • delete p
  • // Point 4
  • return 0
  • String fun()
  • String s1
  • String s2(XY)
  • String s3 NULL
  • String s4 NULL
  • // Point 1
  • s3 new String (AD)
  • s4 new String (TD)
  • // Point 2
  • return s3

22
FREE STORE
STRING CONSTANT AREA
STACK
AR fun
len
\0
0
s1
X
str
Y
X
\0
Y
len
\0
2
s2
str
A
D
s3
\0
s4
T
No arg
D
AR main
\0
p
NO ARGS
POINT 1
23
FREE STORE
STRING CONSTANT AREA
STACK
AR fun
len
\0
0
s1
X
str
Y
X
\0
Y
len
\0
2
s2
str
A
D
len
2
s3
\0
str
s4
A
T
No arg
D
D
len
2
AR main
\0
\0
str
p
T
D
NO ARGS
\0
POINT 2
24
FREE STORE
STRING CONSTANT AREA
STACK
X
Y
\0
len
2
AR main
str
A
p
A
D
D
\0
\0
NO ARGS
2
len
T
Memory leak
str
D
\0
T
D
\0
POINT 3
25
FREE STORE
STRING CONSTANT AREA
STACK
X
Y
\0
AR main
p
A
D
\0
NO ARGS
2
len
Memory leak
str
T
D
\0
POINT 4
26
Questions
  • When do we need a destructor?
  • When do we need assignment operator?
  • When do need copy constructor?
  • When do need default constructor?
  • When is constructor called?
  • When is destructor called
  • What is the law of BIG 3?

27
How many times constructor, destructor,
assignment operator, default constructor, .
Called
  • int main(void)
  • String s1(ABC)
  • String s2(XY
  • String s3 (KLM)
  • String s4
  • s4 new String(BAR)
  • String s5 s1
  • s3 s2
  • String s62
  • delete s4
  • //Point one
  • String s7 fun(s1, s2, s1)
  • S2 fun(s1, s2, s7)
  • Return 0

String fun (String x, Strin y, String z)
String w // Some code return w
28
Class Design
29
How to Design an O.O. Application
  • Understand the requirement
  • Example
  • Problem Statement
  • You have been hired by Sly Tonys Video Rentals
    to build a video rental program. Tony currently
    rents out only movies. For all rentals Tony would
    like the due date, the renters identification
    number, rental identification number, film name
    and producer name stored. For all customers he
    would like their name, address, telephone.

30
How to Design an O.O. Application
  • Find out which entities in the application
    statement must be considered as an object in your
    application.
  • Which nouns in the problem statement must be
    considered as properties of the selected
    entities.

31
How to Design an O.O. Application
  • Selected entities and their properties
  • Rental
  • due date,
  • the renters identification number,
  • rental identification number,
  • film name
  • Customer
  • name,
  • address,
  • telephone,
  • id

32
How to Design an O.O. Application
  • Each selected entity must be considered as a
    class, and the properties will data members of
    the class.
  • Take the following steps to design your class
  • Select an appropriate name for your class
  • Select meaningful name for your class properties
    (data members).
  • Find out, if your class needs one or more
    constructors

33
How to Design an O.O. Application
  • Steps to design your class (continued)
  • Find out if your class needs a default
    constructor
  • Find out if your class needs a destructor
  • If your class needs a destructor, make sure to
    define copy constructor and assignment operator
    (The law of BIG 3).
  • For each data member in your class consider a get
    and a set member functions. For example if your
    class has three member functions, most probably
    you need six access functions (gets and sets)

34
How to Design an O.O. Application
  • Steps to design your class (continued)
  • Make sure, all your get functions are const
    function. For example
  • int get_Id () const
  • If your get function returns a pointer, it must
    be a pointer to a const. For example
  • const char get_name() const
  • Add other functions as needed (implementers).
  • Make sure all the member functions are public.
    Exceptionally some of the implementer functions,
    called HELP functions can be private

35
How to Design an O.O. Application
  • File Organization
  • Put all definitions such as
  • Class definition.
  • C/C struct definitions, if needed.
  • Manifest constants such as define
  • Function prototypes.
  • Etc.
  • In the header file.
  • Put all implementations of member functions in a
    .cpp file and include the header file in this
    file.
Write a Comment
User Comments (0)
About PowerShow.com