5.1

How do structures in C and C++ differ?

Answer

C structure member functions are not permitted but in C++ member functions are permitted.

5.2

What is a class? How does it accomplish data hiding?

Answer

A class is a way to bind the data and its associated functions together. In class we can declare a data as private for which the functions outside the class can not access the data and thus if  accomplish data hiding.

5.3

How does a C++ structure differ from a C++ class?

Answer

Initially (in C) a structure was used to bundle different of data types together to perform a particular functionality C++ extended the structure to contain functions also. The difference is that all declarations inside a structure are default public.

5.4

What are objects? How are they created?

Answer

Object is a member of class. Let us consider a simple example. int a; here a is a variable of int type. Again consider class fruit.
{
}
here fruit is the class-name. We can create an object as follows:
fruit mango;
here mango is a object.

5.5

How is a member function of a class defined?

Answer

member function of a class can be defined in two places:
* Outside the class definition.
* Inside the class definition.

Inside the class definition : same as other normal function.

Outside the class definition : general form:
return-type class-name : function-name (argument list)
{
function body
}

5.6

Can we use the same function name for a member function of a class and an outside function in the same program file? If yes, how are they distinguished? If no, give reasons.

Answer

Yes, We can distinguish them during calling to main ( ) function. The following example illustrates this:

5.7

Describe the mechanism of accessing data members and member functions in the following cases:

  • (a) Inside the main program.
  • (b) Inside a member function of the same class.
  • (c) Inside a member function of another class.
Answer
  • (a) Using object and dot membership operator.
  • (b) Just like accessing a local variable of a function.
  • (c) Using object and dot membership operator.

The following example explains how to access data members and member functions inside a member function of another class.

5.8

When do we declare a member of a class static?

Answer

When we need a new context of a variable the n we declare this variable as static.

5.9

What is a friend function? What are the merits and demerits of using friend functions?

Answer

A function that acts as a bridge among different classes, then it is called friend function.

Merits :
We can access the other class members in our class if we use friend keyword. We can access the members without inheriting the class.

demerits :
Maximum size of the memory will occupied by objects according to the size of friend members.

5.10

State whether the following statements are TRUE or FALSE.

  • (a) Data items in a class must always be private.
  • (b) A function designed as private is accessible only to member functions of that class.
  • (c) A function designed as public can be accessed like any other ordinary functions.
  • (d) Member functions defined inside a class specifier become inline functions by default.
  • (e) Classes can bring together all aspects of an entity in one place.
  • (f) Class members are public by default.
  • (g) Friend junctions have access to only public members of a class.
  • (h) An entire class can be made a friend of another class.
  • (i) Functions cannot return class objects.
  • (j) Data members can be initialized inside class specifier.
Answer
  • (a) FALSE
  • (b) TRUE
  • (c) FALSE

*A function designed as public can be accessed like any other ordinary functions from the member function of same class.

  • (d) TRUE
  • (e) TRUE
  • (f) FALSE
  • (g) FALSE
  • (h) TRUE
  • (i) FALSE
  • (j) FALSE

 


Next Previous