Problem1:
======
#include<iostream>
using namespace std;
class base {
public:
int arr[10];
};
class b1: virtual public base { };
class b2: virtual public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout<<sizeof(derived)<<" "<<sizeof(int)<<endl;;
getchar();
return 0;
}
Output : 48
If you do non-virtual inheritance then it is 80(two copies from b1 & b2)
But if we do virtual inheritence then we are getting 8 additional bytes, where are we are supposed to get 40(single copy).So from where these additional bytes are comming.?
Reason: Since the methods are virtual, compiler add 4 bytes from each b1,b2 for vptr(virtual pointer).
===================================================================
Problem : 2
=========
Problem:4
=======
======
#include<iostream>
using namespace std;
class base {
public:
int arr[10];
};
class b1: virtual public base { };
class b2: virtual public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout<<sizeof(derived)<<" "<<sizeof(int)<<endl;;
getchar();
return 0;
}
Output : 48
If you do non-virtual inheritance then it is 80(two copies from b1 & b2)
But if we do virtual inheritence then we are getting 8 additional bytes, where are we are supposed to get 40(single copy).So from where these additional bytes are comming.?
Reason: Since the methods are virtual, compiler add 4 bytes from each b1,b2 for vptr(virtual pointer).
===================================================================
Problem : 2
=========
#include <iostream> using namespace std; class X { private : static const int a = 76; public : static int getA() { return a; } }; int main() { cout <<X::getA()<<endl; return 0; } |
Output: The program compiles and prints 76
Generally, it is not allowed to initialize data members in C++ class declaration, but static const integral members are treated differently and can be initialized with declaration.
Generally, it is not allowed to initialize data members in C++ class declaration, but static const integral members are treated differently and can be initialized with declaration.
==============================================================================
Problem : 3
=========
Problem : 3
=========
#include<iostream> using namespace std; class Base { public : int fun() { cout << "Base::fun() called" ; } int fun( int i) { cout << "Base::fun(int i) called" ; } }; class Derived: public Base { public : int fun( char x) { cout << "Derived::fun(char ) called" ; } }; int main() { Derived d; d.fun(); return 0; } |
Output: Compiler Error.
In the above program, fun() of base class is not accessible in the derived class. If a derived class creates a member method with name same as one of the methods in base class, then all the base class methods with this name become hidden in derived class (See this for more details)
In the above program, fun() of base class is not accessible in the derived class. If a derived class creates a member method with name same as one of the methods in base class, then all the base class methods with this name become hidden in derived class (See this for more details)
==============================================================================
=======
#include<iostream>
using namespace std;
class Base {
public:
virtual int fun(int i) { cout << "Base::fun(int i) called"; return 0; }
};
class Derived: public Base {
private:
int fun(int x) { cout << "Derived::fun(int x) called"; return 0;}
};
int main()
{
Base *ptr = new Derived;
ptr->fun(10); //Private method of derived will be called out side.VIOLATION???
return 0;
}
output:
Derived::fun(int x) called
This is not violation of oops. In C++, virtual & access specifier are not dependant on each other. It is designed that way. Whereas, when you consider languages like C# and Java you cannot have access specifier narrowed down ( i.e. stricter access specifier ) when compared to base class. So, the code does not even compile in those languages. Since there is no such restriction in C++, the code compiles.
That is the reason why it is actually advised not to make virtual function public. So, make your non-virtual methods public and make them act as interface methods .
Make your virtual methods protected or private ( depending on the accessibility you desire ) and call those virtual methods from the non-virtual methods.
That is the reason why it is actually advised not to make virtual function public. So, make your non-virtual methods public and make them act as interface methods .
Make your virtual methods protected or private ( depending on the accessibility you desire ) and call those virtual methods from the non-virtual methods.
C program to shutdown restart logoff hibernate computer in windowsxp windows7 and ubuntu
ReplyDeletehttp://geeksprogrammings.blogspot.com/2014/02/shutdown-restart-logoff-hibernte-program-cplusplus.html
Thanks Heemanshu
ReplyDelete