Data Structures Remaining Experiments
#include <iostream>
using namespace std;
/********** To display Constructor in cpp **********/
class foo{
public:
foo(){
cout<<"Hola, This is the Constructor!";
}
};
int main() {
cout<<"Now you will see the Constructor...\n";
foo obj;
return 0;
}
#include <iostream>
using namespace std;
/********** To show the concept of Function Overloading **********/
void print(){
cout<<"This is Himesh Singh Chauhan!\n";
}
template<typename foo,typename...buzz>
void print(foo var1,buzz...var2){
cout<<var1<<" ";
print(var2...);
}
/********** Another Example for Function Overloading **********/
void sum(int a,double b){
cout<<"First sum func:-\n";
cout<<"The sum is "<<a+b<<endl;
}
void sum(double a,int b){
cout<<"Second sum func:-\n";
cout<<"The sum is "<<a+b<<endl;
}
int main() {
print(1.2,123,"Buzz","Aadi","Chini");
sum(1,2.0);
sum(1.0,2);
return 0;
}
#include <iostream>
using namespace std;
/********** To display Destructor in cpp *************/
class foo{
public:
foo(){
cout<<"Hola, This is the Constructor!\n";
}
~foo(){
cout<<"Hilo, This is the Destructor!\n";
}
};
int main() {
cout<<"Now you will see the Constructor alongwith Destructor...\n";
foo obj;
return 0;
}
#include <iostream>
using namespace std;
/********** To display Copy Constructor in cpp ***********/
class foo{
public:
foo(){
cout<<"Hola, This is the Constructor!\n";
}
foo(foo& obj){
cout<<"Halo, This is the Copy Constructor\n";
}
~foo(){
cout<<"Hilo, This is the Destructor!\n";
}
};
int main() {
cout<<"Now you will see the Constructor alongwith Destructor "
"alongwith Copy Constructor...\n";
foo obj1,obj2(obj1);
return 0;
}
#include <iostream>
using namespace std;
/********** To display Friend Function in cpp ***********/
class foo{
int a=12;
public:
foo(){
cout<<"Hola, This is the Constructor!\n";
}
foo(foo& obj){
cout<<"Halo, This is the Copy Constructor\n";
}
~foo(){
cout<<"Hilo, This is the Destructor!\n";
}
friend void buzz(foo& obj);
};
void buzz(foo& obj){
// A Friend can access the private variables...beware...
cout<<"The value of the private variable a is "<<obj.a<<endl;
cout<<"A Lonely Friend Function.\n";
}
int main() {
cout<<"Now you will see the Constructor alongwith Destructor "
"alongwith Copy Constructor...\n";
foo obj1,obj2(obj1);
buzz(obj1);
return 0;
}
#include <iostream>
using namespace std;
/********** To show Virtual Base Class concept **********/
class final;
class make_final{
friend class final;
make_final(){}
};
// Base class inherited virtually
class final:virtual make_final{
public:
final(){
cout<<"This is the constructor of the final class!\n";
}
};
// No class will be able to inherit final class
class buzz:public final{};
int main() {
final obj;
// Will give Error
// buzz obj1;
return 0;
}
#include <iostream>
using namespace std;
/********** To show the concept of Virtual Function **********/
class buzz{
public:
virtual void greet(){
cout<<"Good Night!\n";
}
};
class bar:public buzz{
public:
void greet(){
cout<<"Good Morning!\n";
}
};
int main() {
buzz* obj1;
bar obj2;
obj1=&obj2;
obj1->greet();
return 0;
}
#include <iostream>
using namespace std;
/********** To show the overloading of + operator **********/
class buzz{
public:
int a;
// Using the initialisation list feature
buzz(int n):a(n){}
buzz operator +(buzz& obj){
cout<<"Inside the + overloading function!\n";
return buzz(a+obj.a);
}
};
int main() {
buzz obj1(12);
buzz obj2(13);
buzz new_obj=obj1+obj2;
cout<<new_obj.a<<endl;
return 0;
}
#include <iostream>
using namespace std;
/************* To show the overloading of - operator **********/
class buzz{
public:
int a;
// Using the initialisation list feature
buzz(int n):a(n){}
buzz operator -(buzz& obj){
cout<<"Inside the - overloading function\n";
return buzz(a-obj.a);
}
};
int main() {
buzz obj1(12);
buzz obj2(13);
buzz another_new_obj=obj1-obj2;
cout<<another_new_obj.a<<endl;
return 0;
}
#include <iostream>
using namespace std;
/********** To show the concept of multiple inheritance **************/
class foo{
public:
int a=12;
};
class buzz{
public:
int b=23;
};
class derived:foo,buzz{
public:
void bar(){
cout<<a<<" "<<b<<endl;
}
};
int main() {
derived obj;
obj.bar();
return 0;
}
#include <iostream>
using namespace std;
/********** To show the concept of multilevel inheritance **************/
class foo{
public:
int a=12;
};
class buzz:public foo{
public:
int b=23;
};
class fizz:public buzz{
public:
void bar(){
cout<<a<<" "<<b<<endl;
}
};
int main() {
fizz obj;
obj.bar();
return 0;
}
class buzz{
public:
int b=23;
};
class derived:foo,buzz{
public:
void bar(){
cout<<a<<" "<<b<<endl;
}
};
int main() {
derived obj;
obj.bar();
return 0;
}
Comments
Post a Comment