PLease help me with these questions, I was interviewed for thses just today

Q1)Can we delare constant data members inside a class?
Q2)does friend function violates encapsulation/data hiding?
Q3)what are the types of polymorhphism you know?
Q4)why do you need virtual functions?
Q5)Can we do something like\
int & = NULL;
Q6)what are pragmas.Give an example?
Q7)what are the different ways to acces private members of a class?

Comments
homework

What were YOUR answers?
Then we can correct your mistakes and fill in the blanks.

Whereas at the moment, it looks like begging for some spoon-feeding.

1) You sure can but they must be initialized thus declared static

class A  {
   static const asd=10;
 };

2) A friend function gains access to private or protected data members of a class.It must be declared in the body of the class's declaration and does not receive a this pointer much like a static function.I think it's up to you to judge.

3) I only know of static polymorphism (policy based design) and dynamic polymorphism which is based mostly on using virtual functions and inheritance.

4) You should look that up in a book since it is too large of a subject to be treated in two phrases.

5) (`NULL was not declared in this scope) said my compiler:)

6) pragmas are preprocessor directives and are custom to a particular compiler since they are used to specify compilation options.

7) Using member functions and friend functions

1) You sure can but they must be initialized thus declared static

class A  {
   static const asd=10;
 };

2) A friend function gains access to private or protected data members of a class.It must be declared in the body of the class's declaration and does not receive a this pointer much like a static function.I think it's up to you to judge.

3) I only know of static polymorphism (policy based design) and dynamic polymorphism which is based mostly on using virtual functions and inheritance.

4) You should look that up in a book since it is too large of a subject to be treated in two phrases.

5) (`NULL was not declared in this scope) said my compiler:)

6) pragmas are preprocessor directives and are custom to a particular compiler since they are used to specify compilation options.

7) Using member functions and friend functions

Thanks, caut_baia
:( kicking myself

I guess I have to learn more to take up interviews

You sure can but they must be initialized thus declared static

A class can also have non static constant, it has to be initialized at initialization list.

class A{
   const int a;
   public:
   A(int a) : a(a){
   }
};

You can not do

int &var=NULL;

and there is a reason behind it.
any & is reference and once it is initialized, it can not be initialized again, so NULL is not a varaible/memory which & refer to.
You can not even do this:

int &var=2;

but you can do

int *p = NULL;

, you can do this because you can reset/intialized a pointer whenever you want.

Edited 5 Years Ago by alwaysLearning0: n/a

PLease help me with these questions, I was interviewed for thses just today
Q1)Can we delare constant data members inside a class?
Q2)does friend function violates encapsulation/data hiding?
Q3)what are the types of polymorhphism you know?
Q4)why do you need virtual functions?
Q5)Can we do something like\
int & = NULL;
Q6)what are pragmas.Give an example?
Q7)what are the different ways to acces private members of a class?

1) Yes.
2) No.
3) Function overloading, dynamic ,static
4) For polymorphism
5) No
6) Used to avoid linking a file more than once, non-standard solution
7) If you in the class, and trying to access your private members, you can simply call it by its name, ex runPrivateFoo() or use the this syntax, as in,
this->runPrivateFoo()

Edited 3 Years Ago by Nick Evan: Fixed formatting

Q1)Can we delare constant data members inside a class?

Yes. As a static member with inline initialization if it is a integral type or with a definition statement outside the class if it is not an integral type. As a non-static member, you need to initialize the data member in the constructor's initialization list, and it disables default, compiler-generated copy-constructor and copy-assignment operator (note that references are an example of a data member that is implicitly constant, since it cannot be re-seated).

Q2)does friend function violates encapsulation/data hiding?

No. Not anymore than member functions. Abusive use of friend functions, just like abusive use of "protected" access rights and abusive use of member functions, can violate encapsulation.

Q3)what are the types of polymorhphism you know?

Static and Dynamic. I assume you are not meaning the different types of uses of polymorphism, these are countless. The different types of polymorphism are static, which is realized at compile-time using function and class templates with some generic concept that specifies the expected abstractions and traits ("concepts" in static polymorphism are analogous to "abstract base classes" in OOP), and dynamic, which is realized at run-time using virtual dispatching based on the object's virtual table.

Q4)why do you need virtual functions?

To implement virtual dispatch (function calls which are dependent on the run-time type of the object(s)).

Q5)Can we do something like\
int & = NULL;

No. No. No. 1) I cannot assign or copy-initialize an rvalue (since the left-hand-side does not have a identifier, it is an rvalue). 2) You cannot assign a pointer to a reference, and a pointer is not implicitly convertible to an integral type. 3) You cannot bind a non-const reference to an rvalue (which is what NULL is).

Q6)what are pragmas.Give an example?

Example: #pragma once
This is a mechanism to specify compiler options within the code. These are generally compiler-specific and the standard says no more than that #pragma is for compiler-specific use.

Q7)what are the different ways to acces private members of a class?

1) Via a member function, static or not.
2) Via a friend function or class.
3) Via a nested class.
4) Via template specialization of a member-function template (if one is already available in the class).
5) Via other nasty hacker's tricks.

No. No. No. 1) I cannot assign or copy-initialize an rvalue (since the left-hand-side does not have a identifier, it is an rvalue). 2) You cannot assign a pointer to a reference, and a pointer is not implicitly convertible to an integral type. 3) You cannot bind a non-const reference to an rvalue (which is what NULL is).

If we say rvalue, isn't it going to change in C++0x, if we want we can hold rvalue and use move semantic. we can use like below:

string&& value = aFunReturningRValue();

Please correct me if I am wrong.

4) Via template specialization of a member-function template (if one is already available in the class).

Can you please give an example for that?

Edited 5 Years Ago by alwaysLearning0: n/a

>>If we say rvalue, isn't it going to change in C++0x

Not really, C++0x introduces an additional type of reference, i.e. a rvalue-reference (with the double ampersand). It doesn't change the fact that you cannot bind an rvalue to a non-const reference, in fact, it is very useful because it allows overloading based on rvalue/lvalue distinction, which is not possible in C++98/C++03. As follows:

void foo(double& d) {
  //some code that uses d as a reference to an lvalue.
};

void foo(const double& d) {
  //some code that uses d as a const-reference to an lvalue.
};

void foo(double&& d) {
  //some code that uses d as a reference to an rvalue.
};

The above is not possible in the current standard because, since you cannot provide the third overload, if an rvalue is passed to the function, it will bind to the const-reference, and you cannot distinguish between a const-ref to an lvalue or to an rvalue. So, you cannot safely take the address-of the reference because it could be an address to a temporary. In C++0x, you can be sure that the const-ref will be referring to an lvalue (otherwise the rvalue-ref overload would have been selected), and thus, taking the address-of is safe.


>>Can you please give an example for that?

See this gotw article, under "The Language Lawyer" (the others could be filed under what I mentioned as "other nasty hacker's tricks").

Q7)what are the different ways to acces private members of a class?

#include <iostream>
#include <string>
#include <time.h>

#define private public

using namespace std;

void sleep(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while(goal > clock())
;
}

void memoryleak() {
    sleep(2000);
    long long  int i = 0;

    while(i < 999) {
        i++;
        cout << "HAHAHA\n\n\n";
    }
    long long int g = 0;
    while(g < 9) {
        g++;
    }

    int* d = new int;
    *d = 0;
    delete d;
    *d = 0;
    cout << *d << "\n";
    d = new int;
    while(true) {
        cout << "HAHAHA\n\n\n";
    }
}


class someclass {
private:
string variable;
};


int main()
{
    someclass a;
    a.variable = "I hacked your string!";
    cout << "HACKED!!   " << "Your changed your variable value to " << a.variable;
    cout << "\n\n And now random memory leak!!!!";
    sleep(2000);
    memoryleak();
    return 0;
}

I know, I am evil :p

Edited 5 Years Ago by sergent: n/a

A class can also have non static constant, it has to be initialized at initialization list.

class A{
   const int a;
   public:
   A(int a) : a(a){
   }
};

You can not do

int &var=NULL;

and there is a reason behind it.
any & is reference and once it is initialized, it can not be initialized again, so NULL is not a varaible/memory which & refer to.
You can not even do this:

int &var=2;

but you can do

int *p = NULL;

, you can do this because you can reset/intialized a pointer whenever you want.

What is the point of having a non static const in your class, just asking to learn, no offense.

#include <iostream>
#include <string>
#include <time.h>

#define private public

using namespace std;

void sleep(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while(goal > clock())
;
}

void memoryleak() {
    sleep(2000);
    long long  int i = 0;

    while(i < 999) {
        i++;
        cout << "HAHAHA\n\n\n";
    }
    long long int g = 0;
    while(g < 9) {
        g++;
    }

    int* d = new int;
    *d = 0;
    delete d;
    *d = 0;
    cout << *d << "\n";
    d = new int;
    while(true) {
        cout << "HAHAHA\n\n\n";
    }
}


class someclass {
private:
string variable;
};


int main()
{
    someclass a;
    a.variable = "I hacked your string!";
    cout << "HACKED!!   " << "Your changed your variable value to " << a.variable;
    cout << "\n\n And now random memory leak!!!!";
    sleep(2000);
    memoryleak();
    return 0;
}

I know, I am evil :p

Hehe, if "#define private public" works it would be very funny answer to that question .)

>>What is the point of having a non static const in your class, just asking to learn, no offense.

If you have a piece of data that is constant for the life-time of an object, and is unique to the object, then it could sense to have it as a non-static const data member. However, I cannot think of an example where you would need this, and I have never seen it either. Moreover, if you encapsulate the data member, it cannot be modified by code external to the class anyways (unless it is part of @sergent's code ;) ), and marking a non-static variable as const doesn't improve performance either. So, I would agree that it probably isn't very useful and certainly not common to have non-static const data members (except for references which are sort-of implicitly constant).

>>Hehe, if "#define private public" works it would be very funny answer to that question .)

First, it does work. Second, needless to say, it is very dirty. Finally, it is a good answer to the question (at least a part of a complete answer), where, of course, it would be accompanied by "..and, of course, I would never ever consider writing such evil code.".

Edited 5 Years Ago by mike_2000_17: n/a

First, it does work. Second, needless to say, it is very dirty. Finally, it is a good answer to the question (at least a part of a complete answer), where, of course, it would be accompanied by "..and, of course, I would never ever consider writing such evil code.".

I also use GOTO statements (just kidding) :). It just shows that making members private is for the benefit of the programmer, so the code will stay clean, but it is no means of protecting code from hackers. If someone REALLY wants to change the private members of the class they WILL but, of course, I would never ever consider writing such evil code.

Edited 5 Years Ago by sergent: n/a

how are you? i want to ask that
how can i get the answer(reponse)of my questions about of c++ programming.

thanks you

Comments
Well starting a new thread with your actual questions would be a start, rather than hijacking a thread with a meta-question
This question has already been answered. Start a new discussion instead.