few interesting questions.....


I have a four questions.Those who know the answers please  reply.I would be glad to get the
right answer.Thanks in advance.Hope you find the questins interesting.....





(1)Is it possible to get the value of a private data member using the address of objects(pointers.)
   If so, pl explain how it can be done?

(2)Just like the base  class pointer can be assigned a derived class object ,is it possible
   to assign a base class object to a derived class pointer?
   can dynamic_cast be used to accomplish?

(3)In the declaration of virtual functions ,gernerally the declaration is of the form:

    virtual datatype vtual_fn_name(arguments) where datatype is void or int or any other type

Is it syntactically legal on all platforms to declare the virtual function th following way.

   datatype virtual vtual_fn_name(arguments) where datatype is void or int or any other type

That is can the virtual keyword preceed the data type?(usually it is the other way!)


(4)Does a  data type like unsigned double exists?(I tried to check it
           but Sizeof double and unsigned double give 8 and 4 bytes on msvc platform.)
           Why this anomaly?


kesavan_sridharAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yonatCommented:
(1) Not portably. The layout of data members is compiler and platform dependent, so you can't rely on it working.

(2) I am not sure what you mean. First, a base class pointer cannot necessarily be assigned to a derived class pointer - it is the other way around. Assuming B is a base class of D:
    B* pb = new D; // okay
    D* pd = new B; // will not generally work
Now, are you asking if you can do:
    B b;
    D* pd = b;
If this is your question, then generally you can't do that unless the B and D were specifically designed to allow it, but I can't see the reasoning for making such a design. What are you trying to do? Maybe there is another way.

(3) No.

(4) No. This is the same as in C. Apparently, there aren't many people interested in having such a type. Why do you need one? If you're looking for somthing like BigInteger then double isn't it - it loses accuracy.
0
viktornetCommented:
By number one, do you mean something like this?? (code not written by me)

 ///////////////////////////
// Toby Opferman
// Scrat / _Secret
// http://www.nauticom.net/www/secret
// programming@teo.nauticom.net
//
// Compiled on Turbo C++ 3.0 under SMALL Memory Model, this program
// Demonstrates Data Unencapsalation and the ability to violate the
// The strict "Can't Access Private Data" in C++ rules.
//    A Useless but weird program.
///////////////////////////


/////////////////
// Header Files
/////////////////
 #include <iostream.h>
 #include <string.h>


/////////////////
// The Object Class
/////////////////

 class Object {
    public:

      // The Constructor
      Object(double x) {
                   ObjectData = x;
                   strcpy(ObjectString, "Private Data Members " \
                                    "CANNOT! be accessed "  \
                                    "outside of the class.");
                   Print();
                   }
     // The Destructor
     ~Object(void) {
                 Print();
               }

    private:
     double ObjectData;
     char ObjectString[100];
     void Print(void);
} ;

 // Private Print Function
 void Object::Print(void)
{

  cout << "Inside Print() Function "  << endl
       << ObjectString               << endl
       << "Object Data = " << ObjectData << endl
       << "Exiting Print() Function" << endl << endl;

}

////////////////////////
// Main() Function
////////////////////////
 int main(void)
{
 Object N(100.5);
 void *Hehe, *Haha;

 (char *)Hehe = (char *)&N;

 while(*((double *)Hehe) != 100.5)
   ((char *)Hehe)++;

 *((double *)Hehe) = 313373.313373;

 ((double *)Hehe)++;

 Haha = Hehe;

 while(*((char *)Haha))
   ((char *)Haha)++;


 strcpy((char *)Haha,"  SDODIB");

 ((char *)Haha)++;

 for(int x = 0; x < 7; x++, ((char *)Haha)++)
   *((char *)Haha) += 37;


 ((char *)Hehe) += 27;


 for(x = 3; x; x--, ((char *)Hehe)--)
    *((char *)Hehe - 1) = *((char *)Hehe);

 return 0;
}

..-=ViKtOr=-..
0
Oliver_DornaufCommented:
1) no
2) only if the base class has one virtual function as minimum. (usualy the dtor)
class base {
 base() {;}
 virtual ~base(){;}
};

class derived {
 derived() {;}
 virtual ~derived() {;}
};

class derived* x, x2;
class base* y;

x = new derived();
y = x; // y is base

x2 = dynamic_cast <derived*>(y);
if x2 != NULL the cast was successfull
=> something like that will not work:

y = new base();
x = daynamic_cast <derived*>(y);
=> x will be NULL.

3) no
4) There is no unsigned double in C or C++ Standart. (IEEE floatingpoint numbers)












0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kesavan_sridharAuthor Commented:
Thanks for your answers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.