C++ remains best suitable programming language for systems application programming
We present few questions to unlock the tips and tricks
1.Will following code compile?
{
public:
A(int n)
{
std::cout<<” I am in parametrized constructor”<<std::endl;
}
};
int main()
{
A a;
}
Answer- Noif any of the constructor is declared inside class
default constructor must be declared otherwise code will not compile
2.Please provide output of program
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (10); //size of vector is 10
std::vector<int>::size_type sz = myvector.size();
std::cout<< sz <<std::endl;
myvector.push_back(11);
myvector.push_back(19);
std::vector<int>::size_type sz1 = myvector.size();
std::cout<< sz1 <<std::endl;
return 0;
}
Output:-
10
12
Remember always that when vector is provided size when it is initialized ,
it will initialize all the elements with zero.
Hence when additional element is pushed back it increase its size.
Hence after two push_back on vector, size of vector is 12 also notice that it considers size of int as
1 byte instead of 4( ) note-size of int on 32 bit machine is always 4
3.Does private members of base class inherited in derived class?
#include<iostream>
class A
{
private:
int a;
};
class B :public A
{
};
int main()
{
B b;
std::cout<< “size of B : ” << sizeof(b);
return 0;
}
Private members of base class are nt inherited but size in above example is 4 since derived class object also contain base class.
But base class private members can nt be accessed in derived class.
Output:-
size of B : 4