C Overview, Part II - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

C Overview, Part II

Description:

The specification of a data type's logical properties (data values. and operations) versus the details of ... diskette in your text. Write a main function that: ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 13
Provided by: SusanMi6
Category:

less

Transcript and Presenter's Notes

Title: C Overview, Part II


1
C Overview, Part II
Reference Chapter 2, Sections 2.1.1-2.1.5
CMSC 202

1
2
Terminology
  • Abstraction
  • The specification of a data types
    logical properties (data values
  • and operations) versus the details of
    its implementation
  • Abstract Data Type (ADT)
  • The specification of a data types data
    values, operations, and
  • the rules governing these operations
  • Encapsulation
  • Hiding a data types implementation in a
    separate block with a
  • formally specified interface
  • What tool do we have in C to implement an ADT
    and
  • promote encapsulation? Answer Classes

CMSC 202

2
3
C Classes
  • A construct that supports data abstraction and
    encapsulation
  • A class defines a new data type
  • It encapsulates the data types data and
    operations
  • General format
  • class Name
  • class body
  • Dont forget the closing semicolon!
  • Notice that a class looks very much like a
    struct -- but there
  • is a difference (next slide)

CMSC 202

3
4
Class Example (Account.h)
  • class Account
  • public
  • Account(unsigned n, double b) //
    constructor
  • void deposit(double amt) //
    deposit amt into this account
  • int withdraw(double amt) //
    withdraw amt from this account
  • double balance()
    // balance inquiry
  • private
  • unsigned acct_no
    // account number
  • double acct_bal
    // current balance
  • Account is now a data type
  • Operations and data (methods and attributes)
    are
  • encapsulated
  • Public and private members
  • All class members are private by default
  • With a struct, all members are public by
    default

CMSC 202

4
5
Objects
  • Making a new object is essentially the
    declaration of
  • a new variable
  • Account sally(5551234, 600.00) //
    object sally
  • Account bob(5556789, 540.00) //
    object bob
  • Declaring an object is called instantiation
  • Therefore, an object is an instance of a class
  • Space is allocated for an objects attributes
  • sally

acct_no acct_bal
5551234
600.00
CMSC 202

5
6
Accessing an Objects Members
  • General syntax
  • object.member
  • Can access public members only
  • sally.acct_bal 150.00 // illegal
    (private attribute)
  • sally.deposit(150.00) //
    deposit via member function
  • bob.acct_bal - 25.00 // illegal
    (private attribute)
  • bob.withdraw(25.00) //
    withdrawal via member function
  • balance sally.acct_bal // illegal
    (private attribute)
  • balance sally.balance() // using
    member function

CMSC 202

6
7
Pointers to Objects
  • Pointers to objects are useful
  • Account bob(5556789, 540.00) //
    object bob
  • Account acct_ptr bob //
    pointer to object bob
  • Accessing members
  • (pointer).member OR
    pointer-gtmember
  • (acct_ptr).deposit(150.00)
  • OR
  • acct_ptr-gtdeposit(150.))

CMSC 202

7
8
Class Member Functions
  • Code for member functions is not included in
    the class
  • definition (except for inline functions)
  • Place the class definition (specification) in
    a file called
  • class.h (example Account.h)
  • The the code for the member functions in a
    file called
  • class.C (example Account.C)

CMSC 202

8
9
Class Member Functions (continued)
  • Syntax for a member function
  • type classfunction-name(parameters)
  • function body
  • is the scope resolution operator

CMSC 202

9
10
Class Member Functions (continued)
  • Example
  • // File Account.C
  • include Account.h
  • void Accountdeposit(double amt)
  • if (amt gt 0)
  • acct_bal amt
  • int Accountwithdraw(double amt)
  • if (amt gt acct_bal)
  • acct_bal - amt
  • return(acct_bal)

CMSC 202

10
11
Constructors
  • Are used to initialize the attributes of an
    object upon
  • instantiation
  • Example
  • AccountAccount(unsigned id, double amt)
  • acct_no id
  • if (amt gt 0) acct_bal amt
  • Has same name as its class
  • Has no return type

CMSC 202

11
12
Student Exercise
  • Copy the code for Account.h and Account.C from
    the
  • diskette in your text.
  • Write a main function that
  • - Instantiates an Account object with an
    id number and
  • a beginning balance
  • - Checks the balance
  • - Makes a deposit of 50.75
  • - Checks the balance again
  • - Makes a withdrawal of 25.19
  • - Checks the balance again
  • - And anything else that you would like to try!

CMSC 202

12
Write a Comment
User Comments (0)
About PowerShow.com