Transcript Basic C++
MSc Bioinformatics Basic C++ lectures 1 and 2 Basic C++ [email protected] MSc Bioinformatics Basic C++ Introduction Daniel Soto e-mail: [email protected] phone: 93 542 2634 address: Departament de Tecnologia Desp.374 Estació de França Passeig de Circumval·lació, 8 08003 Barcelona lectures 1 and 2 MSc Bioinformatics Basic C++ lectures 1 and 2 Introduction Philosophy: learn to program with objects Bibliography The C++ Programming Language, 3rd Edition by Bjarne Stroustrup. Addison Wesley Professional. http://www.awprofessional.com/title/0201889544 “Aprenda C++ como si estuviera en primero” http://mat21.etsii.upm.es/ayudainf/aprendainf/Cpp/ manualcpp.pdf MSc Bioinformatics Basic C++ Programming Languages High-Level Languages C=A+B Assembly Languages LOAD A ADD B STORE C Machine Languages +1300042774 +1400593419 +1200274027 lectures 1 and 2 MSc Bioinformatics Basic C++ Programming paradigms Imperative Declarative Functional Object-Oriented lectures 1 and 2 MSc Bioinformatics Basic C++ lectures 1 and 2 Evolution of High-Level Programming Languages Functions and Methods Libraries (packages, units, etc.) Abstract Data Types Objects MSc Bioinformatics Basic C++ lectures 1 and 2 C++ language History 1980, creation of different flavours of “C with Classes” 1983, C++ evolution from C with inspiration in Simula67 1991, ANSI C++ C++ vs. C C was chosen as the base language for C++ because it: is versatile, terse, and relatively low-level; is adequate for most systems programming tasks; runs everywhere and on everything; and fits into the UNIX programming environment. MSc Bioinformatics Basic C++ lectures 1 and 2 C++ language What is C++? C++ is a general-purpose programming language with a bias towards systems programming that: is a better C, supports data abstraction, supports object-oriented programming, and supports generic programming. C is retained as a subset inside C++ (this is not completely true!) MSc Bioinformatics Basic C++ C++ language First program (C-style): #include <stdio.h> int main() { printf("Hello world!\n"); } ; First program (Pure C++-style): #include <iostream> int main() { std::cout << "Hello world!\n"; } ; lectures 1 and 2 MSc Bioinformatics Basic C++ lectures 1 and 2 Compilation C++ runtime Edit hello.cpp Source File compile hello.o Link Object File g++ hello.cpp a.out a.exe Executable MSc Bioinformatics Basic C++ lectures 1 and 2 C/C++ Compatibility General: C++ provides the // comments // a comment return a; // another comment C Code that is not C++: Functions in C++ have strict syntax main() {} // Valid in C, not valid in C++ int main() {;} // Valid in C++ Default types not assumed const a=7; // In C is type int, not in C++ Several new keywords in C++: class, this, throw, public, protected, private, virtual, true, false, template, inline, friend, new … MSc Bioinformatics Basic C++ Summary C++ is a evolution of C. We can continue to use C syntax. Formal variations exist. We need to use a different compiler and new file extension. A new programming paradigm is integrated into it. lectures 1 and 2 MSc Bioinformatics Basic C++ lectures 1 and 2 Summary The paradigm of Object-Oriented Programming MSc Bioinformatics Basic C++ lectures 1 and 2 OOP: Objective Say to a machine what it need to do (using objects). In general this is programming MSc Bioinformatics Basic C++ OOP: approach We can view a program as a “object” A This object is the medium to do work using a machine lectures 1 and 2 MSc Bioinformatics Basic C++ OOP: approach Is the Object-Oriented Programming a thing like… Cooking programs with objects? lectures 1 and 2 MSc Bioinformatics Basic C++ OOP: approach Create programs with pieces? Work with programs? lectures 1 and 2 MSc Bioinformatics Basic C++ lectures 1 and 2 OOP: concepts Object: a mix of data and procedures Abstraction: view only a partial view of a thing Message interchange: how to object delegates a task to other object Class: the model of the object Instance: one agent (the object) Heritage: How to an object is a “derivate” from another object Polymorphism: Different views of the same object