copyright © 2006 pearson addison-wesley. all rights reserved. 6-1 learning objectives classes …
DESCRIPTION
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-3 Class Definitions Similar to structures Example: class DayOfYear { public: int month; int day; public : void output(); };TRANSCRIPT
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1
Learning Objectives Classes Constructors Principles of OOP Class type member data
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2
Topic 1: Classes
Similar to structures Adds member FUNCTIONS Not just member data
Object-oriented programming Class: Abstract data type that contains data
and operations Object: Instances of a class
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-3
Class Definitions
Similar to structures Example:
class DayOfYear {
public:
int month;int day;
public :
void output(); };
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-4
Declaring Objects Declared same as all variables Example:
DayOfYear today, birthday; Declares two objects of class type DayOfYear
Objects include: Data (member data)
month, day
Operations (member functions) output()
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-5
Class Member Access
Members accessed same as structures Example:
today.monthtoday.day
And to access member function:today.output(); Invokes member function
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-6
Class Member Functions Must define or "implement" class member
functions Like other function definitions
Must specify class: return_type class_name::function_name(parameters)
Example: void DayOfYear::output()
{…} :: is scope resolution operator Instructs compiler "what class" member is from Item before :: called type qualifier
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-7
Dot and Scope Resolution Operator
Dot (.) operator: Specifies member of particular object
Scope resolution (::) operator: Specifies what class the function
definition comes from
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-8
Class Member Functions Definition
Function used for all objects of the class Will refer to "that object’s" data when invoked Example:
today.output(); Displays "today" object’s data
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-9
Topic 2: Constructors of Classes
Initialization of objects Initialize some or all member variables Other actions possible as well, but not
typicall
A special kind of member function Automatically called when object declared
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-10
Constructor Definitions
Constructors defined like any member function
Except:
1. Must have same name as class
2. Cannot return a value; not even void!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-11
Constructor Definition Example
Class definition with constructor:class DayOfYear{ public: int month; int day; public: DayOfYear(int m, int d);
//Constructor initializes month & day void output();};
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-12
Constructor Code
Constructor definition is like all other member functions:
DayOfYear::DayOfYear(int m, int d){
month = m;day = d;
}
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-13
Constructor Notes
Notice name of constructor: DayOfYear Same name as class itself!
Constructor declaration has no return-type Not even void!
Constructor in public section
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-14
Calling Constructors
Declare objects:DayOfYear date1(7, 4);
DayOfYear date2(5, 5);
Objects are created here Constructor is called Values in parenthes passed as arguments
to constructor
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-15
Constructor Equivalency
Consider: DayOfYear date1, date2
date1.DayOfYear(7, 4); // ILLEGAL!date2.DayOfYear(5, 5); // ILLEGAL!
Seemingly OK… CANNOT call constructors like other
member functions!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-16
Overloaded Constructors
Can overload constructors just like other functions
Recall: a signature consists of: Name of function Parameter list
Provide constructors for all possibleargument-lists
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-17
Constructor with No Arguments
Can be confusing Standard functions with no arguments:
Called with syntax: callMyFunction(); Including empty parentheses
Object declarations with no "initializers": DayOfYear date1; // This way! DayOfYear date(); // Not this way!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-18
Topic 3: Principles of OOP Information Hiding
Details of how operations work not known to "user" of class
Data Abstraction Details of how data is handled within
class not known to user
Encapsulation Bring together data and operations, but keep
"details" hidden
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-19
Public and Private Members
In C++, Principles of OOP are implemented through the qualifier public and private keywords
Private data in class allows manipulation only via member functions
Public items (usually member functions)can be accessed by other functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-20
Public and Private Example
This class definition satisfies principles of OOPclass DayOfYear {private:
int month;int day;
public:void input();void output();
}; Data now private Objects have no direct access to private data
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-21
Public and Private Example(continued)
DayOfYear d;
……..d.month =1; // No longer allowedd.input(); // This is allowed
How data members are handled is hidden
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-22
Topic 4: Class Type Member Variables Class member variables can be any type
Including objects of other classes!