This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
True or false A variable of type char can hold the value 301. ( F )
Chapter 10.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS1061: C Programming Lecture 22: A Brief Look at C++ A. O’Riordan, 2004.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
C++ fundamentals.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
High-Level Programming Languages: C++
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Fundamental Programming: Fundamental Programming Introduction to C++
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
SNPL1 Woochang Lim C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming Programming with C++
Objective: Students will be able to: Declare and use variables Input integers.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Pointers *, &, array similarities, functions, sizeof.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter 15 Strings as Character Arrays
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Program Input and the Software Design Process ROBERT REAVES.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DEF & FACTS! C + 1 = C++ C & C++ ARE MIDDLE LEVEL LANGUAGES INTRODUCTION BRIEF REVIEW OF C NEED OF C++ © arbit club cu 2010.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
C Programming Tutorial – Part I
Visit for more Learning Resources
Basics (Variables, Assignments, I/O)
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 5 Input and Output Streams
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String What it is Why it’s useful
Chapter 3 Input output.
Chapter 4 Program Input and the Software Design Process
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
What Actions Do We Have Part 1
Life is Full of Alternatives
Course Outcomes of Programming In C (PIC) (17212, C203):
Presentation transcript:

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

C++ This slide show introduces C++ & compares it to C. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: attribute bool cin cout encapsulation iostream method namespace object oriented private public string w_char

C appeared in It was developed by Dennis Ritchie at Bell Labs. In 1979, Bjarn Stroustrup at Bell Labs became interested in adding classes to C and overcoming some of the common problems with C. His additions were adopted in 1983 as C++. C++ was first standardized in 1998.

C++ is a superset of C. C++ C

The name: BCPl (Basic Combined Programming Language) …evolved into a language nicknamed “B” Dennis Ritchie adopted parts of B into his new language. Because it was a successor to B, he called it C. Bjarne Stoustrup extended the C language to invent C++ He coined the name C++ to imitate the increment operator (++) adapted from Dale. Weems. “Programming and Problem Solving with C++)

C++ supports (but does not require) object-oriented programming The main objects are in the std namespace and the line using namespace std; appears after the last #include statement

C Fundamental Data Types void char int float double C++ Fundamental Data Types void char int float double bool wchar_t C++ includes two additional data types

Differences: Standard I/O C #include printf scanf C++ #include cout cin cout << “The answer is “ << Result <<endl;printf(“The answer is %d\n”, Result); cin >> Rate; scanf(“\n%f”, &Rate) cout << setw(8) << setprecision(4) << my_float; printf(“%8.4f“, my_float);

C++ provides a robust string header that includes a string data type. C++ Code #include using namespace std; int main () { string FirstName; FirstName = “Oscar”; return 0; } C Code #include int main () { char FirstName[25]; strcpy(FirstName, “Oscar”); return 0; }

// my first program in C++ #include using namespace std; int main () { cout << "Hello World!"; return 0; } /* my first program in C */ #include int main () { printf("Hello World!“); return 0; } C vs C++ Comparison of the Hello World program

C and C++ use the same arithmetic operators: + - * / % C and C++ use the same logical operators: ! && || C and C++ use the same comparison operators: == >= != C and C++ use the same assignment operators: = *= /= %= += -= C and C++ use the same unary operators:

C vs C++ keyboard input scanf(“\n %d”, &x); #include cin >> x;

In C, the programmer can define a new data type based on the fundamental types. The new data type is called a structure and is declared using the struct keyword. Inside the structure, attributes are defined based on existing data types. struct TestScore { char Pnumber[8]; int TestNum; float score; }; Variables can now be declared as type TestScore. variable namesdata types int Age; float Salary; TestScore StudentIn;

A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ; The concept that all the attributes and behaviors of an entity are bound together in a single class is called Encapsulation C++ expands the structure to include both the data (attributes) and functions (methods).

Procedure oriented programming the traditional programming that is based on algorithms or a logical step-by-step process for solving a problem. a class of programming languages and techniques based on the concept of an “object” which is a data structure encapsulated with a set of routines which operate on the data. C++ and Java are object oriented programs Object oriented programming When a language supports the use of classes/objects, it is referred to as object-oriented.

attributes Cat itsAge GetAge(void); SetAge(int age); Meow(void); methods class name Classes are diagrammed as shown below.

objectclass int Age; float Salary; TestScore StudentIn; Cat Puff; An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ;

class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ; void Cat::SetAge(int age) { itsAge = age; } Each method must be defined. Methods are defined in the same manner as a subfunction. The name of the method is justified by the prepending the class name + :: method prototype method definition

int main() { Cat Frisky; int Years; int YearCtr; printf("\nPlease enter Frisky's age "); fflush(stdin); scanf("\n%d", &Years); Frisky.SetAge(Years); cout << "Frisky is a cat who is "; cout << Frisky.GetAge() << " years old.\n"; for(YearCtr = 1; YearCtr <= Years; ++YearCtr) { Frisky.Meow(); } printf("\n\n\n\n"); return 0; } class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ; When calling a method, or referring to an attribute of the class, prepend the OBJECT name (not the class name) and a period to the method or attribute.

Differences: String class C #include …for C-strings C++ #include …for C-strings #include …for string class A C-string is an array of chars terminated with NULL A class string is an object and has methods that can be used to manipulate the string. By default, a literal such as “Ohio University” is a C-string.

Declaring strings C-strings are declared and manipulated as in C string class objects are declared as type string string FirstName; string Greeting = “Hello”; A string declared as type string can... use the assignment operator FirstName = “Jane”; use the + operatorGreeting = Greeting + “ “ + Firstname (note, at least one of the operands must be a string class object) use string methodsFirstName.substr etc use comparison operators == != = > (watch out for upper/lower case)

C++ cin >> >> (extraction operator) extracts desired data value from the input stream skips any leading whitespace characters if data value is a char >> extracts exactly 1 character if data value is an int >> extracts characters until first inappropriate character if data value is a float >> extracts characters until first inappropriate character if data value is a string >> extracts characters until first whitespace Examples: (given: i & j are ints, x is float, ch is char, str is a string) StatementDataResult cin >> I;32 cin >> i >> j;4 60 cin >> i >> ch >> x; 25 A 16.9 cin >> i >> j >> x;12 8 cin >> i >> x; cin >> str;Jane cin >> str;Jane Doe

C++ Drawbacks to the >> operator when inputting strings 1.If string isn’t large enough, the >> operator will continue to store characters into memory beyond the end of the array. 2.The >> operator cannot be used to input strings with embedded whitespace.

Solution cin.get() (assume ch is a char and str is a string and n is an int) get() does not skip leading whitespace cin.get(ch );reads a single character (even a whitespace) cin.get(str, n);reads up to n-1 chars & appends ‘\0’ stops at newline, or n-1 chars newline left in stream cin.get(str, n, delimiter)reads up to n-1 chars & appends ‘\0’ stops at delimiter, or n-1 chars delimiter left in stream getline(cin, str) reads entire line up to ‘ \n’ extracts newline from stream and stores