C++ Programming: Chapter 9 - Objects and Classes

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

The unary scope operator can be used as ::var to tell the compiler...

...that the variable is a global variable.

What are two benefits of separating class definition from implementation?

1. It hides implementation from definition. 2. As a software vendor, this protects your IP as the header file and class object code can be provided with revealing the source code for implementing the class.

object-oriented programming (OOP)

An approach to programming that involves organizing objects and their behavior into classes of reusable components.

Objects of the BankAccount class require a name (string) and a social security number (string) be specified (in that order) upon creation. Define an object named account, of type BankAccount, using the values "John Smith" and "123-45-6789" as the name and social security number respectively.

BankAccount account ("John Smith", "123-45-6789");

Objects of the Calculator class require no additional information when created. Define an object named calc of type Calculator using the no-arg constructor.

Calculator calc;

Class implementation

Carries out the contract. Implements the constructors and functions.

What is the syntax to create an object using a constructor with arguments?

ClassName objectName(arguments);

What is the syntax to create an object using the no-arg constructor?

ClassName objectName; Ex - Circle circle1;

What are the differences between constructors and functions?

Constructors are special kinds of functions that are called when creating an object; they do not have a return type, not even void.

How do you declare an object?

Create an object using a constructor. An object is declared from a class.

What does this statement do? circle2 = circle1;

It copies the contents of circle1 to circle2.

data field encapsulation

To prevent direct modifications of properties through the object reference, you can declare the field private, using the private modifier. Data field encapsulation makes the class easy to maintain.

What does this statement do? Circle c = Circle();

Left half: Declares an object of Circle. Right half: Creates an anonymous object using Circle's no-arg constructor and copies the object to c.

Objects of class Name are defined by supplying last name (string) and a first name (string) in this order. Define an object of type Name named name and corresponding to a last name of "Black" and a first name of "Susan".

Name name ("Black","Susan");

Can a declared object name be reassigned to reference another object?

No. Object names are like constants.

UML

Unified Modeling Language, which used graphical notations to describe classes and objects

What happens when a data field in an object is not initialized?

The program displays an unpredictable number, or random value.

How do you define a class?

Using the keyword class followed by a class name. Class contains the constructors, functions, and data fields. Class defines a type and is like a blueprint for objects.

inline function/definition

When the compiler copies the function code in line at the point of each invocation.

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Define an object named window, of type Window, corresponding to a 80 x 20 window.

Window window (80,20);

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Define an object named window, of type Window, whose width and length are obtained from the (already declared and initialized) variables, w and l.

Window window (w,l);

setter function

a mutator for setting the value / changing private data value.

Constructor

a special kind of function that is invoked to create an object

Class

a template, blueprint, or contract that defines what an object's data fields and functions will be; a construct that defines objects of the same type

Behavior

actions; defined by functions

getter function

an accessor for retrieving private data value.

Object

an instance of a class; represents an entity in the real world that can be distinctly identified

The keyword _____ is required to define a class.

class

Objects of the Window class require a width (integer) and a height (integer) be specified (in that order) upon definition. Declare two integers corresponding to a width and a length and read values into them from standard input (in that order). Use these value to define an object of type Window named newWindow.

int width; int length; cin >> width >> length; Window newWindow (width, length);

State

property or attribute; are represented by data fields with their current values in an object

Accessor function naming convention

returnType getDataFieldName() bool isDataFieldName()

Can data fields and functions be placed in any order in a class?

yes

Mutator function naming convention

void setDataFieldName(dataType propertyValue)

What do you use to prevent multiple-inclusion errors of header files?

#ifndef directive (if not defined)

dot operator (.)

(.) Accesses members of an object. If the member is static, it can be accessed through the class name using the dot operator. Also known as object member access operator.

Three peculiarities of Constructors

- Must have the same name as the class itself - Do not have a return type--not even void - Are invoked when an object is created

Creating objects of the Currency class require a name (string), a currency symbol (string) and the number of decimal places (integer) usually associated with the currency (in that order). Creating objects of the Money class requires a Currency object, and the actual amount (integer) (in that order). Declare an object of type Currency named canadianDollars and corresponding to the name "Canadian Dollar", the symbol "C$", and 2 decimal places. Define an object of type Money named valueInCanadians corresponding to 230 units of canadianDollars.

Currency canadianDollars ("Canadian Dollar", "C$", 2);Money valueInCanadians (canadianDollars,230);

Creating objects of the Currency class requires a name (string), a currency symbol (string) and the number of decimal places (integer) usually associated with the currency (in that order). Define an object named curr, of type Currency corresponding to "US Dollar" with the symbol "$" and 2 decimal places.

Currency curr ("US Dollar","$", 2);

What is the #define directive for?

Defines a constant and is used after the #ifndef directive.

Class definition

Describes the contract of the class. Lists all data fields, constructor prototypes, and function prototypes.

T/F: At least one constructor must always be defined explicitly.

False

inclusion guard

Prevents the inclusion of multiple header files by using the #ifndef directive.

binary scope resolution operator (::)

Qualifies the scope for a member in a class

Objects of the BankAccount class require a name (string) and a social security number (string) be specified (in that order)upon creation. Declare two strings corresponding to a name and a social security number and read values into them from standard input (in that order). Use these values to define an object of type BankAccount named newAccount.

string name; string social; cin >> name >> social; BankAccount newAccount (name, social);

class encapsulation

the details of implementation are encapsulated and hidden from the user

class abstraction

the separation of class implementation from the use of a class

Instantation

to create an instance from a class


Ensembles d'études connexes

HTM 231 CH. 1 History of Marketing

View Set

Culture, Spirituality, and Alternative/Complementary Modalities PASSPORT PrepU

View Set

5.20 Unit Test: Line and Triangle Relationships - Part 1 UNIT TEST: Line and Triangle Relationships - Part 1

View Set

Chapter 12, Section 4: British Imperialism in India

View Set

Chapter 22: Care of Patients with Cancer NCLEX questions

View Set

Praxis 5039 Literary Contexts and Historical Periods

View Set

Combined US History - not including Populism or trade unions

View Set