Nineth Week on-machine practice Project 4--my vector class

Source: Internet
Author: User

Create a vector class Myvector, declare as follows, complete the definition of the class

Class Myvector//define vector class {public:myvector (int m);  constructors, a vector of M elements, with element values preset to 0 myvector (const myvector &AMP;V); Copy constructor ~myvector ();    Destructors: Releasing storage space occupied by dynamic arrays friend IStream &operator>> (IStream &input, Myvector &d);    Friend Ostream &operator<< (ostream &output, const myvector &d); Friend Myvector operator+ (const myvector &d1,const myvector &AMP;D2);//vector addition, the corresponding position of the elements add friend Myvector operator-(co NST myvector &d1,const myvector &d2);//subtract vectors, subtract bool operator== (const myvector &d) on the corresponding position, or determine whether two matrices are equal, i.e. corresponding       Whether all elements in the position are equal private:int *array;          Array is a dynamic array pointer to the vector that occupies the starting address of the memory int num;    The number of elements in the vector};int main () {myvector D1 (5), D2 (5);    cout<< "Input d1--";    cin>>d1;    cout<< "Input d2--";    cin>>d2;    cout<< "d1=" <<d1<<endl;    cout<< "d2=" <<d2<<endl;    cout<< "d1+d2=" <<d1+d2<<endl;   cout<< "d1-d2=" <<d1-d2<<endl; cout<< "D1" << ((d1==d2)? "    = = ":"! = ") <<" D2 "<<endl; return 0;}

Code

/* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Author: Simbin * Completion Date: May 8, 2015 * Version number: v1.0 */#include <iostream> #include <cstring>using name    Space Std;class myvector//define vector class {public:myvector (int m);  constructors, a vector of M elements, with element values preset to 0 myvector (const myvector &AMP;V); Copy constructor ~myvector ();    Destructors: Releasing storage space occupied by dynamic arrays friend IStream &operator>> (IStream &input, Myvector &d);    Friend Ostream &operator<< (ostream &output, const myvector &d); Friend Myvector operator+ (const myvector &d1,const myvector &AMP;D2);//vector addition, the corresponding position of the elements add friend Myvector operator-(co NST myvector &d1,const myvector &d2);//subtract vectors, subtract bool operator== (const myvector &d) on the corresponding position, or determine whether two matrices are equal, i.e. corresponding       Whether all elements in the position are equal private:int *array;          Array is a dynamic array pointer to the vector that occupies the starting address of the memory int num; The number of elements in the vector};    Myvector::myvector (int m) {num=m;    Array=new Int[m]; for (int i=0; i<m; i++) array[i]=0;} Myvector::myvector (Const Myvector &v) {num=v.num;    if (array!=null) delete []array;    Array=new Int[num]; for (int i=0; i<num; i++) array[i]=v.array[i];} Myvector::~myvector () {delete []array;}    IStream &operator>> (IStream &input, Myvector &d) {cout<<d.num<< "vector";    int i;    for (i=0; i<d.num; i++) input>>d.array[i]; return input;}    Ostream &operator<< (ostream &output, const myvector &d) {output<< "(";    for (int i=0; i<d.num-1; i++) output<<d.array[i]<< ",";    output<<d.array[d.num-1];    output<< ")"; return output;}    Myvector operator+ (const myvector &d1,const myvector &d2) {myvector T (d1.num);    T.array=new Int[t.num]; for (int i=0; i<t.num; i++) t.array[i]=d1. Array[i]+d2.    Array[i]; return t;}    Myvector operator-(const myvector &d1,const myvector &d2) {myvector T (d1.num);    T.array=new Int[t.num];      for (int i=0; i<t.num; i++)  T.array[i]=d1. Array[i]-d2.    Array[i]; return t;}    BOOL myvector::operator== (const myvector &d) {int m=0,i;    for (i=0; i<num; i++) if (array[i]!=d.array[i]) m++;    if (m==0) return true; else return false;}    int main () {myvector D1 (5), D2 (5);    cout<< "Input d1--";    cin>>d1;    cout<< "Input d2--";    cin>>d2;    cout<< "d1=" <<d1<<endl;    cout<< "d2=" <<d2<<endl;    cout<< "d1+d2=" <<d1+d2<<endl;    cout<< "d1-d2=" <<d1-d2<<endl; cout<< "D1" << ((d1==d2)? "    = = ":"! = ") <<" D2 "<<endl; return 0;}

Operation Result:

Nineth Week on-machine practice Project 4--my vector class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.