Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class cube{
- friend class a_cube;
- private:
- double x,y,z,r;
- public:
- cube(double X=1, double Y=1, double Z=1, double R=1){x=X;y=Y;z=Z;r=R;}
- cube (const cube& A){x=A.x;y=A.y;z=A.z;r=A.r;}
- void operator=(const cube& A){x=A.x; y=A.y; z=A.z; r=A.r;}
- friend cube operator-(cube,cube);
- friend cube operator*(cube, double);
- friend cube transform(double,double,double,double);
- };
- class a_cube {
- private:
- cube *x; //указатель на куб
- int n;
- public:
- a_cube(int);
- a_cube(const a_cube&);
- ~a_cube();
- void set(int,cube);//вставляем куб на позицию
- void operator=(const a_cube&);//оператор присваивания
- const cube operator[](int) const;//возвращаем с позиции
- cube& operator[](int N) {return x[N];} // <<<<<<<<<< новый оператор)
- friend a_cube operator-(const a_cube&, const a_cube&);
- friend a_cube operator*(const a_cube&, double);//оператор умножения
- friend a_cube operator*(double, const a_cube&);
- void show();
- void del(int);
- void add(const cube&);
- };
- void a_cube::add(const cube &A) {
- cube *tmp;
- tmp=new cube [n+1];
- for(int i=0; i<n; i++) tmp[i]=x[i];
- tmp[n]=A;
- delete[]x;
- x=tmp;
- n++;
- }
- void a_cube::del(int N){
- cube *tmp;
- tmp=new cube[n-1];
- for(int i=0;i<N;i++){
- tmp[i]=x[i];
- for(int i=N+1;i<n;i++){
- tmp[i-1]=x[i];
- }
- delete[]x;
- x=tmp;
- n--;
- }
- cube operator+(cube a, cube b);
- cube operator*(cube a, double b);
- }
- a_cube::a_cube(const a_cube& A){//копиконструктор
- n=A.n;
- x=new cube [n];
- for(int i=0;i<n;i++)
- x[i]=A.x[i];
- }
- void a_cube::operator=(const a_cube& A){//оператор присваивания
- n=A.n;
- x=new cube[n];
- for(int i=0;i<n;i++)
- x[i]=A.x[i];
- }
- cube transform(double x, double y, double z, double r){
- cube tmp;
- tmp.x=x;
- tmp.y=y;
- tmp.z=z;
- tmp.r=r;
- return tmp;
- }
- a_cube::a_cube(int N){//конструктор
- n=N;
- x=new cube[n];
- for (int i=0;i<n;i++){
- x[i].x=i;x[i].y=i+1;x[i].z=i+2;x[i].r=i+3;}
- }
- a_cube::~a_cube(){//деструктор
- delete[]x;
- }
- void a_cube::set(int N, cube tmp){
- if(n<N) return;
- x[N]=tmp;
- }
- const cube a_cube::operator[](int N)const{
- return x[N];
- }
- a_cube operator-(const a_cube& a, const a_cube& b){
- a_cube c(a.n);
- for(int i=0;i<c.n;i++)
- c.set(i,a[i]-b[i]);
- return c;
- }
- cube operator-(cube a, cube b){
- return transform(a.x-b.x,a.y-b.y,a.z-b.z,a.r-b.r);
- }
- cube operator*(cube a, double b){
- return transform(a.x*b,a.y*b,a.z*b,a.r*b);
- }
- a_cube operator*(double k, const a_cube& a){
- a_cube c(a.n);
- for(int i=0;i<c.n;i++)
- c.set(i, a[i]*k);
- return c;
- }
- a_cube operator*(const a_cube& a,double k){
- return a*k;}
- void a_cube::show(){
- cout<<endl<<"X:"; for(int i=0;i<n;i++) cout<<x[i].x<<" ";
- cout<<endl<<"Y:"; for(int i=0;i<n;i++) cout<<x[i].y<<" ";
- cout<<endl<<"Z:"; for(int i=0;i<n;i++) cout<<x[i].z<<" ";
- cout<<endl<<"R:"; for(int i=0;i<n;i++) cout<<x[i].r<<" ";
- cout<<endl;
- }
- int main() {
- a_cube a(5),b(5);
- cube a1,b1,c1,d1;
- a1=transform(1,1,1,1);
- b1=transform(1,2,3,4);
- a.set(0,a1);
- b.set(1,b1);
- a.show();
- b.show();
- (a-b).show();
- a.add(a1);
- a.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment