1 Lab-Cpp
1 Lab-Cpp
Week 1
Yulei Sui
School of Computer Science and Engineering
University of New South Wales, Australia
1
COMP6131 Software Security Analysis 2026
What to Expect from Each Lab
What We Do:
• Lab demonstrations, including configuring IDEs, coding examples, and further
explanations of concepts from lectures.
• Reinforce your skills and knowledge to complete lab exercises and
assignments.
• Answer questions regarding specifications in your exercises and assignments.
• Provide time for you to work on your quizzes and coding exercises.
What We Don’t Do:
• Debug your program
• Teach programming. This course does NOT focus on teaching you C++ or
Python, but this lab provides a brief guide. Completing assessments requires
basic C++ (recommended) or Python syntax/knowledge.
• Give you code solutions or judge the correctness of your exercise/assignment.
No sharing of your solutions in the forums or public GitHub repositories.
2
COMP6131 Software Security Analysis 2026
Quiz-1 and Exercise-1
• Lab-Quiz-1:
[Link]
• C++ programming, software vulnerability assessment, compiler, control-flow,
data-flow, and taint tracking.
• 25 quizzes (each worth 0.2 marks) covering knowledge taught in Week 1 and
Week 2.
• Lab-Exercise-1: [Link]
Software-Security-Analysis/wiki/Lab-Exercise-1
• Implementing the reachability method, a DFS graph traversal algorithm.
• Implementing the solveWorklist method, a constraint graph solving
algorithm for Andersen’s points-to analysis: [Link]
Software-Security-Analysis/blob/slides/[Link]
Submit your Quiz-1 and Lab-Exercise-1 on WebCMS by 23:59 on Tuesday of
Week 3.
3
COMP6131 Software Security Analysis 2026
Today’s Lab: IDE Demo and Introduction/Revisit to C++/Python
4
COMP6131 Software Security Analysis 2026
A Quick Overview of C++
Week 1
Yulei Sui
School of Computer Science and Engineering
University of New South Wales, Australia
5
COMP6131 Software Security Analysis 2026
Introduction to C++ Programming
What is C++?
• A general-purpose programming language that was developed as an
enhancement of the C language to include object-oriented paradigm.
6
COMP6131 Software Security Analysis 2026
Introduction to C++ Programming
What is C++?
• A general-purpose programming language that was developed as an
enhancement of the C language to include object-oriented paradigm.
Why learn C++?
• Language for building system software (e.g., operating systems, web
browsers, game engines, database engines, language runtimes and
cloud/distributed systems)
6
COMP6131 Software Security Analysis 2026
Introduction to C++ Programming
What is C++?
• A general-purpose programming language that was developed as an
enhancement of the C language to include object-oriented paradigm.
Why learn C++?
• Language for building system software (e.g., operating systems, web
browsers, game engines, database engines, language runtimes and
cloud/distributed systems)
• Object-oriented yet high performance
• Pointer and direct memory-access
6
COMP6131 Software Security Analysis 2026
Introduction to C++ Programming
What is C++?
• A general-purpose programming language that was developed as an
enhancement of the C language to include object-oriented paradigm.
Why learn C++?
• Language for building system software (e.g., operating systems, web
browsers, game engines, database engines, language runtimes and
cloud/distributed systems)
• Object-oriented yet high performance
• Pointer and direct memory-access
• One of the most popular languages and fastest-growing
• [Link]/article/c-is-now-the-fastest-growing-programming-language
• [Link]/article/
most-popular-programming-languages-c-knocks-python-out-of-top-three
6
COMP6131 Software Security Analysis 2026
Introduction to C++ Programming
• This short introduction does not aim to cover every detailed aspect of C++,
but rather the basic C++ syntax/features in order to develop algorithms to fulfil
the assignment tasks in this course.
7
COMP6131 Software Security Analysis 2026
Introduction to C++ Programming
• This short introduction does not aim to cover every detailed aspect of C++,
but rather the basic C++ syntax/features in order to develop algorithms to fulfil
the assignment tasks in this course.
• You are encouraged to learn and practice more advanced C++
syntax/features.
• [Link]
• [Link]
• Google search ‘C++ programming‘ or ‘introduction to C++ programming‘
7
COMP6131 Software Security Analysis 2026
Write Your First C++ Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
return 0;
}
8
COMP6131 Software Security Analysis 2026
C++ Primitive Data Types and Variables
9
COMP6131 Software Security Analysis 2026
C++ Classes and Objects
• C++ class: new data type compared with C for
• Abstraction: ”shows” essential attributes and ”hides” unnecessary information
• Encapsulation: ‘expose‘ only the interfaces and hide implementation details
• A C++ class is a template for objects, and an object is an instance of a class.
10
COMP6131 Software Security Analysis 2026
C++ Classes and Objects
• C++ class: new data type compared with C for
• Abstraction: ”shows” essential attributes and ”hides” unnecessary information
• Encapsulation: ‘expose‘ only the interfaces and hide implementation details
• A C++ class is a template for objects, and an object is an instance of a class.
#include <iostream>
using namespace std;
class Graph { // the class
private: // private access specifier
int numOfNodes; // hidden attribute from outside
int numOfEdges; // hidden attribute from outside
public: // public access specifier
// interface to outside world
int getNumOfNodes(){ return numOfNodes;}
// interface to outside world
void setNumOfNodes(int n){ numOfNodes = n;}
};
10
COMP6131 Software Security Analysis 2026
C++ Classes and Objects
• C++ class: new data type compared with C for
• Abstraction: ”shows” essential attributes and ”hides” unnecessary information
• Encapsulation: ‘expose‘ only the interfaces and hide implementation details
• A C++ class is a template for objects, and an object is an instance of a class.
#include <iostream>
using namespace std;
class Graph { // the class
int main() {
// create an object of Graph
private: // private access specifier
Graph graphObj;
int numOfNodes; // hidden attribute from outside
// Access attribute via interface
int numOfEdges; // hidden attribute from outside
[Link](10);
public: // public access specifier
// print out value of the attribute
// interface to outside world
cout << [Link]();
int getNumOfNodes(){ return numOfNodes;}
cout << "\n";
// interface to outside world
}
void setNumOfNodes(int n){ numOfNodes = n;}
};
10
COMP6131 Software Security Analysis 2026
Constructor
• A constructor is a special method automatically called when an object is
created.
#include <iostream>
using namespace std;
class Graph { // the class
private: // private access specifier
int numOfNodes; // hidden attribute from outside
int main() {
// Create an object via its constructor
int numOfEdges; // hidden attribute from outside
Graph graphObj(5,10);
public: // public access specifier
// print out value of the attribute
Graph(int n, int e){ // constructor
cout << [Link]();
numOfNodes = n;
cout << "\n";
numOfEdges = e;
}
}
// interface to outside world
int getNumOfNodes(){ return numOfNodes;}
};
11
COMP6131 Software Security Analysis 2026
Containers/Collections
12
COMP6131 Software Security Analysis 2026
Containers/Collections
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int> nodeIDs;
nodeIDs.push_back(1);
nodeIDs.push_back(2);
nodeIDs.push_back(2);
// iterating elements via loop
for(auto i : nodeIDs)
cout << i << "\n";
}
13
COMP6131 Software Security Analysis 2026
Containers/Collections
#include <vector> #include <set>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main () int main ()
{ {
vector<int> nodeIDs; set<int> nodeIDs;
nodeIDs.push_back(1); [Link](1);
nodeIDs.push_back(2); [Link](2);
nodeIDs.push_back(2); [Link](2);
// iterating elements via loop // iterating elements via loop
for(auto i : nodeIDs) for(auto i : nodeIDs)
cout << i << "\n"; cout << i << "\n";
} }
13
COMP6131 Software Security Analysis 2026
Containers/Collections Used in a Class
#include <set>
using namespace std;
class Graph {
private:
int numOfNodes;
int numOfEdges; int main() {
// Create an object of Graph
set<int> nodeIDs;
Graph graphObj(5,10);
public:
// Increase nodes;
Graph(int n, int e) {
[Link](1);
numOfNodes = n;
[Link](2);
numOfEdges = e;
}
}
void addNode(int id){
[Link](id);
}
};
14
COMP6131 Software Security Analysis 2026
Pointers for Primitive Types
• The memory address of a variable can be taken through the & operator.
• A pointer however, is a variable that stores the memory address as its value.
int nodeID = 5; // A nodeID variable of type int
int* ptr = &nodeID; // A pointer `ptr` storing the address of nodeID
15
COMP6131 Software Security Analysis 2026
Pointers for Primitive Types
• The memory address of a variable can be taken through the & operator.
• A pointer however, is a variable that stores the memory address as its value.
int nodeID = 5; // A nodeID variable of type int
int* ptr = &nodeID; // A pointer `ptr` storing the address of nodeID
// Output the value of NodeID (i.e., 5)
cout << nodeID << "\n";
// Output the memory address of NodeID (e.g., 0x6dfed4)
cout << &nodeID << "\n";
// Output the memory address of nodeID with the pointer (e.g., 0x6dfed4)
cout << ptr << "\n";
// Output the value of nodeID via dereferencing the pointer ptr
cout << *ptr << "\n";
16
COMP6131 Software Security Analysis 2026
References for Primitive Types
ref = 20;
cout << "nodeID = " << nodeID << endl ;
nodeID = 30;
cout << "ref = " << ref << endl ;
17
COMP6131 Software Security Analysis 2026
References for Primitive Types
18
COMP6131 Software Security Analysis 2026
C++ const Type Qualifier
• The const keyword allows you to specify whether or not a variable is
modifiable. It can help (1) document your program more clearly and (2)
enable more compiler optimization opportunities.
// a constant integer.
// modifying `nodeID` will get a compilation error.
const int nodeID = 5;
// const Pointer.
// `cptr` is a pointer, which is const, that points to an int.
// modifying `cptr` will get a compilation error
int anotherNodeID = 6;
int* const cptr = &anotherNodeID;
19
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both references and pointers can be used to change local variables of one
function inside another function.
/// parameters as values
/// (pass by value)
void swap(int n1, int n2){
int tmp = n1;
n1 = n2;
n2 = tmp;
}
int main(){
int node1 = 2, node2 = 3;
swap(node1, node2);
cout << node1 << " " << node2;
}
20
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both references and pointers can be used to change local variables of one
function inside another function.
/// parameters as values
/// (pass by value)
void swap(int n1, int n2){
int tmp = n1;
n1 = n2;
n2 = tmp;
}
int main(){
int node1 = 2, node2 = 3;
swap(node1, node2);
cout << node1 << " " << node2;
}
pass by value: caller and callee have
two independent variables with the
same value (effect not visible to caller)
20
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both references and pointers can be used to change local variables of one
function inside another function.
/// parameters as values /// parameters as references
/// (pass by value) /// (Pass by reference)
void swap(int n1, int n2){ void swap(int& n1, int& n2){
int tmp = n1; int tmp = n1;
n1 = n2; n1 = n2;
n2 = tmp; n2 = tmp;
} }
int main(){ int main(){
int node1 = 2, node2 = 3; int node1 = 2, node2 = 3;
swap(node1, node2); swap(node1, node2);
cout << node1 << " " << node2; cout << node1 << " " << node2;
} }
pass by value: caller and callee have
two independent variables with the
same value (effect not visible to caller)
20
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both references and pointers can be used to change local variables of one
function inside another function.
/// parameters as values /// parameters as references
/// (pass by value) /// (Pass by reference)
void swap(int n1, int n2){ void swap(int& n1, int& n2){
int tmp = n1; int tmp = n1;
n1 = n2; n1 = n2;
n2 = tmp; n2 = tmp;
} }
int main(){ int main(){
int node1 = 2, node2 = 3; int node1 = 2, node2 = 3;
swap(node1, node2); swap(node1, node2);
cout << node1 << " " << node2; cout << node1 << " " << node2;
} }
pass by value: caller and callee have passed by reference: caller and
two independent variables with the callee share the same variable for the
same value (effect not visible to caller) parameter (effect visible to caller)
20
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both references and pointers can be used to change local variables of one
function inside another function.
/// parameters as values /// parameters as references /// parameters as pointers
/// (pass by value) /// (Pass by reference) /// (Pass by pointers)
void swap(int n1, int n2){ void swap(int& n1, int& n2){ void swap(int* n1, int* n2){
int tmp = n1; int tmp = n1; int tmp = *n1;
n1 = n2; n1 = n2; *n1 = *n2;
n2 = tmp; n2 = tmp; *n2 = tmp;
} } }
int main(){ int main(){ int main(){
int node1 = 2, node2 = 3; int node1 = 2, node2 = 3; int node1 = 2, node2 = 3;
swap(node1, node2); swap(node1, node2); swap (&node1, &node2);
cout << node1 << " " << node2; cout << node1 << " " << node2; cout << node1 << " " << node2;
} } }
pass by value: caller and callee have passed by reference: caller and
two independent variables with the callee share the same variable for the
same value (effect not visible to caller) parameter (effect visible to caller)
20
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both references and pointers can be used to change local variables of one
function inside another function.
/// parameters as values /// parameters as references /// parameters as pointers
/// (pass by value) /// (Pass by reference) /// (Pass by pointers)
void swap(int n1, int n2){ void swap(int& n1, int& n2){ void swap(int* n1, int* n2){
int tmp = n1; int tmp = n1; int tmp = *n1;
n1 = n2; n1 = n2; *n1 = *n2;
n2 = tmp; n2 = tmp; *n2 = tmp;
} } }
int main(){ int main(){ int main(){
int node1 = 2, node2 = 3; int node1 = 2, node2 = 3; int node1 = 2, node2 = 3;
swap(node1, node2); swap(node1, node2); swap (&node1, &node2);
cout << node1 << " " << node2; cout << node1 << " " << node2; cout << node1 << " " << node2;
} } }
pass by value: caller and callee have passed by reference: caller and pass by pointer: caller and callee
two independent variables with the callee share the same variable for the share the same variable via pointer
same value (effect not visible to caller) parameter (effect visible to caller) dereferences (effect visible to caller)
20
COMP6131 Software Security Analysis 2026
Parameter Passing using Pointers and References
• Both of them can also be used to save copying of big objects when passed
as arguments to functions or returned from functions, to be more efficient.
class Graph {
public:
int numOfNodes;
int numOfEdges;
};
// If we remove `*` or `&` in below functions, a new copy of the graph object is created.
// `const` used to avoid accidentally updates `g` as the purpose is to print `g` only.
void print(const Graph *g){
cout << g->numOfNodes << " " << g->numOfEdges << " ";
}
void print(const Graph &g){
cout << [Link] << " " << [Link] << " ";
}
21
COMP6131 Software Security Analysis 2026
Using Pointers in Classes
#include <iostream>
using namespace std;
class Node { // The class
private:
int nodeID; // Node ID
public: // Access specifier
Node(int i){ nodeID = i; } // constructor
int getNodeID() { return nodeID;}
};
class Edge {
private:
Node* src;
Node* dst;
public:
Edge(Node* s,Node* d){ src = s; dst = d; }
Node* getSrc() { return src;}
Node* getDst() { return dst;} ;
};
23
COMP6131 Software Security Analysis 2026
Putting All the Above Classes Together to Build a Graph
#include <set>
using namespace std; class Edge; class Graph {
class Node { private:
private: set<Node*> nodes; // a set of nodes
int nodeID; public:
set<Edge*> outEdges; // outgoing edges Graph() { }
public: set<Node*>& getNodes(){ return nodes;}
Node(int i){ nodeID = i; } };
int getNodeID() { return nodeID;} int main () {
set<Edge*>& getOutEdges(){ return outEdges;} Node* src = new Node(1);
}; Node* dst = new Node(2);
Edge* edge = new Edge(src, dst);
class Edge { // add src's outgoing edge
private: src->getOutEdges().insert(edge);
Node* src; // create a graph object
Node* dst; Graph* graph = new Graph();
public: // add two nodes into the graph
Edge(Node* s,Node* d){ src = s; dst = d; } graph->getNodes().insert(src);
Node* getSrc() { return src;} graph->getNodes().insert(dst);
Node* getDst() { return dst;} }
};
24
COMP6131 Software Security Analysis 2026
C++ Inheritance
Allow a child class to inherit attributes and methods from its parent class.
25
COMP6131 Software Security Analysis 2026
C++ Inheritance
Allow a child class to inherit attributes and methods from its parent class.
class GraphBuilder{
public:
GraphBuilder(){}
void build(){
cout << "parent's way to build..\n";
Node* src = new Node(1);
Node* dst = new Node(2);
Edge* edge = new Edge(src, dst);
// add src's outgoing edge
src->addOutEdge(edge);
// create a graph object
Graph* graph = new Graph();
// add two nodes into the graph
graph->addNode(src);
graph->addNode(dst);
}
};
25
COMP6131 Software Security Analysis 2026
C++ Inheritance
Allow a child class to inherit attributes and methods from its parent class.
class GraphBuilder{
public:
GraphBuilder(){}
// SubGraphBuilder is a child (derived) class
// of GraphBuilder
void build(){
cout << "parent's way to build..\n";
class SubGraphBuilder : public GraphBuilder{
Node* src = new Node(1);
public:
SubGraphBuilder(){}
Node* dst = new Node(2);
};
Edge* edge = new Edge(src, dst);
// add src's outgoing edge
src->addOutEdge(edge);
int main () {
SubGraphBuilder* builder = new SubGraphBuilder();
// create a graph object
// reuse the build method in GraphBuilder
Graph* graph = new Graph();
builder->build();
// add two nodes into the graph
}
graph->addNode(src);
graph->addNode(dst);
}
};
25
COMP6131 Software Security Analysis 2026
C++ Function Overriding
Allow a child class to override a function (with same signature) in its parent class.
26
COMP6131 Software Security Analysis 2026
C++ Function Overriding
Allow a child class to override a function (with same signature) in its parent class.
class GraphBuilder{ class SubGraphBuilder : public GraphBuilder{
public: public:
GraphBuilder(){} SubGraphBuilder(){}
// override `build` method in GraphBuilder
void build(){ void build(){
cout << "parent's way to build..\n"; cout << "child's way to build..\n";
Node* src = new Node(1); }
Node* dst = new Node(2); };
Edge* edge = new Edge(src, dst);
// add src's outgoing edge int main () {
src->addOutEdge(edge); SubGraphBuilder* builder1 = new SubGraphBuilder();
// create a graph object // Which `build` method will be called?
Graph* graph = new Graph(); builder1->build();
// add two nodes into the graph
graph->addNode(src); GraphBuilder* builder2 = new SubGraphBuilder();
graph->addNode(dst); // Which `build` method will be called?
} builder2->build();
}; }
26
COMP6131 Software Security Analysis 2026
C++ Virtual Function and Polymorphism
A function declared with a ‘virtual‘ keyword in a parent class can be overridden by
a child class. When you refer to a child class object using a pointer/reference
to the parent class, it will call child class’s version of this virtual function.
27
COMP6131 Software Security Analysis 2026
C++ Virtual Function and Polymorphism
A function declared with a ‘virtual‘ keyword in a parent class can be overridden by
a child class. When you refer to a child class object using a pointer/reference
to the parent class, it will call child class’s version of this virtual function.
class GraphBuilder{ class SubGraphBuilder : public GraphBuilder{
public: public:
SubGraphBuilder(){}
GraphBuilder(){}
virtual void build(){ void build(){ // override `build` in GraphBuilder
cout << "child's way to build..\n";
cout << "parent's way to build..\n";
}
Node* src = new Node(1);
};
Node* dst = new Node(2);
Edge* edge = new Edge(src, dst);
int main () {
SubGraphBuilder* builder1 = new SubGraphBuilder();
// add src's outgoing edge
builder1->build(); // Which `build` will be called?
src->addOutEdge(edge);
// create a graph object
GraphBuilder* builder2 = new SubGraphBuilder();
Graph* graph = new Graph();
builder2->build(); // Which `build` will be called?
// add two nodes into the graph
graph->addNode(src);
GraphBuilder* builder3 = new GraphBuilder();
graph->addNode(dst);
builder3->build(); // Which `build` will be called?
}
}
}; 27
COMP6131 Software Security Analysis 2026
Debugging Your C++ Programs
• VSCode ([Link]
• GDB ([Link]
• LLDB ([Link]
• Eclipse CDT ([Link]
• Other tactics, such as printing your results
([Link]
28
COMP6131 Software Security Analysis 2026
A Quick Overview of Python
Week 1
Yulei Sui
School of Computer Science and Engineering
University of New South Wales, Australia
29
COMP6131 Software Security Analysis 2026
Introduction to Python Programming
What is Python?
• Python is a high-level, interpreted general-purpose multi-paradigm
programming language.
30
COMP6131 Software Security Analysis 2026
Introduction to Python Programming
What is Python?
• Python is a high-level, interpreted general-purpose multi-paradigm
programming language.
Why learn Python?
• Language for web development, data analysis, machine learning, and
scripting.
30
COMP6131 Software Security Analysis 2026
Introduction to Python Programming
What is Python?
• Python is a high-level, interpreted general-purpose multi-paradigm
programming language.
Why learn Python?
• Language for web development, data analysis, machine learning, and
scripting.
• User-friendly syntax which can quickly write programs and easily interface
with high-performance libraries
• Provides rich library support for many applications
30
COMP6131 Software Security Analysis 2026
Introduction to Python Programming
What is Python?
• Python is a high-level, interpreted general-purpose multi-paradigm
programming language.
Why learn Python?
• Language for web development, data analysis, machine learning, and
scripting.
• User-friendly syntax which can quickly write programs and easily interface
with high-performance libraries
• Provides rich library support for many applications
• A popular and extensively used language
30
COMP6131 Software Security Analysis 2026
Python
• This short introduction does not aim to cover every detailed aspect of Python,
but rather the basic Python syntax/features in order to develop algorithms to
fulfil the assignment tasks in this course.
31
COMP6131 Software Security Analysis 2026
Python
• This short introduction does not aim to cover every detailed aspect of Python,
but rather the basic Python syntax/features in order to develop algorithms to
fulfil the assignment tasks in this course.
• You are encouraged to learn and practice more advanced Python
syntax/features.
• [Link]
• [Link]
• [Link]
• Google search ‘Python programming‘ or ‘Introduction to Python programming‘
31
COMP6131 Software Security Analysis 2026
Write Your First Python Program
32
COMP6131 Software Security Analysis 2026
If Statements in Python
x = int(input("Please enter an integer: "))
Please enter an integer: 42
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print("Single")
else:
print('More')
34
COMP6131 Software Security Analysis 2026
For Loops in Python
34
COMP6131 Software Security Analysis 2026
Containers/Collections
#Python lists
node_ids = []
node_ids.append(1)
node_ids.append(2)
node_ids.append(2)
for i in node_ids:
print(i)
35
COMP6131 Software Security Analysis 2026
Containers/Collections
35
COMP6131 Software Security Analysis 2026
Functions in Python
def fib(n): # write Fibonacci series less than n
"""Return a Fibonacci series less than n."""
series = []
a, b = 0, 1
while a < n:
[Link](a)
a, b = b, a+b
print(fib(2000))
36
COMP6131 Software Security Analysis 2026
Functions in Python
def fib(n): # write Fibonacci series less than n
"""Return a Fibonacci series less than n."""
series = []
a, b = 0, 1
while a < n:
[Link](a)
a, b = b, a+b
print(fib(2000))
# An alternative function definition with the typing library
from typing import List
def fib(n: int) -> List[int]:
...
37
COMP6131 Software Security Analysis 2026
Python Classes and Objects
• Python objects: everything in Python is an object, there are no primitive types.
• A Python class is a template for objects, and an object is an instance of a
class.
• All methods are public by default, a prefixed in the function name is used for
protected methods or for private methods.
class Graph:
def __init__(self, n: int, e: int):
self.num_of_nodes: int = n
self.num_of_edges: int = e
def get_num_of_nodes(self) -> int:
return self.num_of_nodes
def set_num_of_nodes(self, n: int):
return [Link]
def get_paths(self) -> Set[str]:
return [Link]
37
COMP6131 Software Security Analysis 2026
Python Classes and Objects
• Python objects: everything in Python is an object, there are no primitive types.
• A Python class is a template for objects, and an object is an instance of a
class.
• All methods are public by default, a prefixed in the function name is used for
protected methods or for private methods.
class Graph:
def __init__(self, n: int, e: int):
self.num_of_nodes: int = n
self.num_of_edges: int = e
def get_num_of_nodes(self) -> int:
return self.num_of_nodes graph_obj = Graph(5, 10)
def set_num_of_nodes(self, n: int): print(graph_obj.get_num_of_nodes())
return [Link]
def get_paths(self) -> Set[str]:
return [Link]
37
COMP6131 Software Security Analysis 2026
Building a Graph with more Functionality
class Node:
def __init__(self, i: int):
self.node_id = i
self.out_edges = set()
def get_node_id(self) -> int:
return self.node_id
def get_out_edges(self) -> Set[Edge]:
return self.out_edges
class Edge:
def __init__(self, s: Node, d: Node):
[Link] = s
[Link] = d
def get_src(self) -> Node:
return [Link]
def get_dst(self) -> Node:
return [Link]
38
COMP6131 Software Security Analysis 2026
Building a Graph with more Functionality
class Node: class Graph:
def __init__(self, i: int): def __init__(self):
self.node_id = i [Link]: Set[Node] = set()
self.out_edges = set() def get_nodes(self) -> Set[Node]:
def get_node_id(self) -> int: return [Link]
return self.node_id src = Node(1)
def get_out_edges(self) -> Set[Edge]: dst = Node(2)
return self.out_edges edge = Edge(src, dst)
class Edge: # add src's outgoing edge
def __init__(self, s: Node, d: Node): src.get_out_edges().add(edge)
[Link] = s # create a graph object
[Link] = d graph = Graph()
def get_src(self) -> Node: # add two nodes into the graph
return [Link] graph.get_nodes().add(src)
def get_dst(self) -> Node: graph.get_nodes().add(dst)
return [Link]
38
COMP6131 Software Security Analysis 2026
Debugging Your Python Programs
• VSCode ([Link]
• PDB ([Link]
• Other tactics, such as printing your results
([Link]
39
COMP6131 Software Security Analysis 2026