Computer Science C M M Questions
Computer Science C M M Questions
What are classes and Objects? How does it accomplish data hiding?
Class is a blueprint and object is instance of a class. Class is a user-defined data type, which holds
its own data members and member functions and helps in code reusability. An Object is an
instance of a class. When a class is defined no memory is allocated but when the object is created
memory is allocated. An object has an identity, state and behavior which each object contains data
and code to manipulate the data.
Explain the use of the access specifiers “private”, “protected” and “public” as they relate to data
members.
1. Public access specifier – members are accessible from outside of the class. All the class
members declared under the public specifier will be available to everyone. The data members
and member functions declared as public can be accessed by other classes and functions too.
2. Private access specifiers – members cannot be accessed or viewed from outside the class. The
class members declared as private can only be accessed within the class itself by the member
functions. They are not allowed to be accessed directly by any other object or function
outside the class. Only the member function that are allowed to access the private data
members of the class.
3. Protected access specifiers – members cannot be accessed from outside the class, but they can
be accessed within the inherited class. The protect access specifier is similar to the private
access specifier in terms as it cannot be accessed outside of its class, but the difference is that
the class members declared as protected can be accessed by any subclass of that class as well,
Give any two differences between Normal member function and Constructor.
1. A construction does not have a return type, but a member function does have a return type.
2. Constructors must be named the same as the class name, but member functions should have a
different name than the class name.