20130330

How to write cpp program to reverse a linked list



//C++ program for reversing single Linked list nodes 
Cpp program for implement reversing Single Linked List
Single linked list program for reversing the nodes
How to reverse linked list 
#include<iostream>
using namespace std;

struct node
{
node *next;
int data;
};
class ll
{
private:
node *start;
public:
ll(){start=NULL;}

void insert(int a)
{
cout<<"Inside Insert."<<endl;
node *newnode=new node;
newnode->data=a;
newnode->next=start;
start=newnode;
//return;
}
void display();
void del();
void reverse();
};

How to write sorted linked list program in cpp with class


How to write sorted linked list program in cpp with class
C++ Program for sorted single linked list with class
//sorted linked list latest
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *start=NULL;

void insert(int d)
{
node *newnode=new node;
newnode->data=d;
newnode->next=NULL;
//insert as first elt
if(start==NULL)
{

How to see vtable virtual table for virtual functions in cpp


Virtual table is look up table for virtual functions. It contains the function pointer to the virtual functions. Virtual table is created for each class having one or more virtual function and classes derived from that class.virtual pointer better known as vptr containing the address of vtable is created for the most base class thats why size of empty class with virtual function is equal to the size of a pointer(8 bytes for a 64 bit computer)while size of a empty class without any virtual functions is only one byte.
Command to see vtable/virtual table of a class.
Here if the name of the program is "classWithVf.cxx" so the command to see the vtable of the class is:

g++ -fdump-class-hierarchy classWithVf.cxx


#include<iostream>
using namespace std;

class M //empty class
{
public:
void f() {}
};
class A //empty class
{

20121022

Constructors in C++/Copy Constructor in CPP

Constructors in C++/Copy Constructor in CPP:
What is constructor?
Constructor is a member function whose task is to initialize the object of a class and  which is automatically gets called when object of that class gets created.
Why do we need constructor?
we need constructor to initialize the objects of class at the time of their creation.
What is Copy Constructor?
Copy Constructor is a constructor which gets called when an object is passed by value as argement or when an object is returnrd by value by a function or when an object is initialize by another object.
Why do we need Copy Constructor?
We need copy constructor to avoid shallow or member wise copy done by the the compy constructor provided by compiler.
for example:

Previous                             Home                               Next

20121020

How to use template in cpp

How to use template in C++:
Template is mechanism in C++ to generate fanily of
 generic classes and generic functions that works with
different types of data types hence eliminate duplicate
 program for diffrent data types and make the program
 more manageable and easier.

C++ Program for 1 argument template class

#include<iostream.h>
#include<string.h>
template <class T>
class test
{
T t;
public:
test(T x)//will work with int, float,char,string
{
t=x;
}
 void show(void)
 {
  cout<<"t= "<<t<<endl;
 }
};
//void test< T>::show(void)
//{
//cout<<"t"<<t<<endl;
//}
int main()
{
test<int>t1(10); //calls int version of test(T t)
t1.show();
test<float>t2(1.0);  //calls float version of test(T t)
t2.show();
test<char>t3('m');   //calls char version of test(T t)
t3.show();
test<char *>t4("HelloTemplate"); // calls string version of test(T t)
t4.show();
return 0;
}


C++ Program for 1 argument template function

#include<iostream.h>
//using namespace std
template <class T>
void display(T a)
{
cout<<a<<endl;
}
int main()
{
display(10);
display(20.12);
display('M');
display("MamaMeaa");
return 0;

}



Previous                             Home                               Next