1.#include<iostream>
using namespace std;
int main()
{
cout<<"Hello Woeld"<<endl;
return 0;
}
2.vector.cxx
#include<iostream>
int main()
{int k;
char x;
cout<<"请输入向量的维度:"<<endl;
cin>>k;
vector a(k),b(k),c(k);
cout<<"请输入A向量坐标:"<<endl;
cin>>a;
cout<<"请输入B向量坐标:"<<endl;
cin>>b;
L:cout<<"请输入运算符(+,-,*,q[退出])"<<endl;
cin>>x;
switch(x)
{case '+':c=a+b;break;
case '-':c=a-b;break;
case '*':c=a*b;break;
case 'q':goto Z;
default:cout<<"错误"<<endl;
}
cout<<"C("<<c<<")";goto L;
Z:cout<<"按回车退出"<<endl;
return 0;
}
classvector.h
class vector
{public:
vector( int =1); //默认长度构造函数
vector(const int*,int); //使用数组参数构造函数
vector(const vector&); //复制构造函数
~vector(); //析构函数
//重载运算符
int operator()()const; //返回K值,即向量的维度
vector &operator=(const vector&);
friend vector operator+(const vector&,const vector&);
friend vector operator-(const vector&,const vector&);
friend vector operator*(const vector&,const vector&);
//重载输出输入
friend ostream&operator<<(ostream &output,const vector&);
friend istream&operator>>(istream &input,vector&);
private:
int *v;
int len;
};
//构造函数
vector::vector(int size)//类对象初始化
{v=new int[size];
for(int i=0;i<size;i++) v[i]=0;
len=size;}
vector::vector(const int*c,int size)//用于运算 加减乘 时构造正确的vector类型对象,然后返回给main中的C,达到正确输出的目的
{v=new int [size];
len=size;
for(int i=0;i<len;i++)
v[i]=c[i];}
vector::vector(const vector& a)
{len=a();
v=new int[len];
for(int i=0;i<len;i++)
v[i]=a.v[i];}
//析构函数
vector::~vector()
{delete[] v;
len=0;}
//运算符重载
int vector::operator()()const//括弧:为了将main中的K值(即向量的维度)传给类当中定义为保护类型的len类成员
{return len;}
istream&operator>>(istream &input,vector&a)//输入
{for(int i=0;i<a();i++)
input>>a.v[i];
return input;}
ostream&operator<<(ostream &output,const vector&a)//输出
{for(int i=0;i<a();i++)
output<<a.v[i]<<" ";
return output;}
vector operator+(const vector&a,const vector&b)//加法
{ int size=a();
int *c=new int[size];
for(int i=0;i<size;i++)
c[i]=a.v[i]+b.v[i];
return vector(c,size);}
vector operator-(const vector&a,const vector&b)//减法
{ int size=a();
int *c=new int[size];
for(int i=0;i<size;i++)
c[i]=a.v[i]-b.v[i];
return vector(c,size);}
vector operator*(const vector&a,const vector&b)//乘法
{ int size=a();
int *c=new int[size];
for(int i=0;i<size;i++)
c[i]=a.v[i]*b.v[i];
return vector(c,size);}
vector &vector::operator=(const vector&a)//赋值号
{int *c=new int[len];
for(int i=0;i<len;i++)
v[i]=a.v[i];return *this;
}
ubuntu@ubuntu-virtual-machine:~$ cd wx/Hello
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ g++ -E -o Hello.i Hello.cxx
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ g++ -S Hello.i -o Hello
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ g++ -c Hello.cxx
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ g++ -E -o Hello.i Hello.cxx
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ g++ Hello.o -o Hello -I ACE
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ Hello.exe
Hello.exe: command not found
ubuntu@ubuntu-virtual-machine:~/wx/Hello$ ./Hello
Hello World!!
ubuntu@ubuntu-virtual-machine:~/wx/Hello$
2.(1)静态链接库:
ubuntu@ubuntu-virtual-machine:~$ cd wx/vec1
ubuntu@ubuntu-virtual-machine:~/wx/vec1$ make
g++ -c vector.cxx -o vector.o
g++ vector.o -o vector.exe
ubuntu@ubuntu-virtual-machine:~/wx/vec1$ ./vector.exe
(2)动态链接库
ubuntu@ubuntu-virtual-machine:~$ cd wx/vec2
ubuntu@ubuntu-virtual-machine:~/wx/vec2$ make
g++ -DDLLBUILD -c vector.cxx -o vector.o
g++ -shared -o libvector.so vector.o
g++ vector.o -o vector.exe
ubuntu@ubuntu-virtual-machine:~/wx/vec2$ ./vector.exe