8 Lecture

CS304

Midterm & Final Term Short Notes

MEMBER FUNCTIONS

Member functions, also known as methods, are functions that are defined within a class and are used to perform operations on the objects of that class. They have access to the data members of the class and can manipulate them as necessary. Membe


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the primary purpose of member functions in object-oriented programming? A. To declare variables B. To initialize objects C. To perform operations on objects D. To create new objects Answer: C Which keyword is used to define a member function in C++? A. class B. function C. method D. this Answer: C Which type of member function can access private data members of a class? A. Public B. Private C. Protected D. Friend Answer: B Which type of member function does not have access to the this pointer? A. Static B. Virtual C. Inline D. Friend Answer: A Which keyword is used to call a member function on an object in C++? A. new B. delete C. this D. dot operator (.) Answer: D Which type of member function is used to initialize an object with a set of default values? A. Constructor B. Destructor C. Virtual function D. Operator function Answer: A Which type of member function is called automatically when an object is destroyed? A. Constructor B. Destructor C. Virtual function D. Operator function Answer: B Which type of member function can be called without creating an object of the class? A. Constructor B. Destructor C. Static function D. Friend function Answer: C Which keyword is used to access a data member of a class within a member function? A. private B. public C. protected D. this Answer: D Which type of member function is used to overload operators such as +, -, *, and /? A. Constructor B. Destructor C. Virtual function D. Operator function Answer: D


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the difference between a member function and a non-member function? Answer: A member function is defined within a class and has access to the data members of the class, whereas a non-member function is defined outside of the class and does not have access to the data members. What is the purpose of a constructor member function? Answer: A constructor member function is used to initialize an object of a class with a set of default values. What is the purpose of a destructor member function? Answer: A destructor member function is called automatically when an object is destroyed and is used to free up any resources allocated by the object. What is a static member function? Answer: A static member function is a function that can be called without creating an object of the class, and it has access only to static data members of the class. Can a member function be overloaded? Answer: Yes, a member function can be overloaded by defining two or more functions with the same name but different parameters or return types. What is the difference between a public and private member function? Answer: A public member function can be accessed by any code that has access to the object, whereas a private member function can only be accessed by other member functions of the class. Can a member function be declared as both virtual and static? Answer: No, a member function cannot be declared as both virtual and static because a virtual function requires a virtual table, whereas a static function does not. What is the purpose of the keyword 'this' in a member function? Answer: The keyword 'this' is a pointer to the current object, and it is used to access the data members and other member functions of the object. Can a constructor member function be overloaded? Answer: Yes, a constructor member function can be overloaded by defining two or more constructors with different parameters. What is the difference between a member function and a friend function? Answer: A member function is defined within a class and has access to the data members of the class, whereas a friend function is not a member of the class but has access to the private and protected members of the class.

In object-oriented programming, member functions are essential to encapsulating behavior within classes. A member function is a function that is defined within a class and operates on the data members of that class. The primary purpose of member functions is to provide a mechanism for performing operations on objects of the class they belong to. There are three types of member functions: public, private, and protected. Public member functions can be accessed from outside the class, whereas private member functions can only be accessed from within the class. Protected member functions can be accessed from within the class and its subclasses. Constructor and destructor functions are special types of member functions. A constructor is a member function that is called when an object of the class is created. It is used to initialize the object with default values. A destructor is a member function that is called when an object is destroyed. It is used to free up any resources allocated by the object. Static member functions are another type of member function that can be called without creating an object of the class. They have access only to static data members of the class. Virtual member functions are member functions that can be overridden in a subclass. The 'this' pointer is a special pointer that is used to refer to the current object within a member function. It is used to access the data members and other member functions of the object. Member functions can be overloaded, meaning that multiple functions with the same name can exist within a class. They can be overloaded based on their parameters or return types. In summary, member functions are an essential component of object-oriented programming, allowing classes to encapsulate behavior and operate on their data members. They provide a mechanism for performing operations on objects of the class they belong to, and they can be defined as public, private, or protected.