38
1 Getting Started with C++ Trenton Computer Festival March 15, 2014 Michael P. Redlich @mpredli about.me/mpredli/ Sunday, March 16, 14

Getting Started with C++ (TCF 2014)

Embed Size (px)

DESCRIPTION

An introduction to the C++ programming language that includes a brief history of C++, how C++ evolved, and how to get started using C++.

Citation preview

Page 1: Getting Started with C++ (TCF 2014)

1

Getting Started withC++

Trenton Computer FestivalMarch 15, 2014

Michael P. Redlich@mpredli

about.me/mpredli/

Sunday, March 16, 14

Page 2: Getting Started with C++ (TCF 2014)

Who’s Mike?

• BS in CS from

• “Petrochemical Research Organization”

• Ai-Logix, Inc. (now AudioCodes)

• Amateur Computer Group of New Jersey

• Publications

• Presentations

2

Sunday, March 16, 14

Page 3: Getting Started with C++ (TCF 2014)

Objectives (1)

• What is C++?

• Evolution of C++

• Features of C++

• Review of Object-Oriented Programming (OOP)

3

Sunday, March 16, 14

Page 4: Getting Started with C++ (TCF 2014)

Objectives (2)

• Getting Started with C++

• introduction to the C++ class mechanism

• how to implement C++ classes

• Live Demos (yea!)

• C++ Resources

4

Sunday, March 16, 14

Page 5: Getting Started with C++ (TCF 2014)

What is C++?

• “...a general purpose programming language with a bias towards systems programming that

• is a better C,

• supports data abstraction,

• supports object-oriented programming,

• supports generic programming.”

Bjarne Stroustrup Web Site, http://www.stroustrup.com/C++.html

5

Sunday, March 16, 14

Page 6: Getting Started with C++ (TCF 2014)

Evolution of C++ (1)

• Created by Bjarne Stroustrup

• AT&T Labs

• 1980 - originally named “C with Classes”

• 1983 - redesigned and renamed to C++

• 1985 - available to the public

6

Sunday, March 16, 14

Page 7: Getting Started with C++ (TCF 2014)

Evolution of C++ (2)

• 1989 - further extensions added

• templates and exception handling

• 1998 - C++ standardized

7

Sunday, March 16, 14

Page 8: Getting Started with C++ (TCF 2014)

Features of C++• Object-Oriented

Programming (OOP) Language

• Pass-by-Reference

• Operator Overloading

• Generic Programming

• Exception Handling

• Namespaces

• Default Arguments

8

Sunday, March 16, 14

Page 9: Getting Started with C++ (TCF 2014)

OOP Review (1)

• Programming Paradigm

• Four (4) Main Attributes

• data encapsulation

• data abstraction

• inheritance

• polymorphism

9

Sunday, March 16, 14

Page 10: Getting Started with C++ (TCF 2014)

OOP Review (2)

• Abstract Data Type (ADT)

• user-defined data type

• use of objects through functions (methods) without knowing the internal representation

10

Sunday, March 16, 14

Page 11: Getting Started with C++ (TCF 2014)

OOP Review (3)

• Interface

• functions (methods) provided in the ADT that allow access to data

• Implementation

• underlying data structure(s) and business logic within the ADT

11

Sunday, March 16, 14

Page 12: Getting Started with C++ (TCF 2014)

OOP Review (4)• Class

• Defines a model

• Declares attributes

• Declares behavior

• Is an ADT

• Object

• Is an instance of a class

• Has state

• Has behavior

• May have many unique objects of the same class

12

Sunday, March 16, 14

Page 13: Getting Started with C++ (TCF 2014)

Advantages of OOP

• Interface can (and should) remain unchanged when improving implementation

• Encourages modularity in application development

• Better maintainability of code

• Code reuse

• Emphasis on what, not how

13

Sunday, March 16, 14

Page 14: Getting Started with C++ (TCF 2014)

Some C++ Keywords• class

• new, delete

• private, protected, public

• try, throw, catch

• friend

• explicit

• virtual

• bool

• inline

14

Sunday, March 16, 14

Page 15: Getting Started with C++ (TCF 2014)

Classes (1)

• A user-defined abstract data type

• Extension of C structs

• Contain:

• constructor

• destructor

• data members and member functions (methods)

15

Sunday, March 16, 14

Page 16: Getting Started with C++ (TCF 2014)

Classes (2)

• Static/Dynamic object instantiation

• Multiple Constructors:

• Sports(void);

• Sports(char *,int,int);

• Sports(float,char *,int);

16

Sunday, March 16, 14

Page 17: Getting Started with C++ (TCF 2014)

Classes (3)

• Class scope:

• scope resolution operator(::)

• Abstract Classes

• contain at least one pure virtual member function (C++)

• contain at least one abstract method (Java)

17

Sunday, March 16, 14

Page 18: Getting Started with C++ (TCF 2014)

Abstract Classes

• Pure virtual member function (C++)

• virtual void draw() = 0;

• Abstract method (Java)

• public abstract void draw();

18

Sunday, March 16, 14

Page 19: Getting Started with C++ (TCF 2014)

Class Inheritance

19

Sunday, March 16, 14

Page 20: Getting Started with C++ (TCF 2014)

20

// Sports class (partial listing)

class Sports {private:char *team;int win;

public:Sports(void);Sports(char const *,int,int);~Sports(void); // destructorint getWin() const;

};

Sports::Sports(void) {// define default constructor here...}

Sports::Sports(const char *team,int win,int loss) {// define primary constructor here...}

int Sports::getWin() const {return win;}

Sunday, March 16, 14

Page 21: Getting Started with C++ (TCF 2014)

21

// Baseball class (partial listing)

class Baseball : public Sports {public:Baseball(void);Baseball(char const *,int,int);~Baseball(void);

};

Baseball::Baseball(void) : Sports() {}

Baseball::Baseball(const char *team,int win,int loss) :Sports(team,win,loss) {}

inline Baseball::~Baseball(void) {}

Sunday, March 16, 14

Page 22: Getting Started with C++ (TCF 2014)

Static Instantiation

• Object creation:

• Baseball mets(“Mets”,97,65);

• Access to public member functions:

• mets.getWin(); // returns 97

22

Sunday, March 16, 14

Page 23: Getting Started with C++ (TCF 2014)

Dynamic Instantiation

• Object creation:

• Baseball *mets = new Baseball(“Mets”,97,65);

• Access to public member functions:

• mets->getWin(); // returns 97

23

Sunday, March 16, 14

Page 24: Getting Started with C++ (TCF 2014)

Deleting Objects

Baseball mets(“Mets”,97,65);

// object deleted when out of scope

Baseball *mets = new Baseball(“Mets”,97,65);

delete mets; // required call

24

Sunday, March 16, 14

Page 25: Getting Started with C++ (TCF 2014)

Operator new (1)

• Allocates memory on the free store (heap)

• Memory size is calculated by the compiler

• No more casting

• Automatic call to the constructor

• Used for built-in and user-defined data types

25

Sunday, March 16, 14

Page 26: Getting Started with C++ (TCF 2014)

Operator new (2)

int *var = new int; // int();

Sports *sports = new Sports();

// initializes an array of pointers to type int

int *var = new int[10]

26

Sunday, March 16, 14

Page 27: Getting Started with C++ (TCF 2014)

Operator delete (1)

• Deallocates memory on the free store (heap)

• Automatic call to the destructor

• Must be used according to how operator new was used

27

Sunday, March 16, 14

Page 28: Getting Started with C++ (TCF 2014)

Operator delete (2)

int *var = new int;

delete var;

int *var = new int[10]

delete[] var;

28

Sunday, March 16, 14

Page 29: Getting Started with C++ (TCF 2014)

Inline Member Functions (1)

• Used for short functions (≤ 5 statements)

• Purpose:

• speed

• Good candidates for inline member functions are those that access data members

29

Sunday, March 16, 14

Page 30: Getting Started with C++ (TCF 2014)

Inline Member Functions (2)

• Explicit Use:

• use keyword inline in the definition of member function

• Implicit Use:

• define the member function within its declaration without using the keyword inline

30

Sunday, March 16, 14

Page 31: Getting Started with C++ (TCF 2014)

Live Demo!

31

Sunday, March 16, 14

Page 32: Getting Started with C++ (TCF 2014)

Popular C++ Compilers

32

• Embarcadero C++ Builder XE5

• embarcadero.com/products/cbuilder

• Microsoft Visual C++

• microsoft.com

• Open Watcom 1.9

• openwatcom.org

Sunday, March 16, 14

Page 33: Getting Started with C++ (TCF 2014)

Local C++ User Groups

• ACGNJ C++ Users Group

• facilitated by Bruce Arnold

• acgnj.barnold.us

33

Sunday, March 16, 14

Page 34: Getting Started with C++ (TCF 2014)

Further Reading (1)

34

• C & C++ Code Capsules

• Chuck Allison

• freshsources.com

• The C++ Programming Language

• Bjarne Stroustrup

• stroustrup.com/4th.html

Sunday, March 16, 14

Page 35: Getting Started with C++ (TCF 2014)

Further Reading (2)

35

• The Annotated C++ Reference Manual

• Margaret Ellis and Bjarne Stroustrup

• stroustrup.com/arm.html

• 1997 C++ Public Review Document

• C++ ISO JTC1/SC22/WG21 Committee

• open-std.org/jtc1/sc22/open/n2356

Sunday, March 16, 14

Page 36: Getting Started with C++ (TCF 2014)

Upcoming Events (1)

• Trenton Computer Festival

• March 14-15, 2014

• tcf-nj.org

• Emerging Technologies for the Enterprise

• April 22-23, 2014

• phillyemergingtech.com

36

Sunday, March 16, 14

Page 37: Getting Started with C++ (TCF 2014)

37

Upcoming Events (2)

Sunday, March 16, 14

Page 38: Getting Started with C++ (TCF 2014)

38

Thanks!

[email protected]

@mpredli

javasig.org

Sunday, March 16, 14