1.5k likes | 3.08k Views
Basic C++. daniel.soto@upf.edu. Introduction. Daniel Soto. 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.
E N D
Basic C++ daniel.soto@upf.edu
Introduction Daniel Soto
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
Programming Languages C=A+B High-Level Languages LOAD A ADD B STORE C Assembly Languages +1300042774 +1400593419 +1200274027 Machine Languages
Programming paradigms • Imperative • Declarative • Functional • Object-Oriented
Evolution of High-LevelProgramming Languages • Functions and Methods • Libraries (packages, units, etc.) • Abstract Data Types • Objects
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.
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!)
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"; } ;
Compilation C++ runtime Edit hello.cpp compile hello.o Link a.out a.exe Source File Object File Executable g++ hello.cpp
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 …
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.
Summary • The paradigm of Object-Oriented Programming
OOP: Objective • Say to a machine what it need to do (using objects). • In general this is programming
A OOP: approach • We can view a program as a “object” • This object is the medium to do work using a machine
OOP: approach • Is the Object-Oriented Programming a thing like… • Cooking programs with objects?
OOP: approach • Create programs with pieces? • Work with programs?
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