This document discusses C++ user-defined data types including enumeration types, namespaces, and strings. It covers creating enumeration types and anonymous data types, using the typedef statement, and defining namespaces to avoid naming collisions. The string type is explored along with functions like length(), size(), find(), substr(), and swap() for manipulating strings. Examples are provided for creating an enumeration type, using namespaces, and a program that converts strings to pig Latin.
This chapter discusses the basic elements of C++ programs, including functions, data types, operators, and input/output statements. It covers fundamental concepts like declaring variables and constants, arithmetic expressions, strings, and comments. The objectives are to familiarize readers with C++ syntax and semantics, and teach them how to properly structure a basic C++ program.
This document discusses C++ programming and includes several sections:
- It provides an overview of how a C++ program is processed by a compiler and linker before being executed.
- It explains the problem analysis-coding-execution cycle used for programming and problem solving.
- It presents an example algorithm for calculating the perimeter and area of a rectangle.
- It outlines the basic elements and components of a C++ program such as functions, data types, operators, and comments.
The chapter introduces the basic components of a C++ program, including functions, data types, operators, and statements. It discusses simple data types like integers, characters, and floating-point numbers. The chapter also covers arithmetic operators, expressions, and the order of precedence. It explains variable declaration, assignment statements, and how to input and output data. The goal is for readers to understand the basic structure and syntax of a C++ program.
This chapter discusses arrays and strings in C++. It covers topics such as declaring and initializing one-dimensional and multi-dimensional arrays, accessing array elements, common array operations like summation and finding largest/smallest elements, passing arrays to functions, and using character arrays (C-strings) to represent strings. The chapter also discusses parallel arrays, arrays of strings, and multidimensional arrays.
This chapter discusses arrays and strings in C++. It covers topics such as declaring and initializing one-dimensional and multi-dimensional arrays, accessing array elements, passing arrays to functions, and built-in functions for manipulating C-strings. The chapter also introduces parallel arrays, arrays of strings, and common array processing tasks like searching, sorting, summing elements, and finding maximum/minimum values.
This document provides an overview of arrays and strings in C++. It discusses how to declare and manipulate one-dimensional arrays, common array operations using loops, restrictions on array processing, passing arrays as function parameters, and accessing array elements. It also covers C-strings (character arrays), comparing and inputting/outputting strings, and using strings for file I/O. The overall goal is to teach the reader about working with arrays and strings in C++.
This chapter discusses user-defined functions in C++. It covers void functions, value and reference parameters, scope of identifiers, static and automatic variables, function overloading, and functions with default parameters. The key topics are how to define and call void functions, the difference between value and reference parameters and their effect on passing data, and how scopes determine where identifiers are accessible within a program. An example program is provided to demonstrate classifying numbers with user-defined functions.
This chapter discusses the basic elements of C++ programs, including functions, data types, operators, and input/output statements. It covers fundamental concepts like declaring variables and constants, arithmetic expressions, strings, and comments. The objectives are to familiarize readers with C++ syntax and semantics, and teach them how to properly structure a basic C++ program.
This document discusses C++ programming and includes several sections:
- It provides an overview of how a C++ program is processed by a compiler and linker before being executed.
- It explains the problem analysis-coding-execution cycle used for programming and problem solving.
- It presents an example algorithm for calculating the perimeter and area of a rectangle.
- It outlines the basic elements and components of a C++ program such as functions, data types, operators, and comments.
The chapter introduces the basic components of a C++ program, including functions, data types, operators, and statements. It discusses simple data types like integers, characters, and floating-point numbers. The chapter also covers arithmetic operators, expressions, and the order of precedence. It explains variable declaration, assignment statements, and how to input and output data. The goal is for readers to understand the basic structure and syntax of a C++ program.
This chapter discusses arrays and strings in C++. It covers topics such as declaring and initializing one-dimensional and multi-dimensional arrays, accessing array elements, common array operations like summation and finding largest/smallest elements, passing arrays to functions, and using character arrays (C-strings) to represent strings. The chapter also discusses parallel arrays, arrays of strings, and multidimensional arrays.
This chapter discusses arrays and strings in C++. It covers topics such as declaring and initializing one-dimensional and multi-dimensional arrays, accessing array elements, passing arrays to functions, and built-in functions for manipulating C-strings. The chapter also introduces parallel arrays, arrays of strings, and common array processing tasks like searching, sorting, summing elements, and finding maximum/minimum values.
This document provides an overview of arrays and strings in C++. It discusses how to declare and manipulate one-dimensional arrays, common array operations using loops, restrictions on array processing, passing arrays as function parameters, and accessing array elements. It also covers C-strings (character arrays), comparing and inputting/outputting strings, and using strings for file I/O. The overall goal is to teach the reader about working with arrays and strings in C++.
This chapter discusses user-defined functions in C++. It covers void functions, value and reference parameters, scope of identifiers, static and automatic variables, function overloading, and functions with default parameters. The key topics are how to define and call void functions, the difference between value and reference parameters and their effect on passing data, and how scopes determine where identifiers are accessible within a program. An example program is provided to demonstrate classifying numbers with user-defined functions.
The document provides an overview of key concepts for C++ programming including:
- The problem analysis to coding execution cycle involves analyzing the problem, designing an algorithm, coding the program, preprocessing directives, compiling, linking, loading, and executing the program.
- Basic C++ program structure includes #include directives, using namespace std, and main() function returning int.
- Comments, identifiers, data types, arithmetic operators, expressions, type conversions, strings, constants, increment/decrement operators, I/O streams, and input/output statements are discussed.
- Syntax errors are reported by the compiler while logic errors are typically not caught and must be debugged using output statements.
This chapter discusses pointers, classes, virtual functions, and abstract classes in C++. Pointers contain the addresses of other variables and can be used to access dynamic memory. The address of and dereferencing operators are used to work with pointers. Classes and structs can have pointer member variables. Virtual functions allow dynamic binding at runtime rather than compile-time. Abstract classes define pure virtual functions that derived classes must implement.
The document discusses chapters 4 and 5 from a C++ programming textbook. Chapter 4 covers selection control structures like if/else statements and relational operators. Chapter 5 discusses repetition control structures like while, for, and do-while loops. The document provides examples and explanations of how to use these different control structures in C++ programs. It also discusses logical operators, order of precedence, switch statements, nested control structures, and the break and continue statements.
This chapter discusses records (structs) in C++. A struct is a collection of members of different types that are accessed by name. Structs allow grouping of related data and functions together. The chapter covers defining and declaring structs, accessing struct members, assigning struct variables, comparing structs, passing structs to functions, using arrays within structs, and nesting structs within other structs.
The document provides an overview of the C programming language, including its history, basic structure, data types, operators, input/output, decision making, looping, functions, arrays, pointers, strings, structures, file handling, and linked data structures. Some key topics covered include the C compilation process, basic C program structure, common data types like int and char, arithmetic, relational, and logical operators, if/else and switch statements, while, do-while and for loops, defining functions, and passing arguments to functions.
This chapter discusses classes and data abstraction in C++. It covers objectives like learning about classes, private/public/protected class members, accessor and mutator functions, constructors and destructors. The key topics covered include defining classes, declaring class objects, accessing class members, passing class objects as function parameters, implementing member functions, and using constructors to initialize class objects. The chapter aims to explain how classes are used to implement abstract data types and differences between classes and structs in C++.
This chapter discusses classes and data abstraction in C++. It covers defining classes, class members, access specifiers for members, constructors and destructors, abstract data types, the differences between structs and classes, information hiding, and static class members. The key topics are defining classes and objects, initializing objects using constructors, cleaning up objects using destructors, separating interface from implementation through information hiding, and declaring static members that are shared across class instances.
This chapter covers overloading operators and templates in C++. It discusses overloading operators as member and non-member functions, and the restrictions on operator overloading. The chapter also explains templates for functions and classes, which allow writing generic code for related types. Pointers, friend functions, and classes with pointer members are additionally addressed.
The chapter discusses user-defined functions in C++, including:
- Value-returning functions that use the return statement to return a value of a specific data type.
- Void functions that do not return a value.
- Function prototypes that declare a function without defining its body, allowing a function to be called before it is defined.
- Value and reference parameters, where reference parameters pass a reference to the variable rather than a copy.
- The scope of identifiers as either local to a function or global across the entire program.
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 ...
This document provides an overview of C++ programming and processing a C++ program. It discusses the evolution of programming languages and how a C++ program is compiled from source code to machine code. The document also explains that a C++ program is run by using an editor to create source code, preprocessing it, compiling it to create an object program, linking the object program, loading the executable code into memory, and executing it.
This document provides an overview of the key concepts covered in Chapter 2 of the C++ Programming textbook. These include basic C++ program components like functions, data types, arithmetic operators, and input/output statements. It also discusses programming fundamentals like declaring variables, writing comments, using preprocessor directives, and properly structuring a C++ program with a main function. An example is provided to demonstrate how to write a program that converts between feet/inches and centimeters.
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++.
This chapter discusses the basic elements of a Java program, including methods, identifiers, data types, expressions, input/output statements, and packages. It explains how to write a simple Java application with a main method, import packages, declare variables, read input from the user, and print output. The chapter also covers arithmetic operators, strings, assignments, and programming style best practices.
The chapter introduces the basic elements of a Java program, including methods, data types, expressions, input/output, and control structures. It discusses primitive data types, arithmetic operators, strings, and control flow. The chapter aims to familiarize readers with creating and structuring Java applications, defining classes and methods, and debugging syntax errors. Consistent formatting and walking through code is advised to avoid bugs. Examples demonstrate converting lengths and making change in cents.
This chapter discusses searching, sorting algorithms, and the vector type in C++. It covers sequential search, bubble sort, insertion sort, and binary search algorithms. It also explains how vectors can implement dynamic lists that can grow and shrink in size during program execution, unlike standard arrays.
This chapter discusses user-defined simple data types in C++ including enumeration types, typedef statements, namespaces, and the string type. It covers creating enumeration types and anonymous data types, using typedef to create type aliases, how namespaces avoid name collisions, and functions for manipulating strings like length(), find(), substr(), and swap(). The chapter includes an example program that converts strings to "pig Latin" form using string functions.
Data structure & Algorithms - Programming in Cbabuk110
This document discusses programming in C and primitive data types. It covers background on C, the structure of a C program, comments, identifiers, variables, constants, data types, primitive data types like integer, float, void, and the sizeof operator to check the size of data types. It provides examples of variable declarations, constants, escape sequences, and using sizeof.
This document outlines the objectives and topics covered in the course EC8393 - Fundamentals of Data Structures in C. The course aims to teach students about linear and non-linear data structures and their applications using the C programming language. Key topics include implementing various data structure operations in C, choosing appropriate data structures, and modifying existing or designing new data structures for applications. Assessment includes continuous internal assessments, a university exam, and a minimum 80% attendance requirement.
This document discusses stacks and queues. It describes their properties and common operations like push, pop, enqueue, and dequeue. It provides implementations of stacks and queues as both arrays and linked lists. Specific algorithms covered include using a stack to evaluate postfix expressions, and a non-recursive algorithm to print a linked list backwards using a stack. The key advantages of stacks (LIFO) and queues (FIFO) as data structures are explained.
The document discusses the topic of Internet of Things (IoT) applications in smart buildings. It describes how IoT can be used in various sectors like healthcare, education, and commercial buildings to improve efficiency and operations. Some key benefits of smart buildings are discussed like reduced costs, improved comfort, and lessened environmental impact. Examples of smart building technologies and applications in areas such as lighting, HVAC, security and maintenance are provided. Challenges to adopting IoT in buildings like security, integration and costs are also highlighted. The document concludes by discussing future advances in IoT and its potential impacts.
Bricks are a common building material made from clay that is molded and fired. There are four main steps to manufacturing clay bricks: preparing the clay, molding the bricks, drying the molded bricks, and firing the dried bricks in a kiln. Firing hardens the bricks and burns off impurities. Good bricks are uniformly shaped, fire to a bright copper color without cracking, and can withstand weathering and structural loads. Bricks are classified based on their quality, with Class I being the highest quality for permanent structures. Timber comes from trees and can be used for building if processed correctly to prevent decay and fire. Trees are classified as exogenous or endogenous based on their growth pattern.
The document provides an overview of key concepts for C++ programming including:
- The problem analysis to coding execution cycle involves analyzing the problem, designing an algorithm, coding the program, preprocessing directives, compiling, linking, loading, and executing the program.
- Basic C++ program structure includes #include directives, using namespace std, and main() function returning int.
- Comments, identifiers, data types, arithmetic operators, expressions, type conversions, strings, constants, increment/decrement operators, I/O streams, and input/output statements are discussed.
- Syntax errors are reported by the compiler while logic errors are typically not caught and must be debugged using output statements.
This chapter discusses pointers, classes, virtual functions, and abstract classes in C++. Pointers contain the addresses of other variables and can be used to access dynamic memory. The address of and dereferencing operators are used to work with pointers. Classes and structs can have pointer member variables. Virtual functions allow dynamic binding at runtime rather than compile-time. Abstract classes define pure virtual functions that derived classes must implement.
The document discusses chapters 4 and 5 from a C++ programming textbook. Chapter 4 covers selection control structures like if/else statements and relational operators. Chapter 5 discusses repetition control structures like while, for, and do-while loops. The document provides examples and explanations of how to use these different control structures in C++ programs. It also discusses logical operators, order of precedence, switch statements, nested control structures, and the break and continue statements.
This chapter discusses records (structs) in C++. A struct is a collection of members of different types that are accessed by name. Structs allow grouping of related data and functions together. The chapter covers defining and declaring structs, accessing struct members, assigning struct variables, comparing structs, passing structs to functions, using arrays within structs, and nesting structs within other structs.
The document provides an overview of the C programming language, including its history, basic structure, data types, operators, input/output, decision making, looping, functions, arrays, pointers, strings, structures, file handling, and linked data structures. Some key topics covered include the C compilation process, basic C program structure, common data types like int and char, arithmetic, relational, and logical operators, if/else and switch statements, while, do-while and for loops, defining functions, and passing arguments to functions.
This chapter discusses classes and data abstraction in C++. It covers objectives like learning about classes, private/public/protected class members, accessor and mutator functions, constructors and destructors. The key topics covered include defining classes, declaring class objects, accessing class members, passing class objects as function parameters, implementing member functions, and using constructors to initialize class objects. The chapter aims to explain how classes are used to implement abstract data types and differences between classes and structs in C++.
This chapter discusses classes and data abstraction in C++. It covers defining classes, class members, access specifiers for members, constructors and destructors, abstract data types, the differences between structs and classes, information hiding, and static class members. The key topics are defining classes and objects, initializing objects using constructors, cleaning up objects using destructors, separating interface from implementation through information hiding, and declaring static members that are shared across class instances.
This chapter covers overloading operators and templates in C++. It discusses overloading operators as member and non-member functions, and the restrictions on operator overloading. The chapter also explains templates for functions and classes, which allow writing generic code for related types. Pointers, friend functions, and classes with pointer members are additionally addressed.
The chapter discusses user-defined functions in C++, including:
- Value-returning functions that use the return statement to return a value of a specific data type.
- Void functions that do not return a value.
- Function prototypes that declare a function without defining its body, allowing a function to be called before it is defined.
- Value and reference parameters, where reference parameters pass a reference to the variable rather than a copy.
- The scope of identifiers as either local to a function or global across the entire program.
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 ...
This document provides an overview of C++ programming and processing a C++ program. It discusses the evolution of programming languages and how a C++ program is compiled from source code to machine code. The document also explains that a C++ program is run by using an editor to create source code, preprocessing it, compiling it to create an object program, linking the object program, loading the executable code into memory, and executing it.
This document provides an overview of the key concepts covered in Chapter 2 of the C++ Programming textbook. These include basic C++ program components like functions, data types, arithmetic operators, and input/output statements. It also discusses programming fundamentals like declaring variables, writing comments, using preprocessor directives, and properly structuring a C++ program with a main function. An example is provided to demonstrate how to write a program that converts between feet/inches and centimeters.
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++.
This chapter discusses the basic elements of a Java program, including methods, identifiers, data types, expressions, input/output statements, and packages. It explains how to write a simple Java application with a main method, import packages, declare variables, read input from the user, and print output. The chapter also covers arithmetic operators, strings, assignments, and programming style best practices.
The chapter introduces the basic elements of a Java program, including methods, data types, expressions, input/output, and control structures. It discusses primitive data types, arithmetic operators, strings, and control flow. The chapter aims to familiarize readers with creating and structuring Java applications, defining classes and methods, and debugging syntax errors. Consistent formatting and walking through code is advised to avoid bugs. Examples demonstrate converting lengths and making change in cents.
This chapter discusses searching, sorting algorithms, and the vector type in C++. It covers sequential search, bubble sort, insertion sort, and binary search algorithms. It also explains how vectors can implement dynamic lists that can grow and shrink in size during program execution, unlike standard arrays.
This chapter discusses user-defined simple data types in C++ including enumeration types, typedef statements, namespaces, and the string type. It covers creating enumeration types and anonymous data types, using typedef to create type aliases, how namespaces avoid name collisions, and functions for manipulating strings like length(), find(), substr(), and swap(). The chapter includes an example program that converts strings to "pig Latin" form using string functions.
Data structure & Algorithms - Programming in Cbabuk110
This document discusses programming in C and primitive data types. It covers background on C, the structure of a C program, comments, identifiers, variables, constants, data types, primitive data types like integer, float, void, and the sizeof operator to check the size of data types. It provides examples of variable declarations, constants, escape sequences, and using sizeof.
This document outlines the objectives and topics covered in the course EC8393 - Fundamentals of Data Structures in C. The course aims to teach students about linear and non-linear data structures and their applications using the C programming language. Key topics include implementing various data structure operations in C, choosing appropriate data structures, and modifying existing or designing new data structures for applications. Assessment includes continuous internal assessments, a university exam, and a minimum 80% attendance requirement.
This document discusses stacks and queues. It describes their properties and common operations like push, pop, enqueue, and dequeue. It provides implementations of stacks and queues as both arrays and linked lists. Specific algorithms covered include using a stack to evaluate postfix expressions, and a non-recursive algorithm to print a linked list backwards using a stack. The key advantages of stacks (LIFO) and queues (FIFO) as data structures are explained.
The document discusses the topic of Internet of Things (IoT) applications in smart buildings. It describes how IoT can be used in various sectors like healthcare, education, and commercial buildings to improve efficiency and operations. Some key benefits of smart buildings are discussed like reduced costs, improved comfort, and lessened environmental impact. Examples of smart building technologies and applications in areas such as lighting, HVAC, security and maintenance are provided. Challenges to adopting IoT in buildings like security, integration and costs are also highlighted. The document concludes by discussing future advances in IoT and its potential impacts.
Bricks are a common building material made from clay that is molded and fired. There are four main steps to manufacturing clay bricks: preparing the clay, molding the bricks, drying the molded bricks, and firing the dried bricks in a kiln. Firing hardens the bricks and burns off impurities. Good bricks are uniformly shaped, fire to a bright copper color without cracking, and can withstand weathering and structural loads. Bricks are classified based on their quality, with Class I being the highest quality for permanent structures. Timber comes from trees and can be used for building if processed correctly to prevent decay and fire. Trees are classified as exogenous or endogenous based on their growth pattern.
The document discusses the conditional operator (?:) and switch statements in C++. The conditional operator takes three arguments and returns either the second expression if the first is true, or the third expression if the first is false. A switch statement allows executing different blocks of code based on the value of an expression. It checks the expression against case values and executes the code for the matching case. A break statement is normally used to exit each case block. Assert statements can be used to check for invalid conditions like dividing by zero, and will terminate the program and report an error if the assertion fails.
The document contains code snippets demonstrating different types of loops in C++ including for, while, do-while loops. It also shows examples of using break, continue and flag-controlled loops. Nested loops and sentinel controlled loops are presented. The output of the code snippets is also discussed in some cases.
The document contains code examples demonstrating the use of functions in C++ programs. It shows function prototypes, definitions, parameters, return statements, and calling functions by passing arguments. Key concepts covered include defining functions with parameters, returning values, and calling functions to pass actual arguments and retrieve returned values.
The document discusses various ways of initializing and accessing arrays in C++. It provides examples of:
1) Initializing a single-dimensional array and accessing elements;
2) Errors that can occur from referring to elements outside the bounds of an array or providing too many initializers;
3) Initializing multi-dimensional arrays and properly accessing elements with two indices;
4) Declaring a multi-dimensional array as a formal parameter and needing to specify the size of the second dimension.
The document discusses database security and SQL injection attacks. It provides an overview of access control in SQL and MySQL, including the use of views and privileges. It then describes SQL injection attacks, giving examples of how attackers can exploit vulnerabilities to view sensitive data or delete tables. The best defense is using prepared statements with bound parameters rather than embedding user input directly into SQL. Other defenses include input validation, output encoding, limiting permissions, and configuring error reporting.
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxOH TEIK BIN
(A Free eBook comprising 3 Sets of Presentation of a selection of Puzzles, Brain Teasers and Thinking Problems to exercise both the mind and the Right and Left Brain. To help keep the mind and brain fit and healthy. Good for both the young and old alike.
Answers are given for all the puzzles and problems.)
With Metta,
Bro. Oh Teik Bin 🙏🤓🤔🥰
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...TechSoup
Whether you're new to SEO or looking to refine your existing strategies, this webinar will provide you with actionable insights and practical tips to elevate your nonprofit's online presence.
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapitolTechU
Slides from a Capitol Technology University webinar held June 20, 2024. The webinar featured Dr. Donovan Wright, presenting on the Department of Defense Digital Transformation.
Information and Communication Technology in EducationMJDuyan
(𝐓𝐋𝐄 𝟏𝟎𝟎) (𝐋𝐞𝐬𝐬𝐨𝐧 2)-𝐏𝐫𝐞𝐥𝐢𝐦𝐬
𝐄𝐱𝐩𝐥𝐚𝐢𝐧 𝐭𝐡𝐞 𝐈𝐂𝐓 𝐢𝐧 𝐞𝐝𝐮𝐜𝐚𝐭𝐢𝐨𝐧:
Students will be able to explain the role and impact of Information and Communication Technology (ICT) in education. They will understand how ICT tools, such as computers, the internet, and educational software, enhance learning and teaching processes. By exploring various ICT applications, students will recognize how these technologies facilitate access to information, improve communication, support collaboration, and enable personalized learning experiences.
𝐃𝐢𝐬𝐜𝐮𝐬𝐬 𝐭𝐡𝐞 𝐫𝐞𝐥𝐢𝐚𝐛𝐥𝐞 𝐬𝐨𝐮𝐫𝐜𝐞𝐬 𝐨𝐧 𝐭𝐡𝐞 𝐢𝐧𝐭𝐞𝐫𝐧𝐞𝐭:
-Students will be able to discuss what constitutes reliable sources on the internet. They will learn to identify key characteristics of trustworthy information, such as credibility, accuracy, and authority. By examining different types of online sources, students will develop skills to evaluate the reliability of websites and content, ensuring they can distinguish between reputable information and misinformation.
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...EduSkills OECD
Andreas Schleicher, Director of Education and Skills at the OECD presents at the launch of PISA 2022 Volume III - Creative Minds, Creative Schools on 18 June 2024.
Brand Guideline of Bashundhara A4 Paper - 2024khabri85
It outlines the basic identity elements such as symbol, logotype, colors, and typefaces. It provides examples of applying the identity to materials like letterhead, business cards, reports, folders, and websites.
Creative Restart 2024: Mike Martin - Finding a way around “no”Taste
Ideas that are good for business and good for the world that we live in, are what I’m passionate about.
Some ideas take a year to make, some take 8 years. I want to share two projects that best illustrate this and why it is never good to stop at “no”.
Accounting for Restricted Grants When and How To Record Properly
9781423902096_PPT_ch08.ppt
1. C++ Programming:
From Problem Analysis
to Program Design, Fourth Edition
Chapter 8: User-Defined Simple Data
Types, Namespaces, and the
string Type
2. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2
Objectives
In this chapter, you will:
• Learn how to create and manipulate your own
simple data type—called the enumeration
type
• Become familiar with the typedef statement
• Learn about the namespace mechanism
• Explore the string data type, and learn how
to use the various string functions to
manipulate strings
3. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3
Enumeration Type
• Data type: a set of values together with a set
of operations on those values
• To define a new simple data type, called
enumeration type, we need three things:
− A name for the data type
− A set of values for the data type
− A set of operations on the values
4. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4
Enumeration Type (continued)
• A new simple data type can be defined by
specifying its name and the values, but not
the operations
− The values must be identifiers
• Syntax:
− value1, value2, … are identifiers called
enumerators
− value1 < value2 < value3 <...
5. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5
Enumeration Type (continued)
• Enumeration type is an ordered set of values
• If a value has been used in one enumeration
type it can’t be used by another in same block
• The same rules apply to enumeration types
declared outside of any blocks
6. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6
Enumeration Type (continued)
7.
8. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8
Declaring Variables
• Syntax:
• For example, given the following definition:
we can declare the following variables:
9. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9
Assignment
• The statement:
popularSport = FOOTBALL;
stores FOOTBALL into popularSport
• The statement:
mySport = popularSport;
copies the value of the popularSport into
mySport
10. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10
Operations on Enumeration Types
• No arithmetic operations are allowed on
enumeration types
• ++ and -- are illegal too:
• Solution: use a static cast:
11. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11
Relational Operators
• An enumeration type is an ordered set of
values:
• Enumeration type is an integer data type and
can be used in loops:
12. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12
Input /Output of Enumeration
Types
• I/O are defined only for built-in data types
− Enumeration type cannot be input/output
(directly)
13. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13
Functions and Enumeration Types
• Enumeration types can be passed as
parameters to functions either by value or by
reference
• A function can return a value of the
enumeration type
14. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14
Declaring Variables When
Defining the Enumeration Type
• You can declare variables of an enumeration
type when you define an enumeration type:
15. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15
Anonymous Data Types
• Anonymous type : values are directly
specified in the declaration, with no type name
• Drawbacks:
− Cannot pass/return an anonymous type to/from
a function
− Values used in one type can be used in
another, but are treated differently:
16. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16
typedef Statement
• You can create synonyms or aliases to a data
type using the typedef statement
• Syntax:
• typedef does not create any new data types
− Creates an alias to an existing data type
17. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17
Namespaces
• ANSI/ISO standard C++ was officially
approved in July 1998
• Most of the recent compilers are also
compatible with ANSI/ISO standard C++
• For the most part, standard C++ and
ANSI/ISO standard C++ are the same
− However, ANSI/ISO Standard C++ has some
features not available in Standard C++
18. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18
Namespaces (continued)
• Global identifiers in a header file used in a
program become global in the program
− Syntax error occurs if an identifier in a
program has the same name as a global
identifier in the header file
• Same problem can occur with third-party
libraries
− Common solution: third-party vendors begin
their global identifiers with _ (underscore)
• Do not begin identifiers in your program with _
19. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19
Namespaces (continued)
• ANSI/ISO Standard C++ attempts to solve
this problem with the namespace mechanism
• Syntax:
where a member is usually a variable
declaration, a named constant, a function, or
another namespace
20. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20
Namespaces (continued)
21. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 21
Namespaces (continued)
• The scope of a namespace member is local
to the namespace
• Ways a namespace member can be
accessed outside the namespace:
22. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 22
Accessing a namespace Member
• Examples:
globalType::RATE
globalType::printResult();
• After the using statement, it is not necessary
to precede the namespace_name:: before
the namespace member
− Unless a namespace member and a global
identifier or a block identifier have same name
23. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23
string Type
• To use the data type string, the program
must include the header file string
• The statement:
string name = "William Jacob";
declares name to be a string variable and
also initializes name to "William Jacob"
− The first character, 'W', is in position 0
− The second character, 'i', is in position 1
− name is capable of storing any size string
24. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24
string Type (continued)
• Binary operator + and the array subscript
operator [], have been defined for the data
type string
− + performs the string concatenation operation
• Example:
str1 = "Sunny";
str2 = str1 + " Day";
stores "Sunny Day" into str2
25. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 25
Additional string Operations
• length
• size
• find
• substr
• swap
26. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 26
length Function
• Returns the number of characters currently in
the string
• Syntax:
where strVar is variable of the type string
• length returns an unsigned integer
• The value returned can be stored in an integer
variable
27.
28. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 28
size Function
• size is the same as the function length
− Both functions return the same value
• Syntax:
where strVar is variable of the type string
• As in the case of the function length, the
function size has no arguments
29. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 29
find Function
• Searches a string for the first occurrence of a
particular substring
• Returns an unsigned integer value of type
string::size_type
− Or string::npos if unsuccessful
• Syntax:
− strExp can be a string or a character
30. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 30
find Function (continued)
31. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 31
substr Function
• Returns a particular substring of a string
• Syntax:
expr1 and expr2 are expressions evaluating to
unsigned integers
− expr1 specifies a position within the string
(starting position of the substring)
− expr2 specifies the length of the substring to
be returned
32. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 32
substr Function (continued)
33. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 33
swap Function
• Interchanges contents of two string variables
• Syntax:
where strVar1 and strVar2 are string
variables
• Suppose you have the following statements:
string str1 = "Warm";
string str2 = "Cold";
• After str1.swap(str2); executes, the value of
str1 is "Cold" and the value of str2 is "War"
34. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 34
Programming Example: Pig Latin
Strings
• Program prompts user to input a string
− Then outputs the string in the pig Latin form
• The rules for converting a string into pig Latin
form are as follows:
− If the string begins with a vowel, add the string
"-way" at the end of the string
• Example: the pig Latin form of "eye" is "eye-
way"
35. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 35
Programming Example: Pig Latin
Strings (continued)
• Rules (continued):
− If the string does not begin with a vowel, first
add "-" at the end of the string
• Then move the first character of the string to the
end of the string until the first character of the
string becomes a vowel
• Next, add the string "ay" at the end
• Example: pig Latin form of "There" is "ere-
Thay"
36. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 36
Programming Example: Pig Latin
Strings (continued)
• Rules (continued):
− Strings such as "by" contain no vowels
• The letter 'y' can be considered a vowel
• For this program the vowels are a, e, i, o, u, y, A,
E, I, O, U, and Y
− Strings such as "1234" contain no vowels
• The pig Latin form of a string that has no vowels
in it is the string followed by the string "-way"
• Example: pig Latin form of "1234" is "1234-
way"
37. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 37
Programming Example: Problem
Analysis
• If str denotes a string:
− Check the first character, str[0], of str
− If it is a vowel, add "-way" at the end of str
− If it is not a vowel:
• First add "-" at the end of the string
• Remove the first character of str from str and
put it at end of str
• Now the second character of str becomes the
first character of str
38. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 38
Programming Example: Problem
Analysis (continued)
• If str denotes a string (continued):
− This process is repeated until either
• The first character of str is a vowel
• All characters of str are processed, in which
case str does not contain any vowels
39. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 39
Programming Example: Algorithm
Design
• The program contains the following functions:
− isVowel determines if a character is a vowel
− rotate moves first character of str to the
end of str
− pigLatinString finds pig Latin form of str
• Steps in the algorithm:
− Get str
− Use pigLatinString to find the pig Latin
form of str
− Output the pig Latin form of str
40. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 40
Programming Example: Function
isVowel
41. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 41
Programming Example: Function
rotate
• Takes a string as a parameter
• Removes the first character of the string
− Places it at end of the string by extracting the
substring starting at position 1 until the end of
the string, then adding the first character of the
string
42. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 42
Programming Example: Function
pigLatinString
• If pStr[0] is a vowel, add "-way" at end
• If pStr[0] is not a vowel:
− Move first character of pStr to the end of pStr
− The second character of pStr becomes the first
character of pStr
• Now pStr may or may not contain a vowel
− Use a bool variable, foundVowel, which is set to
true if pStr contains a vowel and false
otherwise
− Initialize foundVowel to false
43. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 43
Programming Example: Function
pigLatinString (continued)
− If pStr[0] is not a vowel, move str[0] to
the end of pStr by calling the function
rotate
− Repeat third step until either the first character
of pStr becomes a vowel or all characters of
pStr have been checked
• Convert pStr into the pig Latin form
• Return pStr
44. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 44
Programming Example: Main
Algorithm
• Get the string
• Call pigLatinString to find the pig Latin
form of the string
• Output the pig Latin form of the string
45. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 45
Summary
• Enumeration type: set of ordered values
− Created with reserved word enum creates an
enumeration type
• No arithmetic operations are allowed on the
enumeration type
• Relational operators can be used with enum
values
• Enumeration type values cannot be input or
output directly
46. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 46
Summary (continued)
• Anonymous type: a variable’s values are
specified without any type name
• Reserved word typedef creates synonyms
or aliases to previously defined data types
• The namespace mechanism is a feature of
ANSI/ISO Standard C++
• A namespace member is usually a named
constant, variable, function, or another
namespace
47. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 47
Summary (continued)
• Keyword namespace must appear in the
using statement
• A string is a sequence of zero or more
characters
• Strings in C++ are enclosed in ""
• In C++, [] is the array subscript operator
• The function length returns the number of
characters currently in the string
48. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 48
Summary (continued)
• The function size returns the number of
characters currently in the string
• The function find searches a string to locate
the first occurrence of a particular substring
• The function substr returns a particular
substring of a string
• The function swap is used to swap the
contents of two string variables