C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
Introduction to Programming in C++ John Galletly.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
1 C++ Syntax and Semantics, and the Program Development Process.
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.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Pointers by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Pointers, Arrays, And Dynamic Memory Allocation by Bindra Shrestha CSCI 3333 Data Structures.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Chapter 15 - C++ As A "Better C"
Introduction to Programming
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Basic Elements of C++.
Command Line Arguments
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Introduction to C++ October 2, 2017.
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.
Basic Elements of C++ Chapter 2.
User-defined Functions
User-defined Functions
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 15 - C++ As A "Better C"
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Fundamental Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Class rational part2.
SPL – PS1 Introduction to C++.
Presentation transcript:

C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures

Acknowledgement Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding Ms. Krishani Abeysekera

Assumptions Assume that you are familiar with C, C++ or Java.

Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s “C with classes”, Better C Standardized by ISO in 1997  Includes the C++ standard library  Standard Template Library (STL) Part of the C++ standard library Ready made classes for data structures and algorithms

C++ Filenames Can be different from the name of the class or the name of the function in the file.cpp  Extension for the C++ source code files.h  Extension for C++ header files  Usually, code for a data structure is put in a header file, and then it can be added to a program with an include directive, e.g. #include "BasicVector.h" Name of executable file  In MSVS it’s the name of your project  In g++ it’s either “ a.out ” or a name you specify

A Simple C++ Example /* FILE: main.cpp */ #include using namespace std; int main() { cout << "Enter your first name: "; string name; cin >> name; cout << "Hello, " << name << endl; return 0; //optional }

/* FILE: main.cpp */ #include using namespace std; int main() { cout << "Enter your first name: "; string name; cin >> name; cout << "Hello, " << name << endl; return 0; //optional } A Simple C++ Example Comment C++ header files from C++ standard library “Entry” function Namespace for C++ standard library All C++ statements end with a semicolon

Examples…Continued //Variables can be declared anywhere using namespace std; #include int main () { double a; cin >> a; a = (a + 1) / 2; double c; c = a * 5 + 1; cout << "c contains : " << c << endl; int i, j; i = 0; j = i + 1; cout << "j contains : " << j << endl; return 0; }

Example…Variable Scopes using namespace std; #include int main () { int i = 5; for (int i = 0; i < 5; i++) // Local { cout << i << endl; // 0, 1, 2, 3,4 } cout << i << endl; // This outputs 5 return 0; }

Example…Control Structures //for, if, do, while, switch, and Exception using namespace std; #include #include int main () { int a; cout > a; try { if (a >= 5) throw 5; if (a < 5) throw a; throw a * 2; } catch (int e) { cout << “Exception caught: " << e << endl; } return 0; }

Example…String Supports C-style, array of characters string Also C++ provides STL Strings, a part of Standard Template Library #include using std::string; string s = “to be”; string t = “not” + s; string u = s + “ or “ + t; if (s > t) cout << u; …Also, s = “john” Int i = s.size( ); // i = 4 Char c = s[ 3 ]; // c = ‘n’ S += “ smith” Char *p =s.c_str( ); //string

Example – Function Overloading Two or more functions are defined with the same name but different argument lists. using namespace std; #include double addOrSubtract (double a, double b) { return a + b; } int addOrSubtract (int a, int b) { return a - b; } int main () { double x = 5, y = 4; int k = 5, p = 3; cout << “Added:” << addOrSubtract(x,y) << endl; cout << “Subtracted:” << addOrSubtract(x,y); return 0; }

Example - Operator Overloading Allows overloading of operators such as +, *, +=, and <<. E.g. comparison – “p1 == p2” where p1 and p2 are objects. bool operator ==(const Passenger &x, const Passenger &y) { Returnx.name == y.name && x.mealPref = y.mealPref; }

Example - Operator Overloading Another useful application is for defining input and output operators of classes and structures. The type iostream is output stream type, for example cout is of this type. ostream& operator <<(ostream &out, Passenger &pass) { Out << pass.name << “ “ pass.mealPref; Return out; } Example: cout << pass1 << pass2 <<‘\n’;

Example…Structure using namespace std; #include struct sample { int a; int b; int multiply () //FUNCTIONS OR METHODS { int c; c = a * b; return c; } }; int main () { sample sam1; sam1.a = 2; sam1.b = 4; cout << “Result: " << sam1.multiply() << endl; return 0; }

Example…Class using namespace std; #include class sample { public: int a; int b; int multiply () //FUNCTIONS OR METHODS { int c; c = a * b; return c; } }; int main () { sample sam1; sam1.a = 2; //public, so possible sam1.b = 4; cout << “Result: " << sam1.multiply() << endl; return 0; }

Functions Argument Passing  By default, arguments in C++ are passed by value.  When passing the argument by reference, modifications to the arguments in the function would modify the actual argument void f( int value, int &ref) { //one value, one ref value ++; ref ++; }

Constant Reference Arguments Structure and class arguments are usually passed by reference. Efficiency ? Passing by reference is much more efficient since only the address of the structure need to be passed. An even better practice? Pass by argument as a “constant reference”, preventing modification. void f ( const Passenger &const){ Pass.name = “new name” //not allowed } Furthermore, passing the argument to another function that might modify is NOT allowed.

Array Arguments Can arrays be passed by value? No. When an array is passed to a function, it is converted to the pointer to its initial element. T[ ] is converted to T*. Thus functions can modify original array.

Questions and Comments?