using namespace std;
#include <iostream> // This is a key C++ library
#include <cmath> // The standard C library math.h
int main ()
{
double a;
a = 1.2;
a = sin (a);
cout << a << endl;
return 0;
}
| Output |
| 0.932039 |
выкарыстанне імёнаў STD; // Using the standard library namespace.
#include <iostream> // The iostream library is often used.
int main () // The program's main routine.
{
double a; // Declaration of variable a.
a = 456.47;
a = a + a * 21.5/100; // A calculation.
cout << a << endl; // Display the content of a.
return 0; // Program end.
}
| Выхад |
| 554.611 |
using namespace std;
#include <iostream>
int main()
{
int a; // a is an integer variable
char s [100]; // s points to a string of max 99 characters
cout << "This is a sample program." << endl;
cout << endl; // Just a line feed (end of line)
cout << "Type your age : ";
cin >> a;
cout << "Type your name: ";
cin >> s;
cout << endl;
cout << "Hello " << s << " you're " << a << " old." << endl;
cout << endl << endl << "Bye!" << endl;
return 0;
}
| Выхад |
| Гэта прыклад праграмы. Калі ласка, увядзіце свой ??узрост: 12 Калі ласка, увядзіце імя: Эдмонд Прывітанне Эдманд вы 12 старых. Пакуль! |
using namespace std;
#include <iostream>
int main ()
{
double a;
cout << "Hello, this is a test program." << endl;
cout << "Type parameter a: ";
cin >> a;
a = (a + 1)/2;
double c;
c = a * 5 + 1;
cout << "c contains : " << c << endl;
int i, j;
i = 0;
j = i + 1;
cout << "j contains : " << j << endl;
return 0;
}
| Выхад |
| Добры дзень, гэта тэст праграмы. Type parameter a: 7 c contains : 21 j contains : 1 |
using namespace std;
#include <iostream>
int main ()
{
double a;
cout << "Type a number: ";
cin >> a;
{
int a = 1;
a = a * 10 + 4;
cout << "Local number: " << a << endl;
}
cout << "You typed: " << a << endl;
return 0;
}
| Выхад |
| Калі ласка, увядзіце лік: 9 Мясцовы нумар: 14 Вы ўвялі: 9 |
using namespace std;
#include <iostream>
int main ()
{
double a = 12 * 3.25;
double b = a + 1.112;
cout << "a contains: " << a << endl;
cout << "b contains: " << b << endl;
a = a * 2 + b;
double c = a + b * a;
cout << "c contains: " << c << endl;
return 0;
}
| Output |
| a contains: 39 b contains: 40.112 c contains: 4855.82 |
using namespace std;
#include <iostream>
int main ()
{
int i; // Simple declaration of i
i = 487;
for (int i = 0; i < 4; i++) // Local declaration of i
{
cout << i << endl; // This outputs 0, 1, 2 and 3
}
cout << i << endl; // This outputs 487
return 0;
}
| Output |
| 0 1 2 3 487 |
using namespace std;
#include <iostream>
int main ()
{
for (int i = 0; i < 4; i++)
{
cout << i << endl;
}
cout << i << endl; // Bad practice!
i += 5; // Bad practice!
cout << i << endl; // Bad practice!
return 0;
}
| Gnu C + + кампілятар скардзіцца |
| t.cpp: У "функцыя" Int п (): t.cpp: 12: памылка: пошук імя "Я" змяніў новых ISO "за" агляднага t.cpp: 7: памылка: выкарыстанне састарэлых абавязковых на "Я" |
using namespace std;
#include <iostream>
double a = 128;
int main ()
{
double a = 256;
cout << "Local a: " << a << endl;
cout << "Global a: " << ::a << endl;
return 0;
}
| Output |
| Local a: 256 Global a: 128 |
using namespace std;
#include <iostream>
int main ()
{
double a = 3.1415927;
double &b = a; // b is a
b = 89;
cout << "a contains: " << a << endl; // Displays 89.
return 0;
}
| Output |
| a contains: 89 |
using namespace std;
#include <iostream>
void change (double &r, double s)
{
r = 100;
s = 200;
}
int main ()
{
double k, m;
k = 3;
m = 4;
change (k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
return 0;
}
| Output |
| 100, 4 |
using namespace std;
#include <iostream>
void change (double *r, double s)
{
*r = 100;
s = 200;
}
int main ()
{
double k, m;
k = 3;
m = 4;
change (&k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
return 0;
}
| Output |
| 100, 4 |
using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11
cout << endl;
return 0;
}
| Output |
| k: 3 m: 7 k: 3 m: 10 k: 3 m: 11 |
using namespace std;
#include <iostream>
double *biggest (double *r, double *s)
{
if (*r > *s) return r;
else return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
(*(biggest (&k, &m))) = 10;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
(*(biggest (&k, &m))) ++;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
return 0;
}
| Output |
| k: 3 m: 7 k: 3 m: 10 k: 3 m: 11 |
using namespace std;
#include <iostream>
double *silly_function () // This function returns a pointer to a double
{
static double r = 342;
return &r;
}
int main ()
{
double *a;
a = silly_function();
double &b = *a; // Now b is the double towards which a points!
b += 1; // Great!
b = b * b; // No need to write *a everywhere!
b += 4;
cout << "Content of *a, b and r: " << b << endl;
return 0;
}
| Output |
| Content of *a, b and r: 117653 |
using namespace std;
#include <iostream>
#include <cmath>
namespace first
{
int a;
int b;
}
namespace second
{
double a;
double b;
}
int main ()
{
first::a = 2;
first::b = 5;
second::a = 6.453;
second::b = 4.1e4;
cout << first::a + second::a << endl;
cout << first::b + second::b << endl;
return 0;
}
| Output |
| 8.453 41005 |
using namespace std;
#include <iostream>
#include <cmath>
inline double hypothenuse (double a, double b)
{
return sqrt (a * a + b * b);
}
int main ()
{
double k = 6, m = 9;
// Next two lines produce exactly the same code:
cout << hypothenuse (k, m) << endl;
cout << sqrt (k * k + m * m) << endl;
return 0;
}
| Output |
| 10.8167 10.8167 |
using namespace std;
#include <iostream>
#include <cmath>
int main ()
{
int a, b;
cout << "Type a number: ";
cin >> a;
cout << endl;
try
{
if (a > 100) throw 100;
if (a < 10) throw 10;
throw a/3;
}
catch (int result)
{
cout << "Result is: " << result << endl;
b = result + 1;
}
cout << "b contains: " << b << endl;
cout << endl;
// another example of exception use:
char zero [] = "zero";
char pair [] = "pair";
char notprime [] = "not prime";
char prime [] = "prime";
try
{
if (a == 0) throw zero;
if ((a/2) * 2 == a) throw pair;
for (int i = 3; i <= sqrt (a); i++)
{
if ((a/i) * i == a) throw notprime;
}
throw prime;
}
catch (char *conclusion)
{
cout << "The number you typed is "<< conclusion << endl;
}
cout << endl;
return 0;
}
| Выхад |
| Калі ласка, увядзіце лік: 5 Вынік: 10 B змяшчае: 11 Нумар, які набраў простае |
using namespace std;
#include <iostream>
double test (double a, double b = 7)
{
return a - b;
}
int main ()
{
cout << test (14, 5) << endl; // Displays 14 - 5
cout << test (14) << endl; // Displays 14 - 7
return 0;
}
| Output |
| 9 7 |
using namespace std;
#include <iostream>
double test (double a, double b)
{
return a + b;
}
int test (int a, int b)
{
return a - b;
}
int main ()
{
double m = 7, n = 4;
int k = 5, p = 3;
cout << test(m, n) << ", " << test(k, p) << endl;
return 0;
}
| Output |
| 11, 2 |
using namespace std;
#include <iostream>
struct vector
{
double x;
double y;
};
vector operator * (double a, vector b)
{
vector r;
r.x = a * b.x;
r.y = a * b.y;
return r;
}
int main ()
{
vector k, m; // No need to type "struct vector"
k.x = 2; // To be able to write
k.y = -1; // k = vector (2, -1)
// see chapter 19.
m = 3.1415927 * k; // Magic!
cout << "(" << m.x << ", " << m.y << ")" << endl;
return 0;
}
| Output |
| (6.28319, -3.14159) |
using namespace std;
#include <iostream>
struct vector
{
double x;
double y;
};
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
int main ()
{
vector a;
a.x = 35;
a.y = 23;
cout << a << endl; // Displays (35, 23)
return 0;
}
| Output |
| (35, 23) |
using namespace std;
#include <iostream>
template <class ttype>
ttype minimum (ttype a, ttype b)
{
ttype r;
r = a;
if (b < a) r = b;
return r;
}
int main ()
{
int i1, i2, i3;
i1 = 34;
i2 = 6;
i3 = minimum (i1, i2);
cout << "Most little: " << i3 << endl;
double d1, d2, d3;
d1 = 7.9;
d2 = 32.1;
d3 = minimum (d1, d2);
cout << "Most little: " << d3 << endl;
cout << "Most little: " << minimum (d3, 3.5) << endl;
return 0;
}
| Выхад |
| Большасць маленькіх: 6 Большасць маленькіх: 7,9 Большасць маленькіх: 3,5 |
using namespace std;
#include <iostream>
template <class type1, class type2>
type1 minimum (type1 a, type2 b)
{
type1 r, b_converted;
r = a;
b_converted = (type1) b;
if (b_converted < a) r = b_converted;
return r;
}
int main ()
{
int i;
double d;
i = 45;
d = 7.41;
cout << "Most little: " << minimum (i, d) << endl;
cout << "Most little: " << minimum (d, i) << endl;
cout << "Most little: " << minimum ('A', i) << endl;
return 0;
}
| Выхад |
| Большасць маленькіх: 7 Большасць маленькіх: 7,41 Большасць маленькіх: - |
using namespace std;
#include <iostream>
#include <cstring>
int main ()
{
double *d; // d is a variable whose purpose
// is to contain the address of a
// zone where a double is located
d = new double; // new allocates a zone of memory
// large enough to contain a double
// and returns its address.
// That address is stored in d.
*d = 45.3; // The number 45.3 is stored
// inside the memory zone
// whose address is given by d.
cout << "Type a number: ";
cin >> *d;
*d = *d + 5;
cout << "Result: " << *d << endl;
delete d; // delete deallocates the
// zone of memory whose address
// is given by pointer d.
// Now we can no more use that zone.
d = new double[15]; // allocates a zone for an array
// of 15 doubles. Note each 15
// double will be constructed.
// This is pointless here but it
// is vital when using a data type
// that needs its constructor be
// used for each instance.
d[0] = 4456;
d[1] = d[0] + 567;
cout << "Content of d[1]: " << d[1] << endl;
delete [] d; // delete [] will deallocate the
// memory zone. Note each 15
// double will be destructed.
// This is pointless here but it
// is vital when using a data type
// that needs its destructor be
// used for each instance (the ~
// method). Using delete without
// the [] would deallocate the
// memory zone without destructing
// each of the 15 instances. That
// would cause memory leakage.
int n = 30;
d = new double[n]; // new can be used to allocate an
// array of random size.
for (int i = 0; i < n; i++)
{
d[i] = i;
}
delete [] d;
char *s;
s = new char[100];
strcpy (s, "Hello!");
cout << s << endl;
delete [] s;
return 0;
}
| Output |
| Type a number: 6 Result: 11 Content of d[1]: 5023 Hello! |
using namespace std;
#include <iostream>
struct vector
{
double x;
double y;
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.x = 3;
a.y = 4;
cout << "The surface of a: " << a.surface() << endl;
return 0;
}
| Output |
| The surface of a: 12 |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.x = 3;
a.y = 4;
cout << "The surface of a: " << a.surface() << endl;
return 0;
}
| Выхад |
| Паверхня: 12 |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
vector its_oposite()
{
vector r;
r.x = -x;
r.y = -y;
return r;
}
void be_oposited()
{
x = -x;
y = -y;
}
void be_calculated (double a, double b, double c, double d)
{
x = a - c;
y = b - d;
}
vector operator * (double a)
{
vector r;
r.x = x * a;
r.y = y * a;
return r;
}
};
int main ()
{
vector a, b;
a.x = 3;
a.y = 5;
b = a.its_oposite();
cout << "Vector a: " << a.x << ", " << a.y << endl;
cout << "Vector b: " << b.x << ", " << b.y << endl;
b.be_oposited();
cout << "Vector b: " << b.x << ", " << b.y << endl;
a.be_calculated (7, 8, 3, 2);
cout << "Vector a: " << a.x << ", " << a.y << endl;
a = b * 2;
cout << "Vector a: " << a.x << ", " << a.y << endl;
a = b.its_oposite() * 2;
cout << "Vector a: " << a.x << ", " << a.y << endl;
cout << "x of oposite of a: " << a.its_oposite().x << endl;
return 0;
}
| Выхад |
| Вектарны: 3, 5 Вектарны B: -3, -5 Вектарны B: 3, 5 Вектарны: 4, 6 Вектарны: 6, 10 Вектарны: -6, -10 х супрацьлеглае: 6 |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
vector () // same name as class
{
x = 0;
y = 0;
}
vector (double a, double b)
{
x = a;
y = b;
}
};
int main ()
{
vector k; // vector () is called
cout << "vector k: " << k.x << ", " << k.y << endl << endl;
vector m (45, 2); // vector (double, double) is called
cout << "vector m: " << m.x << ", " << m.y << endl << endl;
k = vector (23, 2); // vector created, copied to k, then erased
cout << "vector k: " << k.x << ", " << k.y << endl << endl;
return 0;
}
| Выхад |
| вектар K: 0, 0 вектар м: 45, 2 вектара да: 23, 2 |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
};
int main ()
{
vector k;
cout << "vector k: " << k.x << ", " << k.y << endl << endl;
vector m (45, 2);
cout << "vector m: " << m.x << ", " << m.y << endl << endl;
vector p (3);
cout << "vector p: " << p.x << ", " << p.y << endl << endl;
return 0;
}
| Выхад |
| вектар K: 0, 0 вектар м: 45, 2 вектара р: 3, 0 |
using namespace std;
#include <iostream>
#include <cstring>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char [100]; // better than malloc!
strcpy (name, n);
age = a;
cout << "Instance initialized, 100 bytes allocated" << endl;
}
~person () // The destructor
{
delete name; // instead of free!
// delete [] name would be more
// academic but it is not vital
// here since the array contains
// no C++ sub-objects that need
// to be deleted.
cout << "Instance going to be deleted, 100 bytes freed" << endl;
}
};
int main ()
{
cout << "Hello!" << endl << endl;
person a;
cout << a.name << ", age " << a.age << endl << endl;
person b ("John");
cout << b.name << ", age " << b.age << endl << endl;
b.age = 21;
cout << b.name << ", age " << b.age << endl << endl;
person c ("Miki", 45);
cout << c.name << ", age " << c.age << endl << endl;
cout << "Bye!" << endl << endl;
return 0;
}
| Выхад |
| Прывітанне! Асобнік ініцыялізаваны, 100 байтаў, вылучаных не імя, узрост 0 Асобнік ініцыялізаваны, 100 байтаў, вылучаных Джон, ва ўзросце ад 0 Джон, 1921 г. Асобнік ініцыялізаваны, 100 байтаў, вылучаных Мікі, 45 гадоў Пакуль! Асобнік будзе выдалены, 100 байт вызваленыя Асобнік будзе выдалены, 100 байт вызваленыя Асобнік будзе выдалены, 100 байт вызваленыя |
using namespace std;
#include <iostream>
#include <cstdlib>
class array
{
public:
int size;
double *data;
array (int s)
{
size = s;
data = new double [s];
}
~array ()
{
delete [] data;
}
double &operator [] (int i)
{
if (i < 0 || i >= size)
{
cerr << endl << "Out of bounds" << endl;
exit (EXIT_FAILURE);
}
else return data [i];
}
};
int main ()
{
array t (5);
t[0] = 45; // OK
t[4] = t[0] + 6; // OK
cout << t[4] << endl; // OK
t[10] = 7; // error!
return 0;
}
| Выхад |
| 51 За межы |
using namespace std;
#include <iostream>
#include <cstring>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100];
strcpy (name, n);
age = a;
}
person (const person &s) // The COPY CONSTRUCTOR
{
name = new char[100];
strcpy (name, s.name);
age = s.age;
}
person& operator= (const person &s) // overload of =
{
strcpy (name, s.name);
age = s.age;
return *this;
}
~person ()
{
delete [] name;
}
};
void modify_person (person& h)
{
h.age += 7;
}
person compute_person (person h)
{
h.age += 7;
return h;
}
int main ()
{
person p;
cout << p.name << ", age " << p.age << endl << endl;
// output: no name, age 0
person k ("John", 56);
cout << k.name << ", age " << k.age << endl << endl;
// output: John, age 56
p = k;
cout << p.name << ", age " << p.age << endl << endl;
// output: John, age 56
p = person ("Bob", 10);
cout << p.name << ", age " << p.age << endl << endl;
// output: Bob, age 10
// Neither the copy constructor nor the overload
// of = are needed for this operation that modifies
// p since just the reference towards p is passed to
// the function modify_person:
modify_person (p);
cout << p.name << ", age " << p.age << endl << endl;
// output: Bob, age 17
// The copy constructor is called to pass a complete
// copy of p to the function compute_person. The
// function uses that copy to make its computations
// then a copy of that modified copy is made to
// return the result. Finaly the overload of = is
// called to paste that second copy inside k:
k = compute_person (p);
cout << p.name << ", age " << p.age << endl << endl;
// output: Bob, age 17
cout << k.name << ", age " << k.age << endl << endl;
// output: Bob, age 24
return 0;
}
| Выхад |
| не імя, узрост 0 Джон, 56 гадоў Джон, 56 гадоў Боб, 10 гадоў Боб, 17 гадоў Боб, 17 гадоў Боб, 24 гадоў |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
double surface(); // The; and no {} show it is a prototype
};
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
int main ()
{
vector k;
k.x = 4;
k.y = 5;
cout << "Surface: " << k.surface() << endl;
return 0;
}
| Output |
| Surface: 20 |
клас вектар
{
грамадскасці:
Double X;
двайны ў;
двайны паверхні ();
};
using namespace std;
#include "vector.h"
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
using namespace std;
#include <iostream>
#include "vector.h"
int main ()
{
vector k;
k.x = 4;
k.y = 5;
cout << "Surface: " << k.surface() << endl;
return 0;
}
test20: main.o vector.o
G + + main.o vector.o-O test20
main.o: main.cpp vector.h
G + +-C main.cpp
vector.o: vector.cpp vector.h
G + +-C vector.cpp
ўсё: test20
test20: main.o vector.o
G + + main.o vector.o-O test20
main.o: main.cpp vector.h
G + +-C main.cpp
vector.o: vector.cpp vector.h
G + +-C vector.cpp
Чысціня:
RM-F *. Аб test20 * # *
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x * x + y * y);
}
void set_length (double a = 1)
{
double length;
length = this->module();
x = x/length * a;
y = y/length * a;
}
};
int main ()
{
vector c (3, 5);
cout << "The module of vector c: " << c.module() << endl;
c.set_length(2); // Transforms c in a vector of size 2.
cout << "The module of vector c: " << c.module() << endl;
c.set_length(); // Transforms b in an unitary vector.
cout << "The module of vector c: " << c.module() << endl;
return 0;
}
| Выхад |
| Модуль вектара C: 5,83095 Модуль вектара з: 2 Модуль вектара З: 1 |
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module ()
{
return sqrt (x * x + y * y);
}
};
int main ()
{
vector s [1000];
vector t[3] = {vector(4, 5), vector(5, 5), vector(2, 4)};
s[23] = t[2];
cout << t[0].module() << endl;
return 0;
}
| Выхад |
| 6.40312 |
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double = 0, double = 0);
vector operator + (vector);
vector operator - (vector);
vector operator - ();
vector operator * (double a);
double module();
void set_length (double = 1);
};
vector::vector (double a, double b)
{
x = a;
y = b;
}
vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
return vector (-x, -y);
}
vector vector::operator * (double a)
{
return vector (x * a, y * a);
}
double vector::module()
{
return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
double length = this->module();
x = x/length * a;
y = y/length * a;
}
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
int main ()
{
vector a;
vector b;
vector c (3, 5);
a = c * 3;
a = b + c;
c = b - c + a + (b - a) * 7;
c = -c;
cout << "The module of vector c: " << c.module() << endl;
cout << "The content of vector a: " << a << endl;
cout << "The oposite of vector a: " << -a << endl;
c.set_length(2); // Transforms c in a vector of size 2.
a = vector (56, -3);
b = vector (7, c.y);
b.set_length(); // Transforms b in an unitary vector.
cout << "The content of vector b: " << b << endl;
double k;
k = vector(1, 1).module(); // k will contain 1.4142.
cout << "k contains: " << k << endl;
return 0;
}
| Выхад |
| Модуль вектара C: 40,8167 Змест вектар: (3, 5) Процілегла вектару: (-3, -5) Змест вектар B: (0.971275, 0,23796) Да змяшчае: 1,41421 |
вектарны аператар + (вектар, вектар B)
{
вяртанне вектара (ах + BX, ау + BY);
}
вектарны аператар * (двайны, вектар B)
{
вяртанне вектар (* BX, * па);
}
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double = 0, double = 0);
vector operator + (vector);
vector operator - (vector);
vector operator - ();
vector operator * (double);
double module();
void set_length (double = 1);
};
vector::vector (double a, double b)
{
x = a;
y = b;
}
vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
return vector (-x, -y);
}
vector vector::operator * (double a)
{
return vector (a * x, a * y);
}
double vector::module()
{
return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
vector &the_vector = *this;
double length = the_vector.module();
x = x/length * a;
y = y/length * a;
}
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
int main ()
{
vector c (3, 5);
vector *r; // r is a pointer to a vector.
r = new vector; // new allocates the memory necessary
cout << *r << endl; // to hold a vectors' variable,
// calls the constructor who will
// initialize it to 0, 0. Then finally
// new returns the address of the vector.
r->x = 94;
r->y = 345;
cout << *r << endl;
*r = vector (94, 343);
cout << *r << endl;
*r = *r - c;
r->set_length(3);
cout << *r << endl;
*r = (-c * 3 + -*r * 4) * 5;
cout << *r << endl;
delete r; // Calls the vector destructor then
// frees the memory.
r = &c; // r points towards vector c
cout << *r << endl;
r = new vector (78, 345); // Creates a new vector.
cout << *r << endl; // The constructor will initialise
// the vector's x and y at 78 and 345
cout << "x component of r: " << r->x << endl;
cout << "x component of r: " << (*r).x << endl;
delete r;
r = new vector[4]; // creates an array of 4 vectors
r[3] = vector (4, 5);
cout << r[3].module() << endl;
delete [] r; // deletes the array
int n = 5;
r = new vector[n]; // Cute!
r[1] = vector (432, 3);
cout << r[1] << endl;
delete [] r;
return 0;
}
| Output |
| (0, 0) (94, 345) (94, 343) (0.77992, 2.89685) (-60.5984, -132.937) (3, 5) (78, 345) x component of r: 78 x component of r: 78 6.40312 (432, 3) |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
static int count;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
count++;
}
~vector()
{
count--;
}
};
int vector::count = 0;
int main ()
{
cout << "Number of vectors:" << endl;
vector a;
cout << vector::count << endl;
vector b;
cout << vector::count << endl;
vector *r, *u;
r = new vector;
cout << vector::count << endl;
u = new vector;
cout << a.count << endl;
delete r;
cout << vector::count << endl;
delete u;
cout << b.count << endl;
return 0;
}
| Output |
| 1 2 3 4 3 2 |
using namespace std;
#include <iostream>
class vector
{
public:
double x;
double y;
const static double pi = 3.1415927;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double cilinder_volume ()
{
return x * x/4 * pi * y;
}
};
int main()
{
cout << "The value of pi: " << vector::pi << endl << endl;
vector k (3, 4);
cout << "Result: " << k.cilinder_volume() << endl;
return 0;
}
| Выхад |
| Значэнне ліку "пі": 3,14159 Вынік: 28,2743 |
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x*x + y*y);
}
double surface()
{
return x * y;
}
};
class trivector: public vector // trivector is derived from vector
{
public:
double z; // added to x and y from vector
trivector (double m=0, double n=0, double p=0): vector (m, n)
{
z = p; // Vector constructor will
} // be called before trivector
// constructor, with parameters
// m and n.
trivector (vector a) // What to do if a vector is
{ // cast to a trivector
x = a.x;
y = a.y;
z = 0;
}
double module () // define module() for trivector
{
return sqrt (x*x + y*y + z*z);
}
double volume ()
{
return this->surface() * z; // or x * y * z
}
};
int main ()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl;
cout << "Surface of a: " << a.surface() << endl;
cout << "Volume of b: " << b.volume() << endl;
cout << "Surface of base of b: " << b.surface() << endl;
cout << "Module of a: " << a.module() << endl;
cout << "Module of b: " << b.module() << endl;
cout << "Module of base of b: " << b.vector::module() << endl;
trivector k;
k = a; // thanks to trivector(vector) definition
// copy of x and y, k.z = 0
vector j;
j = b; // copy of x and y. b.z leaved out
vector *r;
r = &b;
cout << "Surface of r: " << r->surface() << endl;
cout << "Module of r: " << r->module() << endl;
return 0;
}
| Выхад |
| (4, 5) B (1, 2, 3) * R = B Паверхня: 20 Аб'ём B: 6 Паверхня базе B: 2 Модуль: 6,40312 Модуль B: 3,74166 Модуль базы B: 2,23607 Паверхня г: 2 Модуль R: 2,23607 |
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
virtual double module()
{
return sqrt (x*x + y*y);
}
};
class trivector: public vector
{
public:
double z;
trivector (double m = 0, double n = 0, double p = 0)
{
x = m; // Just for the game,
y = n; // here I do not call the vector
z = p; // constructor and I make the
} // trivector constructor do the
// whole job. Same result.
double module ()
{
return sqrt (x*x + y*y + z*z);
}
};
void test (vector &k)
{
cout << "Test result: " << k.module() << endl;
}
int main ()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3)" << endl << endl;
vector *r;
r = &a;
cout << "module of vector a: " << r->module() << endl;
r = &b;
cout << "module of trivector b: " << r->module() << endl;
test (a);
test (b);
vector &s = b;
cout << "module of trivector b: " << s.module() << endl;
return 0;
}
| Выхад |
| (4, 5) B (1, 2, 3) модуль вектара: 6,40312 Модуль тривектором B: 3,74166 Вынік тэсту: 6,40312 Вынік тэсту: 3,74166 Модуль тривектором B: 3,74166 |
using namespace std;
#include <iostream>
#include <cmath>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double surface()
{
return fabs (x * y);
}
};
class number
{
public:
double z;
number (double a)
{
z = a;
}
int is_negative ()
{
if (z < 0) return 1;
else return 0;
}
};
class trivector: public vector, public number
{
public:
trivector(double a=0, double b=0, double c=0): vector(a,b), number(c)
{
} // The trivector constructor calls the vector
// constructor, then the number constructor,
// and in this example does nothing more.
double volume()
{
return fabs (x * y * z);
}
};
int main ()
{
trivector a(2, 3, -4);
cout << a.volume() << endl;
cout << a.surface() << endl;
cout << a.is_negative() << endl;
return 0;
}
| Выхад |
| 24 6 1 |
using namespace std;
#include <iostream>
#include <cmath>
class octopus
{
public:
virtual double module() = 0; // = 0 implies function is not
// defined. This makes instances
// of this class cannot be declared.
};
double biggest_module (octopus &a, octopus &b, octopus &c)
{
double r = a.module();
if (b.module() > r) r = b.module();
if (c.module() > r) r = c.module();
return r;
}
class vector: public octopus
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x * x + y * y);
}
};
class number: public octopus
{
public:
double n;
number (double a = 0)
{
n = a;
}
double module()
{
if (n >= 0) return n;
else return -n;
}
};
int main ()
{
vector k (1,2), m (6,7), n (100, 0);
number p (5), q (-3), r (-150);
cout << biggest_module (k, m, n) << endl;
cout << biggest_module (p, q, r) << endl;
cout << biggest_module (p, q, n) << endl;
return 0;
}
| Выхад |
| 100 150 100 |
class octopus
{
virtual double module() = 0;
};
class cuttlefish
{
virtual int test() = 0;
};
class vector: public octopus, public cuttlefish
{
double x;
double y;
double module ()
{
return sqrt (x * x + y * y);
}
int test ()
{
if (x > y) return 1;
else return 0;
}
}
using namespace std;
#include <iostream>
class vector
{
protected:
double x;
double y;
public:
void set_x (int n)
{
x = n;
}void set_y (int n)double surface ()
{
y = n;
}
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.set_x (3);
a.set_y (4);
cout << "The surface of a: " << a.surface() << endl;
return 0;
}
| Выхад |
| Паверхня: 12 |
using namespace std;
#include <iostream>
class vector
{
protected:
double x;
double y;
public:
void set_x (int n)
{
x = n;
}void set_y (int n)
{
y = n;
}
double get_x ()
{
return x;
}double surface ()double get_y ()
{
return y;
}
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.set_x (3);
a.set_y (4);
cout << "The surface of a: " << a.surface() << endl;
cout << "The width of a: " << a.get_x() << endl;
cout << "The height of a: " << a.get_y() << endl;
return 0;
}
| Выхад |
| Паверхня: 12 Шырыня: 3 Вышыня: 4 |
using namespace std;
#include <iostream>
int sign (double n)
{
if (n >= 0) return 1;
return -1;
}
class vector
{
protected:
double x;
double y;
public:
void set_x (int n)
{
x = n;
if (sign (x) != sign(y)) y = -y;
}void set_y (int n)
{
y = n;
if (sign (y) != sign(x)) x = -x;
}
double get_x ()
{
return x;
}double surface ()double get_y ()
{
return y;
}
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.set_x (-3);
a.set_y (4);
cout << "The surface of a: " << a.surface() << endl;
cout << "The width of a: " << a.get_x() << endl;
cout << "The height of a: " << a.get_y() << endl;
return 0;
}
| Выхад |
| Паверхня: 12 Шырыня: 3 Вышыня: 4 |
using namespace std;
#include <iostream>
#include <fstream>
int main ()
{
fstream f;
f.open("test.txt", ios::out);
f << "This is a text output to a file." << endl;
double a = 345;
f << "A number: " << a << endl;
f.close();
return 0;
}
Змест файла test.txt |
| Гэта тэкставы выснова ў файл. Нумар: 345 |
using namespace std;
#include <iostream>
#include <fstream>
int main ()
{
fstream f;
char c;
cout << "What's inside the test.txt file" << endl;
cout << endl;
f.open("test.txt", ios::in);
while (! f.eof() )
{
f.get(c); // Or c = f.get()
cout << c;
}
f.close();
return 0;
}
| Выхад |
| Гэта тэкставы выснова ў файл. Нумар: 345 |
using namespace std;
#include <iostream>
#include <strstream>
#include <cstring>
#include <cmath>
int main ()
{
char a[1024];
ostrstream b(a, 1024);
b.seekp(0); // Start from first char.
b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl )
// ends is simply the
// null character '\0'
cout << a << endl;
double v = 2;
strcpy (a, "A sinus: ");
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
cout << a << endl;
return 0;
}
| Выхад |
| 2 + 2 = 4 Сінус: грэх (2) = 0,909297 |
using namespace std;
#include <iostream>
#include <strstream>
#include <cstring>
int main ()
{
char a[1024];
istrstream b(a, 1024);
strcpy (a, "45.656");
double k, p;
b.seekg(0); // Start from first character.
b >> k;
k = k + 1;
cout << k << endl;
strcpy (a, "444.23 56.89");
b.seekg(0);
b >> k >> p;
cout << k << ", " << p + 1 << endl;
return 0;
}
| Выхад |
| 46.656 444,23, 57,89 |
using namespace std;
#include <iostream>
#include <iomanip>
int main ()
{
int i;
cout << "A list of numbers:" << endl;
for (i = 1; i <= 1024; i *= 2)
{
cout.width (7);
cout << i << endl;
}
cout << "A table of numbers:" << endl;
for (i = 0; i <= 4; i++)
{
cout << setw(3) << i << setw(5) << i * i * i << endl;
}
return 0;
}
| Выхад |
| Спіс нумароў: 1 2 4 8 16 32 64 128 256 512 1024 Табліца адпаведнікаў нумароў: 0 0 1 1 2 8 3 27 4 64 |