Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to C++: Part 1 - Getting Started with Object-Oriented Programming, Exams of History

A tutorial introduction to C++ programming language, focusing on object-oriented programming (OOP) concepts. It covers the history of C++, its advantages over other languages, and the basics of OOP, including classes, objects, and access modifiers. The tutorial also introduces the Code::Blocks IDE and provides examples of basic syntax and functions.

Typology: Exams

2021/2022

Uploaded on 08/05/2022

char_s67
char_s67 🇱🇺

4.5

(111)

1.9K documents

1 / 57

Toggle sidebar

Related documents


Partial preview of the text

Download Introduction to C++: Part 1 - Getting Started with Object-Oriented Programming and more Exams History in PDF only on Docsity!

Introduction to C++: Part 1

tutorial version 0.

Brian Gregor

Research Computing Services

Getting started with the room B27 terminals

 Log on with your BU username

 On the desktop is a Training Files folder. Open it and go to the subfolder:

RCS_Tutorials\Tutorial Files\Introduction to C++

 Copy the CodeBlocks Projects folder to your desktop.

Getting started on the SCC

 If you prefer to work on the SCC and have your own account , login using

your account to the host scc2.bu.edu

 On the room terminals there is a MobaXterm link on the desktop

 Load the codeblocks module:

 Make a folder in your home directory and copy in the files:

module load gcc/5.3. module load hunspell/1.4. module load wxwidgets/2.8. module load gdb/7.11. module load codeblocks mkdir cpp_tutorial && cd !$ unzip /scratch/intro_to_cpp_tutorial_0.1.zip

Getting started with your own laptop

 Go to:

http://www.bu.edu/tech/support/research/training-consulting/live-tutorials/

and download the Powerpoint or PDF copy of the unified presentation.  Easy way to get there: Google “bu rcs tutorials” and it’s the 1 st or 2 nd link.  Also download the “Additional Materials” file and unzip it to a convenient folder on your laptop.  Download the Code::Blocks development environment: http://www.codeblocks.org/downloads/

 Windows: get the codeblocks-16.01mingw-nosetup.zip file and unzip it to a convenient

folder.  Linux: likely available from your Linux distro’s package management system  Mac OSX: get the CodeBlocks-13.12-mac.zip file and unzip it to a convenient folder.  Also you will need Apple’s Xcode software with the command line tools installed.

Tutorial Outline: Part 1

 Very brief history of C++

 Definition object-oriented programming

 When C++ is a good choice

 The Code::Blocks IDE

 Object-oriented concepts

 First program!

 Some C++ syntax

 Polymorphism

Very brief history of C++

For details more check out A History of C++: 1979 −

C

C++

Object-oriented programming

 Programming has many paradigms , or styles,

which are used when writing programs.

 Wikipedia lists >40!: https://en.wikipedia.org/wiki/Programming_paradigm  Procedural (C, Fortran, Matlab)  Dataflow (Simulink, VHDL, Labview)  Functional (Excel, Lisp, F#)

 Object-oriented programming (OOP):

 Seeks to define a program in terms of the things in the problem (files, molecules, buildings, cars, people, etc.) and what they need and can do.

  • Data:
    • molecular weight, structure, common names, etc.
  • Methods:
    • IR(wavenumStart, wavenumEnd) : return IR emission spectrum in range class GasMolecule GasMolecule ch GasMolecule co spectrum = ch4.IR(1000,3500) Name = co2.common_name Objects ( instances of a class) “pseudo-code”

Object-oriented programming

 OOP defines classes to represent these things.  Classes can contain data and methods (internal functions).  Classes control access to internal data and methods. A public interface is used by external code when using the class.  This is a highly effective way of modeling real world problems inside of a computer program. public interface private data and methods “Class Car”

C++ Compared to Some Other Languages

C++ Python Fortran Language Type compiled interpreted compiled Variable type style Strong Strong Strong Variable Safety unsafe safe mostly safe Type checking At compilation and at run-time Run-time Compilation Paradigms OO, procedural, functional, generic, dataflow, and others OO, procedural, functional Procedural C compatibility Nearly 100% Not directly Call C libraries, with many pitfalls Relative speed Fast Slow Fast “Actually I made up the term ‘object-oriented’, and I can tell you I did not have C++ in mind.”

  • Alan Kay (helped invent OO programming, the Smalltalk language, and the GUI)

When to choose C++

 Despite its many competitors C++ has

remained popular for ~30 years and will

continue to be so in the foreseeable

future.

 Why?

 Complex problems and programs can be effectively implemented  OOP works in the real world!  No other language quite matches C++’s combination of performance, expressiveness, and ability to handle complex programs.

 Choose C++ when:

 Program performance matters  Dealing with large amounts of data, multiple CPUs, complex algorithms, etc.  Programmer productivity is less important  It is faster to produce working code in Python, R, Matlab or other scripting languages!  The programming language itself can help organize your code  Not everything is a vector or matrix, right Matlab?  Access to libraries that will help with your problem  Ex. Nvidia’s CUDA Thrust library for GPUs  Your group uses it already! “If you’re not at all interested in performance, shouldn’t you be in the Python room down the hall?” ― Scott Meyers (author of Effective Modern C++)

Pros/Cons of C++

Pros

 Enormous number of available libraries

 Flexibility for programmers

 High (objects) and low (fiddling with memory) level styles are supported

 No automatic memory management

 You are in control of memory usage

 Compiled

 Strong type system

 High performance

Cons

 A very large language - this tutorial won’t

even attempt to describe all of it.

 And your instructor makes no claim to know the entire language!

 No automatic memory management

 You are in control of memory usage

 Includes all the subtleties of C and adds its

own

 Generally requires careful attention to

detail!

“C++: an octopus made by nailing extra legs onto a dog.”

  • Steve Taylor

Code::Blocks

 In this tutorial we will use the Code::Blocks integrated development

environment (IDE) for writing and compiling C++

 Run it right on the terminal or on the SCC (module load codeblocks)

 About C::B

 cross-platform: supported on Mac OSX, Linux, and Windows  Oriented towards C, C++, and Fortran, supports others such as Python  Short learning curve compared with other IDEs such as Eclipse or Visual Studio

 Has its own automated code building system, so we can concentrate on

C++

 It can convert its build system files to make and Makefiles so you are not tied to C::B

 Project homepage: http://www.codeblocks.org

IDE Advantages

 Handles build process for you  Syntax highlighting and live error detection  Code completion (fills in as you type)  Creation of files via templates  Built-in debugging  Code refactoring (ex. Change a variable name everywhere in your code)  Higher productivity IDEs available on the SCC  Code::Blocks (used here)  geany – a minimalist IDE, simple to use  Eclipse – a highly configurable, adaptable IDE. Very powerful but with a long learning curve  Spyder – Python only, part of Anaconda Some Others  Xcode for Mac OSX  Visual Studio for Windows  NetBeans (cross platform)

Opening C::B

 The 1

st

time it is opened C::B will search for compilers it can use.

 A dialog that looks like this will open. Select GCC if there are multiple

options:

 And click OK.

Opening C::B and creating a 1

st

C++ project…

 Step 1. Create a project from the File menu or the Start Here tab:

 Step 2. Choose the Console category and then the Console application and click Go.

 Step 3: Click Next on the “Welcome to the new console application wizard!” screen.  Step 4: Choose C++!  …then click Next.

 Step 5. Enter a project title. Let C::B fill in the other fields for you. If you like you can change the default folder to hold the project. Click Next.

 Step 6: Choose the compiler. For this tutorial, choose GNU GCC as the compiler. Click Next.

Enable C++11 standard

 Step 7.l Right-click on your

project name and choose

Build options

 Check off the C++11 option. Click Release on the left and do the same there as well.  Do this anytime we create a project in C::B

 Step 8: Your project is now created! Click on Sources in the left column, then double-click main.cpp.  Click the icon in the toolbar or press F9 to compile and run the program.

Hello, World!

 Console window:

 Build and compile

messages

Behind the Scenes: The Compilation Process

Hello, World! explained

The main routine – the start of every C++ program! It returns an integer value to the operating system and takes no arguments (). Statement that returns an integer value to the operating system after completion. 0 means “no error”

Hello, World! explained

loads a header file containing function and class definitions Loads a namespace called std. Namespaces are used to separate sections of code for programmer convenience. To save typing we’ll always use this line in this tutorial. cout is the object that writes to the stdout device, i.e. the console window. It is part of the C++ standard library. Without the “using namespace std;” line this would have been called as std::cout. It is defined in the iostream header file. << is the C++ insertion operator. It is used to pass characters from the right to the object on the left. endl is the C++ newline character.