Section A — 1 Mark Questions
1. Where can member functions of a class be defined? [1 Mark]
Answer: Member functions can be defined either inside the class declaration or outside the class declaration using the scope resolution operator.
2. What operator is used to define member functions outside the class? [1 Mark]
Answer: The scope resolution operator (::).
3. What is the default access specifier for members of a class in C++? [1 Mark]
Answer: Private.
4. Can a private member function be called from outside the class? [1 Mark]
Answer: No, private member functions cannot be accessed directly from outside the class.
5. What happens when a member function is defined inside a class? [1 Mark]
Answer: It is treated as an inline function by default.
6. How do you declare an array of integers as a data member within a class? [1 Mark]
Answer: By specifying the data type followed by the name and size, such as int arr[10];.
7. Does memory get allocated for objects as soon as a class is defined? [1 Mark]
Answer: No, memory is allocated only when the object is created.
8. What is the primary purpose of a private member function? [1 Mark]
Answer: To perform internal tasks that are restricted to the class member functions only.
9. If a member function is defined outside, what must be included in the function header? [1 Mark]
Answer: The class name followed by the scope resolution operator.
10. Can an object contain an array as a data member? [1 Mark]
Answer: Yes, arrays can be members of a class.
11. When is memory allocated for static data members? [1 Mark]
Answer: Memory for static members is allocated only once, separate from the objects.
12. What does a class act as in terms of memory? [1 Mark]
Answer: A blueprint or template for objects.
13. If class X has a function void display(), how is it defined outside? [1 Mark]
Answer: void X::display() { /* body */ }.
14. Are member functions stored separately for each object? [1 Mark]
Answer: No, all objects share the same copy of member functions in memory.
15. What is the role of an access specifier? [1 Mark]
Answer: To define the scope and visibility of class members.
16. Can a class have both public and private member functions? [1 Mark]
Answer: Yes.
17. Is it mandatory to define all member functions inside a class? [1 Mark]
18. Answer: No, they can be defined outside.
19. How is the size of an object determined? [1 Mark]
Answer: It is the sum of the sizes of its non-static data members.
20. What is an object? [1 Mark]
Answer: An instance of a class.
21. Can you have an array of objects? [1 Mark]
Answer: Yes.
Section B — 2/3 Mark Questions
1. Explain the difference between defining a member function inside versus outside a class. [2 Marks]
Answer: Defining a function inside the class makes it implicitly inline, which may increase code size. Defining it outside makes the class definition cleaner and avoids implicit inline status.
2. Why are private member functions used? [2 Marks]
Answer: Private member functions are used to restrict access to internal logic or helper functions. They ensure that sensitive operations or internal calculations are not accessed by outside entities.
3. How is memory allocated for objects of a class? [3 Marks]
Answer: When an object is created, memory is allocated only for its data members. Member functions are not duplicated; a single copy is stored in memory and shared by all objects of that class.
4. What are the limitations of arrays within a class? [2 Marks]
Answer: The size of the array must be defined at compile time or via constants. Additionally, they occupy fixed memory within every instance of the object.
5. Write the syntax for defining a member function outside a class. [2 Marks]
Answer: return_type class_name::function_name(parameters) { // code here }.
6. Differentiate between public and private access specifiers. [3 Marks]
Answer: Public members are accessible from anywhere in the program. Private members are only accessible within the class itself. Using private promotes encapsulation by hiding implementation details.
7. Why do we say member functions are shared among objects? [2 Marks]
Answer: Because the code for a member function is identical for every instance of a class. Storing separate copies for each object would be memory-inefficient.
8. Can you define an array member with a variable size? [2 Marks]
Answer: No, in standard C++, array members must have a fixed size defined at compile time, unless dynamic memory allocation (like pointers) is used.
9. What is the significance of the scope resolution operator in class definitions? [3 Marks]
Answer: It allows the compiler to associate a function definition with a specific class. It is mandatory when defining member functions outside the class body to distinguish them from global functions.
10. What constitutes the size of an object? [2 Marks]
Answer: The size of an object is determined by the total memory required for all its non-static data members, potentially including padding for alignment.
Section C — 5 Mark Questions
1. Compare and contrast defining member functions inside and outside a class. [5 Marks]
Inside Class Definition
When a member function is defined inside the class, it is automatically treated as an inline function. This suggests to the compiler that it should replace function calls with the actual code of the function, which can improve execution speed for small functions but may increase the overall binary size.
Outside Class Definition
When a member function is defined outside, the class definition only contains the function prototype. The function is defined elsewhere using the scope resolution operator (::). This approach separates the interface (what the class does) from the implementation (how it does it), which is a core tenet of object-oriented design and improves code readability for large classes.
Key Differences
- Inline status: Implicitly inline inside, normally not inline outside.
- Readability: Outside definition keeps the class structure concise.
- Organization: Outside definition is preferred for complex classes to manage maintenance.
2. Elaborate on the concept of memory allocation for objects and their members. [5 Marks]
Introduction
In C++, classes are blueprints, and objects are the actual instances. Memory allocation behaves differently for data and functions.
Data Member Allocation
Every time an object is instantiated, the system allocates a distinct block of memory for all its non-static data members. This means every object maintains its own unique state, ensuring that modifying data in one object does not affect another.
Member Function Allocation
Unlike data, member functions occupy a single block of memory regardless of how many objects exist. These functions reside in a separate memory area, and they are shared by all objects. To handle multiple objects, the compiler passes a hidden pointer called the 'this' pointer to the function so that it can identify which specific object's data it should operate upon.
Static Members
Static data members are a special case; they share one copy across all objects of the class, meaning they do not consume memory for each instance.
3. Explain the role of access specifiers with specific reference to private member functions. [5 Marks]
Concept of Access Specifiers
Access specifiers (public, private, protected) define the visibility of class members. By default, class members are private.
Private Member Functions
Private member functions are functions defined within a class that can only be invoked by other member functions of the same class. They act as 'helper' or 'utility' functions. By making these functions private, the class designer hides implementation details from the user of the class.
Benefits of Encapsulation
- Security: Prevents external code from tampering with critical internal logic.
- Abstraction: The user only interacts with public functions, while the private functions handle the complex backend processes.
- Maintainability: The internal implementation can be changed without affecting the code that uses the class, provided the public interface remains the same.
4. How are arrays used within classes? Explain with an example and memory implications. [5 Marks]
Arrays as Members
Arrays can be declared as private or public data members within a class. They follow the same rules as other data members: they become part of the object's memory layout. If a class contains an array of size 100, every object created will allocate space for those 100 elements.
Example
Consider a class 'Student' that needs to store 5 subject marks. We define it as: class Student { private: int marks[5]; public: void setMarks(int m[]); }; Here, the 'marks' array is a direct component of the 'Student' object.
Memory Implications
The total memory size of an object is approximately the size of its data members. If you have an array of 5 integers (assuming 4 bytes each), it will add 20 bytes to the footprint of the object. Developers must be careful with large arrays in classes, as every object instance will multiply that memory consumption, potentially leading to stack overflow if objects are created on the stack.
5. Discuss the importance of the scope resolution operator and its role in class member definition. [5 Marks]
Definition and Syntax
The scope resolution operator (::) is a fundamental C++ construct used to clarify the scope of an identifier. In the context of classes, it is used to define a function as belonging to a specific class when the definition is provided outside the class body.
The Necessity of the Operator
When a member function is defined outside, the compiler needs to know that this function belongs to a particular class. Without the scope resolution operator, the compiler would treat it as a standalone global function, which would lead to a compilation error because a class function requires the class context.
Key Advantages
- Scope Identification: Clearly links the implementation to the interface.
- Namespace Management: Helps avoid naming conflicts between functions in different classes that might share the same name.
- Clean Code: By defining functions outside, the class declaration remains compact, serving as a clean reference for what the class offers (the interface).
Conclusion: The operator is an essential bridge between a class's declaration and its implementation in modular software development.