Chapter 3 ( Introduction to Classes, Objects, Member Functions, and Strings)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Parameter List

Parameters must specify a type, followed by a parameter name. When there are multiple parameters, each is separated from the next bya comma.

Arguments

Placed in the function call's parentheses. Helps the function perform its task.

Reusable Code

New classes, when packaged properly, can be reused by other programmers.

Client

A client of a class is any other code that calls the class's member functions.

Validation or Validity Checking

A construcot can perform either before modifying a data member.

Member-Function

A member function call can supply arguments that help the function perform its task.

Parameter

A member function can require one or more parameters that represent the data it needs to perform its task.

Instantiating an Object

A member function cannot be called until you create an object of that class. Ex) Account myAccount; // created Account object "myAccount" The variable's type is Account, the class we defined.

User-defined type

A new type that you can create is known as a user-defined type. The compiler only knows about fundamental types that are "built into" C++

Keyword "Private"

An access specifier. Access specifiers are always followed by a colon(:). Data member name's declaration appears after access specifier private to indicate that name is accessible only to class Account's member functions.

Strings

An object of C++ std lib class string stores character string values. Class string is defined in the <spring> header and belongs to namespace std.

Default Access for Class Members

By default, everything in a class is private, unless you specify otherwise.

Driver Program

Calling member functions without knowing how the class is implemented. A main function can 'drive" an object by calling its member functions - without knowing how the class is implemented. Therefore, main is referred to as a driver program.

Identifiers

Class names, member function names, and data- member names.

Headers

Classes should be reusable and placed in files known as a header, which includes a .h filename extension. Must include (via #include) wherever you need to use the code. Using double quotes (") indicates that the header is located with you program, rather than with the C++ std lib. Ex) Account.h, therefore #include "Account.h"

Data members

Declared inside a class definition, but outside the bodies of the class's member functions.

Empty String " "

Default value for a string

Return Type "Void"

Does not return any information to its calling function.

UML Class Diagram

Each class is modeled in a class diagram as a rectangle with three compartments. "Top Compartment" contains the class name, centered horizontally in boldface type. "Middle Compartment" contains the class's attribute name, which corresponds to the data member of the same name. "Bottom Compartment" contains the class's operations, setName and getName, which correspond to the member functions of the same names.

Extensible Programming Language

Each class you create becomes a new type you can use to create objects. You can define new class types as needed. C++ is an example.

Keyword "Class"

Every class definition contains the keyword "class" followed immediately by the class's name. Every class's body is enclosed in an opening "{, left brace" and a closing "}, right brace. Forgetting the semicolon at end of a class definition is a syntax error. Typically, each class definition is placed in a separate header with the .h filename extension.

Source Code Files

Files ending with the ".cpp" filename extension. These define a program's main function, other functions and more. You include headers into source-code files.

getline Function

From the <string> header, "getline" reads characters up to, but not including, a newline, which is discarded, then places the characters in a string.

Global Functions

Functions that are not members of a class.

Constructor

Specifics custom initialization for objects of that class. Requires a constructor call when each object is created, so this is the ideal point to initialize an object's data members. Can contain parameters.

Keyword "Explicit"

Takes a singel parameter. We declare all single-paramter constructors as "explicit".

Data Hiding

The data member name is encapsulated (hidden) and can be used only in class Account's setName and getName member functions.

Dot Operator

To call a member function for a specific object, you specify the object's name (myAccount), followed by the dot operator (.), then the member function name (getName) and a set of parentheses. Empty parentheses indicate that the function does not require any additional information to perform its task.

Local Variables

Variables declared in a particular function's body are local variables, which can be used only in that function. When a function terminates, the values of its local variables are lost. Ex) A function's parameters are local variables of that function.

in-class initializer

You can initialize fundamental-type data members in their declarations.

Receiving a line of text from the user

getline(cin, theName); // Reads a line of text Requires the <string> header and belongs to namespace std.

setName Function

myAccount.setName(theName); // put theName in myAccount

const Member Functions

std::string getName() cconst { In the process of returning the name the function does not, and should not, modify the account object on which it's called.

getName Function

std::string getName() const { return name; // return name's value to this function's caller }

setName Function

void setName(std::string accountName) { name = accountName; // store the account name }


Conjuntos de estudio relacionados

Chapter 9 Cellular Respiration/Fermentation

View Set

Discrimination II - Equal Pay: The Sex Equality Clause

View Set

PrepU chapter 7 Legal Dimensions of Nursing Practice

View Set

Chapter 14: Exchange Rate and Purchasing Power Parity

View Set

A&P 2 chapter 18 (heart physiology)

View Set

CSP Unit 1 Assessment Test 21-22

View Set