Title: EEL 3801
1EEL 3801
- Part VI
- Fundamentals of C and C Programming
- Classes and Objects
2Object-Oriented Programming
- We think in terms of objects.
- Objects can be
- animate
- inanimate
- Objects have
- attributes (i.e., color, size, weight, etc.)
- behaviors (i.e., move, rotate, cry, laugh)
- communication with other objects
3C vs C
- C is a procedural language
- it revolves around functions
- functions create motion or action
- data exists mainly in support of actions
- C is an object-oriented language
- it revolves around classes of objects
- a class is based upon the C structure
4Classes
- Similar to structures except that
- have member functions
- are capable of inheriting from parent classes
- Data members are the data components.
- Member functions are the function components of a
class. - An instance of a class data type is an object.
5Review of Structures
- In C, structure data types can be created
directly through the struct tag. - Members can be accessed through the
- dot operator if directly referenced (a variable)
- arrow operator if referenced through a pointer.
- Can be dynamically allocated through the new
operator.
6Disadvantages of Structures
- External programs can directly access the
structure members. - Initialization not required or permitted
directly. - Cannot be printed as a unit.
- Cannot be compared in their entirety - must be
compared member by member. - No inheritance.
7Structure Example
- struct Time
- int hour
- int minute
- int second
-
- void printMilitary(const Time )
- void printStandard(const Time )
- main()
- Time dinnertime ...
- See page 598 of textbook.
8Classes
- Defined similarly to a structure, with the
exception that - Uses the class keyword rather than struct.
- Allows the inclusion of function prototypes
inside class definition for member functions. - Can specify the access of its members
- private only accessible through member functions
- public accessible to any part of the program
that has access to its object.
9Example of a Class
- class Time
- public
- void setTime(int, int, int)
- void printMilitary()
- void printStandard()
- private
- int hour
- int minute
- int second
10Classes
- Now the functions used to print the time are
included as member functions of the class data
type Time. - Note that the prototypes for the printing
functions have no need to have the object passed
to it, as they are part of it. - The values of hr, min and sec are set by a member
function, not by outsiders.
11Classes
- The data members are private, thus cannot be
changed by outside functions (for now). - This is called information hiding, and it is an
important aspect of software engrg. - Internal structure is of no concern to the class
user. - Internal modifications to the class are
transparent to user of the class.
12Member Functions
- Member functions can be defined
- inside the class body (rather than just the
prototype). - Typically done when function body is short
- outside the class body using the scope resolution
operator () - Done when definition is long and cumbersome
- preferable in most cases
13Types of Member Functions
- Access functions Used to read or display private
data for a class. Kept public. - Predicate functions Used to determine truth or
falsity of certain class data members or other
class states. For example, list is full. - Utility functions Used to carry our a task not
directly related to private data members. Can be
kept as private, as client has no need.
14Class Scope
- Variable and function names declared inside a
class definition belong to that class scope - So the operator must be used for defining
member functions if outside the class def. - This allows for other classes to have functions
of the same name. - Functions defined inside a class definition are
automatically inlined by the class.
15Class Scope
- Non-member functions have file scope.
- Within a classs scope, all members can be
referenced directly by name by the member
functions. - Outside a classs scope, data members (that are
public) are accessed only through a reference or
a pointer to an object of that class.
16Class Scope
- Member functions have function scope within their
class. - If member function defines a variable with same
name as a variable with class scope, the
class-scope variable is hidden by the
function-scope variable within the function
scope. - Such a hidden class scope variables can be
accessed with the scope resolution operator by
preceding the operator with the class name.
17Class Scope
- Hidden global variables can be likewise accessed
from inside a class where another variable has
the same name through the use of the unary scope
resolution operator (i.e., the class name is
omitted from before the operator) - dinnerTimehour
- vs
- hour
18Example
- include ltiostream.hgt
- class Count
- public
- int x
- void print() cout ltlt x ltlt \n
-
- main()
-
- Count counter
- Count ctrptr counter
- Count ctrref counter
19Example
- counter.x 7
- counter.print()
- ctrref.x 8
- ctrref.print()
- ctrptr-gtx 10
- ctrptr-gtprint()
20Example
- The results obtained after running that program
are - 7
- 8
- 10
- Keep in mind that in this case, the data member x
was made public. Thus, it was able to be
accessed directly from main() through . or -gt.
This is not typical.
21Interface
- Class definitions are typically placed in a
header (.h) file. - Place the definition of the member functions as a
source file (.cpp) to be compiled. - That way, the customer can be given the object
code for the .cpp file and not reveal the
proprietary source code.
22Controlling Access to Members
- An important part of C and OOP.
- Outside access to a classs data members and
member functions can be controlled - public
- private
- protected
- Default mode is private
23Controlling Access to Members
- Therefore, all members after class header and
before next label are private - After each label, the access mode defined by that
label applies until next label or . - Labels may be repeated, but that is rare.
24Controlling Access to Members
- private members - can only be accessed by other
members of the same class or those of a friend
class (later). - public members can be accessed by any function
in the program through the . or -gt operators.
Provide the services or public interfaces of that
class. - protected (later - Chapter 19 - Inherit.)
25Controlling Access to Members
- Private members of a class, as well as the
definitions of public member functions, are not
accessible to the clients of a class. - For the Time class defined previously
- main()
-
- Time t
- t.hour 7 // error - hour is private.
- t.printMilitary() // no error
26Good Practice
- A client may be a global function or another
classs member function. - Keep all data members of a class as private.
- Keep all access to private data members through
public member functions. - Access private members through set() or get()
member functions that are public. - Can serve to check data validity and hide data
forms from client.
27Controlling Access to Members
- Keep all public members together and first in the
class definition. - Keep all private members together and second in
the class definition - explicit. - C structures and unions have a public default
access. - For a structure, can be changed to private and
protected (?).
28Private Functions
- Not all member functions have to be public.
- Functions used as utility functions by other
member functions can be kept private.
29Constructor Functions
- Member functions that have the same name as the
class. - Invoked automatically when an instance of a class
is created (an object). - Data members cannot be initialized in the class
definition - it is only an abstract class! - Constructors can be used to initialize data
members of a class instance.
30Constructor Functions
- Cannot return anything.
- May be overloaded.
- Can have arguments passed to it. Such arguments
can be specified in the declaration of the
instance level object, to the right of the object
name and within parentheses, but before the
semicolon.
31Constructor Functions
- Can contain default arguments - values placed in
the prototype itself which sets the value of each
of the arguments if no other values are specified
when the instance level object is created. - If no constructor function is defined, compiler
creates a default constructor. - Default constructor does nothing useful.
32Example - Constructor Functions
- class Time
- public
- Time(int0, int0, int0)
- void setTime(int, int, int)
- void printMilitary()
- void printStandard()
- private
- int hour
- int minute
- int second
33Example - Constructor Functions
- TimeTime(int hr, int min, int sec)
-
- hour (hrgt0 hr lt24) ? hr 0
- minute(mingt0 minlt60) ? min 0
- second(secgt0 seclt60) ? sec 0
-
- The constructor above ensures validity of
initialized value. - Sets to 0 if hr, min or sec not specified,
34Example - Default Arguments
- main()
-
- Time t1, t2(2), t3(21,34)
- Time t4(12,25,42), t5(27,74,99)
- t1.printMilitary()
- t1.printStandard()
- t2.printMilitary()
- t2.printStandard()
35Example - Default Arguments
- t3.printMilitary()
- t3.printStandard()
- t4.printMilitary()
- t4.printStandard()
- t5.printMilitary()
- t5.printStandard()
-
36Example - Default Arguments
- For t1
- 000000
- 120000 midnight
- For t2
- 020000
- 20000 AM
- For t3
- 213400
- 93400 PM
37Example - Default Arguments
- For t4
- 122542
- 122542 PM
- For t5
- 000000
- 120000 midnight
- (values were illegal, so they were set to 0)
38Destructors
- Have same name as class but with a tilde in
front. - Invoked automatically when class instance leaves
scope (is deleted). - Does not per se delete the object, but performs
termination housekeeping. - Has no parameters and returns no value.
- Only one per class cannot be overloaded.
39Destructors
- Not used with simple classes.
- Used when dynamically allocated memory must be
deleted. - Called when their object leaves scope.
40Things to Not Do
- Do not design a public member function to return
a reference to a private data member. - This compromises the integrity of the data
member, since a reference is an alias, so that
anything we do to it we do to the original
variable. - See Textbook pages 626 to 629.
41Constant Objects and Functions
- Preceding the class object instantiation with the
operator const will make the object a constant
object. - This means that no member function will be
allowed to be called - rather harsh!! - But often, the programmer only means to disable
changing the values of the members, and not
prohibiting get access to data.
42Constant Objects and Functions
- To provide access, but not modification rights
for a particular object - the programmer may declare const member
functions. - These cannot modify the object.
- Only these can operate on const objects.
- But this can be tricky
43const Functions
- A member function is specified as const by doing
both - specifying it as const in its declaration in the
class, and - inserting the keyword const in its definition
after the functions parameter list and before
the left brace begins the functions body. - A const member function can be overloaded with a
non-const version
44const Functions
- Example
- class Whatever
- .
- .
- int getvalue() const
-
- int Whatevergetvalue() const return
private_data
45Initialization of const Objects
- Constructor functions in a const object must be
allowed to modify the data members, as they must
be somehow initialized. - Constructors need not be const.
- But since the values of a const object cannot be
modified by assignment - A member initializer must be used.
46Member Initializers
- Used with Constructor member function.
- Use the notation
- initialized_variable(init_val)
- This should be done after the parameter list but
before the function body opening brace of the
Constructor member function.
47Example of Initializer
- class Increment
- public
- Increment(int c0, int i1)
- void addinc() count increment
- void print() const
- private
- int count
- const int increment
- IncrementIncrement(int c, int i)
- increment(i)
- count c
48Potential Errors in const Objects
- Defining as const a member function that modifies
a data member of a non-const object. - Defining as const a member function that calls a
non-const member function. - Calling a non-const member function for a const
object. - Attempting to modify a const object
49Nested Classes
- Classes can be members of other classes.
- This is referred to as composition.
- Straight forward, except at initialization.
- When an object enters scope, its constructor is
immediately invoked to initialize it. - Member objects are constructed before their
enclosing objects are constructed.
50Example of Nested Classes
- The member objects parent class
- class Date
- public
- Date(int1, int1, int1900)
- void print() const
- private
- int month
- int day
- int year
- int checkDay(int)
51Example of Nested Classes
- The Constructor Function
- DateDate(int mn, int dy, int yr)
-
- month (mn gt 0 mn lt 12) ? mn 1
- year yr
- day checkDay(dy) // validates day
-
52Example of Nested Classes
- The enclosing class note the 6 int args
- class Employee
- public
- Employee(char , char , int, int, int, int,
int, int) - void print() const
- private
- char lastname25
- char firstname25
- Date birthdate
- Date hiredate
53Example of Nested Classes
- Now, for the Constructor function
- EmployeeEmployee(char fname, char lname, int
bmonth, int bday, int byear, int hmonth, int
hday, int hyear) - birthdate(bmonth, bday, byear),
- hiredate(hmonth, hday, hyear)
- strncpy(firstname, fname, 24)
- firstname24 \0
- strncpy(lastname, lname, 24)
- lastname24 \0
54Friend Functions and Classes
- A function that is not a member function of a
particular class, but which has access to private
and protected members of that class is called a
friend function. - A friend class is one all of whose functions have
access to another classs private (and protected)
members.
55Friend Functions and Classes
- Friendship is granted, not taken.
- The class giving friend privileges must declare
the friend class to be a friend - For class B to be a friend of class A, class A
must declare that class B is its friend. - Friendship is neither symmetric or transitive
56Friend Functions and Classes
- To declare such a friend function
- precede the function prototype in the friend
class definition with the keyword friend. - Define the function outside the friend class,
with no scope resolution operator. - To declare class A as a friend of class B, place
a declaration of the form friend class A as a
member of class B.
57Friend Functions and Classes
- The labels public, private and protected do
not affect the friend declaration. - Therefore, it does not matter where the friend
declaration is placed.
58Example of Friends
- class Count
- friend void setx(Count , int)
- public
- Count() x 0
- void print() const ..
- private
- int x
-
- void setx(Count c, int val)
-
- c.x val // no need for set() func
-
59Friend Functions and Classes
- Note that in the previous example, the object
instance of the friendly class is passed to the
friend function. - A function can be a friend of more than one
class, but a member of at most one class. - Friend classes are much less common than friend
functions
60Friend Classes
- class Parttime
- class Employee
- public
- friend class Parttime
- private
- char name30
- int x
-
- The friend class can be put anywhere.
61The This Pointer
- Every object has a pointer, called the this
pointer, that points to itself. - It is needed because only one copy of an classs
member function exist. No copies are made for
each object instantiated. - Data members, however, are cheaper, so each
object has their own data members.
62Static Data Members
- Allows one data member of one class to be shared
by all object instances of that class. - Seem like global variables but have class scope.
- Can be public, private or protected.
- See page 661-665 of Deitel Deitel for details.