Monday, April 30, 2012

Time Class Program in C++ using operator overloading to add two different Time values

The following program is an implementation of operator overloading in C++ to add two time values in the format HH:MM:SS to the resulting time along with rounding off when 24 hours is reached.
A Time class is created and Operator + is overloaded to add the two Time Class Objects.


The complete source code in C++ to Implement Time class is given below
 #include<iostream.h>  


 #include<conio.h>  
 class time  
 {  
 public:  
 time()  
 {  
 hr=0;  
 min=0;  
 sec=0;  
 }  
 int hr,min,sec;  
 void read()  
 {  
 cout<<"Hours=";  
 cin>>hr;  
 cout<<"\nMinutes=";  
 cin>>min;  
 cout<<"\nSeconds=";  
 cin>>sec;  
 }  
 time operator +(time t2)  
 {  
 time t3;  
 t3.sec=sec+t2.sec;  
 if(t3.sec>60)  
 {  
 t2.min+=1;  
 t3.sec-=60;  
 }  
 t3.min=min+t2.min;  
 if(t3.min>60)  
 {  
 t2.hr+=1;  
 t3.min-=60;  
 }  
 t3.hr=hr+t2.hr;  
 return t3;  
 }  
 void display()  
 {  
 if(hr>=24)  
 { hr=hr%24;  
 }  
 if(hr<10)  
 { cout<<"0"<<hr;  
 }  
 else  
      cout<<hr;  
 if(min<10)  
 { cout<<":0"<<min;  
 }  
 else  
      cout<<":"<<min;  
 if(sec<10)  
 { cout<<":0"<<sec;  
 }  
 else  
      cout<<":"<<sec;  
 }  
 };  
 void main()  
 {  
 clrscr();  
 time c1,c2,c3;  
 cout<<"\n\nEnter the First Time\n\n";  
 c1.read();  
 cout<<"\n\nEnter the Second Time\n\n";  
 c2.read();  
 c3=c1+c2;  
 cout<<"\n\nFirst Time  \t\t";  
 c1.display();  
 cout<<"\n\nSecond Time  \t\t";  
 c2.display();  
 cout<<"\n\nAfter addition,the time is ";  
 c3.display();  
 getch();  
 }  


Wednesday, March 14, 2012

Java Program -1 way Client-Server Communication using TCP/IP



In java Networking is done using Sockets and ServerSockets.To get a good idea of how sockets are used in java for creating a client server model see the article. http://c-madeeasy.blogspot.com/2012/03/concept-of-sockets-and-networking-in.html

TCP/IP(Transmission Control Protocol/Internet Protocol) :
Is connection based protocol that is widely used over the internet.It is commonly referred to as IP.The following program uses TCP/IP to communicate.


After reading the above article,see the code below.You can see how socket and server sockets are used to create a one way client server program.Here a client can communicate with the server only.Both are DOS/Terminal Java Programs.

Fist you start the server program followed by client program in two separate terminals.In the client terminal type anything,you can see that appearing in the server terminal window.

The complete Java Program source code to implement 1 way client and server model is provided below as two separate programs Client.java and Server.Java

 //Server Program  
 //c-madeeasy.blogspot.com www.codeuniverse.tk  
 import java.io.*;  
 import java.net.*;  
 class server{  
 public static void main(String []args)  
 {  
 String data;  
 ServerSocket ssock;  
 Socket clientsock=null;  
  DataInputStream is;  
 try{  
 ssock=new ServerSocket(2000);  
 System.out.print("Server Started");  
 clientsock=ssock.accept();  
 is=new DataInputStream(clientsock.getInputStream());  
 System.out.println("Connection Accepted");  
 while(true)  
 {  
 data=is.readLine();  
 System.out.println(data);  
 }  
 }  
 catch(Exception e)  
 {  
 System.out.println("ERROR");  
 }  
 }  
 }  
 ---------------------------------------------------------------  
 //Client Program  
 import java.io.*;  
 import java.net.*;  
 class client{  
 public static void main(String []args)  
 {  
 String text;  
 Socket sock=null;  
  DataOutputStream dout;  
  PrintStream sender;  
  DataInputStream keyboardreader;  
  System.out.println("Connecting to Server.....");  
  try{  
   sock=new Socket("localhost",2000);  
   }  
  catch(Exception e)  
  {  
  }  
  try{  
  System.out.println("Connected");  
  keyboardreader=new DataInputStream(System.in);  
  sender=new PrintStream(sock.getOutputStream());  
  do  
  {  
  text=keyboardreader.readLine();  
  sender.println(text);  
  }while(!text.equals("quit"));  

Friday, March 9, 2012

The Concept of Sockets and Networking in Java Simplified

Socket:
A socket is one end-point of a two-way communication link between two programs running on the network.java.net Package provides support for sockets. Socket classes can be used to implement the connection between a client program and a server program.

ServerSocket:
ServerSocket class is exclusive for the Server side implementation of the client-server model.


 Java programming with Socket class allows easy implementation of a client-server model for one way or two way communication. We can easily implement a simple one way client server model,2 way client server model,a broad cast server,a multicast server using Sockets and Server Sockets in Java. 

                                                                      courtesy:oracle

Client Side
 Basically a socket can used in a client to send message it involves creation of socket by using the code


Socket mysocket=new Socket(localhost,2000)


Here a socket is created in the local machine itself you can specify the
ip address of the server machine instead of localhost.

Server Side


In the Sever Side a ServerSocket is created.ServerSocket is a different
class rather than socket.It is meant for the server side only.
In the server side the following code is usually used


ServerSocket ssock=new ServerSocket(2000)


As you can see the same port number should be used in the client and server side.

Classes like PrintStream is used to send message from the client to the server.It is established easily like this

PrintStream ps=new PrintStream(mysocket.getOutputStream)
ps.println("my message");

This code in the client side sends message to the server.In the server 
side this message can be easily accepted and displayed using the 
following code

ServerSocket ssock=new ServerSocket(2000)
Socket mysocket=ssock.accept()
DataInputStream d=new DataInputStream(mysocket.getInputStream)
String msgfromserver=d.readLine() 
System.out.println(msgfromserver)


The method accept() is used to accept a connection from the client.Note that you need to surround the statements with a try-catch block to handle all the exceptions.
In this way you can send a message from the client to server.



Thursday, March 8, 2012

Hybrid Inheritance Example Program in C++ to Calculate the Marks and Percentage of a Student



Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances. The following is a C++ Program to for Calculating the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.This is the best example for Hybrid Inheritance in c++The complete source code is provided Below 



 #include<iostream.h>  
 #include<conio.h>  
 #include<stdio.h>  
 #include<string.h>  
 class student_id  
 {  
 //c-madeeasy.blogspot.com  
 int rno;  
 char name[20];  
 public:  
 void read_id()  
 {  
 cout<<"\n\nEnter the Name of the Student : ";  
 gets(name);  
 cout<<"\n\nEnter the Roll No. : ";  
 cin>>rno;  
 }  
 void display_nr()  
 {  
 cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : ";  
 puts(name);  
 cout<<"\n\nROLL NO. : "<<rno;  
 }  
 };  
 class marks:public student_id  
 {  
 public:  
 int i,mark[3];  
 void read_m()  
 {  
 read_id();  
 for(i=0;i<3;i++)  
 {  
 cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : ";  
 cin>>mark[i];  
 }  
 }  
 void display_m()  
 {  
 display_nr();  
 cout<<"\n\n\tMarks Secured ";  
 for(i=0;i<3;i++)  
 cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i];  
 }  
 };  
 class sports  
 {  
 public:  
 int sm;  
 void read_sportm()  
 {  
 cout<<"\n\nEnter the marks in SPORTS out of 10 : ";  
 cin>>sm;  
 }  
 };  
 class percentage:public marks,public sports  
 {  
 public:  
 float total,prcntge;  
 void calculate()  
 {  
 read_m();  
 read_sportm();  
 total=0;  
 for(i=0;i<3;i++)  
 {  
 total+=mark[i];  
 }  
 total+=sm;  
 prcntge=(total/310)*100;  
 }  
 void display_totp()  
 {  
 display_m();  
 cout<<"\n\nTOTAL = "<<total;  
 cout<<"\n\nPERCENTAGE = "<<prcntge;  
 }  
 };  
 void main()  
 {  
 int cont;  
 percentage pc;  
 clrscr();  
 do  
 {  
 pc.calculate();  
 clrscr();  
 pc.display_totp();  
 cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)";  
 cin>>cont;  
 }while(cont==1);  
 getch();  
 }  

Saturday, February 25, 2012

C++ Program to Implement Friend Class for Adding two Numbers

Friends are functions or classes declared using the friend keyword.
Friend Class can access the private and protected members of the class for which it is friend to c,it is done by a declaring prototype of this external function within the class, and with the keyword friend.

Friend class is used in C++ to gain access to protected members exclusively by the friend class
The following is an Example Program in C++ using friend class to fins the sum of two numbers
 #include<iostream.h>  
 #include<conio.h>  
 class readint  
 {  
 float a,b;  
 public:  
 void read()  
 {  
 cout<<"\n\nEnter the First Number : ";  
 cin>>a;  
 cout<<"\n\nEnter the Second Number : ";  
 cin>>b;  
 }  
 friend class sum;  
 };  
 class sum  
 {  
 public:  
 float c;  
 void add(readint rd)  
 {  
 c=rd.a+rd.b;  
 cout<<"\n\nSum="<<c;  
 }  
 };  
 void main()  
 {  
 int cont;  
 readint rd;  
 sum s;  
 clrscr();  
 do  
 {  
 clrscr();  
 rd.read();  
 s.add(rd);  
 cout<<"\n\nDo you want to continue?(1-YES,0-NO)";  
 cin>>cont;  
 }while(cont==1);  
 getch();  
 }  

Friday, February 10, 2012

C++ Program using Templates to Find minimum of two Integers,Characters and Floating Point Numbers

Template is an more abstract level of a Class by allowing more than one type of data types to be handled by a same class without the need for the definition of more classes.A template class can handle integers,characters,strings etc.For example you can check if a character is greater that a character or an integer is greater than an integer by using a same template class.C++ supports Template classes.To get a better and brighter idea of classes see http://c-madeeasy.blogspot.in/2011/12/what-is-abstractionthe-concept-of.html

The following program is a template class Implementation in C++ to find out the minimum of two integers,characters and floating point numbers.The template class implementation allows this operation to be done using a same class with the need of different classes for different data types.

 #include<iostream.h>  
 #include<stdio.h>  
 #include<conio.h>  
 //c-madeeasy.blogspot.com  
 template<class T>  
 T min(T n1,T n2)  
 {  
 if(n1<n2)  
 return n1;  
 else if(n1>n2)  
 return n2;  
 else  
 cout<<"EQUAL\n\n";  
 }  
 void main()  
 {  
 int a,b,c,choice,ch;  
 float d,e,m;  
 char f,g,n;  
 clrscr();  
 do{  
 cout<<"\t\tMINIMUM\n\n1.Integers\n\n2.Floating Point\n\n3.Character\n\n";  
 cout<<"Enter your choice : ";  
 cin>>choice;  
 switch(choice)  
 {  
 case 1 : cout<<"\n\nEnter the Integers : ";  
            cin>>a>>b;  
            c=min(a,b);  
            cout<<"Minimum Element : "<<c;  
            break;  
 case 2 : cout<<"\n\nEnter the Floating Point numbers : ";  
            cin>>d>>e;  
            m=min(d,e);  
            cout<<"Minimum Element : "<<m;  
            break;  
 case 3 : cout<<"\n\nEnter the Characters : ";  
            cin>>f>>g;  
            n=min(f,g);  
            cout<<"Minimum Element : "<<n;  
            break;  
 default: cout<<"\n\nInvalid Choice";  
 }  
 cout<<"\n\nDo u want to continue?(YES-1,NO-0)";  
 cin>>ch;  
 clrscr();  
 }while(ch!=0);  
 getch();  
 }  

Wednesday, February 8, 2012

Vector Addition,Multiplication Program using Vector Class and Operator Overloadingin C++

Classes help to develop easier and effective programming style.To get a better understanding of classes and the concept of abstraction see http://c-madeeasy.blogspot.in/2011/12/what-is-abstractionthe-concept-of.html 

In this program a class named Vector is used to receive data in the form of a vector and + and * operators are overloaded to add and multiply these vectors.The complete Source Code of the C++ program to perform vector addition and multiplication using operator overloading is provided below.
 #include<iostream.h>  
 #include<conio.h>  
 class vector  
 {  
 //Visit c-madeeasy.blogspot.com  
 public:  
 int x,y,z;  
 void read()  
 {  
 cout<<"\n\nEnter the magnitude of i : ";  
 cin>>x;  
 cout<<"\n\nEnter the magnitude of j : ";  
 cin>>y;  
 cout<<"\n\nEnter the magnitude of k : ";  
 cin>>z;  
 }  
 vector operator -()  
 {  
 x=-x;  
 y=-y;  
 z=-z;  
 }  
 vector operator +(vector b)  
 {  
 vector c;  
 c.x=x+b.x;  
 c.y=y+b.y;  
 c.z=z+b.z;  
 return c;  
 }  
 vector operator *(vector b)  
 {  
 int prod;  
 vector c;  
 c.x=x*b.x;  
 c.y=y*b.y;  
 c.z=z*b.z;  
 prod=c.x+c.y+c.z;  
 cout<<"\nScalar Product = ";  
 cout<<prod;  
 vector d;  
 d.x=(y*b.z)-(z*b.y);  
 d.y=(x*b.z)-(z*b.x);  
 d.z=(x*b.y)-(y*b.x);  
 return d;  
 }  
 void display()  
 {  
 cout<<x<<"i"<<"+"<<y<<"j"<<"+"<<z<<"k";  
 }  
 };  
 void main()  
 {  
 vector v1,v2,v3;  
 int choice,cont;  
 do  
 {  
 clrscr();  
 cout<<"\n\tVECTORS\n\n1.Negation of The Vector\n\n2.Sum of Vecors\n\n3.Product of Vectors\n\n";  
 cout<<"\nEnter your choice : ";  
 cin>>choice;  
 switch(choice)  
 {  
 case 1 : cout<<"\nEnter the Vector\n";  
            v1.read();  
            -v1;  
            cout<<"Negation of the Vector : ";  
            cout<<v1.x<<"i"<<v1.y<<"j"<<v1.z<<"k";  
            break;  
 case 2 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1+v2;  
            cout<<"\n\nThe Sum of Vectors : ";  
            v3.display();  
            break;  
 case 3 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1*v2;  
            cout<<"\n\nVector Product = ";  
            v3.display();  
            break;  
 default : cout<<"\n\nInvalid Choice";  
 }  
 cout<<"\n\n\nDo you want to continue?(1-YES,0-NO)\n";  
 cin>>cont;  
 }while(cont==1);  
 }  

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...