This chapter discusses classes and data abstraction in C++. It covers key concepts such as class definition syntax, public and private class members, accessing class members, passing class objects as parameters, constructors, and initializing class member variables. The overall focus is on how classes are defined and used to encapsulate data and member functions in C++.
This chapter discusses classes and data abstraction in C++. It covers key concepts such as class definition syntax, public and private class members, accessing class members, passing class objects as parameters, constructors, and initializing class member variables. The overall focus is on how classes are defined and used to encapsulate data and member functions in C++.
Abstraction Classes • Class: collection of a fixed number of components (members) • Definition syntax:
– Defines a data type, no memory is allocated
– Don’t forget the semicolon after closing brace C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2 Classes (cont'd.) • Class member can be a variable or a function • If a member of a class is a variable – It is declared like any other variable • In the definition of the class – You cannot initialize a variable when you declare it • If a member of a class is a function – Function prototype is listed – Function members can (directly) access any member of the class C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3 Classes (cont'd.) • Three categories of class members – private (default) • Member cannot be accessed outside the class – public • Member is accessible outside the class – protected
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4
Classes (cont'd.)
These functions cannot modify
the member variables of a variable of type clockType
const: formal parameter can’t modify
the value of the actual parameter
private members, can’t be accessed from outside the class
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5
Variable (Object) Declaration • Once a class is defined, you can declare variables of that type clockType myClock; clockType yourClock;
• A class variable is called a class object
or class instance
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6
Accessing Class Members
• Once an object is declared, it can access
the public members of the class • Syntax:
– The dot (.) is the member access operator
• If object is declared in the definition of a member function of the class, it can access the public and private members C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7 Accessing Class Members (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8
Built-in Operations on Classes • Most of C++’s built-in operations do not apply to classes – Arithmetic operators cannot be used on class objects unless the operators are overloaded – You cannot use relational operators to compare two class objects for equality • Built-in operations valid for class objects: – Member access (.) – Assignment (=)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9
Assignment Operator and Classes
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10
Functions and Classes • Objects can be passed as parameters to functions and returned as function values • As parameters to functions – Objects can be passed by value or by reference • If an object is passed by value – Contents of data members of the actual parameter are copied into the corresponding data members of the formal parameter C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11 Reference Parameters and Class Objects (Variables) • Passing by value might require a large amount of storage space and a considerable amount of computer time to copy the value of the actual parameter into the formal parameter • If a variable is passed by reference – The formal parameter receives only the address of the actual parameter
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
Reference Parameters and Class Objects (Variables) (cont'd.) • Pass by reference is an efficient way to pass a variable as a parameter – Problem: when passing by reference, the actual parameter changes when formal parameter changes – Solution: use const in the formal parameter declaration
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
Implementation of Member Functions Scope resolution operator
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14
Implementation of Member Functions (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15
Implementation of Member Functions (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16
Implementation of Member Functions (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17
Implementation of Member Functions (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18
Implementation of Member Functions (cont'd.) • Once a class is properly defined and implemented, it can be used in a program – A program that uses/manipulates the objects of a class is called a client of that class • When you declare objects of the class clockType, every object has its own copy of the member variables (hr, min, and sec) • Variables such as hr, min, and sec are called instance variables of the class – Every object has its own instance of the data
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19
Accessor and Mutator Functions • Accessor function: member function that only accesses the value(s) of member variable(s) • Mutator function: member function that modifies the value(s) of member variable(s) • Constant function: – Member function that cannot modify member variables – Use const in function heading
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 20
Order of public and private Members of a Class • C++ has no fixed order in which you declare public and private members • By default all members of a class are private • Use the member access specifier public to make a member available for public access C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21 Order of public and private Members of a Class (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 22
Order of public and private Members of a Class (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
Order of public and private Members of a Class (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24
Constructors • Use constructors to guarantee that data members of a class are initialized • Two types of constructors: – With parameters – Without parameters (default constructor) • The name of a constructor is the same as the name of the class • A constructor has no type C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25 Constructors (cont'd.) • A class can have more than one constructor – Each must have a different formal parameter list • Constructors execute automatically when a class object enters its scope – They cannot be called like other functions – Which constructor executes depends on the types of values passed to the class object when the class object is declared
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 26
Constructors (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 27
Constructors (cont'd.)
Can be replaced with:
setTime(hours, minutes, seconds);
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28
Invoking a Constructor • A constructor is automatically executed when a class variable is declared
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 29
Invoking the Default Constructor • To invoke the default constructor:
• Example: clockType yourClock;
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30
Invoking a Constructor with Parameters • Syntax:
• The number of arguments and their type
should match the formal parameters (in the order given) of one of the constructors – Otherwise, C++ uses type conversion and looks for the best match – Any ambiguity leads to a compile-time error C++ Programming: From Problem Analysis to Program Design, Fifth Edition 31 Constructors and Default Parameters
• If you replace the constructors of
clockType with the constructor in Line 1, you can declare clockType objects with zero, one, two, or three arguments as follows: clockType clock1; //Line 2 clockType clock2(5); //Line 3 clockType clock3(12, 30); //Line 4 clockType clock4(7, 34, 18); //Line 5 C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32 Classes and Constructors: A Precaution • If a class has no constructor(s), C++ provides the default constructor – However, object declared is still uninitialized • If a class includes constructor(s) with parameter(s), but not the default constructor – C++ does not provide the default constructor
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 33
Destructors • Destructors are functions without any type • The name of a destructor is the character '~' followed by class name – For example: ~clockType(); • A class can have only one destructor – The destructor has no parameters • The destructor is automatically executed when the class object goes out of scope C++ Programming: From Problem Analysis to Program Design, Fifth Edition 34