classes 10.2. definition a class is a data type whose variables are objects object – a variable...
TRANSCRIPT
Classes
10.2
Definition
• A class is a data type whose variables are objects
• Object – a variable that has member functions as well the ability to hold data values
• You can build a library of your own class type definitions and use your types as if they were predefined types
Example 1
• DayOfYear – purpose to store holidays, birthdays, or other important annual events– month– day– output();
• Convention: list member functions before variables• Member functions for a class are called using the dot
operator• Encapsulation: combining a number of items, such as
variables and functions, into a single package, such as an object of some class
• When a member function is defined, it must include the class name;
ie void DayOfYear::output()• :: is called the scope resolution operator; similar to the
dot operator – used to tell what a member function is a member of– Dot operator used with an object
• ie birthday.output() – birthday is an OBJECT
– Scope resolution operator (SRO) used with a class• ie DayOfYear::output() – DayOfYear is a CLASS• The class used with the SRO is often called a type qualifier since it
“qualifies” the function to one particular type
Member Functions
• In a member function, the names of member variables are used without applying them to a single object, because they are to apply to any object of the class that calls the function
public and private members
• Class data should only be accessible through member functions
• Let’s change DateOfYear to follow this guideline– Write ACCESSOR functions for each piece of data– Write a MUTATOR function to set the data– Also write a member function called input()– Write a member function checkDate() that checks
for valid data values (month between 1 and 12, day between 1 and 31)
public and private members
• All members listed after the private: keyword are considered private members– Can’t be accessed outside of class members– Can be accessed inside class member functions– To change or access private data, the client must use
public member functions (accessors and mutators)• All members listed after the public: keyword are
considered public members– Can be accessed anywhere
Accessors and Mutators
• “get” functions• “set” functions• Used to access and mutate private data safely
Operators and objects
• == : it is possible to redefine operators to make them work as intended for objects, but we will not learn how to do this yet
• = : the assignment operator does work as intended for objects (not shallow copies)
Example 2
• BankAccount– private: balance, interestRate, fraction()– public:
setRate(double)deposit(double)withdrawal(double)update() //adds one year of simple interest to
balancegetBalance()getRate()output(ostream&) //outputs balance and interest rate
Class Properties (summarized)
• Classes have both member variables and member functions
• A member may be either public or private• Normally, all member variables of a class are private• A private member of a class can’t be used except within
the definition of another member function of the same class
• A class may use another class type for a member variable• A function may have parameters who types are classes• A function may return an object (a class type)
Constructors
• A member function that is automatically called when an object of the class is declared
• It’s job is to initialize private data• Must have the same name as the class• Can’t return a value (thus no return type
either)• Generally, constructors should be public
• Example 2b: write a constructor for BankAccount that accepts two integers (dollar and cents), and a double rate
• Use the constructor you wrote to construct two BankAccount objects
• Write a second constructor that accepts two doubles (balance and rate)
• What if you construct a BankAccount but give it only 1 argument?
Default Constructor
• A constructor that accepts no arguments• Initializes data to “default” values• Do not invoke the default constructor like this:
BankAccount acct();• It is best to always define a default
constructor. Otherwise the compiler will essentially define its own.
Constructor initialization section
• Consists of a colon followed by a list of some or all the member variables separated by commas
• Example:BankAccount::BankAccount(int dollars, int cents, double myRate) : balance(dollars + .01 * cents), rate(myRate){}
Calls to Constructors
• Implicit call to constructor – a constructor is automatically called when an object is declaredClass_Name Object_Name(arguments_for_constructor);Note: can’t call default constructor this way
• Explicit call to constructorobject = Constructor_Name(arguments_for_constructor);
Note: can call default constructor this way