PROGRAMMING IN C++ CVIČENÍ
INFORMACE Michal Brabec http://www.ksi.mff.cuni.cz/ http://www.ksi.mff.cuni.cz/~brabec/ brabec@ksi.mff.cuni.cz gmichal.brabec@gmail.com
REQUIREMENTS FOR COURSE CREDIT Basic requirements are the same for all groups Successful completion of final credit test 1. and 2. attempt during the exam period... 3. in april 2-3 hour work in lab, successful completion an application Completion and presentation of a credit application Specification of requirements end of November Presentation of an alpha version do 29.3.2015 Finished implementation and documentation do 24.5.2015 Additional requirements defined by the teacher Succesfull completion of one homework (application) It is possible to specify individual requirements if there is a reason and they must be agreed upon at the beginning of the course
INTRODUCTION TO C++ Introduction to basics of C++ programming language Visual studio basics
BASIC TYPES Native types: Integral numbers int, long, unsinged, unsigned long Real numbers float, double Characters char, unsigned char, (wchar_t) Complex types (user defined): Structures Classes Special types (renamed) Declared using typedef
VARIABLES Definition of a variable: Data_type var_name = initial_value; Type can be any known data type Initial value must be convertible to the data type Member variables Same definition Belong to a class (structure) Initialization in a constructor
FUNCTIONS AND METHODS Definition of a function: Return_type fun_name (parameters) { code } Parameters are defined the same way as variables Parameters can be treated as local variables The main function int main() {} int main(int argc, char** argv) {} void main() {} (Visual studio) Return value 0 (successful completion) or error code (!= 0)
DECLARATION VS. DEFINITION Declaration Informs the compiler about a data type, function or variable Does not create anything Useless without definition Definition Creates variable including its initialization Defines the body for a function Defines the content of a data type (methods and members) Two different definition cause compilation error
DECLARATION & DEFINITION Declaration int intvar; int converttoint(float f); class UserData; Definition int intvar = 10; int converttoint(float f) { return 0; } class UserData { int intmember; };
DECLARATION AND INCLUDE Data types, functions and variables can be defined in separate files Every file should contain a single data type or a small set of similar functions, but the language does not forbid other solutions Data types and functions declared in other files must be included into a file where they are used Declarations from standard library #include <header.h> Local declaration #include header.h
INPUT & OUTPUT Input and output done by streams Streams are special classes in standard library Streams can be used for input and out to command line and file (network) They support general optimization (buffering, binary files ) Command line stream declaration #include <iostream> Other types in different files All classes are in the namespace std
HELLO WORLD Basic application Contains many important constructs All classes from standard are fully qualified (prevents conflicts in names) #include <iostream> void main() { std::cout << "Hello world!" << std::endl; } getchar(); // forces the program to wait for user input at the end;
APPLICATION PARAMETERS Parameters passed to main as standard parameters Parameters can be passed from command line, shortcut or development environment Parameters can be specified in Visual studio for faster debugging int argc contains the number of all application parameters char** argv parameters passed as an array of strings
PARAMETERS EXAMPLE > \program.exe Hello world Paremeters of the main function are: argc: 3 argv: \program.exe (not in standard) Hello World The first parameter contains the name of the application but this is not specified in C++ standard and this parameter can be omitted
HELLO WORLD REVISITED Second version of the basic application It prints the number of parameters and the alue of the second parameter The verification of parameter count is important! #include <iostream> int main(int argc, char** argv) { // argument[0] is the application file name (+path) std::cout << argc << std::endl; if(argc > 1) std::cout << argv[1] << std::endl; // writes only a single word } getchar(); return 0;
LOCAL VARIABLES Variables defined in function Can be used only inside the function and they exist only while the function is executed Initialized immediately after declaration #include <iostream> int main(int argc, char** argv) { // argument[0] is the application file name (+path) std::cout << argc << std::endl; int count = argc; if(count > 1) std::cout << argv[1] << std::endl; // writes only a single word } getchar(); return 0;
DECLARATION OF A CLASS class Name { private: member <= variable protected: member public: metoda <= function };