Starting Out With Early Objects C Ninth
Starting Out With Early Objects C Ninth
Starting Out With Early Objects C Ninth
1
C++
EARLY OBJECTS
by Tony Gaddis, Judy
Walters, and
Godfrey Muganda
Starting Out With
2
Topics
3
7.9 Passing Objects to Functions
5
Notes on Passing Objects
6
Returning an Object from a Function
7
Returning an Object Example
Square initSquare()
{
Square s; // local object
int inputSize;
cout << "Enter the length of side: ";
cin >> inputSize;
s.setSide(inputSize);
return s;
}
8
7.10 Object Composition
9
Object Composition, cont.
10
7.11 Separating Class Specification,
Implementation, and Client Code
11
Using Separate Files
Place the class declaration in a header file that
serves as the class specification file.
Name the file classname.h (for example,
Square.h)
Place the member function definitions in a class
implementation file.
Name the file classname.cpp (for example,
Square.cpp)This file should #include the
class specification file.
A client program (client code) that uses the
class must #include the class specification
file and be compiled and linked with the class
implementation file. 12
Include Guard
14
7.12 Structures
15
Example struct Declaration
double gpa;
};
Note the
required
;
16
struct Declaration Notes
struct names commonly begin with an
uppercase letter
Multiple fields of same type can be
declared in a comma-separated list
string name,
address;
Fields in a structure are all public by
default
17
Defining Structure Variables
s1
studentID
name
year
gpa
18
Accessing Structure Members
19
Displaying struct Members
20
Comparing struct Members
21
Initializing a Structure (continued)
22
Using an Initialization List
23
More on Initialization Lists
The order of list elements matters: The first value
initializes first data member, second value
initializes second data member, etc.
The elements of an initialization list can be
constants, variables, or expressions
24
Initialization List Example
#include <iostream>
26
Problems with an Initialization List
You can’t omit a value for a data member without omitting
values for all following members
It does not work on most modern compilers if the structure
contains objects, e.g., like string objects
27
Using a Constructor to Initialize Structure
Members
28
A Structure with a Constructor
struct Dimensions
{
int length,
width,
height;
29
Nested Structures
struct Student
{
int studentID;
PersonInfo pData;
short year;
double gpa;
};
30
Members of Nested Structures
Use the dot operator multiple times to access
fields of nested structures
Student s5;
s5.pData.name = "Joanne";
s5.pData.city = "Tulsa";
Reference the nested structure’s fields by the
member variable name, not by the structure
name
s5.PersonInfo.name = "Joanne"; //no!
31
Structures as Function Arguments
32
Notes on Passing Structures
33
Returning a Structure from a Function
A function can return a struct
Student getStuData(); // prototype
s1 = getStuData(); // call
The function must define a local structure variable
for internal use
to use with return statement
34
Returning a Structure Example
Student getStuData()
{
Student s; // local variable
cin >> s.studentID;
cin.ignore();
getline(cin, s.pData.name);
getline(cin, s.pData.address);
getline(cin, s.pData.city);
cin >> s.year;
cin >> s.gpa;
return s;
}
35
7.13 Enumerated Data Types
#include <iostream>
#include <string>
enum DAY
{
saturday = 0,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
};
void main()
{
if (day == 0)
cout << "Day is a weekend day" << endl;
else if (day == wednesday)
cout << "Day is middle of the work week" << endl;
else
cout << "Day is work day" << endl;
}
36
7.13 Enumerated
Data Types
Additional ways that enumerated data types can be used:
Data type declaration and variable definition in a single
statement:
enum Tree { ASH, ELM, OAK } tree1, tree2;
Assign an int value to an enum variable:
enum Tree { ASH, ELM, OAK } tree1;
tree1 = static_cast<Tree>(2); // ELM
cout << tree1 << endl;
37
7.13 More About Enumerated
Data Types
#include <iostream>
int main()
{
38
7.13 More About Enumerated
Data Types
#include <iostream>
int main()
{
Days day = Sunday;
if (day == Saturday) {
cout << "Ok its Saturday" << endl;
}
else
{
std::cout << "its not Saturday" << endl;
}
}
39
Strongly Typed enums (C++ 11)
40
Strongly Typed enums (C++ 11)
41
7.15 Introduction to Object-Oriented
Analysis and Design
Object-Oriented Analysis: that phase of program
development when the program functionality is
determined from the requirements
It includes
identification of classes and objects
definition of each class's attributes
definition of each class's behaviors
definition of the relationship between classes
42
Identify Classes and Objects
44
Define Class Behaviors
45
Relationships Between Classes
Possible relationships
Access ("uses-a")
Ownership/Composition ("has-a")
Inheritance ("is-a")
46
Finding the Classes
Technique:
Write a description of the problem domain (objects,
events, etc. related to the problem)
List the nouns, noun phrases, and pronouns.
These are all candidate objects
47
Determine Class Responsibilities
Class responsibilities:
What is the class responsible to know?
What is the class responsible to do?
48
Object Reuse
49
Object-Based vs. Object-Oriented
50
7.16 Screen Control
51
Screen Control – Concepts
52
Screen Control – Windows - Specifics
53
Screen Control – Windows –
More Specifics
Assign coordinates where the output should appear:
position.X = 30; // column
position.Y = 12; // row
Set the screen cursor to this cell:
SetConsoleCursorPosition(screen, position);
Send output to the screen:
cout << "Look at me!" << endl;
54
55
56