0% found this document useful (0 votes)
15 views7 pages

Oops For Interview

The document discusses static member functions in C++, highlighting their independence from class objects and their ability to access static data members. It also includes a program example demonstrating ambiguity in multiple inheritance, where two base classes have a member function with the same name. The ambiguity can be resolved using the class name and scope resolution operator to specify which base class's function to invoke.

Uploaded by

Ram pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Oops For Interview

The document discusses static member functions in C++, highlighting their independence from class objects and their ability to access static data members. It also includes a program example demonstrating ambiguity in multiple inheritance, where two base classes have a member function with the same name. The ambiguity can be resolved using the class name and scope resolution operator to specify which base class's function to invoke.

Uploaded by

Ram pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Oops for interview

26 October 2024 11:11

Quick Notes Page 1


Quick Notes Page 2
Static Member Function in C++
Static Member Function in a class is the function that is declared as static because of which function
attains certain properties as defined below:
• A static member function is independent of any object of the class.
• A static member function can be called even if no objects of the class exist.
• A static member function can also be accessed using the class name through the scope
resolution operator.
• A static member function can access static data members and static member functions inside
or outside of the class.

Quick Notes Page 3


or outside of the class.
• Static member functions have a scope inside the class and cannot access the current object
pointer.
• You can also use a static member function to determine how many objects of the class have
been created.
The reason we need Static member function:
• Static members are frequently used to store information that is shared by all objects in a
class.
• For instance, you may keep track of the quantity of newly generated objects of a specific class
type using a static data member as a counter. This static data member can be increased each
time an object is generated to keep track of the overall number of objects.

Quick Notes Page 4


Output
Static member function is called through Object name:
The value of the length is: 10
The value of the breadth is: 20
The value of the height is: 30
Static member function is called through Class name:
The value of the length is: 10
The value of the breadth is: 20
The value of the height is: 30

Quick Notes Page 5


From <https://www.geeksforgeeks.org/static-member-function-in-cpp/>

Friend class and funciton --->https://www.geeksforgeeks.org/friend-class-function-cpp/


Virtual base class ----> https://www.geeksforgeeks.org/virtual-base-class-in-c/
Virtual function ----> https://www.geeksforgeeks.org/virtual-function-cpp/

--------------------------------------------------------------------------------------------------------------------

Program to demonstrate the Ambiguity Problem in Multiple


Inheritance
Let's write a simple to invoke the same member function of the parent class using derived class in
C++ programming.
Program4.cpp
1. #include <iostream>
2. #include <conio.h>
3.
4. using namespace std;
5.
6. // create class A
7. class A
8. {
9. public:
10. void show()
11. {
12. cout << " It is the member function of class A " << endl;
13. }
14. };
15.
16. // create class B
17. class B
18. {
19. public:
20. void show()
21. {
22. cout << " It is the member function of class B " << endl;
23. }
24. };
25.
26.
27. // create a child class to inherit the member function of class A and class B
28. class child: public A, public B
29. {
30. public:
31. void disp()
32. {
33. cout << " It is the member function of the child class " << endl;
34. }
35. };
36.
37. int main ()
38. {
39. // create an object of the child class to access the member function
40. child ch;
41. ch.show(); // It causes ambiguity
42. ch.disp();
43. return 0;
44. }
When the above program is compiled, it throws the show() member function is ambiguous. Because
of both the base class A and B, defining the same member function show(), and when the derived

Quick Notes Page 6


of both the base class A and B, defining the same member function show(), and when the derived
class's object call the shows() function, it shows ambiguity in multiple inheritances.

From <https://www.javatpoint.com/multiple-inheritance-in-cpp>

Therefore, we need to resolve the ambiguous problem in multiple Inheritance. The ambiguity
problem can be resolved by defining the class name and scope resolution (::) operator to specify the
class from which the member function is invoked in the child class.

From <https://www.javatpoint.com/multiple-inheritance-in-cpp>

1. ch.A:: show(); // class_name and scope resolution operator with member function
2. ch.B::show();

From <https://www.javatpoint.com/multiple-inheritance-in-cpp>

Quick Notes Page 7

You might also like