Basic C Programming - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Basic C Programming

Description:

may have associated functions and operators ... wallet[0] = new CreditCard('5391 0375 9387 5309', 'John Bowman', 2500); for(int j=1; j =16; j ) ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 21
Provided by: robert1778
Category:

less

Transcript and Presenter's Notes

Title: Basic C Programming


1
Basic C Programming
Review C Programming Elements, Expressions,
Control Flow, Functions, Classes
2
Classes ( 1.5)
  • The concept of a class is fundamental to C
  • provides means to define new user-defined types
  • may have associated functions and operators
  • fundamental to programming using the
    object-oriented approach
  • often used to implement data structures (a
    systematic way of organizing and accessing data)

3
Class Structure ( 1.5.1)
  • A class consists of members
  • properties
  • data members
  • constants or variables
  • may be of any type, even classes
  • methods
  • member functions
  • typically act on the properties, so define the
    behavior of the class

4
Access Control ( 1.5.1)
  • Members may be declared either public, private,
    protected, or friend (using access specifiers)
  • public
  • accessible from within and outside the class (by
    client code)
  • the public interface of the class object
  • private
  • accessible only from within the class
  • usually properties are made private, as they deal
    with how the data structure is implemented (users
    dont need to know)

5
Class Syntax ( 1.5.1)
  • class class_name
  • private
  • //private members
  • public
  • //public members
  • Note No required order between public and
    private sections. Usually public section first,
    since of most interest to users. However, our
    authors focus on the structures, so usually list
    the private section first!
  • Default access is private for classes and public
    for structures.

6
Class Example ( 1.6.1)
  • See Code Fragment 1.5, page 48
  • class CreditCard
  • private
  • string number //cc number
  • string name //cc owner's name
  • int limit //credit limit
  • double balance //cc balance
  • public
  • //constructor
  • CreditCard(string no, string nm, int lim,
    double bal0)
  • //accessor functions
  • string getNumber() const return number
  • string getName() const return name
  • double getBalance() const return balance
  • int getLimit() const return limit
  • //mutator functions
  • bool chargeIt(double price)

7
Member Functions ( 1.5.1 1.6.1)
  • class CreditCard
  • private
  • //..
  • public
  • //constructor
  • CreditCard(string no, string nm, int lim,
    double bal0)
  • //..
  • Constructor
  • initializes the class properties
  • has the same name as the class itself
  • does not have a return type

8
Member Functions ( 1.5.1 1.6.1)
  • Other member functions are categorized as
  • accessor functionsonly read class data
  • class CreditCard
  • private
  • //..
  • public
  • //..
  • //accessor functions
  • string getNumber() const return number
  • string getName() const return name
  • //..
  • Note the const declaration indicates
    getNumber(), etc. are all accessor
    functionsinforms user that function will not
    modify the object properties

9
Member Functions ( 1.5.1 1.6.1)
  • Other member functions are categorized as
  • accessor functionsonly read class data
  • mutator/update functionsmay alter class data
  • class CreditCard
  • private
  • //..
  • public
  • //..
  • //mutator functions
  • bool chargeIt(double price)
  • void makePayment(double payment)
  • balance - payment

10
Member Functions ( 1.5.1 1.6.1)
  • May either be defined within the class body or
    outside.
  • class CreditCard
  • private
  • //..
  • public
  • //..
  • //mutator functions
  • //defined outside
  • bool chargeIt(double price)
  • //defined here
  • void makePayment(double payment)
  • balance - payment

11
Defined Within ( 1.5.1 1.6.1)
  • We give a hint to the compiler to expand this
    function in-linesimply expand the contents of
    the function body each time it is encountered,
    rather than using the usual call-return method.
  • //defined here
  • void makePayment(double payment)
  • balance - payment
  • Thus, in-line functions can be executed more
    efficiently. Use for frequently-used, short
    member functions.

12
Defined Outside ( 1.5.1 1.6.1)
  • Required to specify both the name of the class as
    well as the function name (so compiler knows
    which class is involved). See Code Fragment 1.6,
    page 49.
  • bool CreditCardchargeIt(double price)
  • //make a charge
  • if(price balance gt double(limit)) return
    false
  • balance price
  • return true

13
Using Member Functions ( 1.5.1 1.6.1)
  • Note that member names, like chargeIt(), are used
    without reference to a particular objectthese
    functions will be invoked on a particular object.
    See Code Fragment 1.7, page 50.
  • vectorltCreditCardgt wallet(10)//vector of 10
    CreditCard //pointers
  • //allocate a new card
  • wallet0 new CreditCard("5391 0375 9387
    5309", "John Bowman", 2500)
  • //..
  • for(int j1 jlt16 j)
  • //make some charges
  • wallet0-gtchargeIt(double(j))
  • //..
  • Since wallet0 is a pointer to a CreditCard, use
    -gt to invoke the member function chargeIt()
    (otherwise if not a pointer type use . ).

14
Constructors Destructors ( 1.5.2 1.6.1)
  • Constructors
  • special member functions
  • initialize the class properties
  • have the same name as the class itself
  • do not have a return type
  • Destructors
  • called when a class object goes out of existence
  • if class name is T, denoted T()

15
Constructors ( 1.5.2 1.6.1)
  • Special constructors are
  • default constructors
  • no arguments
  • copy constructors
  • Note our example
  • class CreditCard
  • public
  • //constructor
  • CreditCard(string no, string nm, int lim, double
    bal0)
  • contains a default argument, bal. If a
    CreditCard object is instantiated with only two
    arguments, then bal will be set to 0.
  • Default arguments may be used with any function
    (not just constructors.)

16
Destructors ( 1.5.2)
  • a member function automatically called when a
    class object ceases to exist
  • if class name is T, denoted T()
  • not required to supply our own
  • if our class allocates memory via new, we should
    write a destructor to free its memory via delete
    (or introduce memory leaks)

17
Classes Memory Allocation ( 1.5.3)
  • Every class that allocates its own objects using
    new should
  • Define a destructor to free allocated objects
  • Define a copy constructor, which allocates its
    own new member storage and copies the contents of
    member variables
  • Define an assignment operator, which deallocates
    old storage, allocates new storage, and copies
    all member variables
  • Read this section for more details.

18
Class Friends and Class Members( 1.5.4)
  • Well come back to this.

19
The Standard Template Library ( 1.5.5)
  • The STL is a collection of useful classes for
    common data structures/standard containers
  • string
  • stack
  • queue
  • deque
  • vector
  • list
  • priority-queue
  • set
  • map

20
The Standard Template Library ( 1.5.5)
  • Each object can store objects of any type.
  • Such a class whose definition depends on a
    user-defined type is called a template.
  • Specify the type of object being stored in angled
    brackets lt..gt
  • vectorltintgt scores(100)//100 integer scores
  • vectorltCreditCardgt wallet(10)//vector of 10
    CreditCard //pointers
  • Read this section for further details
Write a Comment
User Comments (0)
About PowerShow.com