Q – Below C++ program’s expected output is 10, but gives 5. What’s wrong?
class A{
int a;
public:
A(){
this->a = 5;
}
void setValue(int a){
this->a = a ;
}
int getValue(){
return a;
}
};
class B{
A ob;
public:
void foo(A ob){
this->ob = ob;
ob.setValue(10);
}
};
int main(){
A ao;//Line-1
B bo;
bo.foo(ao); //Line-2
cout << "value = " << ao.getValue()<< '\n';
return 0;
}
Answer:This program has pass by reference issue.
Explanation: In class A, value of variable a has been assigned with 5 in A’s constructor. So, when we create an object of class A( Line-1), the value get initialized with 5. At Line-2 in main() C++ program, object of class A is being passed to class B function foo()as a pass by value.So,function foo() is having local copy of class A object inside it and not referring to the same A object which is in main() program. So, setting value of variable a is not reflecting to calling method main();
To get it reflected in calling method, we have to pass class A object by reference.So, the function foo() can point to the same object of main() function. For example,
class B{
A ob;
public:
//pass class A object as a reference
void foo(A& ob){
this->ob = ob;
ob.setValue(10);
}
};
Then output will be printed as 10.
Q – In below C++ program, explain memory allocation and de-allocation mechanism for Student and Teacher class in main () program
class Student{
public:
void* operator new(size_t num)
{
int *p = NULL;
cout<<"Custom allocation"<<"\n";
return p;
}
void operator delete(void *ptr)
{
cout<<"Custom delete"<<"\n";
}
};
class Teacher{
};
int main(){
Student *s = new Student();
delete s;
Teacher *t = new Teacher();
delete t;
return 0;
}
Output:
Custom allocation
Custom delete
Explanation:
Note that if we overload new and delete operator in a class and if we dynamically create objects of the class using new operator or de allocate it using delete operator, new and delete overloaded functions will be called. Means, program will not request memory to system but to overloaded new and delete function of that particular class.
In above C++ program, on creation and deletion of object of the class Student (Student *s = new Student();delete s;, the overloaded new and delete function of the class Student will be called for memory allocation and de-allocation respectively.
And on creation and deletion of object of the class Teacher, Teacher *t = new Teacher(); delete t; memory allocation and deletion will be requested to the Operating System.
Q – Complete the program below. Pass a null pointer to a function and get data into it from function in C++.
Complete step-1, 2 and 3 in below program.
void GetData( /* Step -2: write parameter*/){
string str = "Hello World";
//Step -3
//Allocate memory and wrtie string data into it.
}
int main(){
char * data = NULL;
//Step -1
//pass pointer in this function parameter
GetData();
printf("Data:%s",data);
return 0;
}
Answer: Below is the complete program. Basically, this is one of the c++ pointer questions, interviewer wants to test the concept of pointer, specially about pointer to pointer. This is what below implementation includes.
#include
using namespace std;
void GetData( char** data){
string str = "Hello World";
//Step -3
//Allocate memory and wrtie str into it.
*data = new char[ str.length()+1];
//copy data
strcpy(*data, str.c_str());
}
int main(){
char * data = NULL;
//Step -1
//pass pointer in this function
GetData( &data );
printf("Data:%s",data);
return 0;
}