Classes in C++-part 2
Classes in C++-part 2
[CSE202]
Classes can have fields and methods which could be visible to or hidden from the outside world
◦ The former are called public members and the latter are called private members
◦ The default behaviour is that members are private, unless defined in the public section
◦ We did not discuss the static members of the class last time, so we will do it now
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Static Members of a class
Static members of a class – fields or methods – are essentially associated with the class …
◦ … as against the non-static members, which are rather associated with an object
Only one copy of a static field is maintained for the whole class
◦ Static variables are stored in a separate memory section and initialised before any object is created
Static members of a class can be accessed using the class’s name with scope resolution operator
◦ For instance, <class name>::<static field> or <class name>::<static method>
◦ By the way, you can access them using an object as well, but logically, it doesn’t make much sense !!
◦ The access rules (public or private) apply to static members in a similar fashion
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Extended Toy Example – static members
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Extended Toy Example – static members
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Extended Toy Example – static members
It is just that it can only use the static fields of the class
(because static fields too, are the property of the class and
not associated with a particular object)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Extended Toy Example – static members
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Copy Constructors
There is a special type of constructor, that is also added by added by the compiler implicitly
◦ However, a Copy Constructor can never be used for the first object of a class …
◦ … or for any subsequent objects, unless the required action is – creating a copy of an existing object
A copy constructor is called implicitly whenever the compiler needs to create a copy of an object
◦ For example, when you pass an object to a function by value
You can also use it to create a copy of an object explicitly in your code
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Extended Toy Example – copy constructer
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
(Single) Inheritance in C++
Assume that you now want to make the class Toy a little more “specific”
◦ You also wish to store information about the type and number of batteries required to run the toy
◦ But, not all toys require batteries – some can be played with, without any battery as well
One option is to add some fields and methods in the Toy class to cover this aspect
◦ But then, we will need to adopt a convention like “Battery not required” for some Toys
Another option is to extend the Toy class and create a new class for “battery operated toys”
◦ Basically, a battery operated toy has all the properties and behaviour of a toy …
◦ … plus, some more properties and behaviour
◦ There is no point creating a copy of all fields and methods of the Toy class …
◦ … instead, through Inheritance we “import” this behaviour, and add more to the new class
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
The Battery Operated Toy Example
Here, we created another class to as a specific
type of toys – those that require batteries
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
The Battery Operated Toy Example
This is how we inherit the class Toy2 in
BatteryOperatedToy
The syntax is
class <D> : <a> <B>
where D is the derived class, B is the base class
and a could be public, private or
protected
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
The protected access specifier
Other than public and private, there is one more access specifier in C++ for class members
The specifier, protected, is similar to the specifier private, if there is no inheritance
The private members of a class cannot be inherited by any derived class
◦ One way to let a member be inherited is by making it public
◦ But, it also provides access to the member to the outside world
◦ A mid path is to make these members protected
A protected member behaves the same way as a private member, with just one exception
◦ The protected members can be inherited by a derived class
◦ To reiterate, for every other case, protected and private mean exactly the same
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Access specifiers in inheritance
Scenario:
class <D> : public <B>
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Access specifiers in inheritance
Scenario:
class <D> : protected <B>
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Access specifiers in inheritance
Scenario:
class <D> : private <B>
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Constructors and Inheritance
When a class inherits another class, there is a hierarchical creation of the objects
This involves, creation of an object of the base class first, and then adding more fields to it …
◦ … thus making it an object of the derived type
◦ Keep in mind that space is allocated in the memory for even the private fields of the base class …
◦ … it is just that they are not accessible directly to the derived class
Thus, when a constructor of the derived class is called, “some” base class constructor is also invoked
◦ The base class constructor must be invoked before the derived class constructor is executed
If there is a default constructor available in the base class, no additional measures are required …
◦ … the default constructor is automatically invoked, when any derived class constructor is called
However, if a default constructor is not present in the base class, some additional work is required
◦ Basically, all derived class constructors must make a call to some base class constructor
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Calling base class constructors
This is how an explicit call to a base class
constructor is achieved in the derived class
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Calling base class constructors
Syntax:
derived constructor header :
base constructor call
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Accessing private members externally
The general rule of thumb is that private members of a class are inaccessible externally
However, in some peculiar cases, you may wish to make an exception …
◦ … i.e. allow external access to the private members of the class
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Friend Functions
This is how you specify a function as a friend of
a class – it looks similar to a function
declaration, preceded by the keyword friend
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Friend Functions
While declaring the function, it is a good practice to
provide a declaration of the class for which this
function will be a friend (called a forward
declaration)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Friend Functions
Friend functions can access the private members of a class using the <object>.<member> notation
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Behaviour Extension Points – Virtual Methods
Sometimes, a method may be defined for a base class …
◦ … but it’s definition may be “more precise” (or “different”) with respect to a derived class
To achieve this in C++, the method can be defined as virtual in the base class
A virtual method indicates that multiple definitions of this method “may be” found at the runtime
The definition that must be invoked is decided by the type of the object
◦ If the object is of base class type, the definition from the base class is executed, and …
◦ … if the object is of derived class type, the definition executed is the one provided in the derived class
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Virtual Functions
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Virtual Functions
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Virtual Functions
… and these are the definitions for the methods in the derived class
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Virtual Functions
The base class versions can also be accessed, using the scope resolution operator
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Virtual Functions
The methods can be invoked either with the base class object or derived class object
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Virtual Functions
You can observe how the output of
the code changes when the same
method is invoked with different
types of objects
Note: To build these executables, run the command make clean all in the example directory
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Homework !!
Copy Constructors are “usually” not required to be defined explicitly
◦ It was certainly not required in our example, but in some cases it may be necessary
◦ Try to find out about these cases; may be give this article a read:
https://www.geeksforgeeks.org/copy-constructor-in-cpp/
If you remember our discussion about “passing values by reference” in the previous semester …
◦ … we briefly discussed how the same term means slightly different things in C and C++
◦ Basically, C++ has an explicit way to create references – without the use of pointers
◦ You can, of course, also use the C way to pass parameters by reference, i.e. via pointers
◦ But using references are more popular (and usually convenient)
◦ Read more about these; you may start with these links:
https://www.geeksforgeeks.org/references-in-c/
https://stackoverflow.com/questions/8627956/ways-of-passing-arguments-value-vs-reference-vs-pointer
On the last slide, try to find out why the destructor is being called twice?
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD