Project „New Search Engine“

C++ - Summary

 

Introduction

 

C++ is structural, object oriented and effective programming language.

Links: http://www.milionovastranka.net/www_odkazy.htm#c_plus_plus

Compilers links: http://www.milionovastranka.net/www_odkazy.htm#c_plus_plus_prekladace

Probably best compilers are DevC++ for Windows and GNU (G++,GCC) for Linux (Ubuntu).

 

Compilation

 

Do not use parameter –c in g++ of Ubuntu.

Main

g++ hello.cpp -o hello

Main with library

g++ example_012.cpp example_library.o -o example_012

Library

g++ example_library.cpp –o example_library.o

Windows – main and library

- library must have ending .lib.

- in main, use command #include "example_library.lib",

 

Structure of program

Include

system libraries: #include <name>

user libraries: #include “name”

System libraries - examples:

string (strings)

cstring (extended strings)

sstream (strings, conversions)

stdlib.h (standard library: format conversions…)

stdio.h (basic input/output – fopen, fclose, fget, fprint, fprintf…)

iostream (keyboard, display etc.: cin, cout, getline)

fstream (files: .open, .close, .read, .write…)

iomanip (formatting when writing: number length, number of decimal places…)

Names of libraries are written without .h or with .h, as stated above.

Using

Example: using namespace std; (basic library for names, identifiers…).

Statements are finished by ; (exceptions are some initial statements, e.g. #include).

Blocks are within { and }, after } is no ;.

Its good to offset begins of lines in block by spaces (1-2).

Comments: // at begin of line or between /* and */.

Declaration of main program: main() or int main() (for returning parameter using: return result_of_program;) or (example): int APIENTRY WinMain(HINSTANCE hInstx,HINSTANCE hPrev,LPSTR line,int CmdShow) (APIENTRY is for running C++ programs from other programs, for Windows only ).

Declaration of procedures and functions: before main or after main (stating head of procedure or function before main).

 

Constants

¨

‘a’, “content of string“, false, true, 1, -1, 3.14, -3.14, 0.314e1, -31.4e-1.

Characters are within ’ ’, strings are within “”.

Constant definition: #define pi 3.14159; or const float pi=3.14;.

! = non (always outside characters or strings within ‘’ or ““).

Special characters are after \ (backslash).

Examples: \n (end of line), \’, \”, \:, \?, \\, \0 (end of char array or string).

Keyword (constant) endl is equivalent to “\n”.

If string exceeds one line of program, \ is written at end of line and string continues on next line.

 

Data types, declarations

 

Simple data types (Fundamental data types)

char, string (no length declaration), bool, int, short int, long int, float, double, long double.

Properties

The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems. But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding on.

Arrays: dimensions between [ and ], eg.: int my_array [5][10][15];.

Indices of arrays start with zero: 0,1,2,…

Before main, procedures and functions, re declared global variables.

Within main, procedures and functions, are declared local variables.

See also structures, classes and objects below

 

Identifiers (names), variables


Letters, digits or underscore characters (_).

Maximal length = 31.

Case sensitive.

Reserved keywords:

asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while,

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq.

 

Operators

 

Arithmetic operators> +, -, *, / (for integers: whole part), % (modulo=remainder, integer).

Equality and relational operators: == (equal), != (not equal), >, <, >=, <=.

Logical operators: ! (not), && (and), || (or).

Bitwise Operators: ~ (not), & (and), | (or), ^ (xor), << (shift left), >> (shift right).

Sizes

my_int=sizeof(my_type);

my_type: char, int, long int, float, double, long double…

std::cout << std::numeric_limits<long double>::digits10 << std::endl;

type: int, long int, float, double, long double…

my_int=strlen(my_chars);

my_int=my_string.length();

my_int=my_string.size();

Casting operators (converting types):

memcpy(from,to,size);

strcpy(my_chars_2,my_chars);

my_string=my_chars;

// my_chars=my_string.c_str();

strcpy(my_chars,my_string.c_str());

my_position=0;

my_length=my_string.copy(my_chars,my_string.length(),my_position);

my_chars[my_length]='\0';

my_int=atoi(my_chars);

my_base=10;

itoa(my_int,my_chars,my_base);

my_base: int, 2=binary, 8=octal, 10=decimal, 16=hexadecimal.

my_float=atof(my_chars);

sprintf(my_chars,"%f",my_float);

my_int=my_int_2/my_int_3;

my_int=my_int_2%my_int_3;

my_int=int(my_float);

my_int=int(my_float+0.5);

Another converting integer to string:

int i = 5;

std::string s;

std::stringstream out;

out << i;

s = out.str();

Remark: std::string is and std::stringstream are classes.

Converting integer to real: r=float(i);.

Converting real to integer (whole part): i=int (f);.

Converting real to integer (rounding): i=int (f+0.5);.

Reference operator (address of object in memory) is designed by & - example: &my_variable.

Pointer: variable which contans reference operator value.

Declaration of pointer – example: int * my_pointer, short * my_pointer; long * my_pointer;.

Usage of reference pointer: my_pointer=&my_variable;.

Dereference operator (value which is in the place, where pointer points) is designed by * - example: *my_ variable.

Usage of dereference operator: my_variable=*my_ pointer;.

Pointer of array - example:

int numbers [20];

int * p;

p = numbers;

numbers = p;

Assignment: =.

Precedence of operators: unsoled, difficult, using briket sis recommended.

 

Input/Output

 

Vyčištění displeje: clrscr();.

Library iostream:

cin >> my_string;

getline (cin,my_string);

cout << "Hello, " << "I am " << "a C++ statement"; (without writing end of line).

cout << "First sentence.\n";

cout << "Second sentence.\nThird sentence. \n "; (with writing new lines).

cout << "First sentence." << endl;

cout << "Second sentence." << endl; (with writing new lines).

Library fstream

Declarations

file for input: ifstream my_file;

file for output: ofstream my_file;

file for input and output: fstream my_file;

Usage, functions

open: my_file.open("example.txt");

close: my_file.close();

read: my_file >> my_string; (reads until space or end of line)

read: getline(my_file, my_string); (reads until end of line)

write: my_file >> my_string; (writes without end of line)

write: my_file << "Writing line to file.\n"; (writes with end of line)

write: my_file << "Writing line to file." << endl; (writes with end of line)

Open

my_file.open(filename, mode1|mode2 |…|modeN);

Mode

ios::in Open for input operations.

ios::out Open for output operations.

ios::binary Open in binary mode.

ios::ate Set the initial position at the end of the file. If this flag is not set to any value, the initial position is the beginning of the file.

ios::app All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.

ios::trunc If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.

Test of open: myfile.is_open()

State flags of file - functions

good(): It is the most generic state flag: it returns false in the same cases in which calling any of the next flags (functions) would return true.

bad(): Returns true if a reading or writing operation fails. For example in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left.

fail(): Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.

eof(): Returns true if a file open for reading has reached the end.

Pointers (positions where to read or write)

Finding out the position

my_file.tellg(): position to get

my_file.tellp(): position to put

Definition of position (offset)

my_file.seekg(position);

my_file.seekp(position);

my_file.seekg(offset,direction);

my_file.seekp(offset,direction);

Position (offset) = integer number, expressed in bytes, first position in file is 0.

Direction

ios::beg: offset counted from the beginning of the stream

ios::cur: offset counted from the current position of the stream pointer

ios::end: offset counted from the end of the stream

Formatting

printf(string,variables);

String contains text plus formatting tags, preceeded by %.

Variables are separated by commas.

example

printf("Convert character array to integer: %s -> %d \n",my_chars,my_int);

formatting tags: %c, %s, %i, %4i , %d, %o, %f, %4.2f, %e, %10.2e, %g, %p, %- (=justified left, justified right is default), %(number) (=length).

Attention, %s is for char array, to write string, conversion must be made before:

strcpy(my_chars,my_string.c_str());.

See also http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Binary files, read and write

my_file.read(memory_block,size);

my_file.write(memory_block,size);

Where memory_block is of type "pointer to char" (char*), and represents the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken. The size parameter is an integer value that specifies the number of characters (bytes) to be read or written from/to the memory block

Question: can memory_block be pointer to array or structure?

Example:

// reading a complete binary file

#include <iostream>

#include <fstream>

using namespace std;

ifstream::pos_type size;

char * memblock;

int main () {

  ifstream file ("example.bin", ios::in|ios::binary|ios::ate);

  if (file.is_open())

  {

    size = file.tellg();

    memblock = new char [size];

    file.seekg (0, ios::beg);

    file.read (memblock, size);

    file.close();

    cout << "the complete file content is in memory";

    delete[] memblock;

  }

  else cout << "Unable to open file";

  return 0;

}

In this example the entire file is read and stored in a memory block. Let's examine how this is done:

First, the file is open with the ios::ate flag, which means that the get pointer will be positioned at the end of the file. This way, when we call to member tellg(), we will directly obtain the size of the file. Notice the type we have used to declare variable size:

ifstream::pos_type size;

ifstream::pos_type is a specific type used for buffer and file positioning and is the type returned by file.tellg(). This type is defined as an integer type, therefore we can conduct on it the same operations we conduct on any other integer value, and can safely be converted to another integer type large enough to contain the size of the file. For a file with a size under 2GB we could use int:

int size;

size = (int) file.tellg();

Once we have obtained the size of the file, we request the allocation of a memory block large enough to hold the entire file:

memblock = new char[size];

Right after that, we proceed to set the get pointer at the beginning of the file (remember that we opened the file with this pointer at the end), then read the entire file, and finally close it:

file.seekg (0, ios::beg);

file.read (memblock, size);

file.close();

At this point we could operate with the data obtained from the file. Our program simply announces that the content of the file is in memory and then terminates.

Buffers and Synchronization

When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream) or simply freed (if it is an input stream). This process is called synchronization and takes place under any of the following circumstances:

- When the file is closed: before closing a file all buffers that have not yet been flushed are synchronized and all pending data is written or read to the physical medium.

- When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized.

- Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl.

- Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns 0.

 

Control structures and statements

 

After head or continuation of a control structure either statement (finished with ;) or block (started with { and finished with }) follows..

Conditional structures

if (condition) – else if (condition) – else.

switch (expression)

{

  case constant 1:

  case constant 2:

  case constant 3:

     group of statements 1;

     break;

  case constant 4:

     group of statements 2;

     break;

  default:

     default group of statements;

}

Iteration structures

while (condition)

do – while (condition) (testing at end, is always at least once performed).

for (start; not end; step), start = assignment statements, not end = boolean (logical ) condition, step = assignment statements to be performed at end of cycle; of more statements, then separated by comma.

Jump statements

label: my_label:.

jump statement to label: goto glabel;.

end of cycle: break;.

go before end of cycle: continue;

End statements

end of main program: exit(my_result);

end of main program, return of result: exit(my_result);

end of procedure: return;.

end of function, return of result: return my_result;.

 

Run (execute) program from program

 

program – run and wait

system(“program_name parameters”);

system("d:/mini/programs/windows/example_017.exe");

system("d:/mini/programs/windows/example_018.exe Hello Petr");

system("\"d:some_path\\program.exe\" \"d:\\other_path\\file_name.ext\"");

get parameters

int main(int number_of_parameters, char *parameters_of_program[])

{

  string my_string;

  int i;

  cout << "Number of parameters: " << number_of_parameters << endl;

  cout << "Parameters:" << endl;

  for (i=0;i<=(number_of_parameters-1);i=i+1)

    {

      my_string=parameters_of_program[i];

      cout << my_string << endl;

    }

}

program – run and no wait (?)

fork(“program_name parameters”);

batch

WinExec(“batch_name parameters”, how_window) ;

how_window: SW_SHOW_ MINIMIZED, SW_SHOW_NORMAL, SW_SHOW_MAXIMISED.

example

WinExec("cmd \"d:some path\\program.bat\" \"d:\\other path\\file name.ext\"",SW_SHOW_MINIMIZED);

 

Procedures and functions

 

Declaration

type name ( parameter_1, parameter_2, ..., parameter_n)

{ statements }

Type for procedure: void.

Types for function and parameters: char, string, bool, int, short int, long int, float, double, long double.

Parameter passed by value: type name.

Parameter passed by reference: type& name.

Array as formal parameter - examples: int my_array[][][] or int my_array[3][4][5].

If no parameter, then this declaration: type name ().

Compile procedure or function to each place, where it is called: inline type name (parameters).

Declaration of procedures and functions: before main or after main (stating head or procedure or function before main).

Call

name ( parameter_1, parameter_2, ..., parameter_n);.

Parameters: expression (same type, as declared).

If no parameter, then this call: name ().

Before main, procedures and functions, there are declared global variables.

Within main, procedures and functions, there are declared local variables.

 

Structures (data structures, compound data types)

 

#include <iostream.h>

#include <conio.h>

struct datum

{

   int day;

   int month;

   int year;

   int number_of_day_within_year;

   char month_name[10];

};

main()

{

  datum d1={4,7,1621,185,"July"};

  cout << "Anno Domini : " << d1. month_name << “ “ << d1.day << “ “ << d1.year

   << " it was "  << d1.number_of_day_within_year <<  " day of the year." << endl;

}

Arrays of structures

struct key_words

{

  char *key_word; // Why?

  int number_of_occurences; // to count number of keywords in program

};

main()

{

  key_words kw[];

  kw={"auto",0},{"break",0},{"case",0},{"cdecl",0},{"char",0},

  {"continue",0},{"default",0},

  // ...

  {"unsigned",0},{"void",0},{"volatile",0},{"while",0}

  // ...

  kw[i].number_of_occurences=kw[i].number_of_occurences+1;

  // ...

}

Structures with various types of indices

Declaration - example:

struct my_record

{

  int weight;

  float price;

}

apple, banana, melon;

After } there are possible values of the name of the structure my_record.

Usage – examples:

apple.weight=30;

banana.price=2;

Nesting structures

Declaration

truct movies_t

{

  string title;

  int year;

};

struct friends_t

{

  string name;

  string email;

  movies_t favorite_movie;

}

charlie, maria;

friends_t * pfriends = &charlie;

Usage

 

charlie.name

maria.favorite_movie.title

charlie.favorite_movie.year

pfriends->favorite_movie.year

 

Classes

 

#include <iostream.h>

#include <conio.h>

class datum

{

   // private: class can be used only by internal methods

   public:  // structure can be used also external operations

   int day;

   int month;

   int year;

   int number_of_day_within_year;

   char month_name[10];

};

main()

{

  datum d1={4,7,1621,185,"July"};

  cout << "Anno Domini : " << d1. month_name << “ “ << d1.day << “ “ << d1.year

   << " it was "  << d1.number_of_day_within_year <<  " day of the year." << endl;

}

Class arrays

class key_words

{

  public:

  char *key_word; // Why?

  int number_of_occurences; // to count number of keywords in program

};

main()

{

  static key_words kw[];

  kw={"auto",0},{"break",0},{"case",0},{"cdecl",0},{"char",0},

  {"continue",0},{"default",0},

  // ...

  {"unsigned",0},{"void",0},{"volatile",0},{"while",0}

  // ...

  kw[i].number_of_occurences=kw[i].number_of_occurences+1;

  // ...

}

 

Objects

 

Objektově orientovaní programování (OOP)

OOP je technika, která pracuje tzv. objekty. Objekt může mít následující vlastnosti:

- Dědičnost

Podřízený objekt dědí vlastnosti nadřízeného obektu a může k nim něco přidávat. Vlastnosti objektu dědí nově vytvářený objekt, tzv. potomek. Potomek dědí vlastnosti předka a může je rozšiřovat. Tato vlastnost je vlastností objektů reálného světa. Předpokládejme např. že základním objektem je "budova". Jeho potomkem může být "škola", protože má vlastnosti budovy a speciální vlastnost - je to školní budova. Potomky objektu "škola" mohou být "mateřská škola", "základní škola", "gymnázium" atd. Při definování objektu přecházíme od obecných vlastností k vlastnostem speciálním.

- Zapouzdření

Objekt spojuje data a metody (metoda je pojem OOP a znamená totéž, čemu jinde říkáme algoritmus, procedura, podprogram nebo funkce) do jedné struktury. Nechceme-li, nejsou data dostupná jinak než prostřednictvím metod objektu.

- Polymorfismus

Metoda může být použita pro různé objekty. Tuto vlastnost je možno pojmenovat česky mnohotvárnost. Podstatou je, že některé metody se mohou jmenovat stejně a mohou být sdíleny v hierarchii objektů tak, že každý objekt implementuje stejně se jmenující metodu způsobem, který je pro něj vhodný a patřičný.

Použití metodiky OOP je výhodné zejména při tvorbě informačních systémů, kde vlastnosti objektů OOP se blíží vlastnostem reálných objektů (i když pochopitelně svět je ve své mnohotvárnosti přeci jen složitější, než si myslí někteří fundamentalističtí zastánci OOP), počítačové grafice a někdy i ve vědeckých a technických výpočtech, speciálně při složitých projektech.

Definujme třídu, která zajistí přičítání a odečítání jedničky ka od proměnné hodnota, která je dostupná pouze metodám (v C++ funkcím zapouzdřeným ve třídě.

// Program definujici tridu, ktera je nazvana pocitadlo

// Nazev souboru pocitad1.h

class pocitadlo

{

 private:

 unsigned int hodnota;

 public:

 /* prototyp konstruktoru a definice vnitřní funkce. Slovní symbol

 se nemusí psát. Konstruktor při definici typu pocitadlo zajistí

 vytvoření objektu a inicializaci potřebných dat objektu. Jmenuje

 se vzdy jako typ class*/

 pocitadlo(){hodnota=0;}

 // Nasleduji dalsi definice metod jako vnitřní funkce

 void zvetseni(){if(hodnota<65535) hodnota++;}

 void zmenseni(){if(hodnota>0) hodnota--;}

 unsigned int zpristupnena_hodnota(){return(hodnota);}

}; // Vsimnete se, ze definice tridy musi koncit strednikem!!!

// Nasleduje program využívající trídy počitadlo

// Nazev souboru pocitad1.cpp

#include <stdio.h>

#include <pocitad1.h"

void main()

{

 /* Definice objektu c1, c2 a automaticke pocatecni prirazeni

 hodnoty pomocí konstruktoru */

 pocitadlo c1,c2;

 for(int i = 1;i <= 15;i++)

 {

  c1.zvetseni();

  printf("\nc1 = %u",c1.zpristupnena_hodnota());

  c2.zvetseni();

 }

 printf("\nPo ukonceni cyklu je c2 = %u",c2.zpristupnena_hodnota());

 for (i=1;i<=5;i++)

  c2.zmenseni();

 printf("\nPo ukonceni druheho cyklu je c2 = %u",

           c2.zpristupnena_hodnota());

 printf("\nKonecna hodnota c1 =%u",c1.zpristupnena_hodnota());

}

 

Sources

 

http://www.cplusplus.com/doc/tutorial/

http://www.fsid.cvut.cz/cz/U201/skrcpp.html

See also

http://www.sallyx.org/sally/c/