Lecture 2 2024-Structured Data and Pointers
Lecture 2 2024-Structured Data and Pointers
Pointers
Chung-Ming Chen
Department of Biomedical Engineering
Textbook:
1. C++ Programming: From Problem Analysis to Program Design, 8th Edition, D.S. Malik
2. Data Structures Using C++, 2nd Edition, D.S. Malik Page 1
Structured Data Type: Arrays
houseType tempHouse;
struct studentType {
//...
}
PassByValue(newStudent);
PassByReference(seniorStudent);
l A list is a set of elements of the same type. Thus, a list has two things
associated with it: the values (that is, elements) and the length.
const int ARRAY_SIZE = 1000;
struct listType
{
int listElem[ARRAY_SIZE]; //array containing the list
int listLength; //length of the list
} intList; int seqSearch(constlistType& list, int
searchItem)
l Consider the function on the right: The {
formal parameter list of the function int loc;
bool found = false;
seqSearch is declared as a constant for (loc = 0; loc < list.listLength; loc++)
reference parameter. What are the pros if (list.listElem[loc] == searchItem)
{
and cons of this declaration ? found = true;
– Data protection break;
}
– Memory allocation if (found)
– Duplication time return loc;
else
return -1;
}
l For a list of entities with the same struct, e.g., employees’ salary info
struct employeeType
{
string firstName;
string lastName;
int personID;
string deptID;
double yearlySalary;
double monthlySalary;
double yearToDatePaid;
double monthlyBonus;
}
double payCheck; //variable to calculate the paycheck
for (int counter = 0; counter < 50; counter++)
employeeType employees[50]; {
cout << employees[counter].firstName << " "
<< employees[counter].lastName << " ";
payCheck = employees[counter].monthlySalary +
employees[counter].monthlyBonus;
employees[counter].yearToDatePaid =
employees[counter].yearToDatePaid + payCheck;
cout << setprecision(2) << payCheck << endl;
}
newEmployee.contact.cellphone
Spring 2024 Data Structure 12
Classes and Objects Oriented Design
private or public.
l Variable Declaration
clockType myClock;
clockType yourClock;
l Accessing Class Members
classObjectName.memberName
Spring 2024 Data Structure 17
Implementation of Member Functions
l Accessor function: A member function of a class that only accesses (that is, does not
modify) the value(s) of the member variable(s).
l Mutator function: A member function of a class that modifies the value(s) of the
member variable(s).
// Mutator Function // Mutator Function
void clockType::incrementHours() void clockType::setTime(int hours, int minutes, int
{ seconds)
hr++; {
if (hr > 23) if (0 <= hours && hours < 24) hr = hours;
hr = 0; else hr = 0;
} if (0 <= minutes && minutes < 60) min = minutes;
else min = 0;
if (0 <= seconds && seconds < 60) sec = seconds;
else sec = 0;
}
// Accessor Function
void clockType::getTime(int&
hours, int& minutes, int&
seconds) const // Accessor Function
{ bool clockType::equalTime(const clockType&
hours = hr; otherClock) const
minutes = min; {
seconds = sec; return (hr == otherClock.hr
} && min == otherClock.min
&& sec == otherClock.sec);
}
private:
int hr;
l Invoking the Default Constructor int min;
int sec;
className classObjectName; };
clockType yourClock;
l Invoking a Constructor with Parameters
className classObjectName(argument1, argument2, …);
clockType myClock(5, 12, 40);
Spring 2024 Data Structure 20
Constructors and Default Parameters
implementation file.
l The private members of a base class are private to the base class;
hence, the members of the derived class cannot directly access them.
In other words, when you write the definitions of the member functions
of the derived class, you cannot directly access the private members of
the base class.
l The public members of a base class can be inherited either as public
members or as private members by the derived class. That is, the
public members of the base class can become either public or private
members of the derived class.
l The derived class can redefine the public member functions of the base
class. That is, in the derived class, you can have a member function
with the same name, number, and types of parameters as a function in
the base class. However, this redefinition applies only to the objects of
the derived class, not to the objects of the base class.
l All member variables of the base class are also member variables of
the derived class. Similarly, the member functions of the base class
(unless redefined) are also member functions of the derived class.
(Remember Rule 1 when accessing a member of the base class in the
derived class.)
//Examples:
rectangleType myRectangle(5.0, 3.0);
boxType myBox(6.0, 5.0, 4.0);
l Recall that a data type is a set of values, called the domain of the
data type, together with a set of operations.
l In addition to these two properties, until now, all of the data types you
have encountered have one more thing associated with them: the
name of the data type. For example, there is a data type called int.
l Pointer Data Type: The values belonging to pointer data types are
the memory addresses of your computer. However, there is no name
associated with the pointer data type in C++. Because the domain,
(that is, the values of a pointer data type), consists of addresses
(memory locations), a pointer variable is a variable whose content is
an address, that is, a memory location.
int k = 10;
float x = 3.15;
int *kPtr = &k; // int pointer pointing to an int variable
float *xPtr = &x; // float pointer pointing to a float variable
l The only difference between classes and structs is that, by default, all
members of a class are private, and, by default, all members of a
struct are public. Therefore, the following discussion applies to both.
struct studentType
{
char name[26]; pointerVariableName -> classMemberName
double gpa; is equivalent to
int sID; (*pointerVariableName).classMemberName
char grade;
};
studentType student;
studentType* studentPtr;
studentPtr = &student;
(*studentPtr).gpa = 3.9; //Correct
*studentPtr.gpa = 3.9; //Wrong !!! The . has a higher precedence than *
studentPtr->gpa = 3.9; // equivalent to (*studentPtr).gpa = 3.9;
Spring 2024 Data Structure 51
Initializing Pointer Variables
delete p;
delete [] name;
delete str;
delete [] second;