Important Introduction to Programming Short Questions
Chapter: 40
Question 1:
Can a nested class access private members of its containing class?
Answer:
Yes, if the containing class declares the nested class as a friend.
Chapter: 40
Question 2:
What is the output sequence of constructors and destructors for nested classes in the provided example?
Answer:
Nested class constructors are called in order of their declaration, and destructors are called in reverse order.
Chapter: 40
Question 3:
How can you define member functions of nested classes outside the surrounding class?
Answer:
Use the scope resolution operator to specify the nested class and its members.
Chapter: 40
Question 4:
How does C++ handle structures as class members?
Answer:
Structures can be used as class members with similar rules to class objects, but their members default to public scope.
Chapter: 40
Question 5:
What are the visibility rules for members of a nested class?
Answer:
Visibility depends on where the nested class is declared; public nested classes are accessible from outside, private are not.
Chapter: 40
Question 6:
What is the effect of declaring a nested class as friend of another class?
Answer:
It allows the nested class to access private members of the class declaring it as a friend.
Chapter: 40
Question 7:
How can you access the members of a nested class from an instance of the surrounding class?
Answer:
By using the nested class's type name along with the scope resolution operator.
Chapter: 41
Question 8:
What is a template in C++?
Answer:
A template in C++ is a blueprint that allows the creation of functions or classes to handle various data types with a single piece of code, which the compiler generates specific versions for based on the types used.
Chapter: 41
Question 9:
Name the two types of templates in C++.
Answer:
The two types of templates in C++ are function templates and class templates.
Chapter: 41
Question 10:
How does a function template differ from function overloading?
Answer:
A function template allows creating a single function definition to handle multiple data types, whereas function overloading requires defining multiple functions with the same name but different argument types or counts.