CSci 3081W: Program Design and Development
Lecture 05 – Inheritance and Pointers
Logistics
Reminder – Quiz on Thursday
Project Update: Virtual Drone Delivery Project Project Manager: Dan Orban
Please read and ask clarifying questions on Piazza.
https://github.umn.edu/umn-csci-3081-f20/project-portal/blob/master/Iteration1Requirements.pdf
Drone Delivery System Specification Released!
Project Rocket (Visualization)
Project Jet Powered (Ecommerce Website)
Project ANVIL (Simulation)
Uses the Visualization
There are three separate parallel projects.
Prototype: Deliver packages between 4 customers
Visualization
Entity Project
Drone Delivery System Facade
The current structure of Project ANVIL dependencies
Visualization
Entity Project
Drone Delivery System Facade
The current structure of Project ANVIL dependencies
Teams will not change any of these interfaces.
To build the drone system, you will need to extend these classes.
Timeline of Project ANVIL
High Five Proposal Form: https://forms.gle/KZKg9aEQkeiV8efj6
The High Five Extensions are also included In the specification. (more may be added)
Roadmap for Today
Project Update
Design: Class Relationships
Inheritance
* & ->
References and Pointer Introduction
In-Class Exercise: In a paragraph, describe the difference between ¡°is a¡± vs ¡°has a¡± relationships. Use the diagram below from our project to explain examples.
In-Class Exercise: In a paragraph, describe the difference between ¡°is a¡± vs ¡°has a¡± relationships. Use the diagram below from our project to explain examples.
Why is there no dotted line between the DroneDeliverySystem and the Drone?
Inheritance is key for building your Drone Delivery System.
Inheritance is key for building your Drone Delivery System.
The major difference between C and C++ is Object Oriented Programming.
Encapsulation
Inheritance
Polymorphism
C
No
No
No
C++
Yes
Yes
Yes
Done Today
Roadmap for Today
Project Update
Design: Class Relationships
Inheritance
* & ->
References and Pointer Introduction
Inheritance in c++ is what you would think it is. Inheriting traits from a parent class.
class derived : public base {
};
In Human Biological Systems, multiple inheritance is implemented. Inheriting traits from multiple parents.
class derived : public baseA, public baseB {
};
In C++ inheritance allows us to inherit functionality from other classes. Derived Class Name Access Qualifier Parent Class Name
class derivedClass :
};
Colon
In C++ inheritance allows us to inherit functionality from other classes.
Definition:
class derivedClass :
};
Example:
void setWidth(int w) {
width = w;
}void setHeight(int h) { height = h;
}
#include
using namespace std;
// Base class Shape
class Shape {
public:
int main() {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
}
}
return (width * height);
https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
};
In C++ inheritance allows us to inherit functionality from other classes.
Definition:
class derivedClass :
};
#include
using namespace std;
// Base class Shape
class Shape {
public:
int main() {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
Example:
void setWidth(int w) {
width = w;
}void setHeight(int h) { height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape { public:
int getArea() {
return (width * height);
}
}
Almost always public so we can reuse code.
https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
};
In C++ inheritance allows us to inherit functionality from other classes.
Definition:
class derivedClass :
};
#include
using namespace std;
// Base class Shape
class Shape {
public:
int main() {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
Example:
void setWidth(int w) {
width = w;
}void setHeight(int h) { height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
}
What do you think protected means?
}
return (width * height);
https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
};
The protected access qualifier allows derived classes to use member variables and methods.
In C++ inheritance allows us to inherit functionality from other classes.
Example:
}
protected:
// Base class Shape
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
int main() {
};
int width;
int height;
<<
}
height = h;
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea()
endl;
return 0;
Protected allows us to use width and height in the derived class.
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
};
}
return (width * height);
https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
Example: Let¡¯s create more derived classes from the Shape class.
// Base class Shape
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
};
}
return (width * height);
Example: Let¡¯s create more derived classes from the Shape class.
// Base class Shape
class Shape { public:
void setWidth(int w) {
width = w;
// Derived class
class Triangle: public Shape { Public:
int getArea() {
return 0.5 * (width * height);
}} void setHeight(int h) { };
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
};
}
return (width * height);
Example: Let¡¯s create more derived classes from the Shape class.
// Base class Shape
class Shape {
public:
void setWidth(int w) {
// Derived class
class Triangle: public Shape {
Public:
int getArea() {
width = w; }}
return 0.5 * (width * height);
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape { public:
};
};
int getArea() {
return (width * height);
}
}
// Derived class
class Square: public Rectangle { public:
void setWidth(int w) {
width = w;
height = width;
}
void setHeight(int h) {
};
height = h;
width = height;
Roadmap for Today
Project Update
Design: Class Relationships
Inheritance
* & ->
References and Pointer Introduction
References are like aliases for existing variables (e.g. int& b)
#include
using namespace std;
int main()
{
int a = 10;
int& b = a;
b++;
cout << a << " " << b << endl;
b = 32;
// a = 32;
cout << a << " " << b << endl;
return 0;
}
b is another name for the variable a a and b point to the same memory!
When a change occurs in one, the other changes since it is the same memory being updated.
References are considered ¡°safe¡± pointers with restrictions.
Address
Content
90000000
01010110
90000001
00000110
90000002
11000111
90000003
11000111
90000004
11000000
90000005
11111011
90000006
10000101
90000007
10000101
90000008
10000100
90000009
10110101
9000000A
10110101
9000000B
10110101
float sum; (4 bytes)
A reference saves the address to a variable in memory.
float& sumReference; (8 bytes) = 9000000
References are considered ¡°safe¡± pointers with restrictions.
Address
Content
90000000
01010110
90000001
00000110
90000002
11000111
90000003
11000111
90000004
11000000
90000005
11111011
90000006
10000101
90000007
10000101
90000008
10000100
90000009
10110101
9000000A
10110101
9000000B
10110101
float sum; (4 bytes)
A reference saves the address to a variable in memory.
float& sumReference = sum; (8 bytes)
= 9000000
Restrictions
¡ñ The memory is assumed to exist!
¡ð Cannot point to invalid location
¡ñ It must be initialized from another variable. ¡ñ It cannot be changed.
Why should we use them?
#include
using namespace std;
int main()
{
int a = 10;
int& b = a;
b++;
cout << a << " " << b << endl;
a = 32;
cout << a << " " << b << endl;
return 0;
}
Read Only Variables: The keyword const means that the variable cannot be changed.
#include
using namespace std;
int main()
{
Fail!
int a = 10;
const int& b = a;
b++;
cout << a << " " << b << endl;
a = 32;
cout << a << " " << b << endl;
return 0;
}
Output Parameters: Passing by reference allows you to change variables within a function.
#include
using namespace std;
void add(int a, int b, int& sum) {
sum = a + b;
}
int main()
{
int sum;
add(1, 2, sum);
cout << sum << endl;
return 0;
}
Sum is changed within the add function!
There are other great reasons to use references, but we will learn more about this as we study pointers.
Preview:
¡ñ They can reduce the memory stored on the stack.
¡ñ They allow safe access to memory (safer than pointers)
¡ñ They can be used to dereference pointers.
¡ð int& a = *b;
Key Takeaway: In functions, pass by reference if you would like to use parameters as output variables.
int divide(int a, int b, int& remainder) {
remainder = a % b;
return a / b;
}
Here we get the remainder from the parameter. Multiple outputs!
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
struct Employee {
...
};
...
Employee jsmith;
Feel free to create other structures to help you!
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
struct Employee {
...
};
...
Employee jsmith;
Feel free to create other structures to help you!
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
struct Date {
int month;
int day;
int year;
};
struct PhoneNumber {
int areaCode;
int number;
};
struct Employee {
char name[50];
int id;
float salary;
Date dateOfBirth;
int age;
PhoneNumber phone;
char email[100];
};
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
¡ñ Manager
struct Date {
int month;
int day;
int year;
};
struct PhoneNumber {
int areaCode;
int number;
};
struct Employee {
char name[50];
int id;
float salary;
Date dateOfBirth;
int age;
PhoneNumber phone;
char email[100];
Employee manager; // ????
};
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
¡ñ Manager
struct Date {
int month;
int day;
int year;
};
struct PhoneNumber {
int areaCode;
int number;
};
struct Employee {
char name[50];
int id;
float salary;
Date dateOfBirth;
int age;
PhoneNumber phone;
char email[100];
Employee manager; // ????
};
Incomplete Type!
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
¡ñ Manager
struct Date {
int month;
int day;
int year;
};
struct PhoneNumber {
int areaCode;
int number;
};
struct Employee {
char name[50];
int id;
float salary;
Date dateOfBirth;
int age;
PhoneNumber phone;
char email[100];
Employee& manager; // ????
};
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
¡ñ Manager
struct Date {
int month;
int day;
int year;
};
struct PhoneNumber {
int areaCode;
int number;
};
struct Employee {
char name[50];
int id;
float salary;
Date dateOfBirth; Uninitialized Reference int age;
PhoneNumber phone;
char email[100];
Employee& manager; // ????
};
Consider a data structure for an employee at a company using C++.
¡ñ Name
¡ñ Employee ID
¡ñ Salary
¡ñ Date of Birth
¡ñ Age
¡ñ Phone Number
¡ñ Email
¡ñ etc...
¡ñ Manager
struct Date {
int month;
int day;
int year;
};
struct PhoneNumber {
int areaCode;
int number;
};
struct Employee {
char name[50];
int id;
float salary;
Date dateOfBirth;
int age;
PhoneNumber phone;
char email[100];
Employee* manager;
};
First example of where pointers are useful!
Common data structures like linked lists and binary trees use this technique.
// Linked List
struct Node {
DataType data;
Node* next;
}
// Binary Tree
struct Node {
DataType data;
Node* left;
Node* right;
}
Roadmap for Today
Project Update
Design: Class Relationships
Inheritance
* & ->
References and Pointer Introduction
A pointer is a memory address. That¡¯s all folks…
Memory
int main()
{
int a = 5;
int* pointerToA = &a;
cout << "a: " << a << endl;
cout << "address of a: " << pointerToA << endl;
}
Variable
a
pointerToA
Value
5
0x7ffeee67c81c
a: 5
address of a: 0x7ffeee67c81c
A pointer is declared with the *
Type Declares Pointer Type Variable Name
int* a;
To get the address of an existing variable the & operator can be used.
Type Declares Pointer Type Variable Name
int* a;
int myInt = 5;
a = &myInt;
Gets address of myInt
To dereference a pointer to a value, you can use the * operator.
Type Declares Pointer Type Variable Name
int* a;
int myInt = *a;
Gets value at pointer a