Monthly Archives: May 2012

C++ Standard Template Library [Map]

Store Different Class Object to File [STL + File I/O Solution]

Problem :

I am having classes in a project like [Student, Customer and many more classes] now I have to manage [Add, Update,

Delete & Display All] all the class data/object to File. Requirement is much efficient manner + minimum overhead.

Much Efficient Solution:

1. Create the 2 Methods for Read All Data & Write All Data Method. [File => STL]

2. Create 4 Methods for Add, Update, Delete , Display. [STL]

Code:


#include
#include
#include
#include

using namespace std;

class Student;

map myData;

template
void writeData(T obj,char *fname);
template
void readData(T obj,char *fname);

void createStudent();
void displayAllStudent();
void displayIDStudent();
void deleteIDStudent();
void updateIDStudent();

//Student Class
class Student
{
int roll;
int marks;

public:

void setData();
void display();
int getID()
{
return roll;
}
};

void Student::setData()
{
cout<>roll;
cout<>marks;
}

void Student::display()
{
cout<<"\n The Roll No:"<<roll;
cout<<"\n The Marks :"<<marks;
}

//Function Definition==================================

//File I/O Add Data
template
void writeData(T obj,char *fname)
{
fstream file;
Student sobj;
map::iterator itr;

file.open(fname,ios::out|ios::binary);

for(itr=myData.begin();itr!=myData.end();itr++)
{
sobj=(*itr).second;
file.write((char *)&sobj,sizeof(sobj));
}
file.close();
}

//File I/O Display Data
template
void readData(T obj,char *fname)
{
int size=0;
int tobj=0;
int i=0;

fstream file;
file.open(fname,ios::in|ios::binary);

if(file.fail())
{
return;
}
else
{
file.seekg(0,ios::end);
size=file.tellg();
tobj=size/sizeof(obj);
file.seekg(0);

for(i=0;i<tobj;i++)
{
file.read((char *)&obj,sizeof(obj));

myData[obj.getID()]=obj;
}
file.close();
}
}

//Write Student Data to Map
void createStudent()
{
Student sobj;
sobj.setData();

myData[sobj.getID()]=sobj;
}

//Read All Student Data from Map
void displayAllStudent()
{
map::iterator itr;

cout<<"\n ============================\n";
for(itr=myData.begin();itr!=myData.end();itr++)
{
(*itr).second.display();
}
cout<<"\n ============================\n";
}

//Reall Student Data from Map base on ID.
void displayIDStudent()
{
int id;
cout<>id;

map::iterator itr;

for(itr=myData.begin();itr!=myData.end();itr++)
{
if(id==(*itr).second.getID())
{
(*itr).second.display();
}
}
}

//Delete Student Data from Map base on ID.
void deleteIDStudent()
{
int id;
Student sobj;
int tid;

cout<>id;

map::iterator itr;

for(itr=myData.begin();itr!=myData.end();itr++)
{
if(id==(*itr).second.getID())
{
sobj=(*itr).second;
tid=sobj.getID();
sobj.display();

myData.erase(tid);
}
}
}

//Update Student Data from Map base on ID.
void updateIDStudent()
{
int id;
Student sobj;
int tid;

cout<>id;

map::iterator itr;

for(itr=myData.begin();itr!=myData.end();itr++)
{
if(id==(*itr).second.getID())
{
(*itr).second.display();

cout<<"\n ========= Enter New Data ==========\n";

sobj.setData();

(*itr).second = sobj;
}
}
}

//Main Method
int main()
{
int choice;
Student sobj;

int end=0;

readData(sobj,"suser.dat");

while(1)
{
cout<<"\n ============================\n";

cout<<"\n 1. Create Student.";
cout<<"\n 2. Display Student ID";
cout<<"\n 3. Display All Student";
cout<<"\n 4. Update Student ID";
cout<<"\n 5. Delete Student ID";

cout<<"\n\n 0. Exit";

cout<<"\n ============================\n";

cout<>choice;

switch(choice)
{
case 1:
createStudent();
break;
case 2:
displayIDStudent();
break;
case 3:
displayAllStudent();
break;
case 4:
updateIDStudent();
break;
case 5:
deleteIDStudent();
break;

case 0:
writeData(sobj,"suser.dat");
end=1;
break;
}

if(end==1)
{
break;
}
}
return 0;
}

Leave a comment

Filed under C++, Standard Template Library

Socket Programming (UDP) using C:

Socket Programming User Datagram Protocol (UDP):
-Please have look at [TCP Programming in C – youtube.com/vinodthebest]
-Server & Client Concept.
-Major steps.
-Server:
=> Create a socket with the socket()
=> Bind the socket to an address using the bind().
=> Send and receive data, use the recvfrom() and sendto() system calls.

-Client:
=> Create a socket with the socket()
=> Send and receive data, use the recvfrom() and sendto() system calls.

-Server coding.
-Client coding.
-Running.

Coding

Server:


/* Creates a datagram server. The port number is passed as an argument. This server runs forever */

#include
#include
#include
#include
#include

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sock, length, fromlen, n;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[1024];

if (argc < 2)
{
fprintf(stderr, "ERROR, no port provided\n");
exit(0);
}

sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
error("Opening socket");
}
length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(argv[1]));
if (bind(sock,(struct sockaddr *)&server,length)<0)
{
error("binding");
}
fromlen = sizeof(struct sockaddr_in);
while (1)
{
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
if (n < 0)
{
error("recvfrom");
}
write(1,"Received a datagram: ",21);
write(1,buf,n);
n = sendto(sock,"Got your message\n",17,
0,(struct sockaddr *)&from,fromlen);
if (n < 0)
{
error("sendto");
}
}
}

Client:

/* UDP client in the internet domain */

#include
#include
#include
#include
#include
#include

void error(char *);

int main(int argc, char *argv[])
{
int sock, length, n;
struct sockaddr_in server, from;
struct hostent *hp;
char buffer[256];

if (argc != 3)
{
printf("Usage: server port\n");
exit(1);
}

sock= socket(AF_INET, SOCK_DGRAM, 0);

if (sock h_addr, (char *)&server.sin_addr,hp->h_length);
server.sin_port = htons(atoi(argv[2]));
length=sizeof(struct sockaddr_in);

printf("Please enter the message: ");

bzero(buffer,256);
fgets(buffer,255,stdin);
n=sendto(sock,buffer,strlen(buffer),0,&server,length);
if (n < 0)
{
error("Sendto");
}
n = recvfrom(sock,buffer,256,0,&from, &length);
if (n < 0)
{
error("recvfrom");
}
write(1,"Got an ack: ",12);
write(1,buffer,n);
}

void error(char *msg)
{
perror(msg);
exit(0);
}

6 Comments

Filed under C Network Programming, C Programming

Socket Programming (TCP) using C:

Socket Programming:
-Minimum Requirement.
-Linux OS [Lib. Header Files]
-IP & Port No.
-Server & Client Concept.
-Major steps.
-Server:
=Create a socket with the socket()
=Bind the socket to an address using the bind().
=Listen for connections with the listen().
=Accept a connection with the accept().
=Send and receive data, use the read() and write() system calls.

-Client:
=Create a socket with the socket()
=Connect the socket to the address of the server using the connect() system call.
=Send and receive data, use the read() and write() system calls.
-Server coding.
-Client coding.
-Running.

Coding:

Server :

/* A simple server in the internet domain using TCP the port number is passed as an argument */

#include
#include
#include
#include

void error(char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{

int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;

if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
{
error("ERROR opening socket");
}

bzero((char *) &serv_addr, sizeof(serv_addr));

portno = atoi(argv[1]);

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
}

listen(sockfd,5);
clilen = sizeof(cli_addr);

newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

if (newsockfd < 0)
{
error("ERROR on accept");
}

bzero(buffer,256);

n = read(newsockfd,buffer,255);

if (n < 0)
{
error("ERROR reading from socket");
}

printf("Here is the message: %s\n",buffer);

n = write(newsockfd,"I got your message",18);

if (n < 0)
{
error("ERROR writing to socket");
}
return 0;
}

Client :


#include
#include
#include
#include
#include

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];

if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}

portno = atoi(argv[2]);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);

serv_addr.sin_port = htons(portno);

if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR connecting");
}
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
{
error("ERROR writing to socket");
}
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
{
error("ERROR reading from socket");
}
printf("%s\n",buffer);

return 0;
}

17 Comments

Filed under C Network Programming, C Programming

C++ Template Sorting

Sorting using Template:
-Overview of Need of Template
-Template Sort values.
-Template Swap values.
-Tempalte Dispaly values.
-Sorting Integer Array.
-Float Array.
-Student Array.


Code :

#include
#include

using namespace std;

template
void myBubbleSort(T arr[],int size)
{
for(int i=0;i<size-1;i++)
{
for(int j=i+1;jarr[j])
{
mySwap(arr[i],arr[j]);
}
}
}
}

template
void mySwap(T &val1,T &val2)
{
T temp=val1;
val1=val2;
val2=temp;
}

template
void display(T arr[],int size)
{
cout<<"\n";
for(int i=0;i<size;i++)
{
cout<<"\t "<<arr[i];
}
cout<<"\n";
}

class Student
{
int roll;
int marks;
string name;

public:

Student()
{

}

Student(int troll,string tname,int tmarks)
{
roll=troll;
name=tname;
marks=tmarks;
}

void setData()
{
cout<>roll;
cout<>name;
cout<>marks;
}

void display()
{
cout<<"\n Entered Roll No:"<<roll;
cout<<"\n Entered Name :"<<name;
cout<<"\n Entered Marks :"<(Student obj1,Student obj2)
{
int rel=0;

if(obj1.roll>obj2.roll)
{
rel=1;
}
return rel;
}

friend ostream & operator<<(ostream & ccout,Student & obj)
{
ccout<<"\n Entered Roll No:"<<obj.roll;
ccout<<"\n Entered Name :"<<obj.name;
ccout<<"\n Entered Marks :"<<obj.marks;

return ccout;
}
};

int main()
{
cout< Interger Array :\n";

int arr[5]={4,5,2,1,10};

display(arr,5);

myBubbleSort(arr,5);

display(arr,5);

cout< Float Array :\n";

float arr1[5]={10.5,2.6,7.8,9.1,9.5};

display(arr1,5);

myBubbleSort(arr1,5);

display(arr1,5);

cout< Student Array :\n";

Student arr2[3]={Student(5,"Vikas",20),Student(10,"Vikram",50),Student(1,"Anil",90)};

display(arr2,3);

myBubbleSort(arr2,3);

display(arr2,3);

return 0;
}

Leave a comment

Filed under C++

What is Abstraction in OOP?

Abstraction is one of the OOPS features. Abstraction means to show only the necessary details without including the background details. Class uses the concept of abstraction. Example of abstraction is class.

Simple words:

When we create the class we include methods inside the class which will perform some complex operations as per our requirements so whenever we would like to do those operations we simple call the methods by passing necessary arguments if required and it does all the work for us, now this we all know but this is abstraction.

Example:

1. I mean suppose we have created a Bank class and its having one of the methods called getInterest(accno) which will return the interest of particular user. Now whenever we would like to know the interest of particular account holder will simple call this methods, we are not interested how the interest is calculated + what all operations or formulas its using and all we are simple interested in the result that is to [show only the necessary details without including the background details].

2. When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed thats it!! This is abstraction; show only the details which matter to the user.

So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This certainly helps in reusability of the code.

5 Comments

Filed under C++

Servlet : Hello World

Developing Simple Web Application (Java)
==================================
1. Software Done

2. Install so first we will install java

3. We will unzip the tomcat

4. We have to do some configuration.

5. We will develop an application

Leave a comment

Filed under Servlet & JSP

C++ : Type Conversions

Type Conversions:
=============================

Three types of Conversions:

1. Basic to Class. [obj=100] [Constructor]
2. Class to Basic. [int rel; rel=obj] [Operator Overloading]
3. Class to Class. [stuobj=empobj] [Constructor (Destination) & Operator Overloading(Source)]

============================================================================================


#include
#include

using namespace std;

class A
{
int rel;

public:

A()
{
rel=0;
}

//Basic to Class Conversion
A(int trel)
{
rel=trel;
}
void set()
{
cout<>rel;
}
void display()
{
cout<<"\n The rel value is:"<<rel<<"\n";
}

//Class to Basic Conversion
operator int()
{
int value=0;

value=rel+100;

return value;
}
friend class B;
};

class B
{
int rel1;
int rel2;

public:

B()
{
rel1=0;
rel2=0;
}

/*
//Destination Constructor.
B(A tobj)
{
rel1=tobj.rel/2;
rel2=tobj.rel/2;
}*/

void set()
{
cout<>rel1;
cout<>rel2;
}
void display()
{
cout<<"\n The Rel 1 is:"<<rel1<<"\n";
cout<<"\n The Rel 2 is:"<<rel2<<"\n";
}

//Source Operator
operator A()
{
A tobj;
tobj.rel=rel1+rel2;

return tobj;
}
};

int main()
{

int result=100;

A obj;
obj.set(); //obj.rel=200
obj.display();

//Calling - Basic to Class Conversion
obj=result;

cout<<"\n Basic to Class conversion result is:";
obj.display();

cout<<"\n============================================";

//Calling - Class to Basic Conversion
result=obj;

cout<<"\n Class to Basic conversion result is:"<<result<<"\n";

cout<<"\n============================================";

//Class to Class Conversion

cout<<"\n\n Class A Details:";
A aobj;
aobj.set(); //100
aobj.display(); //100

cout<<"\n\n Class B Details:";
B bobj;
bobj.set(); //200,300
bobj.display(); //200,300

/*
//Destination=Source
bobj=aobj;
bobj.display();

*/

aobj=bobj; //200+300
aobj.display(); //500

return 0;
}

2 Comments

Filed under C++