Classes in C++-part 1
Classes in C++-part 1
[CSE202]
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (1/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (1/4)
Here, we are creating a new class called Toy in the namespace example
The class has four fields, with one of them being static (more on this in the
next lecture)
The class has eight methods, with two of them being special methods, called
a constructor and a destructor (more on this soon)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (2/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (2/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Access control over members of a class
You may have noticed that the fields were declared in the private section of the class
All the fields defined in the private section are kind of “invisible” outside the class
◦ So, they cannot be accessed outside the class by simply using <object>.<member> mechanism
◦ Although, it is common to keep the fields of a class private, methods too can be made private
If you do not create these access control sections, by default, all members become private
The definitions of private methods outside the class’s declaration though is valid
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (2/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Constructors and Destructors (1/2)
Constructors are methods which are invoked when a new object of a class is to be initialised
◦ It has the same name as the name of the class, without any return type (not even void)
A default constructor is added by the compiler for you, if you do not define one explicitly
◦ This added constructor does not have any instructions though – you can assume it to have empty body
◦ All the fields of the object are thus, initialised to their respective defaults
When you create a new object, you can pick any one of the constructors to initialise the fields
◦ For example, the statements, A ob = A(); and A ob = A(5), invoke different constructors
◦ The former invokes one with no arguments, and the latter invokes one with an int argument
If you define even a single constructor, the compiler doesn’t add the default constructor
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (3/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Constructors and Destructors (2/2)
A destructor is a method that is called right before an object is “thrown into trash”
◦ It is not literally thrown, it means the memory allocated for the object is reclaimed
Its name is the name of the class, preceded by a tilde (~), without any return type (not even void)
A destructor can be used to perform any “clean-up” before the object becomes inaccessible
◦ For instance, if we allocated any memory dynamically, say via malloc(), it should be freed
A default destructor with empty body is added to a class automatically if it is not defined explicitly
While you can provide multiple constructors in a class, there can only
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (3/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (4/4)
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Example – A Toy Class (4/4)
On executing the code on the left, you will see the above output
The objects t1 and t2 are created before t3, but are destroyed
after it – because the scope of t3 is only within func()
Among the local variables, the objects are destroyed in the reverse
order of their construction
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD
Homework !!
For the class Toy, attempt to create an object without supplying the values for the parameters
◦ What can you infer from your observations?
Comment the supplied constructor in the class, and retry the above
Can you try to guess why the local objects are destroyed in the reverse order of their creation?
◦ Hint: It may have something to do with the use of a Stack frame for execution of a method
SAURABH SRIVASTAVA | DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING | IIT (ISM) DHANBAD