Classes
A Deeper Look
Part 1
Contents
▪ Class Scope and Accessing Class Members
▪ Class functions
▪ Access functions
▪ Utility functions
▪ Constructor
▪ Passing arguments to constructors
▪ Using default arguments in a constructor
▪ Destructor
▪ Default Member wise Assignment
Class Scope and Accessing Class Members
Dot member selection operator (.)
Accesses the object’s members
Used with an object’s name or with a reference to
an object
Arrow member selection operator (->)
Accesses the object’s members
Used with a pointer to an object
Access Functions and Utility Functions
Access functions
Can read or display data
Can test the truth or falsity of conditions
Such functions are often called predicate functions
Utility functions (also called helper functions)
private member functions that support the operation of the
class’s public member functions
Not part of a class’s public interface
Not intended to be used by clients of a class
1
2 // Demonstrating the class member access operators . and ->
3 #include <iostream>
4 using namespace std;
5
6
7 // class Count definition
8 class Count
9 {
10 public: // public data is dangerous
11 // sets the value of private data member x
12 void setX( int value )
13 {
14 x = value;
15 } // end function setX
16
17 // prints the value of private data member x
18 void print()
19 {
20 cout << x << endl;
21 } // end function print
22
23 private:
24 int x;
25 }; // end class Count
26
27 int main()
28 {
29 Count counter; //Using the dot
create memberobject
counter selection operator with an object
30 Count *counterPtr = &counter; // create pointer to counter
31 Count &counterRef = counter; // create reference to counter
32
33 cout << "Set x toUsing
1 andthe print using
dot member the object's
selection name:
operator with ";
a reference
34 counter.setX( 1 ); // set data member x to 1
35 counter.print(); // call member function print
36
37
Using the arrow member selection operator with a pointer
cout << "Set x to 2 and print using a reference to an object: ";
38 counterRef.setX( 2 ); // set data member x to 2
39 counterRef.print(); // call member function print
40
41 cout << "Set x to 3 and print using a pointer to an object: ";
42 counterPtr->setX( 3 ); // set data member x to 3
43 counterPtr->print(); // call member function print
44 return 0;
45 } // end main
Set x to 1 and print using the object's name: 1
Set x to 2 and print using a reference to an object: 2
Set x to 3 and print using a pointer to an object: 3
Constructor
1. A constructor is a special member function whose task is to
initialize the object of its class.
2. It is special because its name is same as the class name.
3. The constructor is invoked whenever an object of its
associated class is created.
4. It is called constructor because it constructs the values of
data members of the class.
Special Characteristics of Constructors
1. They should be declared in the public section.
2. They are invoked automatically when the objects are
created.
3. They do not have return types, not even void and
therefore, and they cannot return values.
Default Constructor
Parameterized Constructor
1. C++ permits passing arguments to the constructor function,
when the objects are created.
2. The constructors that can take arguments are called
parameterized constructors.
We can modify integer constructor as follows:
Parameterized Constructor
Uses of Parameterized Constructor
It is used to initialize the various data elements of different objects
with different values when they are created.
It is used to overload constructor.
Can we have more than one constructors in a class?
Yes, It is called Constructor Overloading.
Special Note
Whenever we define one or more parametric constructors for
a class, a default constructor( without parameters )should also
be explicitly defined as the compiler will not provide a default
constructor in this case.
However, it is not necessary but it’s considered to be the best
practice to always define a default constructor.
Copy Constructor
A copy constructor is a member function which initializes an
object using another object of the same class. A copy
constructor has the following general function prototype:
ClassName (const ClassName &old_obj);
Multiple Constructors in a Class
So far we have used two kind of constructors. They are:
integer(); //No arguments
integer(int,int); // two arguments
1. In the first case, the constructor itself supplies the data
values and no values are passed by the calling
program.
2. In the second case, the function call passes the
appropriate values from main().
3. C++ permits us to use both these constructors in the
same class.
A class may have multiple constructors
Destructors
1. Destructors have the same name as that of class preceded
by ~(tilde) sign.
2. Similar to constructors , destructors do not have return type
not even void.
3. Only one destructor can be defined in a class , destructors
do not have any parameters.
4. Destructor overloading is not allowed. If the programmer
does not explicitly provide a destructor, the compiler
creates an “empty” destructor.
5. Destructor calls are made in the reverse order of the
corresponding constructor calls.
Default Member wise Assignment
Default memberwise assignment
Assignment operator (=)
Can be used to assign an object to another
object of the same type
Each data member of the right object is
assigned to the same data member in the
left object
1
2 // Declaration of class Date.
3 // Member functions are defined in Date.cpp
4 // class Date definition
5 class Date
6 {
7 public:
8 Date( int = 1, int = 1, int = 2000 ); // default constructor
9 void print();
10 private:
11 int month;
12 int day;
13 int year;
14 }; // end class Date
1
2 // Member-function definitions for class Date.
3 #include <iostream>
4 using namespace std;
7 #include "Date.h" // include definition of class Date from Date.h
8
9 // Date constructor (should do range checking)
10 Date::Date( int m, int d, int y )
11 {
12 month = m;
13 day = d;
14 year = y;
15 } // end constructor Date
16
17 // print Date in the format mm/dd/yyyy
18 void Date::print()
19 {
20 cout << month << '/' << day << '/' << year;
21 } // end function print
1
2 // Demonstrating that class objects can be assigned
3 // to each other using default memberwise assignment.
4 #include <iostream>
5 using namespace std;
6 #include "Date.h" // include definition of class Date from Date.h
7 int main()
8 {
9 Date date1( 7, 4, 2004 );
10 Date date2; // date2 defaults to 1/1/2000
11
12 cout << "date1 = ";
13 date1.print();
14 cout << "\ndate2 = ";
15 date2.print();
16 Memberwise assignment assigns data
17 date2 = date1; // default members
memberwise of date1 to date2
assignment
18
19 cout << "\n\nAfter default memberwise assignment, date2 = ";
20 date2.print();
21 cout << endl;
22 return 0;
23 } // end main
date2 now stores the
same date as date1
date1 = 7/4/2004
date2 = 1/1/2000
After default memberwise assignment, date2 = 7/4/2004 29