Introduction to C Programming Module 2 Classes - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Introduction to C Programming Module 2 Classes

Description:

Module 1 - An Overview of C and OO Programming. Module 2 ... The copy assignment is called when an object is assigned with anther object of the same class. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 34
Provided by: yaodo
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming Module 2 Classes


1
Introduction to C ProgrammingModule 2 Classes
Operator Overloading
  • Yaodong Bi, Ph.D.
  • Department of Computing Sciences
  • University of Scranton
  • (717)941-6108
  • bi_at_cs.uofs.edu

2
Outline
  • Module 1 - An Overview of C and OO Programming
  • Module 2 - Classes and Operator Overloading
  • Module 3 - Inheritance and Dynamic Binding
  • Module 4 - Function and Class Templates
  • Module 5 - C Stream I/O and Exception Handling

3
Review of Module 1
  • C single-line comments //comments
  • C stream I/O cin gtgt, cout ltlt
  • Declarations of variables declare in the
    smallest scope
  • Inline functions inline int square(int x)
    return xx
  • Reference parameters int age 30 int rAge
    age
  • The const qualifier const double PI 3.1415
  • The new and delete operators int ptr new
    int(30) delete ptr
  • Default arguments void SetAge(int age 30)
  • Scope operator cout ltlt value //global var.
    value
  • Function overloading int square(int)
    double square(double)

4
Module 2 Classes Operator Overloading
  • Class data members and member functions
  • Constructors destructors
  • Copy constructors and copy assignments
  • Constant objects and constant member functions
  • The this pointer
  • Static members and member functions
  • Class objects as members of another class
  • Operator overloading
  • Friend functions and classes
  • Conversion operators

5
Example 1 The Stack Class of Module 1
  • //Class Specification
  • // Stack.h
  • const int maxSize 10
  • class CStack
  • public // public interface
  • CStack(int sz maxSize) // constructor
  • void Push (int item)
  • int Pop()
  • bool IsEmpty() const
  • bool IsFull() const
  • CStack() // Destructor
  • private // private implementation
  • int m_Array
  • int m_nTop
  • int m_nSize
  • //Class Implmentation
  • // stack.cpp
  • include ltiostream.hgt
  • include "Stack.h"
  • CStack // class scope operator
  • CStack(int sz)
  • m_Array new intsz
  • m_nSize sz
  • m_nTop -1
  • CStackCStack()
  • delete m_Array

6
Example 1 Stack - contd
  • // stack.cpp - continued
  • void CStackPush(int item)
  • m_Arraym_nTop item
  • int CStackPop()
  • return m_Arraym_nTop--
  • bool CStackIsEmpty() const
  • return (m_nTop lt 0)
  • bool CStackIsFull() const
  • return (m_nTop m_nSize - 1)
  • // class.cpp
  • include ltiostream.hgt
  • include "Stack.h"
  • void main()
  • CStack stack
  • for ( int i 1 i lt maxSize3 i)
  • if (!stack.IsFull()) stack.Push(i100)
  • while (!stack.IsEmpty())
  • cout ltlt stack.Pop() ltlt endl

7
Class members and member functions
  • Access Specifiers
  • public Any member or member function after
    public and before the next specifier is
    accessible to public.
  • void main() CStack stack ... stack.Push(item)
    ...
  • Private Any member or member function after
    private and before the next specifier is
    accessible only to the member functions of the
    class.
  • void main() CStack stack ...
  • stack.m_nSize 10 // illegal - private
    member
  • cout ltlt stack.m_nSize // illegal - private
    member
  • ...
  • Access specifiers may appear multiple times in a
    class definition although generally not
    recommended.
  • class CStack public void Push(int) int Pop()
  • private char m_Array int m_nSize
  • public CStack()

8
Class members and member functions
  • In-Class function definition
  • Member functions may be defined in the class
    definition.
  • Defining member functions outside the class
    definition separates the interface from
    implementation
  • It should only be used for small, frequently used
    functions.
  • Put class definition into a .h file and member
    function definitions in a .cpp file.
  • When a member function is defined outside the
    class definition, the function name must be
    preceded by the class name
  • int CStackPush(int item)
  • Common Programming Error Forgetting the
    semicolon at the end of the class definition
  • class CStack

9
Example 2 A Date Class
  • // date.cpp
  • include ltiostream.hgt
  • include "date.h"
  • CDateCDate(int dd, int mn, int yy)
  • m_day dd
  • m_month mn
  • m_year yy
  • void CDatePrint() const
  • cout ltlt m_day ltlt endl
  • cout ltlt m_month ltlt endl
  • cout ltlt m_year ltlt endl
  • void CDateAddYear(int n)/.../
  • void CDateAddMonth(int n) /.../
  • void CDateAddDay(int n) /.../
  • // date.h
  • class CDate
  • public
  • CDate(int dd20, int mn8,
  • int yy1998)
  • void AddYear(int n)
  • void AddMonth(int n)
  • void AddDay(int n)
  • void Print() const
  • private
  • int m_day
  • int m_month
  • int m_year

10
Class Constructors and Destructors
  • Constructors
  • Initialize the objects of the class type -- Dont
    initialize members in class definition.
  • Constructors are member functions with the same
    name of the class. Constructors cannot specify
    return types nor return values.
  • Constructors obey the same overloading rules as
    do other functions
  • class CStack CStack(int size) // constructor
    with one argument
  • Cstack() // constructor without
    arguments
  • Constructor may take default argument.
  • class CStack CStack(int size 10)
  • Default constructor a constructor that can be
    called without supplying an argument
  • class CStack CStack(int size 10) //
    default constructor
  • class CStack CStack() // default
    constructor
  • // dont use both in the same class -- ambiguous

11
Class Constructors and Destructors
  • When no user-defined constructor is found, the
    compiler would generate one as needed. Classes
    with consts or references cannot be constructed
    by a compiler-generated constructor.
  • Accessing data members of a class in its
    constructors may cause error because the members
    may not have been properly initialized
  • Destructors
  • Clean up and release resources
  • They are called when an object of the type goes
    out of scope, or when an object is deleted.
  • class CStack Cstack() // Destructor
  • Common use is to release memory acquired in
    constructors.
  • CStackCStack(int size) m_Array new
    charsize ..
  • CStackCStack() delete m_Array

12
Class Constructors and Destructors
  • When destructor needs defined?
  • In the date class, the constructor does not
    acquire any system resources including memory.
    Thus, when a date object goes out of scope, the
    memory occupied by the data members is collected
    automatically by the system.
  • For the date class , no destructor needs to be
    explicitly defined
  • In contrast, the stack classs constructor
    acquires memory from the free store. When a stack
    object goes out of scope, the memory occupied by
    the data members are collected. For m_Array, the
    pointer itself is removed, but the memory of the
    string pointed by m_Array will not collected
    automatically. Thus, the memory acquired in the
    constructor must be freed explicitly before the
    stack object is destroyed.
  • Destructors should be defined, when system
    resources that are generally acquired in the
    constructors must be manually released.
  • For the stack class, a destructor must be defined
    to release the memory acquired in the constructor.

13
Const objects and const member functions
  • Constant objects
  • Use const to specify that an object is not
    modifiable.
  • Ex const CDate date
  • date.AddDay(7) // illegal
  • const member functions.
  • A const function is declared with const and it
    does not modify the object.
  • Ex CStackIsEmpty() const and CDatePrint()
    const
  • Only const member functions can acess a const
    object.
  • Ex const CDate date
  • date.AddDay(7) // illegal
  • date.Print() // okay
  • const must be used in both the declaration and
    definition of the member function
  • A const member function can be overloaded with a
    non-const version. Good Design??

14
Copy constructor and copy assignment
  • Memberwise copy
  • When an object is assigned to another object of
    the same type, by default, memberwise copy is
    performed.
  • Ex In the date class date1 date2 implies
  • date1.m_day date2.m_day
  • date1.m_month date2.m_month
  • date1.m_year date2.m_year
  • Memberwise copy may not be proper for some
    classes, such as the stack class.
  • By memberwise copy, stack1 stack2 would
    perform
  • stack1.m_Array stack2.m_Array // only the
    pointer is copied
  • stack1.m_nSize stack2.m_nSize
  • stack1.m_nTop stack2.m_nTop
  • The problem is that both stacks now refer to the
    same array
  • Two cases
  • Copy initialization, ex., CStack stack1 stack2
    and void foo(CStack)
  • Copy assignment, ex., CStack stack1 stack1
    stack2

15
Copy constructor and copy assignment
  • Copy constructors
  • The copy constructor is called when an object is
    initialized with anther object of the same class.
  • Ex CStack stack1 stack2 // stack1 is defined
    and initialized
  • void foo(CStack stack) // stack will
    initialized with the passed.
  • Copy constructor definition ClassName(const
    ClassName)
  • class CStack ...
  • CStack(const CStack) // copy constructor
    declaration
  • CStackCStack(const CStack stack) // copy
    constructor definition
  • m_nSize stack.m_nSize
  • m_nTop stack.m_nTop
  • m_Array new intm_nSize
  • for (int i0 iltm_nTop i) m_Arrayi
    stack.m_Arrayi

16
Copy constructor and copy assignment
  • Copy Assignments
  • The copy assignment is called when an object is
    assigned with anther object of the same class.
  • Ex CStack stack1 ...
  • stack1 stack2 // stack1 is assigned with
    stack2
  • Copy assignment definition
  • General form ClassName operator(const
    ClassName)
  • The stack class
  • class CStack ...
  • CStack operator(const CStack)
  • // copy assignment declaration
  • CStack CStackoperator(const CStack stack)
  • // copy assignment definition
  • delete m_Array
  • m_nSize stack.m_nSize m_nTop
    stack.m_top
  • m_Array new intm_nSize
  • for (int i0 iltm_nTop i) m_Arrayi
    stack.m_Arrayi
  • return ????

17
The this pointer
  • In each member function, there is a pointer to
    the object for which the function was invoked --
    this.
  • In a non-const member function the type of this
    is X const in a const member function its type
    is const X const.
  • It can be used explicitly to reference the
    members of the object.
  • bool CStackIsEmpty() return this-gtm_nTop lt
    0
  • The stack class -- what if it is a
    self-assignment
  • CStack CStackoperator(const CStack stack)
  • if (stack ! this)
  • delete m_Array
  • m_nSize stack.m_nSize m_nTop
    stack.m_nTop
  • m_Array new intm_nSize
  • for (int i0 iltm_nTop i)
    m_Arrayi stack.m_Arrayi
  • return this

18
Example 3 Modified Date Class
  • // data.cpp
  • include ltiostream.hgt
  • include "date.h"
  • void CDateSetDefault(int dd, int mn, int yy)
  • default_date CDate(dd, mn, yy)
  • CDateCDate(int dd, int mn, int yy)
  • m_day dd? dd default_date.m_day
  • m_month mn ? mn default_date.m_month
  • m_year yy ? yy default_date.m_year
  • // date.h
  • class CDate
  • public
  • static void SetDefault(int, int, int)
  • CDate(int dd0, int mn0, int yy0)
  • void Print() const
  • CDate()
  • private
  • int m_day
  • int m_month
  • int m_year
  • static CDate default_date

19
Example 3 Date Class -- contd
  • // examp3.cpp
  • // main program
  • include ltiostream.hgt
  • include "date.h"
  • void main()
  • CDate initial
  • CDate userSpecified(1, 1, 2000)
  • CDateSetDefault(20, 8, 1998)
  • CDate newDefault
  • cout ltlt "Default date\n"
  • initial.Print()
  • cout ltlt "User Specified Date\n"
  • userSpecified.Print()
  • cout ltlt "New Default Date\n"
  • newDefault.Print()
  • // data.cpp -- contd
  • CDateCDate()
  • void CDatePrint() const
  • cout ltlt m_day ltlt endl
  • cout ltlt m_month ltlt endl
  • cout ltlt m_year ltlt endl
  • // The static member is initialized here
  • CDate CDatedefault_date(19, 12, 1961)

20
Static Members and Member Functions
  • Static member a variable that is part of a
    class, but is not part of an object of the class.
    Only one copy for all the objects of the class.
  • static CDate default_date
  • Static member function a function that needs to
    access to members of the class, yet doesnt need
    to be invoked for individual object.
  • static void SetDefault(int, int, int)
  • Static members must be initialized at file scope.
  • CDate CDatedefault_date(19, 12, 1961) // in
    date.cpp, not in main()
  • Public static members (and functions) can be
    accessed via an object of the class or qualified
    with the class name.
  • class CDate public static CDate
    default_date
  • ptr-gtdefault_date userSpecified // okay
  • CDatedefault_date userSpecified // okay-
    preferred
  • initial.SetDefault(20, 8, 1998) // okay
  • CDate ptr ptr-gtSetDefault(20, 8, 1998) //
    okay
  • Static members exist even when no objects of the
    class exist.

21
Class Objects as Members of Another Class
  • Class Date is embedded in CPerson.
  • Class string is in CPerson also.
  • Constructor with member initialization.
  • CPersonCPerson(string nm, CDate date)
  • m_name(nm), DOB(date)
  • // m_name and DOB are initialized with their
  • // copy constructors
  • Constructor with no initialization
  • CPersonCPerson(string nm, CDate date)
  • m_namenm
  • DOB date
  • // m_name and DOB are first initialized with
  • // their default constructors( user defined or
  • // compiler generated) , then m_name and
  • // DOB are set with their copy assignments or
  • // memberwise copy..
  • Member initialization is preferred.
  • When a class has consts or references, they can
    only be member-initialized

class CDate class CPerson public
CPerson(const string name, CDate date)
CPerson() string GetName() const int
GetAge() const void SetName(string)
void SetDate(int, int, int) virtual void
Print() const private string m_name
CDate DOB
22
Example 4 A String Class
  • // string.h
  • class CString
  • public
  • CString(char str 0)
  • CString(const CString str)
  • CString operator(const CString str)
  • CString operator(const CString str)
  • int GetSize() const
  • void Print() const
  • CString() delete m_str
  • private
  • char m_str
  • int m_size
  • // string.cpp
  • include ltcstringgt
  • include ltiostreamgt
  • include "string.h"
  • using namespace std
  • CStringCString(char str)
  • if (str ! 0)
  • m_size strlen(str)
  • m_str new charm_size1
  • for (int i 0 i lt m_size1 i)
  • m_stri stri
  • else
  • m_str 0

23
Example 4 String -- contd
  • // string.cpp -- contd
  • CStringCString(const CString str)
  • m_size str.GetSize()
  • m_str new charm_size1
  • for (int i 0 i lt m_size1 i) m_stri
    str.m_stri
  • CString
  • CStringoperator (const CString str)
  • if (str ! this )
  • m_size str.m_size
  • delete m_str
  • m_str new charm_size1
  • for (int i 0 i lt m_size1 i) m_stri
    str.m_stri
  • return this
  • // string.cpp -- contd
  • CString CStringoperator(const CString str)
  • CString NewStr
  • NewStr.m_size str.m_size m_size
  • NewStr.m_str
  • new charNewStr.m_size1
  • strcpy(NewStr.m_str, m_str)
  • strcat(NewStr.m_str, str.m_str)
  • return NewStr
  • int CStringGetSize() const
  • return m_size
  • void CStringPrint() const

24
Example 4 String -- contd
  • // main.cpp
  • include ltiostream.hgt
  • include "string.h"
  • void main()
  • CString str1("Hello, ")
  • str1.Print()
  • cout ltlt endl
  • CString str2("world!\n")
  • str2.Print()
  • CString str3 str1str2
  • str3.Print()
  • str3 str1"attached\n"
  • str3.Print()
  • Overload operator
  • CString CStringoperator(const CString str)
  • ...
  • Overload operator
  • CString CStringoperator (const CString str)
  • ...
  • CString str3 str1str2 in main()
  • operator() is called first for str1str2, the
    result is held in a temporary location. Then, the
    copy constructor is called to initialize str3.
  • str3 str1attached\n in main()
  • The default constructor is called to convert
    attached\n to string tmp, then, operator() is
    called for str1tmp. Lastly, the copy constructor
    is called to initialize str3, and tmp is deleted.

25
Operator Overloading
  • Operator Functions as Class Members
  • 40 operators can be overloaded. See Fig. 18.1 in
    the text.
  • The precedence of an operator cannot be
    overloaded.
  • The of operands of an operator cannot be
    changed.
  • Default arguments cannot be used to overload an
    operator.
  • Treat operator together as a function name
  • str3 str1"attached\n" is equivalent to
  • str3 str1.operatpr(attached\n)
  • Overload operators to perform the same or similar
    functions on class objects as the operators
    perform on objects of built-in types
  • Nonmember Operator Functions
  • ostream operatorltlt(ostream output, const
    CString string)
  • cout ltlt string.m_str ltlt endl // is this
    okay????
  • return output
  • CString operator(const char cstr, const
    CString str)
  • return CString(cstr) str
  • CString str1, str2 ... str2 string
    str1 cout ltltstr1 ltltstr2

26
Friend Functions and Classes
  • Friend function a nonmember function that has
    been given by the class the right to access
    private (and protected) members of the class.
  • class CStringfriend ostream operatorltlt(ostream
    , const CString)
  • ... // declare a friend
  • ostream operatorltlt(ostream output, const
    CString string) // define the friend
  • cout ltlt string.m_str ltlt endl // if not a
    friend, this would be illegal
  • return output
  • CString string .... cout ltlt string // use the
    friend function
  • Friends can be declared anywhere in the class
    definition.
  • Friend class a class that has been given by the
    class the right to access private (and protected)
    members of the class
  • class CString
  • friend class CStringStack // Declare
    CStringStack as a friend
  • ....
  • class CStringStack
  • ... void Print() ... // print
    all the strings in stack
  • private CString m_Array // an array of
    CString
  • ....

27
Friend Classes -- contd
  • // CStringStack.cpp
  • ......
  • void CStringStackPrint()
  • for (int i0 iltm_nTop i)
  • // Access private member m_str of class CString
  • cout ltlt m_Arrayi.m_str ltlt endl
  • // main.cpp
  • void main()
  • ......
  • CStringStack stack
  • stack.Push(str1)
  • stack.Push(str2)
  • .....
  • stack.Print() // print all the strings in the
    stack

28
Conversion Operators
  • A class can define how it can be converted to a
    prefined or a user defined data type.
  • Syntax -- convert X to T
  • operator T() const // declaration
  • Xoperator T() const .... //definition
  • // no returned values and no arguments
  • Examples
  • class CString public ... operator char()
    const // declaration
  • operator UserDefinedTypes() const //
    declaration
  • ...
  • CStringoperator char() const return m_str
    //definition
  • CStringoperator UserDefinedTypes() const ...
    // definition
  • CString string(Hello)
  • char ptr string // conversion operator
    char() called here
  • cout ltlt ptr
  • ptr 0

29
Advice
  • Make a function a member only if it accesses the
    representation of the class
  • If a constructor acquires resources, define a
    destructor to release them.
  • If a class has a pointer member, it is likely you
    need copy operations (constructor and
    assignment).
  • Check for self-assignment in copy assignments
  • Dont return a private member (or its address) to
    a non-const reference (or a pointer).
  • Use member initialization to initialize const and
    reference members.
  • Pass big objects as const X to functions.
  • Declare as const all member functions that do not
    change the object.
  • Dont forget the semicolon at the end of a class
    definition
  • Declare classes in the order of friends, public,
    protected, and private.
  • Use member initialization to initialize embedded
    objects

30
Summary of Module 2
  • Class data members and member functions
  • Constructors destructors
  • Copy constructors and copy assignments
  • Constant objects and constant member functions
  • The this pointer
  • Static members and member functions
  • Class objects as members of another class
  • Operator overloading
  • Friend functions and classes
  • Conversion operators

31
Programming Assignments
  • Design a time class.
  • Similar to the date class, it contains hour,
    minute, and second.
  • Use a static member for class-wide default time.
  • Code a main() to test it.
  • Improve Example 3 Modified Date Class
  • Overload I/O operators ltlt and gtgt as nonmember
    functions
  • ostream operatorltlt(ostream output, const CDate
    date)
  • istream operatorgtgt(istream input, CDate date)
  • see section 18.5 (pp. 685-687) of the text for
    an example
  • Modify the main() function to test your
    operators.
  • Design an integer array class - Incremental
    development
  • Start with a simple array class with basic
    operations.
  • Add features like overloading operators , ,
    !, ltlt, gtgt etc. and range checking.
  • Having trouble? See 18.8 (pp. 689-698) for a
    complete example.
  • H. Deitel and P. Deitel, C How to Program, 2nd
    Ed., Prentice Hall, 1994.

32
Additional Programming Assignments
  • 1. For the CString class, overload the equality
    operator "" as a member function of CString. It
    returns true when the two CString objects (the
    two operands) are the same(same length and same
    string content), return false otherwise. When the
    two operands (the two CString objects) are the
    same object (hint the this pointer), return
    true.
  • 2. Modify the conversion operator
    CStringoperator char() const. Instead of
    returning the address of the existing string of
    characters, it dynamically allocates a new
    character array and copies the existing string
    into the new string and then returns the address
    of the new string. Thus, the char pointer would
    not point to the private member (m_str) of
    CString
  • 3. Add another static member to CDate. The new
    static member keeps track of the number of CDate
    objects that exist in the system. Hint
    Initialize the counter to zero. Increment it by
    one in every constructor and decrement it by one
    in the destructor.
  • 4. In the section of friend classes, we used a
    stack (CStringStack) of CString objects as an
    example and declared CStringStack as a friend
    class of CString. complete the design and coding
    of CStringStack by coding a default construnctor,
    a destructor, the Push, Pop, IsEmpty, and IsFull
    functions. When the copy constructor/ assignmeng
    of CString would be called? If the CString class
    did not define the copy constructor/assignment,
    would your program run correctly?
  • 5. Continue assignment 4. Design a copy
    constructor and a copy assignment for
    CStringStack. Make sure check for self-assignment
    in the copy assignment operation.

33
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com