C ++ Note

xiaoxiao2021-03-05  28

// Example of seeking boulders and the number of conventions: #include "stdio.h" #include

Void main () {Int A, B, I, Minnum, MaxNum; Printf ("Please enter the value of A, B:"); scanf ("% D% D", & A, & b); i = a> B? A: b; for (; i <= a * b; i ) IF ((i% a == 0) && (i% b == 0)) {minnum = i; Break;} for (i = a; I> = 1; I - ((a% i == 0) && (b% i == 0)) {MaxNum = i; Break;} printf ("The least mile is:% D / N", Minnum); Printf ("Number of"% D / N ", MaxNum); getCh ();

// Example of the pre-bubble method #include using namespace std;

Void main () {INT I, J, NUM [10], TEMP; Printf ("Please enter 10 numbers, end:"); for (i = 0; i <= 9; i ) scanf (" % D ", & NUM [I]); Printf (" Original: / N "); for (i = 0; i <= 9; i ) Printf ("% D / N ", NUM [I]);

For (i = 0; i <9; i ) {for (j = 0; j <9-i; j ) IF (NUM [J]> NUM [J 1]) {TEMP = NUM ​​[J]; Num [J] = NUM ​​[J 1]; NUM [J 1] = Temp;}}

Printf ("Adding method: / n"); for (i = 0; i <= 9; i ) Printf ("% d / n", NUM [I]);}

// Select the example: #include #include

Void main () {Int Num [10]; INT I, J, P; INT TEMP; Printf ("Please enter 10 numbers, end:"); for (i = 0; i <= 9; i ) Scanf ("% d", & number [i]); Printf ("Orignal: / N"); for (i = 0; i <= 9; i ) Printf ("% d / n", NUM [i] ); For (i = 0; i <9; i ) {p = i; for (j = i 1; j <= 9; j ) {if (NUM [P]> Num [j]) {p = J; temp = NUM ​​[I]; NUM [i] = NUM ​​[P]; NUM [P] = Temp;}}}}}}}}}}}}}}}}}} PRINTF ("SELECT SORT: / N"); for (i = 0; i <= 9 i ) Printf ("% d / n", NUM [i]); getch ();} // file contains #include "file.h" and #include all legal. The difference between the two is to use the shape of a header () to find the file to be included in the directory where the C library function file is located, referred to as a standard method. When using dual quotation ("file.h"), the system is first looking for files to be included in the user's current directory. If you can't find it, follow it in a standard method.

// Const Limited Modifier #include Using Namespace STD;

Void main () {const INT A = 3; // const INT A = 3 and INT const a = 3 is equivalent. Const Int * p; // P is a pointer to the int type const object. P = & A; }

Void main () {Int a; const INT * p; p = & a; // This is also correct.

Pointer constant int * const p = & a; must be initialized when defined.

// Reference Type INT A = 3; INT & REFA = a; // The reference must be initialized when defined. Quote is the alias of the variable .// Truth of the pointer: int A = 5; int * P = & a; int * & refp = p; // Const reference You can use different types of object initialization as long as it can be converted from one type to another, can also be impersonatory, such as the text constant, such as Double Dval = 3.14159; // only Const reference is a legal const IR = 1024; const amount = DVAL; Const Double & DR = DVAL 1.0; // Enumeration Type Enumeration Enumeration provides an alternative method that does not only define integer constants but also They make a collection such as enum open_modes {input = 1, output, append}; // array type number group name is equivalent to constant pointers, such as Int a [10]; A is equivalent to & a [0]; a [2] [ 3]; A 1 equivalent to the address of A [1] // Container type array habits STL habits: Vector text; text.push_back (3); // Complex #include // typedef int inteer INTEGER A = 3; // Volatile Limits Modifier When the value of an object may change, for example, a variable updated by the system clock is changed, such as a variable that is updated by the system clock, should be declared into volatile./pair Type #include // Switch 2 Number Void Change (INT * PA, INT * PB) {INT TEMP; TEMP = * Pa; * PA = * Pb; * Pb = Temp;

Void main () {Int a = 5, b = 3; INT * PA, * Pb; PA = & A; PB = & B; Change (PA, PB); Printf ("A =% D, B =% D / N ", b);}

// inline function (inline function) call frequency, when the code segment is not very long, the defined member function is also inline function when expanding the class definition at the call.

// Function Overloads in the same scope, allowing multiple identical functions to exist // constructors and destructive functions

// Domain and Life Domain: In C , each name has a unique entity (object, function, class, template) C , there are three domains, local domains, name spatial domains, class name analysis: will express One name in the formula is associated with a declaration. Local domain name analysis: extern extension scope, the same life period has three local object auto objects: Automatic Object Register Object, Register Object, local static object local static object // Name Space ****

// A simple example #include using namespace std;

Class sample {private: void setValue (int A, int b) {x = a; y = b;}; void print () {cout << x << endl; cout << Y <

Void main () {sample Samples; Sample1.SetValue (3, 5); Sample1.Print ();} The PUBLIC of the class can define variables, but cannot be assigned; the variables in the class cannot be assigned, because the class There is no distribution of storage when defined.

// Constructor is used to initialize the object, and each object is instantiated, the constructor will call the constructor, which itself has a default constructor. The constructor name is the same as the class name, and the function is not content. In a class, there can be multiple constructors. Eg1.class sample {private: int x, y; public: sample (); sample (int A, int b) {x = a; y = b;};};}; eg2. # incrude Using Namespace STD;

Class sample {private: int x, y; public: Sample (int A, int b); void printall () {cout << "x =" << x << ", y =" << Y << ENDL; }};

Sample :: Sample (int A, int b) {

X = a; y = b;}

Void main () {Sample C (4, 8); C.Printall ();

Eg3.class sample {private: int x, y; public: sample (int A, int b = 1) {x = a; y = b;}; void printall () {cout << "x =" << x << ", y =" << Y << endl;}};

Void main () {Sample C (4); // y = 1; Sample D (4, 5); // y = 5; the front of the previous parameter cannot be used as a constant C.Printall (); D.Printall (); }

EG4: An example to determine if the 3 points can form a triangle, if you can ask for an area #include #include using namespace std;

Class cpoint {public: float x, y; // Because you want to access x, y, you don't define them in the private (float a, float b) // constructor {x = a; y = b ;}};

Float S (cpoint a, cpoint b) // Sign long {float bian; bian = SQRT ((AX-bx) * (AX-BX) (AY-by) * (AY-by)); COUT << "Edward:" << bian << endl; return bian;}

Void MJ (Float S1, Float S2, Float S3) // Determines if it can constitute a triangle, if it can be spaced {Float S; Float Mianji; ((S1 S2)> S3) && ((S1 S3) )> S2) && ((S2 S3)> S1)) {cout << "These three points can constitute a triangle, and the area of ​​this triangle is:"; s = (S1 S2 S3) / 2; Mianji = SQRT (S * (S-S1) * (S-S3) * (S-S3)); cout << mianji;} else cout << "These three points cannot form a triangle";

Void main () {Float X1, Y1, X2, Y2, X3, Y3; Float S1, S2, S3; // 3 Side of the length CIN >> X1 >> Y1 >> X2 >> Y2 >> X3 >> Y3; CPOINT P1 (X1, Y1), P2 (X2, Y2), P3 (X3, Y3); // Instantiate P1, P2, P3 S1 = S (P1, P2); // Call the S function to seek edge length S2 = S (P2, P3); S3 = S (P1, P3); MJ (S1, S2, S3); // Call MJ function} // Decoction function When the object is destroyed, the function is called before the function is ~ // Friends Function Defines the characteristics of Friends of Friend Keywords: 1. In addition to accessing private variables, there is no special place 2, the friend function does not have this pointer 3, in the class declaration The location impact can be declared in the private variable, or in public variables 4, the definition of the friend function does not need to add the prefix class name and: symbol 5, the friend function must be the class of the data accessed Declaring EG1: Using Friends Letters to ask for teachers than students' age of students #include using namespace std;

Class Teacher; Class Student {Private: INT ST_AGE; Public: Friend Void Display (Student A, Teacher B);

Class Teacher {Private: Int TE_AGE; Public: Friend Void Display (Student A, Teacher B);

Void Display (Student A, Teacher B) // must write {a.st_age = 20 outside; B.TE_AGE = 23; cout << b.te_age-a.st_age << endl;}

Void main () {student Stu; Teacher Tea; Display (STU, TEA);}

EG2: Using Friends Functions to Seek Rounds and Rectangles #include Using Namespace Std;

Class Rectangle; Class Circle {Private: Float R; Float Area; Public: Circle (Float A) {r = a;}; void jsarea () {area = 3.14 * r * r;}; Friend Void Display (Circle A, Rectangle b);

Class Rectangle {Private: Float Length; Float Width; PUBLIC: Rectangle (FLOAT I, FLOAT J) {Length = i; width = J;}; void jsarea () {area = length * width;}; friend void Display (Circle A, Rectangle B);

Void Display (Circle A, Rectangle B) {a.jsarea (); B.jsarea (); Cout << "The area of ​​the garden:" << a.Area << Endl; cout << "The area of ​​the rectangle:" <

Void main () {Circle Circle1 (3); Rectangle Rectangle1 (3,4); Display (circle1, rectangle1);}

EG3: In a class is a member function, another class is an example of a friend function #include using namespace std;

Class Beta; Class Alpha {Private: INT A_DATA; Public: Alpha () {A_DATA = 10;}; Void Display (Beta);}; Class Beta {private: int b_data; public: beta () {b_data = 20;} Friend void alpha :: Display (beta bb);

Void alpha :: Display (beta bb) {cout << "" indateofbeta = << bb.b_data << endl; cout << "indateofalpha =" << a_data;}

Void main () {alpha A1; beta b1; a1.display (b1);}

// Friends class EG1: #include using namespace std;

Class Beta; Class Alpha {Private: Int Data; Public: Alpha () {data = 10;};

Class beta {public: void display (alpha d) {cout << d.data << endl;}; void get_data (alpha d) {int x = d.data; cout << x << endl;};

Void main () {alpha A; beta b; b.display (a); b.get_data (a);}

The overload keyword Operator of the operator is associated with the existing operator with the member function and is used as its operand as its operand, which is called an operator overload. EG1: Binary operator overload #include #include

Const size = 80; class phrase {private: char str [size]; public: phrase () {structure (STR, ");}; phrase (char * s) {struct (str, s);}; void display () {COUT << Str << Endl;}; phrase operator = (phrase aa) {IF (Strlen Strlen (aa.str))

Void main () {phrase s2 ("Check the program"); ​​S1 = S2; S1.Display (); S1 = "next"; phrase S3; S3 = S1 = S2; s3.display (); phrase S4; phrase S5 ("and again"); S4 = S2 = S5 = S5; s4.display ();} // Run results Check the programNext Check The ProgramCheck the program and again and again

EG2: The overload of a yuan operator, the result is 1s # include using namespace std;

Class sample {private: int counter; public: sample () {counter = 0;} Sample Operator () {Sample T; t.counter = counter; returnit;} void display () {cout << counter << Endl;}};

Void main () {Sample Obj1, Obj2; Obj2 = Obj1; Obj2.disPlay ();

// THIS pointer example #include using namespace std;

Class a {public: a (int i, int j) {a = i; b = j;} a () = b = 0;} void copy (a & aa); int Returna () {Return A;} Int retURNB () {RETURN B;} private: int a, b;}

Void A :: Copy (A & AA) {IF (this == & aa) Return; * this = aa;}

Void main () {A1, A2 (3, 4); A1.Copy (a2); cout << a1.returna () - A2.RETURNA () << ","; cout << a1.returnb () A2.RETURNB () << endl;

The example of defining static variables in the class #include using namespace std; class myclass {private: int A, b, c; static int sum; public: myclass (int A, int b, int c); void getNumber (); Int GETSUM (MyClass M);

INT myclass :: sum = 0; // int myclass :: sum (0); same effect

Myclass :: MyClass (int D, int E, int f) {a = d; b = E; c = f; SUM = A B C;

Void myclass :: getnumber () {cout << a << ";" << b << "; << c << endl;}

INT myclass :: getsum (myclass m) {return myclass :: sum;}

Void main () {MyClass M (3, 4, 5), N (5, 6, 7); N.GETNumber (); cout << m.getsum (m) << ";"; COUT << m. Getsum (n) << endl;

// Static member function #include

Class M {Private: Int A; Static B; Public: M (INT A) {a = a; b = a;} static void fun (m m);

Void m :: fun (m) {cout << "a =" << m.a << endl; cout << "b =" << b << endl; // can be m.b or m :: b can}

INT m :: b = 10;

Void main () {mp (6), q (8); m :: fun (p); // Write into P.fun (p) can also q.fun (q);} // class, abstraction, object, Inheritance, Package, Polymorphism 1, Object: It is a boundary that defines the boundaries, concepts and things related to issues to be solved, ie instantiation 2, class: a set of identical features, common behavior and common relationships Objects, that is, abstract 3, data abstraction: the process related to the specific entity related to the application related process 4, inherits: it is allowed to build new classes by reusing existing classes 5, pack: Yes You have the process of selectively hiding the features and methods in the class, polymorphism: Make the same function with different behavior 7 on different classes, inherit: inheritance is divided into single inheritance and multiple inheritance

// Single inheritance #include Using Namespace STD;

Class Employee {Private: int Priva; PUBLIC: INT PUBA;

Class Manager: Public Employee {PUBLIC: Void Fn () {Int a; a = priva; // Error, cannot be inherited a = prota; //, inherited into private a = puba; //}} ;

Class Clerk: Private Employee {PUBLIC: Void Cl () {Int A; A = Priva; // Wrong A = Prota; // Write a = Puba; // Write}};

Void main () {Employee Emp; Emp.Priva = 1; // 错 Emp.Prota = 1; // 错 Emp.puba = 1; // Pair Manager Man; Man.Priva = 1; // Fair man.prota = 1; // error man.puba = 1; // Pair of Clerk Cle; Cle.Priva = 1; // Wrongcle.Prota = 1; // Wrongcle.puba = 1; // Wrong} In derived class declaration Class B: Public A), keyword public specifying the object of the fault to access the "public" member of the base class. And use private, main () objects that can be used to be born with the base class, the public data member (this The private of the base class cannot be inherited by the derived class. After inheriting the Private, PRIVICI inherits, and the Publci is also turned into a private), and the objects outside the class can never access the class Private and protected data members in the base class. PRIVATE is not inherited, protected is still protected, and PUBLIC inherits is also protected.

#include #include

class vehicle {protected: int wheels; double weight; public: void initialise (int whls, double wght); int get_wheels () {return wheels;} double get_weight () {return weight;} double wheel_loading () {return (weight / Wheels);}};

Class Car: Public Vehicle {Private: INT Passenger_load; public: Void Initialise (int whls, double wght, int point = 4); int pointsengers () {return passenger_load;}};

class truck: public vehicle {private: int passenger_load; double payload; public: void init_truck (int number = 2, double max_load = 24000.0); double efficiency (); int passengers () {return passenger_load;}};

Void Vehicle :: Initialise (int whls, double wght) {wheels = whls; = WGHT;

Void Car :: Initialise (int whls, double wght, int public {passenger_load = people; wheels = whls; weight = wght;}

Void TRUCK :: init_truck (int number, double max_load) {passenger_load = number; payload = max_load;

Double Truck :: effect () {return payload / (payload weight);

Void main () {// clrs (); vehicle bicycle; bicycle.initialise (2,25); cout << "The bicycle HAS" << bicycle.get_wheels () << "Wheels./N"; cout << "THE BICYCLE Weights" << Bicycle.Get_Weight () << "Pounds./n"; Car Coupe; Coupe.Initialise (4,3500,5); coup << "The coupe carrier << COUPE.PASSENGERS () << "passsengers./n"; coup. "the coupe weights" << COUPE.GET_WEIGHT () << "pounds./n"; coup << "The couope wheit loading is" << COUPE.WHEEL_LOADING << "Poublents Per Tire./N"; Truck Van; Van.Initialise (18, 12500.0); Van.init_Truck (1,33675.0); cout << "the van weight" << Van.get_weight ( << "Pounds./N"; cout << "The Van's Efficiency IS" << 100.0 * van.efficient () << "Percent./n";} // derived class" Constructor and destructor In the derived class, the constructor is the first-class constructor in the base class, and then perform the sequence of its own constructor destructor, if it is not: base () can, it will not call the base class. Constructor Eg1: #include using namespace std;

Class base {protected: int A; public: base () {a = 0;} base (int C) {a = C;}};

Class Derived: Public Base {public: derived (): base () {} Derived (INT C): base (c) {}}; void main () {derived obj1; derived obj2 (20);}

EG2: #include class a {private: int A; public: a () {a = 0; cout << "default constrct called a / n";} a (int i) {a = i; COUT << "Constructor Called A / N";} ~ a () {cout << "Destructor Called A / N";} void print () {cout << a << ",";} int GETA () { Return a;}};

Class B: Public a {private: INT B; a aa; public: b () {b = 0; cout << "default constructor caled b / n";} b (int I, int J, int K); ~ ~ B () {cout << "deStructor caled b / n";} void print () {a :: print (); cout << b << "," << aa.geta () << Endl;}} ;

B :: B (INT I, INT J, INT K) {cout << "Constructor Called B / N";

Void main () {b bb [2]; bb [0] = B (3, 4, 5); BB [1] = B (7, -8, 9); for (int i = 0; i <2 ; i ) bb [i] .print ();

EG3: // Output 0,0,0; 0,0,5; 1,2,3 # include using namespace std;

Class a {private: int A; public: a () {a = 0;} a (int i) {a = i;} void print () {cout << a << ",}};

Class B: Public A {Private: INT B1, B2; Public: b () {b1 = b2 = 0;} b (int i) {b1 = 0; b2 = i;} b (INT I, INT J, INT K): a (i), b1 (j), b2 (k) {} void print () {a :: print (); cout << B1 << "," << b2 << endl;}};

Void main () {B B1; B B2 (5); B B3 (1, 2, 3): A (1), B1 (2), B2 (3) b1.print (); b2.print (); B3.Print ();

// Derivative class calling member function class base {protected: int suunc () {return ss;}};

Class Derived: PUBLIC BASE {public: int func () {return base :: func ();}}

Void main () {base b1; b1.func (); Derived A1; A1.Func ();

// Container class class engage: {private: int Num; public: engine (int S) {Num = S;}}

Class Jet {Private: Int Jt; Engine Eobj; Public: Jet (INT X, INT Y): EOBJ (Y) {JT = X;}};

// Multiple inheritance Class Teacher {Private: .....}; class student {private: .....

Class TS: Public TEACHER, PUBLIC Student {.....}; erliness, if there is a member function of the same name in the two base classes, it will cause this issue when the class call is called.

// multiple inheritance constructors and destructors eg1: output: constructing B22constructing B11constructing B3 * constructing B13constructing B24constructing B3 * destrcting B3destrcuting B2destructing B1destrcting B3destructing B1destrcuting B2

#include class b1 {public: b1 (int i) {cout << "constructing b1" << i << endl;}

~ B1 () {cout << "deStructing B1" << endl;}};

Class b2 {public: b2 (int J) {cout << "constructing b2" << j << endl;} ~ b2 () {cout << "deStructing B2" << endl;}};

Class b3 {public: b3 () {cout << "constructing b3 *" << endl;} ~ b3 () {cout << "deStructing b3" << endl;}};

Class C: Public B2, Public B1, Public B3 {Public: C (INT A, INT B, INT C, INT D): B1 (a), Memberb2 (D), Memberb1 (C), B2 (b) {} PRIVATE: B1 MEMBERB1; B2 MEMBERB2; B3 MEMBERB3;

Void main () {c Obj (1, 2, 3, 4);

EG2: Output: Constructor A17Constructor A26Constructor A38Constructor D9769, 8Constructor A246Constructor A127

#include

Class A1 {Private: Int A1; Public: A1 (INT I) {A1 = I; COUT << "Constructor A1" << A1 << Endl;} void print () {cout << A1 << endl;}} ;

Class A2 {Private: INT A2; Public: A2 (INT J) {A2 = J; COUT << "Constructor A2" << A2 << Endl;} void print () {cout << A2 << endl;}} ;

Class A3 {Private: Int A3; Public: A3 (INT K) {A3 = K; COUT << "Constructor A3" << A3 << Endl;} INT GETA3 () {Return A3;}};

Class D: Public A1, Public A2 {Private: INT D; A3 A3; Public: D (INT I, INT J, INT K, INT L): A2 (i), A1 (J), A3 (k) {D = L; cout << "constructor d" << D << Endl;} void print () {a1 :: print (); a2 :: print (); cout << d << ", << A3. Geta3 () << Endl;}};

Void main () {D DD (6, 7, 8, 9); DD.Print (); A2 A2 (4); A2 = DD; A2.Print (); A1 A1 (2); A1 = DD; A1 .print ();

Many inheritance defects in the example: A class is branch into 2 classes, then merge, use the virtual base class to solve the class b1: Virtual public a {}; Class B2: Virtual public a {}; Class C: Public B1 , public b2 {};

// virtual function and dynamic cable EG1: // Run results 137.5 # include

Class Point {Private: Double X, Y; Public: Point (Double I, Double J) {x = I, Y = J;} Virtual Double Area () Const // Constance This purpose is to prevent the transformation of private variables in the class {RETURN 0.0;}};

Class Rectangle: Public Point {Private: Rectangle (Double I, Double J, Double K, Double L); Virtual Double Area () Const // Plus Virtual Subgrade class After inheriting, retaining party, If it is not added, there are two members of the same name in this derived class {Return W * h;}}.

Rectangle :: Rectangle (Double i, Double J, Double K, Double L): Point (i, j) {w = k; h = L;}

Void Fun (Point & S) {cout << S.Area () << endl;}

Void main () {Rectangle Rec (3.4, 15.2, 5.0, 27.5); Fun (REC);

EG2: The above case after #include

Class Shape {Private: Double X, Y; Public: Shape () {} Virtual Double Area () Const {Return 0.0;}}

Class Rectangle: Public Shape {Private: Rectangle (Double K, Double L) {W = K; H = L;} Virtual Double Area () Const {Return W * h;}}

Class Circle: Public Shape {Private: Double R; Public: Circle (Double i) {r = i;} Virtual Double Area () Const {Return 3.14 * r * r;}};

Void fun (shape * s) {cout << (* s) .area () << endl;} // with S-> Area () equivalent

Void main () {Rectangle * REC = New Rectangle (10, 10); Fun (REC); Circle * Cir = new circle (10); fun (CIR);}

// Pure virtual function and abstract type pure virtual functions are virtual functions without specific implementation EG1: #include using namespace std;

Class Point {Private: INT X0, Y0; Public: Point (int i = 0, int J = 0) {x0 = I, Y0 = j;} Virtual Void Draw () = 0;

Class Line: Public Point {Private: INT X1, Y1; Public: line (int i = 0, int J = 0, INT M = 0, INT N = 0): Point (i, j) {x1 = m, Y1 = N;} void Draw () {cout << "line :: draw () caled / n";}}

Class Ellipse: PUBLIC POINT {Private: INT X2, Y2; Public: Ellipse (INT i = 0, INT J = 0, INT P = 0, INT Q = 0): Point (i, j) {x2 = p, y2 = Q;} void draw () {cout << "Ellispe :: Draw () Called / N";}}

Void Drawobj (POINT * P) {P-> DRAW ();

Void main () {line * lineobj = new line; Ellipse * Ellipseobj = new Ellipse; Drawobj (LINEOBJ); Drawobj (Ellipseobj);}

EG2: // This example also includes a pointer to the pointer, running result area's sum = 380.713 # include const Double pi = 3.1415; Class Shap {public: Virtual double area () const = 0;};

Class Triangle: PUBLIC SHAP {Private: triangle h, w; public: triangle (double h, double w) {h = h; w = W;} Double area () const {return 0.5 * h * w;}}

Class Rectangle: Public Shap {Private: Double H, W; Public: Rectangle (Double H, Double W) {h = h; w = W;} Double Area () Const {Return H * W;}};

Class Circle: Public Shap {Private: Double R; PUBLIC: CIRCLE (Double R) {r = r;} Double Area () Const {Return Pi * r * r;}};

Class Trapezoid: PUBLIC SHAP {Private: Double T, B, H; Public: Trapezoid (Double T, Double B, Double H) {T = T; B = B; h = h;} Double Area () const {return 0.5 * (T b) * h;}};

Class Square: PUBLIC SHAP {Private: Double S; Public: Square (double s) {s = s;} Double Area () const {return s * s;}}

Class Application {public: Double Compute (Shap * s [], int N) const;

Double Application :: Compute (Shap * S [], INT N) const {double sum = 0; for (int i = 0; i area (); returnium; } Class myprogram: public application {private: shap ** s; public: myprogram (); ~ myprogram (); Double Run ();} DOUBLE RUN ();

MyProgram :: myprogram () {s = new shap * [5]; s [0] = new triangle (3.0, 5.0); s [1] = new Rectangle (5.0, 8.0); s [2] = new circle 8.5); s [3] = New TRAPEZOID (12.0, 8.0, 6.0); s [4] = New Square (6.8);

MyProgram :: ~ myprogram () {for (int i = 0; i <5; i ) delete s [i]; delete [] s;}

Double myprogram :: run () {double sum = compute (s, 5); return sum;}

Void main () {MyProgram M; cout << "Area's sum =" << m.run () << Endl;}

// Function Template Keyword TemplateInt Add (INT A, INT B) {Return (A B);} FLOAT ADD (Float A, Float B) {Return (A B);} 1, Define Template () {// } Parameter Type Table can be a built-in data type, or a class type, if it is a class type, add Class Keyword function template does not allocate storage Template t add (t a, t b) {RETURN (A B);}

EG1: #include using namespace std;

Template T MIN (T A, T B) {RETURN A

Void main () {INT x1 = 5, Y1 = 8; Double X2 = 5.76, Y2 = 5.77; CHAR X3 = 'm, Y3 =' n '; cout << "MIN (X1, Y1) =" << Min (x1, y1) << Endl; cout << "min (x2, y2) =" << min (x2, y2) << endl; cout << "min (x3, y3) =" << min X3, Y3) << endl;}

EG2: #include #include

Template void bubble (stype * item, int count);

Void main () {char str [] = "gtyfsarcb"; bubble (Str, (int) Strlen (str)); cout << "The sorted string is:" << str << Endl; int Nums [] = { 9, 4, 2, 6, 8, 5, 1}; bubble (NUMS, 7); cout << "The sorted number area:"; for (int i = 0; i <7; i ) cout << NUMS [i] << "; cout << Endl;} template void bubble (stype * item, int count) {register INT I, J; Stype T; for (i = 1; i = i; j--) {= f (item [j-1]> item [j]) {t = item [j-1]; item [j-1] = Item [J]; Item [J] = T;}}}

// Class Template Run: Integer Array: 1 2 3 4 5 6 7 8 9 10double Array: 3.14 6.28 9.42 12.56 15.7

Index Value of 20 is out-of-bounds.

#include #include template class array {private: int layth; atype * a; public: array (int size); ~ array () {delete [] A;} Atype & Operator [] (INT I);

Template array :: array (int size) {register int i; length = size; a = new atype [size]; if (! a) {cout << "cannot allocate array./n" EXIT (1);} for (i = 0; i

Template atype & array :: operator [】 {= (i <0 || i> length-1) {cout << "/ nindex value of"; cout << i << " IS out-of-bounds./n "; exit (1);} Return a [i];} void main () {array intob (10); array Doubleob (5);

COUT << "Integer Array:"; for (int i = 0; i <10; i ) intob [i] = i 1; for (i = 0; i <10; i ) cout << intob [i] << "; cout << ENDL;

COUT << "Double Array:"; Cout.Precision (4); for (i = 0; i <5; i ) Doubleob [i] = (double) (i 1) * 3.14; for (i = 0; i <5; i ) cout << Doubleob [i] << "" "" "" "" "; cout << endl; intob [20] = 30; Doubleob [20] = 89.5;}

// Class Integer {Private: INT VALUE; PUBLIC: INTEGER (INT I = 0) {Value = 0;} INTEGER OPERATOR (Integer & Other) // Member Function {Integer Temp; Temp.Value = Value Other .value; return;

Change the friend's function for Friend Integer Operator (Integer Other1, Integer Other2) {Integer Temp; Temp.Value = Other1.Value Other2.Value; Return Temp;

转载请注明原文地址:https://www.9cbs.com/read-35033.html

New Post(0)