//Inserting After given node of Single Linked List
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *start=NULL;
void push(struct node** ref,int d)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*ref);
(*ref)=newnode;
}
void display(struct node *start)
{
struct node *ptr=start;
while(ptr)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
struct node* insertAfterNode(struct node *start,int d, int afternode)
{
int i;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
struct node* current=start;
while(current!=NULL)
{
if(current->data==afternode)
{
newnode->next=current->next;
current->next=newnode;
//return start;
}
current=current->next;
}
}
int main()
Singleton Design Pattern example in C++
//Singleton is a creational design pattern.
//A design pattern to provide one and only instance of an object.
//Declare the constructors of the class private.
//Declatre a static public instance of class type.
//Provide a static public method to get the instance
//If instance is NULL then only once create a object else
//return the already created instance
#include<stdio.h>
using namespace std;
class singleton
{
private:
singleton(){printf("Constructor called only once.\n");}
public:
static singleton* instance;
static singleton* getinstance();
};
singleton* singleton::instance=NULL;
singleton* singleton::getinstance()
{
if(instance==NULL)
{
instance = new singleton;//mind the syntax//no singleston s;
}
else
{
return instance;
}
}
int main()
{
//singleton s1;//singleton() is private within this context
singleton *sptr;
sptr=singleton::getinstance();
sptr=singleton::getinstance();
sptr=singleton::getinstance();
sptr=singleton::getinstance();
sptr=singleton::getinstance();
return 0;
}
/*
Constructor called only once.
*/
//A design pattern to provide one and only instance of an object.
//Declare the constructors of the class private.
//Declatre a static public instance of class type.
//Provide a static public method to get the instance
//If instance is NULL then only once create a object else
//return the already created instance
#include<stdio.h>
using namespace std;
class singleton
{
private:
singleton(){printf("Constructor called only once.\n");}
public:
static singleton* instance;
static singleton* getinstance();
};
singleton* singleton::instance=NULL;
singleton* singleton::getinstance()
{
if(instance==NULL)
{
instance = new singleton;//mind the syntax//no singleston s;
}
else
{
return instance;
}
}
int main()
{
//singleton s1;//singleton() is private within this context
singleton *sptr;
sptr=singleton::getinstance();
sptr=singleton::getinstance();
sptr=singleton::getinstance();
sptr=singleton::getinstance();
sptr=singleton::getinstance();
return 0;
}
/*
Constructor called only once.
*/
Observer Design Pattern example in C++
Observer Design Pattern example in C++
//Observer Design pattern Best Example.
//Observer Design pattern is implemented using two classes.
//First is observable or subject class: in this class even occurs and to be notified to the registered observers.
//subject class contains attachObserver(), notifyObserver(),and other task.
//Second class is observer class: class which wants to be notified when some even occurs in observable or subject class.
//observer class contains a pure viryual function notify().
//Example:
//When any event like new blog/article is available in a website it should notify to all the registered users.
//So here Blog will derived from subject class and
//users want to get notified of new articles. So User will be derived from class observer
#include<iostream>
using namespace std;
#include <vector>
#include <typeinfo>
#include <string>
class Subject;//forward declaration
class Observer//class which are interested to get notified
{
public:
virtual void notify(Subject* s) = 0;//pure virtual function
virtual ~Observer() {};
};
class Subject//Class in which even take place
{
vector<Observer *> observers;
protected:
void notify_observers()
{
vector<Observer *>::iterator iter;
for (iter = observers.begin(); iter != observers.end(); ++iter)
(*iter)->notify(this);
}
public:
virtual ~Subject() {};
void register_observer(Observer* o)//pusshing the attached observers in a vector
{
observers.push_back(o);
}
};
class Blog : public Subject
{
public:
Blog()
{
cout << "New Article Posted." << "\n";
}
void eventTriggerNewArticle()
{
cout << "The new article event is triggered." << "\n";
notify_observers();
}
int const get_Blog_id()
//int get_Blog_id()
{
return 21022015;
}
};
class User : public Observer
{
public:
virtual void notify(Subject* s)
{
Blog *a;
a = dynamic_cast<Blog*>(s);
cout << a->get_Blog_id() << "\n";
}
};
int main ()
{
Blog newArticle = Blog();
User u = User();
newArticle.register_observer(&u);
newArticle.eventTriggerNewArticle();
return 0;
}
/*OUTPUT:
New Article Posted.
The new article event is triggered.
21022015
*/
//Observer Design pattern is implemented using two classes.
//First is observable or subject class: in this class even occurs and to be notified to the registered observers.
//subject class contains attachObserver(), notifyObserver(),and other task.
//Second class is observer class: class which wants to be notified when some even occurs in observable or subject class.
//observer class contains a pure viryual function notify().
//Example:
//When any event like new blog/article is available in a website it should notify to all the registered users.
//So here Blog will derived from subject class and
//users want to get notified of new articles. So User will be derived from class observer
#include<iostream>
using namespace std;
#include <vector>
#include <typeinfo>
#include <string>
class Subject;//forward declaration
class Observer//class which are interested to get notified
{
public:
virtual void notify(Subject* s) = 0;//pure virtual function
virtual ~Observer() {};
};
class Subject//Class in which even take place
{
vector<Observer *> observers;
protected:
void notify_observers()
{
vector<Observer *>::iterator iter;
for (iter = observers.begin(); iter != observers.end(); ++iter)
(*iter)->notify(this);
}
public:
virtual ~Subject() {};
void register_observer(Observer* o)//pusshing the attached observers in a vector
{
observers.push_back(o);
}
};
class Blog : public Subject
{
public:
Blog()
{
cout << "New Article Posted." << "\n";
}
void eventTriggerNewArticle()
{
cout << "The new article event is triggered." << "\n";
notify_observers();
}
int const get_Blog_id()
//int get_Blog_id()
{
return 21022015;
}
};
class User : public Observer
{
public:
virtual void notify(Subject* s)
{
Blog *a;
a = dynamic_cast<Blog*>(s);
cout << a->get_Blog_id() << "\n";
}
};
int main ()
{
Blog newArticle = Blog();
User u = User();
newArticle.register_observer(&u);
newArticle.eventTriggerNewArticle();
return 0;
}
/*OUTPUT:
New Article Posted.
The new article event is triggered.
21022015
*/
C++ program to overload operator+ for string concatenation
//operator overloading for concatenating two strings in C++
#include<iostream>
using namespace std;
#include<string.h>
class sample
{
private:
char str[20];
public:
sample()
{
cout<<"def constructor."<<endl;
strcpy(str,"C++ Linux Code.");
}
sample(char s[])
{
cout<<"a arg constructor."<<endl;
strcpy(str,s);
}
sample operator+(const sample& ref )
{
cout<<"overloaded + called"<<endl;
sample temp;//def constructor
strcpy(temp.str,str);
strcat(temp.str,ref.str);
return temp;
}
//return *this;//seg fault
void display(){cout<<str<<endl;}
};
int main()
{
sample s1;//def constructor
s1.display();
sample s2("blogspot.com");//1 arg constructor
s2.display();
sample s3;//def constructor
s3=s1+s2;
s3.display();
//cout<<str<<endl;//str was not declared in this scope. As str is private and only mf can access
//cout<<s1.str<<endl;//aaa
//cout<<'m'<<endl;//a
return 0;
}
/*
def constructor.
C++ Linux Code.
a arg constructor.
blogspot.com
def constructor.
overloaded + called
def constructor.
C++ Linux Code.blogs
*/
#include<iostream>
using namespace std;
#include<string.h>
class sample
{
private:
char str[20];
public:
sample()
{
cout<<"def constructor."<<endl;
strcpy(str,"C++ Linux Code.");
}
sample(char s[])
{
cout<<"a arg constructor."<<endl;
strcpy(str,s);
}
sample operator+(const sample& ref )
{
cout<<"overloaded + called"<<endl;
sample temp;//def constructor
strcpy(temp.str,str);
strcat(temp.str,ref.str);
return temp;
}
//return *this;//seg fault
void display(){cout<<str<<endl;}
};
int main()
{
sample s1;//def constructor
s1.display();
sample s2("blogspot.com");//1 arg constructor
s2.display();
sample s3;//def constructor
s3=s1+s2;
s3.display();
//cout<<str<<endl;//str was not declared in this scope. As str is private and only mf can access
//cout<<s1.str<<endl;//aaa
//cout<<'m'<<endl;//a
return 0;
}
/*
def constructor.
C++ Linux Code.
a arg constructor.
blogspot.com
def constructor.
overloaded + called
def constructor.
C++ Linux Code.blogs
*/
Observer Design Pattern example in C++
Singleton Design Pattern example in C++
C++ program to overload operator+ for string concatenation
Copy constructor Overloaded assignment operator Destructor and default constructor implementation in C++
C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.
#include<iostream>
using namespace std;
#include<string.h>
class sample
{
private:
char *name;
public:
sample(){cout<<"\n Default constructor called."<<endl;}
sample(const char *p);
sample(const sample &ref);
sample& operator=(const sample &obj);
sample& operator+(const sample& obj);
~sample();
};
sample::sample(const char* p)
{
cout<<" One argument constructor called."<<endl;
name = new char[strlen(p)+1];
strcpy(name,p);
cout<<"name: "<<name<<endl;
}
sample::sample(const sample &ref)
{
cout<<"copy constructor called."<<endl;
name=new char[strlen(ref.name)+1];
strcpy(name,ref.name);
}
sample& sample::operator=(const sample &ref)
{
cout<<"overloaded = operator called.\n";
//if(this==&ref)
//return *this;
//delete[] name;//Seg fault
name=new char[strlen(ref.name)+1];
strcpy(this->name,ref.name);
return *this;
}
sample& sample::operator+(const sample &obj)
{
cout<<"overloaded + called.\n";
//name=new char[strlen(obj.name)+1];
strcat(name,obj.name);
cout<<"cancatename is: "<<name<<endl;
}
sample::~sample()
{
cout<<"Destructor called.\n";
//delete[] name;
}
int main()
{
sample s1;//def constr.//destr
sample s2("Hello");//1 arg constr.//destr
sample s3(s2);//copy constr.//destr
sample s4=s3;//copy constr.//destr
sample s5;//def constr.//destr
s5=s1;//overloaded = opr.//No destructor
s2+s3;//overloaded + called//No destr
//cout<<s2.name<<endl;//name is private and accessible only by mf
return 0;
}
/*OUTPUT:
Default constructor called.
One argument constructor called.
name: Hello
copy constructor called.
copy constructor called.
Default constructor called.
overloaded = operator called.
overloaded + called.
cancatename is: HelloHello
Destructor called.
Destructor called.
Destructor called.
Destructor called.
Destructor called.
*/
#include<iostream>
using namespace std;
#include<string.h>
class sample
{
private:
char *name;
public:
sample(){cout<<"\n Default constructor called."<<endl;}
sample(const char *p);
sample(const sample &ref);
sample& operator=(const sample &obj);
sample& operator+(const sample& obj);
~sample();
};
sample::sample(const char* p)
{
cout<<" One argument constructor called."<<endl;
name = new char[strlen(p)+1];
strcpy(name,p);
cout<<"name: "<<name<<endl;
}
sample::sample(const sample &ref)
{
cout<<"copy constructor called."<<endl;
name=new char[strlen(ref.name)+1];
strcpy(name,ref.name);
}
sample& sample::operator=(const sample &ref)
{
cout<<"overloaded = operator called.\n";
//if(this==&ref)
//return *this;
//delete[] name;//Seg fault
name=new char[strlen(ref.name)+1];
strcpy(this->name,ref.name);
return *this;
}
sample& sample::operator+(const sample &obj)
{
cout<<"overloaded + called.\n";
//name=new char[strlen(obj.name)+1];
strcat(name,obj.name);
cout<<"cancatename is: "<<name<<endl;
}
sample::~sample()
{
cout<<"Destructor called.\n";
//delete[] name;
}
int main()
{
sample s1;//def constr.//destr
sample s2("Hello");//1 arg constr.//destr
sample s3(s2);//copy constr.//destr
sample s4=s3;//copy constr.//destr
sample s5;//def constr.//destr
s5=s1;//overloaded = opr.//No destructor
s2+s3;//overloaded + called//No destr
//cout<<s2.name<<endl;//name is private and accessible only by mf
return 0;
}
/*OUTPUT:
Default constructor called.
One argument constructor called.
name: Hello
copy constructor called.
copy constructor called.
Default constructor called.
overloaded = operator called.
overloaded + called.
cancatename is: HelloHello
Destructor called.
Destructor called.
Destructor called.
Destructor called.
Destructor called.
*/
Observer Design Pattern example in C++
Singleton Design Pattern example in C++
C++ program to overload operator+ for string concatenation
Purpose of run time Polymorphism in C++/CPP.
What is the main purpose of run time polymorphism? Why run time polymorphism is used in object oriented programming like C++? What is the main reason to use polymorphism in C++? Why late binding is used in C++?
These are the most frequently asked questions in interviews for C++ walkins or face to face interviews. If you are fresher then telling the definition alone is enough but by experienced professionals it is not acceptable. So what should be the answer?
If you are able to mention a real time scenario where it is used in proper manner then your work is done.
One such example is to create array of base class pointer which holds addresses of related derived class objects. I.e. Suppose an employee management system. Employee is base class and derived classes are employees like trainee, junior software engineer, senior. Software engineer, Lead engineer, manager etc.
Employee base class has virtual/pure virtual function like salaryIncrement, empPromotion.
Each derived class has its own implementation of these function. If management wants to use salaryIncrement then run time polymorphism allows to create array of base class pointers each having address of different derived class object and in a loop with same call syntax it can increment all employees salary at once without knowing the derived class object type at compile time.
C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.
These are the most frequently asked questions in interviews for C++ walkins or face to face interviews. If you are fresher then telling the definition alone is enough but by experienced professionals it is not acceptable. So what should be the answer?
If you are able to mention a real time scenario where it is used in proper manner then your work is done.
One such example is to create array of base class pointer which holds addresses of related derived class objects. I.e. Suppose an employee management system. Employee is base class and derived classes are employees like trainee, junior software engineer, senior. Software engineer, Lead engineer, manager etc.
Employee base class has virtual/pure virtual function like salaryIncrement, empPromotion.
Each derived class has its own implementation of these function. If management wants to use salaryIncrement then run time polymorphism allows to create array of base class pointers each having address of different derived class object and in a loop with same call syntax it can increment all employees salary at once without knowing the derived class object type at compile time.
C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.
Observer Design Pattern example in C++
Singleton Design Pattern example in C++
C++ program to overload operator+ for string concatenation
C/C++ Linux Interview Questions asked in real IT companies in India
Watch this space for coming soon C++ Class object data members constructor destructor abstraction encapsulation inheritance operator over loading polymormophism run time and static morphism virtual base class virtual function virtual keyword private protected public access specified template exception handling STL default constructor copy constructor destructor overloaded assignment operator shallow vs deep constructor malloc vs new delete vs free heap and static memory dangling pointer wild pointer crash hang static dynamic reinterpret cast pointers array reference.
C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.
C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.
Subscribe to:
Posts (Atom)