SlideShare a Scribd company logo
1 of 4
Download to read offline
Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 1
Chapter 2 – Identifiers, Constants,
Objects, & Assignment Statements
1. Identifiers
Identifiers are the names given to anything in C++ that needs a name. Valid
identifiers in C++ must be formed according to a few simple rules. An identifier may start
with an alphabetic character (A-Z, a-z) or the underscore ( _ ), then may include any
number of alphabetic characters, underscores, or digits (0, 1 , 2, …, 9). Spaces may not
be included in an identifier.
Examples of Valid Identifiers
Sam, SUE, _go, nUmBeR, v1, v2, v3
Examples of Invalid Identifiers
1v, 2v, 3v, one-potatoe, $pay, a twit, dot.period
Lastly, C++ is case sensitive. For example, the identifier “Sam” is not the same
identifier as “SAM” or “sam”;
2. Constants, Objects, and Assignment Statements
Types and classes are not directly used in C++ programs. Instead, types and classes
are expressed as literal constants or objects.
Literal constants are simple values, such as the number 42 and all of the state
examples in Chapter 1. In general, it is only possible to use literal constants for the built-
in types.
Objects are named instances of types and classes. Each object has all of the
functionality of its type or class. One way to think of this is to regard the type or class as a
set of blueprints or plans and the object as the finished product. Objects of the built-in
types or other classes serve the same function as variables in other programming
languages. Objects are sometimes referred to as variable objects or variable identifiers.
Objects must be declared before being used. Once declared, objects can have their
state information altered, be used in expressions and have their member functions called.
Objects can be declared in statements of the following forms.
class-or-type object;
class-or-type object, object;
class-or-type object, object,…, object;
Here are some example object declarations.
int year;
int month, day;
double rate;
Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 2
Objects can be assigned values via the assignment statement. The assignment
statement has the following form:
object = expression;
In the assignment statement, the expression on the right of the equal sign is evaluated and
the results of that evaluation are copied into the object on the left of the equal sign. Here
are some examples of assignment statements.
firstInitial = ‘R’;
hourlyRate = 7.55;
cookiesLeft = cookies % number_of_guests;
Assignment statements are very common statements. In addition, they are very
versatile and can be used inside of some other statements (with the semicolon omitted).
Declaration statements followed by assignments are valid and usual, but can be shortened
to the following forms:
class object = expression;
class object = expression, object = expression, …, object = expression;
Long Version Examples
long cookiesLeft;
cookiesLeft = cookies % numberOfGuests;
char firstInitial;
frstInitial = ‘R’;
Shortened Version Examples
long cookiesLeft = cookies % numberfOfGuests;
char firstInitial = ‘R’;
Placing const before a declaration of an object that is using the shortened version of
declaration and assignment produces a named constant identifier. For example:
const long YEARS = 100;
const long DAYS_IN_YEAR = 365;
Unlike a variable identifier, a named constant identifier may not have its value changed
while the programming is running.
Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 3
3. Example Program: As Time Goes By
Here is an example program to calculate the number of days, weeks, hours, and
minutes in 100 years. (As in this example, named constants are often declared outside of a
function in order that they may be shared with all functions declared after them.)
#include <iostream>
using namespace std;
const long YEARS = 100;
const long DAYS_IN_YEAR = 365;
void main( )
{
long leap_days = YEARS / 4;
long days = YEARS * DAYS_IN_YEAR + leap_days;
long weeks = days / 7;
long hours = days * 24;
cout << “100 years contain “ << weeks << “ weeks, “
<< days << “ days, “
<< hours << “ hours, and“
<< hours * 60 << “ minutes.”;
}
This program outputs:
100 years contain 5217 weeks, 36525 days, 876600 hours, and 52596000 minutes.
Exercises
1. What is an identifier?
2. Which of the following identifiers are valid?
Dog _Cat 1984 _1984
one-two one two one_two a1
3. What is a literal constant?
4. What is an object?
5. What is declaring an object?
6. What is a variable identifier?
7. What is a named constant identifier?
8. What is an assignment statement?
Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 4
9. What are the values of A, B, and C after each assignment statement in the example
program has been executed? If the value is unknown, write a question mark in the
blank. What is the final output of the program?
#include <iostream>
using namespace std;
void main( )
{ A B C
int a, b, c; _____ _____ _____
a = 3; _____ _____ _____
b = a + 2; _____ _____ _____
c = b + a; _____ _____ _____
a = b + c; _____ _____ _____
cout << a << “ “ << b << “ “ << c;
}
Programming Problem 2.1
Use the following declarations to write a program to calculate and output the area of a
plot of land. Units are feet. Output should be expressed in terms of square feet.
double length = 88.5;
double width = 100.0;
Programming Problem 2.2
Calculate and output the following powers of 2: 21
, 22
, 23
, 24
, 25
. In this program, each
power of 2 should be assigned to its own object and the output should be produced from
that object.

More Related Content

What's hot

C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#Dr.Neeraj Kumar Pandey
 
Learn C# Programming - Operators
Learn C# Programming - OperatorsLearn C# Programming - Operators
Learn C# Programming - OperatorsEng Teong Cheah
 
Complete Tokens in c/c++
Complete Tokens in c/c++Complete Tokens in c/c++
Complete Tokens in c/c++Shobi P P
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#Prasanna Kumar SM
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsWilliam Olivier
 
Chapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsChapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsIt Academy
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceIt Academy
 
Basic Structure Of C++
Basic Structure Of C++Basic Structure Of C++
Basic Structure Of C++DevangiParekh1
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrongJulien Wetterwald
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 
02 Primitive data types and variables
02 Primitive data types and variables02 Primitive data types and variables
02 Primitive data types and variablesmaznabili
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programmingNico Ludwig
 

What's hot (19)

Structure and Enum in c#
Structure and Enum in c#Structure and Enum in c#
Structure and Enum in c#
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
Learn C# Programming - Operators
Learn C# Programming - OperatorsLearn C# Programming - Operators
Learn C# Programming - Operators
 
INTRODUCTION TO C
INTRODUCTION TO CINTRODUCTION TO C
INTRODUCTION TO C
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Complete Tokens in c/c++
Complete Tokens in c/c++Complete Tokens in c/c++
Complete Tokens in c/c++
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculations
 
Chapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsChapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic Concepts
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class Inheritance
 
Basic Structure Of C++
Basic Structure Of C++Basic Structure Of C++
Basic Structure Of C++
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrong
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
C Token’s
C Token’sC Token’s
C Token’s
 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
 
02 Primitive data types and variables
02 Primitive data types and variables02 Primitive data types and variables
02 Primitive data types and variables
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
Handout#04
Handout#04Handout#04
Handout#04
 

Similar to Kernel Adiutor

Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxChereLemma2
 
introductory concepts
introductory conceptsintroductory concepts
introductory conceptsWalepak Ubi
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENTLori Moore
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingSherwin Banaag Sapin
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfAbrehamKassa
 
presentation of java fundamental
presentation of java fundamentalpresentation of java fundamental
presentation of java fundamentalGanesh Chittalwar
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...RSathyaPriyaCSEKIOT
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

Similar to Kernel Adiutor (20)

Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptx
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
 
presentation of java fundamental
presentation of java fundamentalpresentation of java fundamental
presentation of java fundamental
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
java tutorial 2
 java tutorial 2 java tutorial 2
java tutorial 2
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

Kernel Adiutor

  • 1. Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 1 Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements 1. Identifiers Identifiers are the names given to anything in C++ that needs a name. Valid identifiers in C++ must be formed according to a few simple rules. An identifier may start with an alphabetic character (A-Z, a-z) or the underscore ( _ ), then may include any number of alphabetic characters, underscores, or digits (0, 1 , 2, …, 9). Spaces may not be included in an identifier. Examples of Valid Identifiers Sam, SUE, _go, nUmBeR, v1, v2, v3 Examples of Invalid Identifiers 1v, 2v, 3v, one-potatoe, $pay, a twit, dot.period Lastly, C++ is case sensitive. For example, the identifier “Sam” is not the same identifier as “SAM” or “sam”; 2. Constants, Objects, and Assignment Statements Types and classes are not directly used in C++ programs. Instead, types and classes are expressed as literal constants or objects. Literal constants are simple values, such as the number 42 and all of the state examples in Chapter 1. In general, it is only possible to use literal constants for the built- in types. Objects are named instances of types and classes. Each object has all of the functionality of its type or class. One way to think of this is to regard the type or class as a set of blueprints or plans and the object as the finished product. Objects of the built-in types or other classes serve the same function as variables in other programming languages. Objects are sometimes referred to as variable objects or variable identifiers. Objects must be declared before being used. Once declared, objects can have their state information altered, be used in expressions and have their member functions called. Objects can be declared in statements of the following forms. class-or-type object; class-or-type object, object; class-or-type object, object,…, object; Here are some example object declarations. int year; int month, day; double rate;
  • 2. Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 2 Objects can be assigned values via the assignment statement. The assignment statement has the following form: object = expression; In the assignment statement, the expression on the right of the equal sign is evaluated and the results of that evaluation are copied into the object on the left of the equal sign. Here are some examples of assignment statements. firstInitial = ‘R’; hourlyRate = 7.55; cookiesLeft = cookies % number_of_guests; Assignment statements are very common statements. In addition, they are very versatile and can be used inside of some other statements (with the semicolon omitted). Declaration statements followed by assignments are valid and usual, but can be shortened to the following forms: class object = expression; class object = expression, object = expression, …, object = expression; Long Version Examples long cookiesLeft; cookiesLeft = cookies % numberOfGuests; char firstInitial; frstInitial = ‘R’; Shortened Version Examples long cookiesLeft = cookies % numberfOfGuests; char firstInitial = ‘R’; Placing const before a declaration of an object that is using the shortened version of declaration and assignment produces a named constant identifier. For example: const long YEARS = 100; const long DAYS_IN_YEAR = 365; Unlike a variable identifier, a named constant identifier may not have its value changed while the programming is running.
  • 3. Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 3 3. Example Program: As Time Goes By Here is an example program to calculate the number of days, weeks, hours, and minutes in 100 years. (As in this example, named constants are often declared outside of a function in order that they may be shared with all functions declared after them.) #include <iostream> using namespace std; const long YEARS = 100; const long DAYS_IN_YEAR = 365; void main( ) { long leap_days = YEARS / 4; long days = YEARS * DAYS_IN_YEAR + leap_days; long weeks = days / 7; long hours = days * 24; cout << “100 years contain “ << weeks << “ weeks, “ << days << “ days, “ << hours << “ hours, and“ << hours * 60 << “ minutes.”; } This program outputs: 100 years contain 5217 weeks, 36525 days, 876600 hours, and 52596000 minutes. Exercises 1. What is an identifier? 2. Which of the following identifiers are valid? Dog _Cat 1984 _1984 one-two one two one_two a1 3. What is a literal constant? 4. What is an object? 5. What is declaring an object? 6. What is a variable identifier? 7. What is a named constant identifier? 8. What is an assignment statement?
  • 4. Chapter 2 – Identifiers, Constants, Objects, & Assignment Statements (version 2002.05) page 4 9. What are the values of A, B, and C after each assignment statement in the example program has been executed? If the value is unknown, write a question mark in the blank. What is the final output of the program? #include <iostream> using namespace std; void main( ) { A B C int a, b, c; _____ _____ _____ a = 3; _____ _____ _____ b = a + 2; _____ _____ _____ c = b + a; _____ _____ _____ a = b + c; _____ _____ _____ cout << a << “ “ << b << “ “ << c; } Programming Problem 2.1 Use the following declarations to write a program to calculate and output the area of a plot of land. Units are feet. Output should be expressed in terms of square feet. double length = 88.5; double width = 100.0; Programming Problem 2.2 Calculate and output the following powers of 2: 21 , 22 , 23 , 24 , 25 . In this program, each power of 2 should be assigned to its own object and the output should be produced from that object.