This document summarizes key aspects of a C++ program discussed in Chapter 2 of the textbook. It explains the parts of a basic C++ program including comments, preprocessor directives, namespaces, functions and blocks. It also describes important C++ concepts such as data types, variables, constants, operators, and input/output using cout. Special characters, escape sequences and standard libraries are defined. Best practices for writing readable and maintainable C++ code are also covered.
Here is a C++ program to calculate the volume of a sphere:
#include <iostream>
#define PI 3.14159
using namespace std;
int main() {
float radius, volume;
cout << "Enter the radius of the sphere: ";
cin >> radius;
volume = (4/3.0) * PI * radius * radius * radius;
cout << "Volume of the sphere is: " << volume << endl;
return 0;
}
This program first includes the iostream library for input/output functions. It then defines PI as a constant. In main(), it prompts the user to enter the radius, reads it using cin and stores it in
- This document introduces key concepts in C++ including variables, data types, input/output, arithmetic operations, and program style. It discusses using libraries like iostream and namespaces for console input/output. Important concepts covered are variable declaration, data type compatibility, and precision in arithmetic operations.
The document discusses C++ basics including variables, data types, input/output, and expressions. It covers declaring and initializing variables, assigning values, and built-in data types like int, double, char. It also covers input using cin and output using cout, including formatting real numbers.
This document outlines the course objectives and structure for CS-200 Object Oriented Programming taught by Muhammad Usman. The course will introduce students to object-oriented programming concepts in C++, including applying features to program design and implementation. Students will learn about classes, objects, inheritance and design patterns. The document provides information on textbooks, tools, assignments, exams and contact details for the instructor.
#Code2Create series: C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on.Start your programming journey and join us to learn C++ basics!
by Google Developers Group and Women Tech-markers Kuwait chapter:
Instagram and Twitter: @GDGWTMKUWAIT
This document summarizes Chapter 2 of a C++ basics textbook. Section 2.1 discusses variables, assignments, and data types. Section 2.2 covers input and output using cin and cout. Section 2.3 describes different data types like int, double, char, and string. Section 2.4 discusses arithmetic expressions and operators. The final section introduces simple flow of control in programs.
This document summarizes Chapter 2 of a C++ basics textbook. Section 2.1 discusses variables and assignments, including declaring and initializing variables, assignment statements, and variable types. Section 2.2 covers input and output using streams like cout and cin, as well as formatting output. Section 2.3 describes C++ data types like int, double, and char, and how they store numeric values differently.
This document discusses basic program elements in C++, including:
1. Initializing variables can be done in two ways - by appending an equal sign followed by the value (int a = 0), or by enclosing the value in parentheses (int a(0)). Both methods are valid in C++.
2. Variables can have either local or global scope - local variables are declared within a function while global variables are declared outside all functions.
3. Keywords are reserved words in C++ that have special meaning and cannot be used as regular identifiers. Common keywords include int, float, if, else, while, etc.
The document provides an introduction to programming fundamentals in C++, including basic syntax and components of a C++ program. It covers variables and data types, input/output, comments, and how to write a simple C++ program with preprocessor directives and a main function. The key topics discussed are variable declaration, fundamental data types like int, float, char, comments, and how to write a basic "Hello World" program in C++.
The document discusses various C++ programming concepts including variables, data types, input/output streams, and expressions. It defines variables as memory locations that can store values, and describes different data types like int, double, char. It also covers input with cin, output with cout, and formatting output values.
The document is a chapter from an introductory C programming textbook. It covers basic C concepts like comments, variables, data types, functions, input/output, arithmetic operators, and conditional statements. It includes examples of simple C programs that print text, get user input, perform addition, and test for equality/relations between numbers. The chapter lays out fundamental building blocks to prepare the reader for more advanced C programming concepts covered later.
The document is an outline from a textbook on C programming. It covers basic concepts like simple programs to print text and perform math operations, use of variables and data types, memory concepts, decision making using if statements and relational operators. It includes examples of simple C programs and explanations of the core components like main function, comments, preprocessor directives, functions etc.
This document provides an overview of introductory concepts in C programming, including simple programs to print text and perform arithmetic. It covers basic syntax like comments, functions, variables, data types, operators, input/output, and conditional statements. Memory concepts are introduced, as well as the preprocessor, standard libraries, and control flow structures like if/else. Examples are provided to demonstrate printing text, taking user input, performing calculations, and making decisions based on relational comparisons.
Esoft Metro Campus - Programming with C++
(Template - Virtusa Corporate)
Contents:
Overview of C++ Language
C++ Program Structure
C++ Basic Syntax
Primitive Built-in types in C++
Variable types
typedef Declarations
Enumerated Types
Variable Scope
Constants/Literals
Storage Classes
Operators
Control Constructs
Functions
Math Operations in C++
Arrays
Multi-dimensional Arrays
Strings
C++ Pointers
References
Date and Time
Structures
Basic Input / Output
Classes and Objects
Inheritance
Overloading
Polymorphism
Interfaces
Files and Streams
Exception Handling
Dynamic Memory
Namespaces
Templates
Preprocessor
Multithreading
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and ... Note: This page does not list esoteric programming languages. .... Computer programming portal ...
02 functions, variables, basic input and output of c++Manzoor ALam
油
This document discusses computer programming functions, variables, and basic input/output in C and C++. It covers:
- Defining and calling functions, function parameters and return values.
- Declaring and assigning values to variables of different data types like int, float, and double.
- Using basic input/output functions like cout and cin to display output and get user input.
- The scope of variables and how they work within and outside of functions.
Let's explore some other fundamental programming concepts
Chapter 2 focuses on:
character strings
primitive data
the declaration and use of variables
expressions and operator precedence
data conversions
This presentation comes with many additional notes (pdf): http://de.slideshare.net/nicolayludwig/2-cpp-imperative-programming-38499061
Check out these exercises: http://de.slideshare.net/nicolayludwig/2-cpp-imperative-programmingexercises
- Imperative Programming
- Style and Conventions
- Constants
- Fundamental Types
- Console Basics
- Operators, Precedence, Associativity and Evaluation Order
- Control Structures and Blocks
This document summarizes key concepts from an introduction to C++ programming chapter, including:
- The main parts of a C++ program are comments, preprocessor directives, the main() function, and statements.
- Variables are used to store and manipulate data in a program. Variables are declared with a name and type before use.
- Arithmetic operators allow performing calculations in C++ programs. Expressions follow order of operations rules.
- Input and output streams allow getting user input and displaying output to the screen.
This document provides an introduction to the C programming language. It discusses fundamental C elements like data types, variables, constants, operators, and input/output functions. It explains how a basic C program is structured and compiled. Examples are provided to demonstrate simple C statements, arithmetic expressions, and how to write and run a first program that prints text. The key topics covered include basic syntax, program structure, data types, identifiers, operators, and input/output functions like printf() and scanf().
The document introduces fundamental programming concepts including variables, constants, data types, and operators. It discusses defining and declaring variables and constants, their naming rules and scope. The key data types in C like char, int, float and double are explained along with their ranges. The types of operators - arithmetic, relational, logical and their use in expressions are defined. Finally, it shows how to convert formulas into C expressions and the relationship between operands, operators and expressions.
This document provides an overview of programming in C. It discusses the importance of C as the base language for other programming languages and its efficiency. The basic structure of a C program is outlined as having documentation, include, define, global declaration, and main sections. Interpreters and compilers are explained as the two methods for running C programs, with compilers producing faster executable programs. Key aspects of C as a language are covered such as variables, data types, and constants.
Maximizing PMI Chapter Success to Streamline Events + Programs with OnePlanOnePlan Solutions
油
Running a PMI chapter is no easy feat. From managing ongoing events to keeping volunteers aligned, it can quickly become an overwhelming amount of projects to keep track of. Join us for an exclusive webinar designed for PMI Chapter Leaders. Well demonstrate how OnePlans free licensing can help streamline your chapters project management, improve collaboration, and provide real-time insights into your initiatives.
Managing Changing Data with FME Part 1 - Compare & DetectSafe Software
油
Data changes are inevitable, but keeping up doesnt have to be a struggle. Are you still manually tracking updates or managing unwieldy scripts? Thats a time-sink. Automate change detection with FME and ensure your data stays up-to-date without the hassle.
Discover how FMEs powerful transformers, like the ChangeDetector, can compare and detect changes to your data, automating incremental updates to keep your data warehouse and other systems synchronized in real-time.
While many tools can achieve this, they often lack support for spatial data or require complex, time-consuming processes. With FME, you get a flexible, intuitive platform that supports all data types, making change detection simple and efficient.
Join this webinar to learn change management strategies from demos that include:
- Detecting changes in your data
- Detecting changes in spatial data
- Incremental updates and data synchronization
- and more!
With FME, change is no longer something to fear. Because no matter what type of data you have, or at what stage it is in your workflow, FME makes working with change easy.
* This is part 1 of a 2-part series on managing changing data with FME. Stay tuned for Part 2 in the weeks to follow, where well cover further change-detection methods like database triggers and transaction logs!
This document summarizes Chapter 2 of a C++ basics textbook. Section 2.1 discusses variables and assignments, including declaring and initializing variables, assignment statements, and variable types. Section 2.2 covers input and output using streams like cout and cin, as well as formatting output. Section 2.3 describes C++ data types like int, double, and char, and how they store numeric values differently.
This document discusses basic program elements in C++, including:
1. Initializing variables can be done in two ways - by appending an equal sign followed by the value (int a = 0), or by enclosing the value in parentheses (int a(0)). Both methods are valid in C++.
2. Variables can have either local or global scope - local variables are declared within a function while global variables are declared outside all functions.
3. Keywords are reserved words in C++ that have special meaning and cannot be used as regular identifiers. Common keywords include int, float, if, else, while, etc.
The document provides an introduction to programming fundamentals in C++, including basic syntax and components of a C++ program. It covers variables and data types, input/output, comments, and how to write a simple C++ program with preprocessor directives and a main function. The key topics discussed are variable declaration, fundamental data types like int, float, char, comments, and how to write a basic "Hello World" program in C++.
The document discusses various C++ programming concepts including variables, data types, input/output streams, and expressions. It defines variables as memory locations that can store values, and describes different data types like int, double, char. It also covers input with cin, output with cout, and formatting output values.
The document is a chapter from an introductory C programming textbook. It covers basic C concepts like comments, variables, data types, functions, input/output, arithmetic operators, and conditional statements. It includes examples of simple C programs that print text, get user input, perform addition, and test for equality/relations between numbers. The chapter lays out fundamental building blocks to prepare the reader for more advanced C programming concepts covered later.
The document is an outline from a textbook on C programming. It covers basic concepts like simple programs to print text and perform math operations, use of variables and data types, memory concepts, decision making using if statements and relational operators. It includes examples of simple C programs and explanations of the core components like main function, comments, preprocessor directives, functions etc.
This document provides an overview of introductory concepts in C programming, including simple programs to print text and perform arithmetic. It covers basic syntax like comments, functions, variables, data types, operators, input/output, and conditional statements. Memory concepts are introduced, as well as the preprocessor, standard libraries, and control flow structures like if/else. Examples are provided to demonstrate printing text, taking user input, performing calculations, and making decisions based on relational comparisons.
Esoft Metro Campus - Programming with C++
(Template - Virtusa Corporate)
Contents:
Overview of C++ Language
C++ Program Structure
C++ Basic Syntax
Primitive Built-in types in C++
Variable types
typedef Declarations
Enumerated Types
Variable Scope
Constants/Literals
Storage Classes
Operators
Control Constructs
Functions
Math Operations in C++
Arrays
Multi-dimensional Arrays
Strings
C++ Pointers
References
Date and Time
Structures
Basic Input / Output
Classes and Objects
Inheritance
Overloading
Polymorphism
Interfaces
Files and Streams
Exception Handling
Dynamic Memory
Namespaces
Templates
Preprocessor
Multithreading
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and ... Note: This page does not list esoteric programming languages. .... Computer programming portal ...
02 functions, variables, basic input and output of c++Manzoor ALam
油
This document discusses computer programming functions, variables, and basic input/output in C and C++. It covers:
- Defining and calling functions, function parameters and return values.
- Declaring and assigning values to variables of different data types like int, float, and double.
- Using basic input/output functions like cout and cin to display output and get user input.
- The scope of variables and how they work within and outside of functions.
Let's explore some other fundamental programming concepts
Chapter 2 focuses on:
character strings
primitive data
the declaration and use of variables
expressions and operator precedence
data conversions
This presentation comes with many additional notes (pdf): http://de.slideshare.net/nicolayludwig/2-cpp-imperative-programming-38499061
Check out these exercises: http://de.slideshare.net/nicolayludwig/2-cpp-imperative-programmingexercises
- Imperative Programming
- Style and Conventions
- Constants
- Fundamental Types
- Console Basics
- Operators, Precedence, Associativity and Evaluation Order
- Control Structures and Blocks
This document summarizes key concepts from an introduction to C++ programming chapter, including:
- The main parts of a C++ program are comments, preprocessor directives, the main() function, and statements.
- Variables are used to store and manipulate data in a program. Variables are declared with a name and type before use.
- Arithmetic operators allow performing calculations in C++ programs. Expressions follow order of operations rules.
- Input and output streams allow getting user input and displaying output to the screen.
This document provides an introduction to the C programming language. It discusses fundamental C elements like data types, variables, constants, operators, and input/output functions. It explains how a basic C program is structured and compiled. Examples are provided to demonstrate simple C statements, arithmetic expressions, and how to write and run a first program that prints text. The key topics covered include basic syntax, program structure, data types, identifiers, operators, and input/output functions like printf() and scanf().
The document introduces fundamental programming concepts including variables, constants, data types, and operators. It discusses defining and declaring variables and constants, their naming rules and scope. The key data types in C like char, int, float and double are explained along with their ranges. The types of operators - arithmetic, relational, logical and their use in expressions are defined. Finally, it shows how to convert formulas into C expressions and the relationship between operands, operators and expressions.
This document provides an overview of programming in C. It discusses the importance of C as the base language for other programming languages and its efficiency. The basic structure of a C program is outlined as having documentation, include, define, global declaration, and main sections. Interpreters and compilers are explained as the two methods for running C programs, with compilers producing faster executable programs. Key aspects of C as a language are covered such as variables, data types, and constants.
Maximizing PMI Chapter Success to Streamline Events + Programs with OnePlanOnePlan Solutions
油
Running a PMI chapter is no easy feat. From managing ongoing events to keeping volunteers aligned, it can quickly become an overwhelming amount of projects to keep track of. Join us for an exclusive webinar designed for PMI Chapter Leaders. Well demonstrate how OnePlans free licensing can help streamline your chapters project management, improve collaboration, and provide real-time insights into your initiatives.
Managing Changing Data with FME Part 1 - Compare & DetectSafe Software
油
Data changes are inevitable, but keeping up doesnt have to be a struggle. Are you still manually tracking updates or managing unwieldy scripts? Thats a time-sink. Automate change detection with FME and ensure your data stays up-to-date without the hassle.
Discover how FMEs powerful transformers, like the ChangeDetector, can compare and detect changes to your data, automating incremental updates to keep your data warehouse and other systems synchronized in real-time.
While many tools can achieve this, they often lack support for spatial data or require complex, time-consuming processes. With FME, you get a flexible, intuitive platform that supports all data types, making change detection simple and efficient.
Join this webinar to learn change management strategies from demos that include:
- Detecting changes in your data
- Detecting changes in spatial data
- Incremental updates and data synchronization
- and more!
With FME, change is no longer something to fear. Because no matter what type of data you have, or at what stage it is in your workflow, FME makes working with change easy.
* This is part 1 of a 2-part series on managing changing data with FME. Stay tuned for Part 2 in the weeks to follow, where well cover further change-detection methods like database triggers and transaction logs!
ESET NOD32 Antivirus Crack with License Key 2025umeerbinfaizan
油
https://up-community.net/dl/
Copy This Link and paste in new tab & get Crack File
Download essential protection with award-winning antivirus. Protects against hackers, ransomware and phishing with the optimum balance of speed
Internet Download Manager Crack Latest version 2025mohsinrazakpa26
油
艶COPY LINK & PASTE ON GOOGLE https://9to5mac.org/after-verification-click-go-to-download-page
Internet Download Manager or IDM is an advanced download manager software that makes it easier to manage your downloaded files with the intelligent system, this program will speed up the downloading of files with its new technology, and according to the manufacturer, It can download up to 5 times faster than usual.
艶COPY LINK & PASTE ON GOOGLE https://9to5mac.org/after-verification-click-go-to-download-pageThank
Free Download Adobe Illustrator CC Pre-Activated Offline Installer + Portable for Windows PC. The industry-standard vector graphics software lets you create logos, icons, drawings, typography, and illustrations for print, web, video, and mobile.
Migrating GitHub Actions with Nested Virtualization to Cloud Native Ecosystem...KCD Guadalajara
油
GitHub recently announced the end of support for macOS 12, which was the only GitHub-hosted runner capable of supporting nested virtualization. As a result, many teams relying on this feature for CI/CD workflows face the challenge of migrating existing dependencies to alternative solutions. In this session, I will share my experience migrating workloads from GitHub-hosted runners to a cloud-native solution. By leveraging Actions Runner Controller (ARC), KubeVirt, Tekton, and Longhorn, we were able to provision self-hosted runners on demand, replicating the functionality of nested virtualization while benefiting from the scalability and flexibility of the cloud-native ecosystem.
Virtual DJ Pro Crack 2025 Full Version Download [Latest]umeerbinfaizan
油
https://up-community.net/dl/
Copy This Link and paste in new tab & get Crack File
Virtual DJ is a professional DJ software used for audio and video mixing. It is designed for both beginners and professional DJs, offering powerful tools
Movavi Video Editor Crack + Activation Key [2025]l07307095
油
COPY & PASTE LINK
https://upcommunity.net/dl/
Movavi Video Editor is a beginner-friendly multimedia application that can help you create professional-level videos.
The slides of my presentation in Utrecht, Nederlands, at the 29th of march.
It is a high-level presentation of our mORMot 2 Open Source toolbox, in modern object pascal, for both Delphi and FPC/Lazarus.
It tries to demystify some of the main misunderstanding about Object Pascal or our Open Source framework.
Cypress Parallel Testing Tutorial: Speed Up Your Test Runs with EaseShubham Joshi
油
Parallel testing with Cypress can dramatically reduce test execution time and accelerate your CI/CD pipeline. This tutorial walks you through setting up parallel execution using the Cypress Dashboard or third-party CI tools like GitHub Actions and CircleCI. Includes practical code examples to help you get started quickly.
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odooAxisTechnolabs
油
Tour, Travel and Hotel booking management module in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Visit And Buy Now : https://bit.ly/3THskJQ
Tour and Travel Management odoo module helps to manage contracts with all your suppliers with your accommodation, transportation, restaurants, guides and other logistical requirements.
Lets checkout some Amazing Key Features for Tour and Travel Booking Management in odoo:
Key Features :
Tour Management Dashboard
Tour Itinerary
Tour Consulting
Booking Services
Hotel Room Type
And much more...
Just visit our app link to know more exciting features of tour and travel management odoo module :
Want to Download ?
Odoo18 : https://bit.ly/3ULBe90
Odoo17 : https://bit.ly/3THskJQ
Odoo 16 : https://bit.ly/3Vx1KRe
Odoo 15 : https://bit.ly/3tZ53qj
Odoo 14 : https://bit.ly/3nEvL39
Odoo 13 : https://bit.ly/3nKZFTx
Odoo 12 : https://bit.ly/32fZN5W
Odoo 11 : https://bit.ly/3rtiJ9O
¥Explore more our Apps : https://bit.ly/3oFIOCF
¥Want A demo ? business@axistechnolabs.com
¥Click here And explore "Axistechnolabs" : https://www.axistechnolabs.com/
¥Contact us : 091066 49361
艶COPY LINK & PASTE ON GOOGLE https://9to5mac.org/after-verification-click-go-to-download-page
Updating drivers is usually an initial step to avoid hardware failure, system instability, and hidden security vulnerabilities. Update drivers regularly is also an effective way to enhance your overall PC performance and maximize your gaming experience.
Based on the cloud library, IObit Driver Booster Pro can always be the first to identify outdated drivers and download and update drivers at an unrivaled speed. The backup feature is an easy, effective, and risk-free solution to keeping your drivers up to date.
ESET Smart Security Crack + Activation Key 2025 [Latest]umeerbinfaizan
油
Copy Link and paste new tab
https://up-community.net/dl/
ESET activation key is a unique sequence of letters and numbers separated by a dash, provided by ESET to allow the legal use of ESET Internet Security.
New-4K Video Downloader Crack + License Key 2025abbaskanju3
油
¥ 艶COPY & PASTE LINK
https://click4pc.com/after-verification-click-go-to-download-page/
4K Video Downloader is a cross-platform app that lets you save high-quality videos from YouTube and other websites in seconds. It works faster than any free online video downloader just a click, and you can enjoy content anytime, anywhere.
艶COPY LINK & PASTE ON GOOGLE https://9to5mac.org/after-verification-click-go-to-download-page
Free download Autodesk MotionBuilder for Windows. It is 3D character animation software for virtual production that helps you manipulate and refine data with excellent reliability and efficiency. Capture, edit, and playback complex character animation in a highly responsive, interactive environment and work with a display optimized for both animators and directors.
Marketo User Group - Singapore - April 2025BradBedford3
油
Singapore MUG: Elevate Your Marketo Game!
Marketo at Adobe Summit: Get the latest updates on Marketo's roadmap and highlights, including the new GenAI-powered email designer. Learn how this no-code tool is revolutionizing email marketing.
AI-Powered Email Marketing: See how Marketo Engages new features, like the AI Assistant and Adobe Express integrations, are helping teams create professional, on-brand emails effortlessly.
Business Outcomes with Marketo x SFDC: Explore how the integration of Marketo and Salesforce (SFDC) can enhance lead management, improve collaboration, and optimize campaign performance. Gain actionable insights from Andrew Ong on best practices for data synchronization, lead scoring, and more.
TVersity Pro Media Server Free CRACK Downloadmohsinrazakpa43
油
艶COPY LINK & PASTE ON GOOGLE https://9to5mac.org/after-verification-click-go-to-download-page
TVersity Pro Media Server CRACK is an awesome application that has been developed which will help you detect and share multimedia files on the Internet. With this stellar application you can play as well as record online audio/video content.
TVersity Pro Media Server Free CRACK Downloadmohsinrazakpa43
油
Chapter 2 Introduction to C++
1. Chapter 2
Introduction to C++
Starting Out with C++ Early Objects
Seventh Edition
by Tony Gaddis, Judy Walters,
and Godfrey Muganda
2-1
2. 2.1 Parts of a C++ Program
// sample C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, there!";
return 0;
}
2-2
comment
preprocessor directive
which namespace to use
beginning of function named main
beginning of block for main
output statement
send 0 back to operating system
end of block for main
3. Special Characters (pg. 30)
2-3
Character Name Description
// Double Slash Begins a comment
# Pound Sign Begins preprocessor directive
< > Open, Close Brackets Encloses filename used in
#include directive
( ) Open, Close Parentheses Used with function & other
times
{ } Open, Close Braces Encloses a group of statements
" " Open, Close Quote Marks Encloses string of characters
; Semicolon Ends a programming statement
4. Important Details
C++ is case-sensitive. Uppercase &
lowercase characters are different
characters. Main is not the same as
main.
Every { must have a corresponding },
and vice-versa.
Watch for the NOTE and WARNING boxes n
your text. They provide good information! 2-4
5. 2.2 The cout Object
Displays information on computer screen
Equivalent to print to screen
Use << to send information to cout
cout << "Hello, there!";
Can use << to send multiple items to cout
cout << "Hello, " << "there!";
Or equivalently
cout << "Hello, ";
cout << "there!"; 2-5
6. cout
<< stream insertion operator
Must be between each different element
cout << Total is << Tot;
Total is 49
2-6
7. Starting a New Line
To get multiple lines of output on screen
- Use endl
cout << "Hello, there!" << endl;
- Use n in an output string
cout << "Hello, there!n";
2-7
8. New lines of output
endl a command
Causes a new line of output
Dumps the output buffer to output file
n a special character
Causes a new line of output only
MUST be inside quotes
Side-effect: when used with file output,
some output may not get printed
Test question
2-8
9. Common Escape Sequences (p.35)
~~ MUST be contained in quotes
~~ Considered a single character
n Newline
t Tab
a Alarm
b Backspace
r Return (same line)
Prints 1
Prints single quote
Prints double quote 2-9
11. 2.3 The #include Directive
Inserts the contents of another file into the
program
Is a preprocessor directive
Not part of the C++ language
Not seen by compiler
Example:
#include <iostream>
There will be several different files that we will include
for different purposes. 2-11
No
semicolon;
goes here
12. 2.4 Standard & Pre-standard C++
Older-style C++ programs
Use .h at end of header files
#include <iostream.h>
Do not use using namespace convention
May not compile with a standard C++ compiler
We WILL NOT use the .h format.
2-12
13. 2.5 Variables, Constants, & the
Assignment Statement
Variable
Has a name (identifier) & a type of data it can hold
char letter;
Is used to reference a location in memory where a
value can be stored
Must be defined before it can be used
The value that is stored can be changed,
i.e., it can vary
When use name in program, referring to data stored
in the corresponding memory location.
2-13
variable
name
data type
14. Variables
Represents a location in memory
Stores/Holds on data value
If a new value is stored in the variable, it
replaces previous value
The previous value is overwritten and can no
longer be retrieved
int age;
age = 17; // age is 17
cout << age; // Displays 17
age = 18; // Now age is 18
cout << age; // Displays 18 2-14
15. Assignment Statement
Uses the = operator (is assigned)
Has single variable on left side and a
value or expression on right side
Copies the value on right into the
variable on left, i.e. its memory location
item = 12;
total = 15 + 2;
tax = Bal * 0.0825; 2-15
16. Constant
Data item whose value does not change
during program execution NEVER
Is also called a literal
'A' // character constant
"Hello" // string literal
12 // integer constant
3.14 // floating-point constant
Often a test question
2-16
17. 2.6 Identifiers
Programmer-chosen names to represent parts of
program, such as variables & constants
Any length, but you must type it repeatedly
Name indicates use of the identifier
Cannot use C++ key words as identifiers
See Table 2.4 pg. 41
Legal form (this is a question on test 1)
Must begin with alphabetic character or _
followed by alphabetic, numeric, or _ . Alpha may be
upper- or lowercase
2-17
18. Valid and Invalid
Identifiers
2-18
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
total.Sales No Cannot contain period
4thQtrSales No Cannot begin with digit
totalSale$ No Cannot contain $
19. 2.7 Integer Data Types
Designed to hold whole numbers
Can be signed or unsigned
12 -6 +3
Available in different sizes (i.e., number of
bytes): short, int, and long
size of short size of int size of long
Table 2-6 pg. 44
2-19
20. Defining Variables
Variables of the same type can be defined
- In separate statements
int length;
int width;
- In the same statement
int length, width;
Variables of different types must be defined
in separate statements
int length;
short width;
2-20
21. 2.8 The char Data Type
Used to hold single characters
Or very small integer values: 0 to 15 *but dont*
(Usually) Occupies 1 byte of memory
A numeric code representing the character
is stored in memory
2-21
SOURCE CODE MEMORY
char letter = c'; letter
0110 0011
22. String Constant
Can be stored a series of characters in
consecutive memory locations
"Hello"
Stored with null terminator, 0, at end
Is comprised of characters between
the " "
2-22
H e l l o 0
23. A character or a string constant?
A character constant is a single character,
enclosed in single quotes:
'C'
A string constant is a sequence of
characters enclosed in double quotes:
"Hello, there!"
A single character in double quotes is a
string constant, not a character constant:
"C"
2-23
24. 2.9 The C++ string Class
Must #include <string> to create & use
string objects
Can define string variables in programs
string name;
Can assign values to string variables with
assignment operator
name = "George";
Can display with cout
cout << name;
Note: # include <string> unnecessary if using
only string literals
cout << George; 2-24
25. 2.10 Floating-Point Data Types
Hold real numbers
12.45 -3.8
Stored in form similar to scientific notation
Numbers are all signed
Types are
float - 4 bytes
double 8 bytes
long double 8 bytes (usually)
2-25
27. Floating-point Constants
Can be represented in
- Fixed point (decimal) notation:
31.4159 0.0000625
- E-notation: (we wont use but need to recognize)
3.14159E1 6.25e-5
Usually indicate extremely large
or small value
Are double by default
2-27
28. Assigning Floating-point
Values to Integer Variables
If floating-point value is assigned to an
integer variable
The fractional part will be truncated
(i.e., chopped off & discarded)
The value is NOT rounded
int rainfall = 3.88;
cout << rainfall; // Displays 3
2-28
29. 2.11 The bool Data Type
Represents values that are true or false
bool values are stored as short integers
false is represented by 0, true by 1
bool allDone = true;
bool finished = false;
Note: Any non-zero value is considered
true. 2-29
allDone finished
1 0
30. 2.12 Determining the Size
of a Data Type
The sizeof operator gives size of any data
type or variable
double amount;
cout << "A float is stored in "
<< sizeof(float) << " bytesn";
cout << "Variable amount is stored in "
<< sizeof(amount) << " bytesn";
2-30
31. 2.13 More on Variable
Assignments and Initialization
Assigning value to a variable
Assigns a value to a previously created variable
A single variable name must appear on left side
of the = symbol
int size;
size = 5; // legal
5 = size; // not legal
2-31
32. Variable Assignment vs.
Initialization
Initializing a variable
Gives initial value to variable at time it is
created
Can initialize some or all variables of
definition
int length = 12;
int width = 7, height = 5, area;
2-32
33. What if a variable is not initialized??
double amount;
cout << amount;
What happens?? . It depends
*Ignore - continues, repeats message
*Abort program stops
*Retry tries again then stops
- get MS send error report?
2-33
34. 2.14 Scope
The scope of a variable is that part of the
program where the variable may be used
Scope of variable begins with definition,
continues through the block in which it is
defined
A variable cannot be used before it is defined
int a;
cin >> a; // legal
cin >> b; // illegal
int b;
(Test question)
2-34
35. Scope
To avoid problems
Define ALL variables & constants at the
beginning of the program!!!
Later we will discuss other issues
related to scope.
2-35
36. 2.15 Arithmetic Operators
Used for performing numeric calculations
C++ has unary, binary, and ternary
operators
unary (1 operand) -5
binary (2 operands) 13 - 7
ternary (3 operands) exp1 ? exp2 : exp3
2-36
37. Binary Arithmetic
Operators
2-37
SYMBOL OPERATION EXAMPLE ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Order of operation is standard mathematics.
38. / Operator
C++ division operator (/)performs integer
division if both operands are integers
cout << 13 / 5; // displays 2
cout << 2 / 4; // displays 0
If either operand is floating-point, the result
is floating-point
cout << 13 / 5.0; // displays 2.6
cout << 2.0 / 4; // displays 0.5
2-38
39. % Operator - Modulus
Same priority as * and /
C++ modulus operator (%) computes the
remainder resulting from integer division
cout << 9 % 2; // displays 1
% requires integers for both operands
cout << 9 % 2.0; // error
2-39
40. 2.16 Comments
Used to document parts of program
Written for persons reading source code
of program
Indicate purpose of program
Describe use of variables
Explain complex sections of code
Ignored by compiler
REQUIRED for all programs (by RH)
No absolute rules, but STANDARDS 2-40
41. Single-Line Comments
Begin with // through to the end of line
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area
// Calculate rectangle area
area = length * width;
2-41
42. Multi-Line Comments
Begin with /* & end with */
Can span multiple lines
/*----------------------------
Here's a multi-line comment
----------------------------*/
Can be used as single-line comments
int area; /* Calculated area */
MUST have both
2-42
43. Comment Guidelines
Use your textbook as a GOOD Example
Always comment with name, project name,
project description at beginning of every
program
Descriptive comments within body of program
are REQUIRED
If comment is so generic it could be moved to any
other program then it is not a good comment
If comment states exactly what the codes says
then it is not a good comment
2-43
44. Chapter 2 Homework
Checkpoints
These are at the end of most sections (e.g. page
31). You should do all of these
End of Chapter questions
Page 70+; All (1 27) 8E
Quizzes will usually come from checkpoints &
end of chapter questions
2-44