CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++

Slides:



Advertisements
Similar presentations
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
Advertisements

Class and Objects.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS-2303 System Programming Concepts
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Basic Elements of C++ Chapter 2.
Introduction to C++ Systems Programming.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Chapter 15 - C++ As A "Better C"
Hank Childs, University of Oregon
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Java Yingcai Xiao.
C++ INTERVIEW QUESTIONS
Introduction to C++ Systems Programming.
Basic Elements of C++.
Review: Two Programming Paradigms
Chapter 5 Classes.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Basic Elements of C++ Chapter 2.
Introduction to Classes
More About Data Types & Functions
Classes, Constructors, etc., in C++
What about multi-dimensional arrays?
Engineering Problem Solving with C++ An Object Based Approach
Chapter 15 - C++ As A "Better C"
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Classes and Objects
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++

C++ – a better C, and more Born in 1982 – “Classes: An Abstract Data Type Facility for the C Language” –A collection of C macros and library routines by Bjarne Stroustrup, Bell Labs Evolved during 1980s to whole new language –But always backward compatible to C Means any C program is also a C++ program Also means C++ has “legacy problems” –Effective use requires abandoning some C features Most notably C’s I/O library, strings, and memory allocation

Well-styled C++ programs don’t look like C programs Named with.cpp extension: program.cpp Compiling: g++ -o program program.cpp or make program New style of commenting – // Includes new way to initialize variables –int x(7), sum(0); –By the way, get used to initializing instead of assigning later – reason will become clear soon –A preview: MyClass myObject(constructor arguments); A Boolean type: bool isMax = true; main – must be declared int explicitly (not the default type like C, and main may not be void ) –And automatically returns 0 by the way

using namespace std; // huh? Namespaces – a way to manage global symbols 3 ways to access things that are in a namespace: –Directly – with scope resolution operator each time a name is used – std::cout << data; –Or with a using declaration – using std::cout; –Or access all names in a namespace with a using directive – using namespace std; std – the namespace for most C++ library tools Can create your own namespaces: –namespace mynames { /* C++ code */ }

#include Instead of (actually that is cstdio now) –i.e., instead of printf, scanf, putchar, FILE *, … –No worries: iostream easy to use (except for formatting) cout – technically an ostream object –Use insertion operator ( << ) to insert data into stream cout << value; // prints value to standard output cin – technically an istream object –An extraction operator ( >> ) extracts data from stream cin >> value; // gets value from standard input

Example: Distance Between Two Points Code at

// Read in two points, output distance #include using namespace std; int main() { double x1, y1, x2, y2, s1, s2, dist; cout << endl; cout << "Coordinates of first point : "; cin >> x1 >> y1; cout << "Coordinates of second point : "; cin >> x2 >> y2; s1 = x2 - x1; s2 = y2 - y1; dist = sqrt(s1*s1 + s2*s2); cout << fixed << setprecision(2); cout << endl << "Distance between points: "; cout << dist << endl << endl; return 0; }

Default function arguments Can specify parameter values in the function declaration void func(int x = 12); Then function user can choose to accept the default value(s) or specify new one(s) func() // accept default value for x func(97) // specify new value for x Mandatory arguments are ones without default values – and these must come first void func(int x = 12, int y); // illegal void func(int y, int x = 12); // okay Note also must specify in declaration, not definition – so compiler is sure to know about default values See

Default Parameters Example #include using namespace std; // Returns the volume of a box. int volume(int, int = 1, int = 1); int main() { cout << "\nSame function called three ways:\n\n" << " volume(4, 6, 2) = " << volume(4, 6, 2) << endl << " volume(4, 6) = " << volume(4, 6) << endl << " volume(4) = " << volume(4) << "\n\n"; return 0; } int volume(int length, int width, int height) { return length * width * height; }

Declaring variables, tags Can declare anywhere in block now –Even in for loop header: for (int i = 0; …) Tag name is the type name – no two-word types to declare struct, enum or class objects struct Foo { … }; // still need it to define type Foo myFoo; // not struct Foo myFoo;

Dynamic memory with C++ Use new and delete instead of malloc and free No need to specify the size – compiler deduces it In C++: int *ip = new int; // compiler knows sizeof(int) But in C: int *ip = (int*)malloc(sizeof(int)); Best to initialize when created: int *ip = new int(7); … delete ip; // free the space when done

C++ Classes and Objects

C++ classes A class is similar to a struct –Contains data, or member variables Unlike struct can also contain functions, or methods A class that represents a rectangle: Class Rect{ int length; int width; public: Rect(int l, int w) {length = l; width = w;} int area() { return length*width; } };

C++ accessibility Three levels of access: –Public:everyone –Protected:only derived (child) classes/structs –Private:only the containing class Every data and function member has an access level

C++ structures: struct vs class Both can have data and functions Only two differences – both in default accessibility –Data and functions default to public in a struct and to private in a class –Other difference is related to default accessibility when inheriting data or functions from a parent In the previous example the integers length and width defaulted to private

Declaring and defining classes class Foo;// just a declaration –Sometimes all you need – usually in header files class Foo { … };// a definition –Note: some or all implementation likely elsewhere Usual definition style is most to least accessible public: // the public interface is listed first int getValue(); private: // most data should be here – listed last int value ;

Implementing classes Usually in a separate file – foo.cpp, not foo.h –So #include “ foo.h ” Identify class with scope resolution operator int Foo::getValue() { return value; } Implementation can include other stuff too –Use helper functions, data, constants, even classes –No worries about name conflicts in other files Usually one implementation file per public class

Using class instances – objects Declare to create on stack or global space –Foo foo1, foo2; // created two Foos Or use new to dynamically allocate –Foo *fooPtr = new Foo; // one more Foo Contact object directly with. operator –foo1.getValue(); Or through a pointer with -> operator –fooPtr->getValue();

Constructors Has same name as its class, and no return type –Can have default values for any or all parameters A constructor is invoked every time an instance is created (whenever a class is instantiated) –Includes objects on the stack or dynamically allocated –But not invoked by creation of pointer or reference Compiler supplies default constructor if no constructor –Compiler-supplied version takes no arguments

Destructors A destructor is invoked whenever an object goes out of scope, or by delete for dynamically allocated objects –Compiler supplies one if you don’t –Need to write your own version if need to release dynamically allocated space or other resources Defined like a constructor, but with a ~ in front, and it may not take any arguments ~Foo(); // syntax in header file Foo::~Foo() { … } // syntax in implementation file

Getters and Setters Common method of handling private members –Function to get value and function to set value Applied to the rectangle example: Class Rect{ int length; int width; public: int getLength() { return length; } int getWidth() { return width; } void setLength(int l) { length = l; } void setWidth(int w) { width = w; } int area() { return length*width; } };