C++ Tutorial For C Users
C++ Tutorial For C Users
htm
a = 456;
Input from keyboard and output to screen can be performed through cout << and cin >> :
#include <iostream.h>
void main()
1 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
cin >> a;
cin >> s;
cout << "Hello " << s << " you're " << a << " old." << endl;
cout << endl << endl << "Bye !" << endl;
#include <iostream.h>
void main ()
double a;
cin >> a;
2 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
a = (a + 1) / 2;
double c;
c = a * 5 + 1;
int i, j;
i = 0;
j = i + 1;
#include <iostream.h>
void main ()
double a = 12 * 3.25;
double b = a + 1.112;
a = a * 2 + b;
double c = a + b * a;
3 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
void main()
double a;
cin >> a;
int a = 1;
a = a * 10 + 4;
C++ allows to declare a variable inside the for loop declaration. It's like if the variable had
been declared just before the loop :
#include <iostream.h>
4 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
void main ()
cout << i;
A global variable can be accessed even if another variable with the same name has been
declared inside the function :
#include <iostream.h>
double a = 128;
void main ()
double a = 256;
5 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
void main ()
double a = 3.1415927;
double &b = a; // b IS a
b = 89;
cout << "a contains : " << a << endl; // Displays 89.
(If you are used at pointers and absolutely want to know what happens, simply think double
&b = a is translated to double *b = &a and all subsequent b are replaced by *b.)
The value of REFERENCE b cannot be changed afther its declaration. For example you cannot
write, a few lines further, &b = c expecting now b IS c. It won't work.
Everything is said on the declaration line of b. Reference b and variable a are married on that
line and nothing will separate them.
#include <iostream.h>
6 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
r = 100;
s = 200;
void main ()
double k, m;
k = 3;
m = 4;
cout << k << ", " << m << endl; // Displays 100, 4.
If you are used at pointers in C and wonder how exactly the program above works, here is how
the C++ compiler translates it (those who are not used at pointers, please skip this ugly piece of
code) :
#include <iostream.h>
*r = 100;
s = 200;
void main ()
double k, m;
7 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
k = 3;
m = 4;
cout << k << ", " << m << endl; // Displays 100, 4.
#include <iostream.h>
if (r > s) return r;
else return s;
void main ()
double k = 3;
double m = 7;
8 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
Again, provided you're used at pointer arithmetics and if you wonder how the program above
works, just think the compiler translated it into the following standard C program :
#include <iostream.h>
else return s;
void main ()
double k = 3;
double m = 7;
9 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
To end with, for people who have to deal with pointers yet do not like it, references are very
useful to un-pointer variables :
#include <iostream.h>
return &r;
void main()
double *a;
a = silly_function();
b += 1; // Great !
b += 4;
10 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
If they contain just simple lines of code, use no for loops or the like, C++ functions can be
declared INLINE. This means their code will be inserted right everywhere the function is used.
That's somehow like a macro. Main advantage is the program will be faster. A little drawback
is it will be bigger, because the full code of the function was inserted everywhere it is used :
#include <iostream.h>
#include <math.h>
void main ()
double k = 6, m = 9;
You know the classical structures of C : for, if, do, while, switch... C++ adds one more structure
named EXCEPTION :
11 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
#include <math.h>
void main ()
int a, b;
cin >> a;
try
throw a / 3;
b = result + 1;
12 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
try
if (a == 0) throw zero;
throw prime;
cout << "The number you typed is "<< conclusion << endl;
#include <iostream.h>
return a - b;
void main ()
13 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
One important advantage of C++ is the "operators overload". Different functions can have the
same name provided something allows to distinguish between them : number of parameters,
type of parameters...
#include <iostream.h>
return a + b;
return a - b;
void main ()
double m = 5, n = 3;
int k = 5, p = 3;
cout << test(m, n) << " , " << test(k, p) << endl;
The "operators overload" can be used to define the basic symbolic operators for new sorts of
parameters :
14 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
struct vector
double x;
double y;
};
vector r;
r.x = a * b.x;
r.y = a * b.y;
return r;
void main ()
m = 3.1415927 * k; // Magic !
cout << "(" << m.x << ", " << m.y << ")" << endl;
Besides multiplication, 43 other basic C++ operators can be overloaded, including +=, ++, the
matrix [], and so on...
The operation cout << is an overload of the binary shift of integers. That way the << symbol is
used a completely different way. It is thus possible to define the output of vectors :
15 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
struct vector
double x;
double y;
};
o << "(" << a.x << ", " << a.y << ")";
return o;
void main ()
vector a;
a.x = 35;
a.y = 23;
The keywords new and delete can be used to allocate and deallocate memory. They are much
more sweet than the functions malloc and free from standard C :
#include <iostream.h>
#include <string.h>
16 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
void main ()
*d = *d + 5;
// is given by pointer d.
// of 15 doubles
d[0] = 4456;
17 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
delete (d);
int n = 30;
d[i] = i;
delete (d);
char *s;
s = new char[100];
delete (s);
What is a CLASS ? Well, that's a struct yet with more possibilities. METHODS can be
defined. They are C++ functions dedicated to the class. Here is an example of such a class
definition :
18 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
class vector
public:
double x;
double y;
double surface ()
double s;
s = x * y;
if (s < 0) s = -s;
return s;
};
void main(void)
vector a;
a.x = 3;
a.y = 4;
Just like a function, a method can be an overload of any C++ operator, have any number of
parameters (yet one parameter is always implicit : the instance it acts upon), return any type of
19 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
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;
x = a - c;
y = b - d;
20 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
vector r;
r.x = x * a;
r.y = y * a;
return r;
};
vector a, b;
a.x = 3;
b.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;
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;
21 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
Very special and essential methods are the CONSTRUCTORS and DESTRUCTORS. They are
automatically called whenever an instance of a class is created or destroyed.
The constructor will initialize the variables of the instance, do some calculation, allocate some
memory for the instance, output some text... whatever is needed.
#include <iostream.h>
class vector
public:
double x;
double y;
x = 0;
y = 0;
x = a;
y = b;
22 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
};
void main ()
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
cout << "vector m : " << m.x << ", " << m.y << endl << endl;
erased
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
It is a good practice to try not to overload the constructors. Best is to declare only one
constructor and give it default parameters wherever possible :
#include <iostream.h>
class vector
public:
double x;
double y;
23 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
x = a;
y = b;
};
void main ()
vector k;
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
cout << "vector m : " << m.x << ", " << m.y << endl << endl;
vector p (3);
cout << "vector p : " << p.x << ", " << p.y << endl << endl;
The destructor is often not necessary. You can use it to do some calculation whenever an
instance is destroyed or output some text for debugging. But if variables of the instance point
towards some allocated memory then the role of the destructor is essential : it must free that
memory ! Here is an example of such an application :
#include <iostream.h>
#include <string.h>
class person
public:
char *name;
int age;
24 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
age = a;
cout << "Instance going to be deleted, 100 bytes freed" << endl;
};
void main ()
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;
cout << c.name << ", age " << c.age << endl << endl;
25 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
If you cast an object like a vector, everything will happen all right. For example if vector k
contains (4, 7), afther the cast m = k the vector m will contain (4, 7) too. Now suppose you're
playing with objects like the person class above. If you cast such person object p, r by writing
p = r it is necesary that some function does the necessary work to make p be a correct copy of r.
Otherwise the result will be catastrophic; a mess of pointers and lost data. The method that will
do that job is the COPY CONSTRUCTOR :
#include <iostream.h>
#include <string.h>
class person
public:
char *name;
int age;
age = a;
age = s.age;
~person ()
delete (name);
};
26 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
void main ()
person p;
cout << p.name << ", age " << p.age << endl << endl;
cout << k.name << ", age " << k.age << endl << endl;
p = k;
cout << p.name << ", age " << p.age << endl << endl;
cout << p.name << ", age " << p.age << endl << endl;
In all the examples above the methods are defined inside the class definition. That makes them
automatically be inline methods.
If a method cannot be inline, if you do not want it to be inline or if you want the class
definition contain the minimum of information, then you must just put the prototype of the
method inside the class and define the method below the class :
#include <iostream.h>
class vector
public:
double x;
double y;
27 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
};
double vector::surface()
double s = 0;
s = s + y;
return s;
void main ()
vector k;
k.x = 4;
k.y = 5;
When a method is applied to an instance, that method may use the instance's variables, modify
them... But sometimes it is necessary to know the address of the instance. No problem, the
keyword "this" is intended therefore :
#include <iostream.h>
#include <math.h>
class vector
28 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
public:
double x;
double y;
x = a;
y = b;
double module()
double length;
length = this->module();
x = x / length * a;
y = y / length * a;
};
void main ()
cout << "The module of vector c : " << c.module() << endl;
29 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
cout << "The module of vector c : " << c.module() << endl;
cout << "The module of vector c : " << c.module() << endl;
#include <iostream.h>
#include <math.h>
class vector
public:
double x;
double y;
x = a;
y = b;
double module ()
};
30 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
void main ()
vector s[1000];
s[23] = t[2];
#include <iostream.h>
#include <math.h>
class vector
public:
double x;
double y;
double module();
31 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
};
x = a;
y = b;
vector vector::operator - ()
double vector::module()
32 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
x = x / length * a;
y = y / length * a;
o << "(" << a.x << ", " << a.y << ")";
return o;
void main ()
vector a;
vector b;
a = c * 3;
a = b + c;
c = b - c + a + (b - a) * 7;
c = -c;
cout << "The module of vector c : " << c.module() << endl;
33 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double k;
It is also possible to define the sum of vectors without mentioning it inside the vector class
definition. Then it will not be a method of the class vector. Just a function that uses vectors :
In the example above of a full class definition, the multiplication of a vector by a double is
defined. Suppose we want the multiplication of a double by a vector be defined too. Then we
must to write an isolated function outside the class :
Of course the keywords new and delete work for class instances too. What's more, new
automatically calls the constructor in order to initialize the objects, and delete automatically
calls the destructor before deallocating the zone of memory the instance variables take :
34 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
#include <math.h>
class vector
public:
double x;
double y;
double module();
};
x = a;
y = b;
35 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
vector vector::operator - ()
double vector::module()
x = x / length * a;
y = y / length * a;
o << "(" << a.x << ", " << a.y << ")";
return o;
36 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
void main ()
r->x = 94;
r->y = 345;
*r = *r - c;
r->set_length(3);
*r = (-c * 3 + -*r * 4) * 5;
37 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
delete (r);
int n = 5;
delete (r);
A class' variable can be declared STATIC. Then only one instance of that variable exists,
shared by all instances of the class :
#include <iostream.h>
class vector
public:
38 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double x;
double y;
x = a;
y = b;
count = count + 1;
~vector()
count = count - 1;
};
void main ()
vector::count = 0;
vector a;
vector b;
r = new vector;
39 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
u = new vector;
delete (r);
delete (u);
A class variable can also be CONSTANT. That's just like static, except it is alocated a value
inside the class declaration and that value may not be modified :
#include <iostream.h>
class vector
public:
double x;
double y;
x = a;
y = b;
double cilinder_volume ()
return x * x / 4 * pi * y;
40 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
};
void main(void)
cout << "The value of pi : " << vector::pi << endl << endl;
A class may be DERIVED from another class. The new class INHERITS the variables and
methods of the BASE CLASS. Additional variables and/or methods can be added :
#include <iostream.h>
#include <math.h>
class vector
public:
double x;
double y;
x = a;
y = b;
double module()
41 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double surface()
return x * y;
};
public:
// m and n.
{ // cast to a trivector
x = a.x;
y = a.y;
z = 0;
42 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double volume ()
return this->surface() * z; // or x * y * z
};
void main()
cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl;
trivector k;
vector j;
vector *r;
r = &b;
43 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
In the program above, r->module() calculates the vector module, using x and y, because r has
been declared a vector pointer. The fact r actually points towards a trivector is not taken into
account. If you want the program to check the type of the pointed object and choose the
appropriate method, then you must declare that method VIRTUAL inside the base class.
(If at least one of the methods of the base class is virtual then a "header" of 4 bytes is added to
every instance of the classes. This allows the program to determine towards what a vector
actually points.)
#include <iostream.h>
#include <math.h>
class vector
public:
double x;
double y;
x = a;
y = b;
};
public:
double z;
44 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double module ()
};
void main()
cout << "a (4, 5) b (1, 2, 3)" << endl << endl;
vector *r;
r = &a;
r = &b;
45 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
test (a);
test (b);
vector &s = b;
Maybe you wonder if a class can be derived from more than one base class. Answer is yes :
#include <iostream.h>
#include <math.h>
class vector
public:
double x;
double y;
x = a;
y = b;
double surface()
46 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
};
class number
public:
double z;
number (double a)
z = a;
int is_negative ()
if (z < 0) return 1;
else return 0;
};
public:
double volume()
47 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
};
void main()
Class derivation allows to construct "more complicated" classes build above other classes.
There is another application of class derivation : allow the programmer to write generic
functions.
Suppose you define a base class with no variables. It makes no sense to use instances of that
class inside your program. But you write a function whose purpose is to sort instances of that
class. Well, that function will be able to sort any types of objects provided they belong to a class
derived from that base class ! The only condition is that inside every derived class definition all
methods the sort function needs are correctly defined :
#include <iostream.h>
#include <math.h>
class octopus
public :
};
48 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double r = a.module();
return r;
public:
double x;
double y;
x = a;
y = b;
double module()
};
public:
double n;
number (double a = 0)
n = a;
49 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double module()
if (n >= 0) return n;
};
void main ()
Perhaps you think "okay, that's a good idea to derive classes from the class octopus because
that way I can apply to instances of my classes methods and function that were designed a
generic way for the octopus class. But what if there exists another base class, named cuttlefish,
which has very interesting methods and functions too ? Do I have to make my choice between
octopus and cuttlefish when I want to derive a class ?" No, of course. A derived class can be at
the same time derived from octopus and from cuttlefish. That's POLYMORPHISM. The
derived class simply has to define the methods necessary for octopus together with the methods
necessary for cuttlefish :
class octopus
};
50 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
class cuttlefish
};
double x;
double y;
double module ()
int test ()
if (x > y) return 1;
else return 0;
Probably you wonder what all those public: keywords mean. They mean the variables or the
methods below them may be accessed and used everywhere in the program.
If you want them to be accessible only to methods of the class AND to methods of derived
classes then you must put the keyword protected: above them.
If you want variables or methods be accessible ONLY to methods of the class then you must
put the keyword private: above them.
The fact variables or methods are declared private or protected means no function external to
the class may access or use them. That's ENCAPSULATION. If you want to give to a specific
function the right to access those variables and methods then you must include that function's
prototype inside the class definition, preceded by the keyword friend.
Now let's talk about input/output. In C++ that's a very broad subject.
51 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
#include <iostream.h>
#include <fstream.h>
void main ()
fstream f;
f.open("c:\\test.txt", ios::out);
double a = 345;
f.close();
#include <iostream.h>
#include <fstream.h>
void main ()
fstream f;
char c;
52 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
f.open("c:\\test.txt", ios::in);
while (! f.eof() )
f.get(c); // Or c = f.get()
cout << c;
f.close();
Roughly said, it is possible to do on character arrays the same operations as on files. This is
very useful to convert data or manage memory arrays.
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
#include <math.h>
void main ()
char a[1024];
53 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
double v = 2;
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
void main ()
char a[1024];
double k, p;
b >> k;
k = k + 1;
54 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
b.seekg(0);
b >> k >> p;
This program performs formated output two different ways. Please note the width() and setw()
MODIFIERS are only effective on the next item output to the stream. The second next item
will not be influenced.
#include <iostream.h>
#include <iomanip.h>
void main ()
int i;
cout.width (7);
55 of 56 12.03.99 01:19
C++ Tutorial file:///C|/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
You now have a basic knowledge about C++. Inside good books you will learn many more
things. The file management system is very powerful. It has many other possibilities than those
illustrated here. There is also a lot more to say about classes : template classes, virtual classes...
In order to work correctly with C++ you will need a good reference book, just like you needed
one for C.
You will also need information on how C++ is used in your particular domain of activity. The
standards, the global approach, the tricks, the typical problems encountered and their
solutions...
56 of 56 12.03.99 01:19