39 Lecture
CS304
Midterm & Final Term Short Notes
TEMPLATES & STATIC MEMBERS
Templates and static members are two important concepts in C++ programming. Templates allow for the creation of generic functions and classes that can work with different data types. Static members, on the other hand, are shared by all instances
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
Which keyword is used to define a template in C++? a) template b) class c) typename d) all of the above Answer: a) template Which of the following is not a benefit of using templates in C++? a) Code reusability b) Improved efficiency c) Flexibility d) Simplified syntax Answer: d) Simplified syntax What is the purpose of static members in C++? a) To create class objects b) To provide a single instance of a variable for all class objects c) To define functions that can be accessed without creating an object d) To provide a way to create objects dynamically Answer: b) To provide a single instance of a variable for all class objects Which keyword is used to declare a static member in a class definition? a) static b) const c) friend d) virtual Answer: a) static Which of the following is true about static member functions in C++? a) They can be called using an object of the class. b) They cannot access non-static members of the class. c) They can only be declared in the private section of a class. d) They cannot be overloaded. Answer: b) They cannot access non-static members of the class. Which of the following is not a valid template parameter type in C++? a) int b) float c) void d) char* Answer: b) float What is the purpose of the typename keyword in template definitions? a) To indicate a class type b) To indicate a function type c) To indicate a pointer type d) To indicate a void type Answer: a) To indicate a class type Can a static member of a class be accessed using the class name and the scope resolution operator? a) Yes b) No Answer: a) Yes Can a template function be defined outside the class definition? a) Yes b) No Answer: a) Yes Which of the following is not a valid way to specialize a template function? a) Explicit specialization b) Partial specialization c) Function overloading d) None of the above Answer: c) Function overloading
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a template in C++? Answer: A template is a feature of C++ that allows for the creation of generic functions and classes that can work with different data types. What is a static member in C++? Answer: A static member is a member of a class that is shared by all instances of the class and can be accessed without creating an object. What is the purpose of using templates in C++? Answer: The purpose of using templates in C++ is to create generic functions and classes that can work with different data types, improving code reusability, flexibility, and efficiency. How is a static member variable initialized in C++? Answer: A static member variable is initialized outside the class definition, typically in the source file, using the scope resolution operator and the class name. Can a template class have static members? Answer: Yes, a template class can have static members that are shared by all instances of the class. What is template specialization in C++? Answer: Template specialization is a feature of C++ that allows for the creation of specialized versions of a template function or class for a specific data type. Can a static member function access non-static members of a class? Answer: No, a static member function cannot access non-static members of a class. Can a template function be overloaded in C++? Answer: Yes, a template function can be overloaded with different argument types. What is the syntax for declaring a static member function in C++? Answer: The syntax for declaring a static member function in C++ is to use the "static" keyword before the function name in the class definition. How can a template function be defined outside the class definition in C++? Answer: A template function can be defined outside the class definition by specifying the template parameter list and using the "template" keyword followed by the function signature.
template <typename T>
void print(T data) {
std::cout << data << std::endl;
}
template <typename T>
class Stack {
public:
// class implementation
};
class Person {
private:
static int count;
public:
Person() {
count++;
}
static int getCount() {
return count;
}
};
int Person::count = 0;