ur opinions and development
Oct 10, 2011 at 7:39pm UTC
I want to know how to declare the = operator ... And i want to ask y the default constructor does not working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#include <iostream>
#include <cmath>
using namespace std ;
class Vector
{
private : int n ;
double sum ;
int *V;
public :
Vector () {n=1;
V=new int [n];
}
Vector (int a);
int get_n ();
double get_length();
void read_vector ();
void print_vector ();
~Vector ()
{cout<<"You Are InSide The Distructor" <<endl;}
Vector operator + (Vector V2);
Vector operator - (Vector V2);
Vector operator * (Vector V2);
//Vector operator = (Vector V );
};
Vector::Vector(int a)
{
n=a;
V=new int [n];
}
int Vector :: get_n()
{
return n ;
}
void Vector :: read_vector ()
{
if (n==1){
cout<<"Plz fill the vector of size " << n <<endl ;
cin>>V[0];
}
else {
cout<<"Plz fill the vector of size " << n <<endl ;
for (int i=0;i<n;i++)
cin>>V[i];
}
}
void Vector :: print_vector ()
{
cout << " Your Vector Is : " << endl ;
for (int i=0;i<n;i++)
cout<<endl<<"V[" <<i<<"] is : " << V[i] <<endl;
}
double Vector :: get_length()
{
int par;
sum=0;
for (int i=0 ;i<n;i++)
{
par=V[i]*V[i];
sum+=par;
}
return (sqrt(sum));
}
Vector Vector :: operator + (Vector V2)
{
Vector tmp(V2.n);
if (n==V2.n)
{
for (int i=0 ; i<n ; i++)
tmp.V[i]= V[i]+ V2.V[i];
}
else
{
cout<<"error size" ;
return 0 ;
}
return tmp ;
}
Vector Vector :: operator - (Vector V2)
{
Vector tmp(V2.n) ;
if (n==V2.n)
{
for (int i=0 ; i<n ; i++)
tmp.V[i]= V[i]- V2.V[i];
}
else {
cout<<"error size" ;
return 0;
}
return tmp ;
}
Vector Vector :: operator * (Vector V2)
{
Vector tmp(V2.n) ;
if (n==V2.n)
{
for (int i=0 ; i<n ; i++)
tmp.V[i]= V[i] * V2.V[i];
}
else
{
cout<<"error size" ;
return 0 ;
}
return tmp ;
}
/*Vector Vector :: operator = (Vector V1 )
{
if (n==V1.n)
{
Vector tmp(V1.n) ;
for (int i=0 ; i<n ; i++)
tmp.V[i]= V1.V[i] ;
return tmp ;
}
else
cout<<"error size";
}*/
int main ()
{
double length1 ;
Vector V1(5) , V2(5) , V3(5), V4(5), V5(5) ;
V1.read_vector();
V2.read_vector();
V3=V1+V2;
V3.print_vector () ;
length1 = V1.get_length();
cout << " The Length For V1 Is : " << length1 << endl;
V4=V1-V2;
V4.print_vector();
V5=V1*V2;
V5.print_vector();
return 0 ;
}
Oct 10, 2011 at 7:45pm UTC
I don't see anything in your main() function that tests the default constructor. How do you know that it is not working?
Oct 10, 2011 at 7:49pm UTC
I Delete it because it is not working
just define V6() and V7() and V8()
and make V8=V6+V7
it will make errors
Oct 10, 2011 at 8:09pm UTC
I'm actually not getting any errors when I run this. Something seems strange with your math but I haven't looked into it that far yet.
Oct 10, 2011 at 8:12pm UTC
is it good and work properly ?
Oct 10, 2011 at 8:15pm UTC
I'm not sure how to answer that, obviously adding together uninitialized data and outputting it to the screen has undefined results. But I can say that it works as well as I expect it to.
Topic archived. No new replies allowed.