CHAPTER – 14
Classes and objects in C++ [ PART A ]
Part – 1
Choose the best answer
1) The variables declared inside the class are known as
(A) data functions (B) inline functions (C) member functions D) attributes
2) Which of the following statements about member functions are True or False?
i) A member function can call another member function directly with using the dot
operator.
ii) Member function can access the private data of the class.
(A) i-True, ii-True (B) i-False, ii-True (C) i-True, ii-False (D) i-False, ii-False
3) A member function can call another member function directly, without using the dot operator
called as
(A) sub function (B) sub member
(C) nesting of member function (D) sibling of member function
4) The member function defined within the class behave like
(A) inline functions (B) Non inline function (C) Outline function (D)Data function
5) Which of the following access specifier protects data from inadvertent modifications?
(A) Private (B) Protected (C) Public (D) Global
6) class x
{
int y;
public:
x(int z)
{
y=z;
}
} x1[4];
int main()
{
x x2(10);
return 0;
}
How many objects are created for the above program
(A) 10 (B) 14 (C) 5 (D) 2
7) State whether the following statements about the constructor are True or False.
i) constructors should be declared in the private section.
ii) constructors are invoked automatically when the objects are created.
(A) True, True (B) True, False (C) False, True (D) False, False
8) Which of the following constructor is executed for the following prototype ?
add display( add &); // add is a class name
11 CS / 14 Class and Objects / Page 1 of 3
(A) Default constructor (B) Parameterized constructor
(C) Copy constructor (D) Non Parameterized constructor
PART II
Answer to all the questions (2 Marks):
1) What are called members?
• Class comprises of members.
• Members are classified as Data Members and Member functions.
• Data members are the data variables that represent the features or properties of a
class. Data members are also called as attributes.
• Member functions are the functions that perform specific tasks in a class.
• Member functions are called as methods.
2) Differentiate structure and class though both are user defined data type.
structure class
Structure has all members by default Public Class has all members by default Private
Structure cannot be inherited Class can be inherited
Structure does not support data hiding Class supports data hiding
Structure contains only Data members Class contains both Data members and member
functions
3) What is the difference between the class and object in terms of oop?
Class Object
Class is a mechanism of hiding data members and Object is the instance of the class. It is also
associated methods in a single unit . called Class variables.
It generates the objects It gives life to the class
Memory space is not allocated, when it is created Memory space is allocated, when it is created
Eg. Eg.
Car is the class BMW, Audi are the objects
4) Why it is considered as a good practice to define a constructor though compiler can
automatically generate a constructor?
• A defined constructor allows you to explicitly set the initial values for your object's data
members
• prevent your objects from being created in an unusable state.
• can define multiple constructors with different parameters
5) Write down the importance of destructor.
• The purpose of the destructor is to free the resources that the object may have acquired during
its lifetime.
• A destructor function removes the memory of an object which was allocated by the constructor
at the time of creating a object.
11 CS / 14 Class and Objects / Page 2 of 3
PART III
3. What are advantages of declaring constructors and destructor under public accessibility?
• A constructor can be defined either in private or public section of a class.
• If it is defined in public section of a class, then its object can be created in any function.
• It means that the constructor and destructor can be called directly by code in main() function.
PART IV
1. Mention the differences between constructor and destructor.
CONSTRUCTOR DESTRUCTOR
same name as that of the class. Same name as that of the class prefixed by
the Tilde symbol (~).
can be overloaded. can’t be overloaded.
can have parameter (Arguments) list cannot have parameter (Arguments) list.
can be more than one constructor in a class only one destructor in a class
executed automatically when the object is executed automatically when the control
created reaches the end of class scope to destroy the
object.
Allocated memory space for the objects. Destroy the object.
In the absence of user-defined constructor the In the absence of user-defined destructor the
compiler generates the constructor compiler generates the destructor
11 CS / 14 Class and Objects / Page 3 of 3