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.
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);  
 }  
No comments:
Post a Comment