Mostly Asked in Google, Facebook, Microsoft, IBM, Amazon...
Salary up to 20+ Lacs
Destructor:
1. As you
know that the constructor is basically get used to initialize the newly created
object of class, but when the role of the object is get over then we call the
destructor to kill the object.
2. Like
constructor which is used to take the argument, the destructor is never used to
take the argument.
3. Since the
destructor is not going to take any argument, it never used to return any
value.
4. It will be invoked implicitly by the compiler upon exit from the program to clean up the storage that is no longer going to be get used.
Example:
#include
<iostream>
#include
<string.h>
using
namespace std;
class word
{
private:
char *str_word;
public:
word (char *s)
{
str_word = new char(strlen(s));
strcpy(str_word,s);
}
int getlen()
{
return strlen(str_word);
}
char *getword()
{
return str_word;
}
~word()
{
delete str_word;
}
};
int
main(void)
{
word *word1;
//word1
-> word::~word;
//word1="Amit";
//word1->
word;
}
Comment Your Output :-
Inheritance:
It is the
process of reusability of the code. In CPP we are having 5 types of
inheritance.
1. Single:
Here the ratio of parent and child class are always 1:1. and should have
exactly one level.
2.
Multilevel: Here the ratio of parent and child class are always 1:1. and should
have at least one level.
3.
Hierarchy: Here the concept of sibling will occur. So a single parent class may
have more than one child.
4. Multiple:
Here we are used to have more than one parent which are used to get combine to
produce the child class.
5. Hybrid: When the synonym of Hierarchy and Multiple are going to get combine than the Hybrid instance get occur.
Example:
Hierarchy
Inheritance:
#include
<iostream>
#include
<string.h>
using
namespace std;
class base
{
public:
void show()
{
cout<<"I am a base class";
}
};
class
der1:public base
{
public:
void show()
{
cout<<"Hello I am a child class";
}
};
class
der2:public base
{
public:
void show()
{
cout<<"Hello I am another child class";
}
};
int
main(void)
{
der1 d1;
d1.show();
der2 d2;
d2.show();
}
Output:
Hello I am a
child class
Hello I am another child class
Example-2
(Mind-fire solution)
(The
reference of child to base)
#include
<iostream>
#include
<string.h>
using
namespace std;
class base
{
public:
void show()
{
cout<<"I am a base class";
}
};
class
der1:public base
{
public:
void show()
{
cout<<"Hello I am a child class";
}
};
class
der2:public base
{
public:
void show()
{
cout<<"Hello I am a another child class";
}
};
int
main(void)
{
der1
d1;
// d1.show();
der2 d2;
//d2.show();
base *ptr;
ptr=&d1;
ptr->show();
ptr=&d2;
ptr->show();
}
Output:
I am a base
class
I am a base class
Note:
It is an approach where we are going to provide the same method in same scope with different nature of argument.
Example:
#include
<iostream>
using
namespace std;
void
swap(float , float);
void
swap(int * , int*);
int
main(void)
{
int x,y;
cout<<"\n Enter the value of x and y";
cin>> x>>y;
cout<<"\n The Entered value of x and y before swapping
are="<<x<<""<<y;
swap(x,y);
swap(&x,&y);
cout<<"\n The Entered value of x and y after swapping
are="<<x<<""<<y;
}
void swap(
float a, float b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swap(
int *a, int *b)
{
int *temp;
*temp=*a;
*a=*b;
*b=*temp;
}
Virtual Function:
A virtual
function let derived classes provide a different version of a base class
function.
int base::fun(int)
int
derived::fun(int)
Example:
#include
<iostream>
using
namespace std;
class base
{
public:
virtual void
show_mesg(void)
{
cout<<"I
am a base class mesg";
}
void
show()
{
cout<<"The
base class is active here";
}
};
class
derived:public base
{
public:
void
show_mesg(void)
{
cout<<"I am in a child class : ";
}
void show()
{
cout<<"The
child class is active here";
}
};
int
main(void)
{
base
*bptr; derived d;
bptr=&d; bptr->show_mesg();
// calling the virtual function bptr->show();
// calling the Non
-virtual function
}
Output:
I am in a
child class..
The base
class is active here
star 1
Go through this Page to brush up your learning and get in top most MNC's.
ReplyDeleteGood
ReplyDeleteThis questions were next level...
ReplyDeleteThanks for sharing
ReplyDelete