Clrscr(); cout<<"Египетские числа"<<endl; Egipt E1,E4; Egipt E2("ЦЦРРРПП
Quot;); Egipt E3(126); cout<<"Египетские числа"<<endl; cout<<"\nегип число E2 = "<<E2; cout<<" по нашему равно "<<E2.GetVal(); cout<<"\nегипетское число E3 "<<E3.GetVal(); cout<<"по египетски равно "<<E3<<endl; getch(); E1=E2; cout<<"E1=E2 ="<<E1; //копирует хорошо cout<<"\nВвести егип число типа ЦЦРРРПП
Quot;<<endl; cin>>E1; cout<<"\nВвели E1= "<<E1<<" знач = "<<E1.GetVal()<<endl; cout<<"\nвв E4 "; cin>>E4; cout<<E4<<endl; getch(); }//-------------------------------------- Примеры программ для лаб Приложение 1 #include <iostream.h> // Файл comp1/cpp class cmplx { static int n; //счетчик объектов double re,im; public: cmplx(){ n++; // конструктор 1 по умолч cout <<"\nсоздался объект № "<<n;} cmplx(double r, double i){ n++; //конструктор-инициализатор cout <<"\nсоздался объект № "<<n; re=r; im=i;} cmplx(cmplx& R){n++; re=R.re; im=R.im; //конструктор копии cout <<"\nсоздался объект-копия № "<<n;} /* cmplx(double r=0.0, double i=1.0){ n++; //эквивалентен cmplx(){ } cout <<"\nсоздался объект № "<<n; re=r; im=i;} */ ~cmplx(){cout <<"\nразрушился объект № "<<n--;} void define(double r=0.0, double i=0.0){re=r; im=i;} void display(void){ cout <<"("<<re <<"," << im<<")\n"; } }; //---------------------------------------- int cmplx::n=0; void main(void){ cmplx x1,x2,x3(3,3),x4,x6; x4=cmplx(7,7); x4.display(); cmplx x5=cmplx(x3); //конструктор копии x6=cmplx(2,3); cmplx x7=cmplx(x3); //конструктор копии x4= x3; cout <<"X3 = "; x3.display(); cout <<"X4 = "; x4.display(); cout <<"X5 = "; x5.display(); // cout <<"\nСколько же на самом деле создается объектов ?"; }//==================================================== Приложение 2 #include <iostream.h> struct complex { complex (double re=1.0, double im=0) { Re=re; Im=im; cout<<"создался новый объект "<<Re<<" "<<Im<<endl; } // konstruktor void display(void){ cout << "("<< Re<<","<<Im<<")"<<endl;} double &RE(void) {return Re;} double &IM(void) {return Im;} friend complex add(complex a1, complex a2); private: double Re,Im; }; //------------------------------------- complex add(complex a1, complex a2) { complex z(a1.Re+a2.Re, a1.Im+a2.Im); return z; } void main(void){ clrscr(); complex CC,SS(10.3,0.22),EE(2.5); complex xx;m double a,b,c; CC.display(); SS.display(); EE.display(); complex X=complex(2,3); cout << "X= "; X.display(); a=CC.IM(); b=SS.RE(); c=X.IM(); cout <<"a="<<a<<" b="<<b<<" c="<<c<<endl;; complex x1(1.0,1.0), x2(5.,5.),x3; cout <<"\nпри x1 и x2 = "; x1.display(); x2.display(); x3=add(x1,x2); cout << "имеем x3= "<<endl; x3.display(); } Приложение 3 #include <iostream.h> #include <stdio.h> class cmplx { public: float re,im; public: void define (float r=0.0, float i=0.0){ re=r; im=i;} void display(void){ cout <<"("<<re <<"," << im<<")\n"; } }; void main(void){ cmplx x1,x2,x3, *adrr; cout <<"обект X1 но не инизиализирован= "; x1.display(); x1.define(); x2.define(2.5,103); cout <<"комплексная переменная X1 = "; x1.display(); cout <<"\n X2 = "; x2.display(); cout << "при ключе struct компонента im доступна = "<< x2.im; cout <<"\n X2 = "; x2.display(); Приложение 4 #include "stroka.cpp" // konkat.cpp перегрузка операции + для склейки строк stroka& operator +(stroka& A, stroka& B); void main(void){ stroka X("Qui"); stroka Y(" Vivra"); stroka Z(" Verra!"); stroka C,F; C=X+Y+Zпоживем - увидим"; char d[]="склеиват!"; stroka D(d); stroka E(e); F=D+E+" ***"; F.display(); C.display(); } stroka& operator +(stroka& a, stroka& b){ int ls=a.len_str()+b.len_str(); stroka *ps; //вспомогат указатель ps= new stroka(ls); // резервируем память strcpy(ps->string(),a.string()); strcat(ps->string(),b.string()); ps->len_str()=ls; // сохраняем длину строки return *ps; // возвращаем новый объект типа строка }//----------------------------------------------------------------------------- #include <iostream.h> // stroka.cpp #include <string.h> struct stroka{ char *ch; int len; public: stroka(int N=80): len(0)// konstruktor 1 {ch=new char[N+1];ch[0]='\0';} stroka(const char *s){ len=strlen(s); ch=new char[len+1]; strcpy(ch,s); } int &len_str(void) {return len;} char *string(void) {return ch;} void display(void){ cout <<"\nдлина строки:" <<len <<" ее содержимое - "<<ch;} ~stroka() {delete [] ch;} }; #ifndef POINTH // POINT.H класс point #define POINTH 1 class point{ protected: int x,y,c; public: point(int xi, int yi, int c); int &givex(void); int &givey(void); void show(void); void display(void); void move(int xn=0, int yn=0, int cn=1); // private: void hide(); }; #endif // ------------------------------------------- #ifndef POINTCPP // Файл POINT.CPP #define POINTCPP 1 #include <graphics.h> // #include <iostream.h> #include "point.h" point::point(int xi=0, int yi=0, int ci=0) {x=xi; y=yi; c=ci;} int &point::givex(void) {return x;} int &point::givey(void) {return y;} void point::show(void) {putpixel(x,y,c);} void point::hide(void) {putpixel(x,y,getbkcolor());} void point::move(int xn, int yn, int cn){ hide(); x=xn; y=yn; c=cn; show(); } #endif // POINTMAIN ---------------------------- #include <graphics.h> #include <conio.h> #include <iostream.h> #include "point.h" void point::display(void){ cout << "\t\t\tпараметры объекта x:y:c =" << x<<" "<<y<<" "<<c<<endl;} void main(void){ int i,cv,xt,yt; point A(200,50,3); point D(500,200,8); point B(); // создали объекты A B и D int gd=DETECT, gm; initgraph(&gd,&gm,"c:\borlanndc\bgi"); A.show(); // cout << "Объект A ="<< A.x<<" "<< A.y<<" "<< A.c; A.display(); getch(); //B.show(); getch(); D.show(); getch(); A.move(); //cout << "объект A ="<< A.x<<" "<< A.y<<" "<< A.c<<endl; A.display(); getch(); A.move(50,60,9); // cout << " объект A ="<< A.x<<" "<< A.y<<" "<< A.c<<endl; A.display(); getch(); for(i=1;i<16;i++){ A.move(50+2*i,60+5*i,i); cout << " объект A ="<< A.x<<" "<< A.y<<" "<< A.c<<endl; A.display(); getch(); } closegraph(); }//================================================================== Приложение 5 #include <iostream.h> //файл OVRLOAD1.cpp перегрузка + f(arg1,arg2) #include <string.h> #include "ovrload1.h" void main(void){ stroka LAT("VENI VEDI VICI "); stroka RUS("Пришел Увидел Победил !"); stroka X("VENI "),Y("VEDI "),Z("VICI "),C,F, STR(23);; LAT.display(); cout <<"\nобъект RUS сoдержит: "<<RUS.string(); STR=LAT; cout <<"\nSTR копия LAT "; STR.display(); cout<<"обращение к компонентам ch len = " << LAT.ch<<" len(LAT) ="<<LAT.len; C="Склейка X +Y + Z "+X+Y+Z; C.display(); char d[]="Ура ",e[]=" склеивает !"; stroka D(d); stroka E(e); F=D+E + " ***"; F.display(); // F=d+e + "----"; F.display(); // симв массив не может, не тот тип cout<<"вызов как обычной функции F(arg1,arg2) "<<endl; F=operator+(D,E+ " как F(arg1,arg2) "); F.display(); cout<<endl; } //--------------------------------------------- // файл OVRLOAD1.H перегрузка + для строк как f(arg1,arg2) #include <iostream.h> #include <string.h> class stroka{ public: char *ch; int len; stroka(int ); // konstruktor 1 stroka(const char *s); // konstruktor 2 int &len_str(void) {return len;} char *string(void) {return ch;} void display(void); friend stroka& operator +(stroka& a, stroka& b); ~stroka() {delete [] ch;} }; //----------------------------------------------------- stroka::stroka(int N=80):len(0){ch=new char[N+1];ch[0]='\0';} stroka::stroka(const char *s){ len=strlen(s); ch=new char[len+1]; strcpy(ch,s); } void stroka::display(void){ cout <<"\nдлина строки:"<<len <<" ее содержимое - "<<ch<<endl; }//----------------------------------------------------- stroka& operator +(stroka& a, stroka& b){ int ls=a.len_str()+b.len_str(); stroka *ps; //вспомаг указатель ps= new stroka(ls); // объект в динамической памяти strcpy(ps->string(),a.string()); strcat(ps->string(),b.string()); ps->len_str()=ls; // сохраним длину новой строки return *ps; // возвращаем новый объект } //----------------------------------------------------- #include <iostream.h> // файл OVRLOAD2.H перегрузка + в классе #include <string.h> // для строк как arg1 operator +(arg2) class stroka{ public: char *ch; int len; public: stroka(int ); // konstruktor 1 stroka(const char *s); // konstruktor 2 int &len_str(void) {return len;} char *string(void) {return ch;} void display(void); stroka& operator +(stroka& b); ~stroka() {delete [] ch;} }; //----------------------------------------------------- stroka::stroka(int N=80):len(0){ch=new char[N+1];ch[0]='\0';} stroka::stroka(const char *s){ len=strlen(s); ch=new char[len+1]; strcpy(ch,s); } void stroka::display(void){ cout <<"\nдлина строки:"<<len <<" ее содержимое - "<<ch<<endl; }//----------------------------------------------------- stroka& stroka::operator +(stroka& b){ int ls=this->len_str()+b.len_str(); stroka *ps; //вспомаг указатель ps= new stroka(ls); // созд новая стр где освобождать strcpy(ps->string(),this->string()); strcat(ps->string(),b.string()); ps->len_str()=ls; // сохраним длину новой строки return *ps; // возвращаем новый объект }//----------------------------------------------------- #include <iostream.h> #include <string.h> #include "ovrload2.h" void main(void){ stroka LAT("VENI VEDI VICI "); stroka RUS("Пришел Увидел Победил !"); stroka X("VENI "),Y("VEDI "),Z("VICI "),C,F, STR(23); LAT.display(); cout <<"\nобъект RUS сoдержит: "<<RUS.string(); STR=LAT; cout <<"\nSTR копия LAT "; STR.display(); cout<<"обращение к компонентам .ch .len = " << LAT.ch<<" len (LAT) ="<<LAT.len; // C=X+Y+Z; C.display(); //не раб "Склейка X +Y + Z "+ char d[]="Ура ",e[]=" склеивает !"; stroka D(d); stroka E(e); F=D+E + " ***"; F.display(); //F=d+e + "----"; F.display(); // симв массив не может, не тот тип cout<<"вызов как arg1.operator +(arg2) "<<endl; F= D+ " с одним аргум "; D=F.operator+(E); D.display(); cout<<endl; } //--------------------------------------------- #include <iostream.h> // stroka.cpp #include <string.h> struct stroka{ char *ch; int len; public: stroka(int N=80): // konstruktor 1 len(0) {ch=new char[N+1];ch[0]='\0';} stroka(const char *s){ len=strlen(s); ch=new char[len+1]; strcpy(ch,s); } int &len_str(void) {return len;} char *string(void) {return ch;} void display(void){ cout <<"\nдлина строки:"<<len <<" ее содержимое "<<ch;} ~stroka() {delete [] ch;} }; //-------------------------------- #include <iostream.h> // stroki.cpp #include "stroka.cpp" void main(void){ stroka LAT("VENI VEDI VICI "); stroka RUS("Пришел, увидел победил "); stroka STR(23); LAT.display(); cout <<"\nобъект RUS содержит: "<<RUS.string(); STR.display(); STR=LAT; cout <<"\nSTR копия LAT"<<endl; STR.display(); cout <<" "<<endl; cout << LAT.ch<<" len="<<LAT.len; }//===============================================================
Дата добавления: 2018-04-15; просмотров: 253; Мы поможем в написании вашей работы! |
Мы поможем в написании ваших работ!