UCCD1024 Lecture3 Advanced Struct Updated
UCCD1024 Lecture3 Advanced Struct Updated
Lecture 3
Advanced Struct
Lecture 3: Advanced Struct
Advanced Struct: Encapsulation
Combining attributes and capabilities together
Another example
Information hiding using multiple files
Function overloading.
11 money += m;
12 }
13 bool giveMoney(float borrowedSum, float &moneyGiven) {
14 if (money > borrowedSum) {
15 money -= borrowedSum;
16 moneyGiven = borrowedSum;
17 return true; // send output to other object
18 }
return false;
}
}; Programming
L05: Object-based 6
Struct attributes & capabilities
The operations inside this function allow a Person struct
variable to give money to other, i.e. capability to give.
The new Person type can be used as shown on next page.
1 struct Person {
2 char name[20], phone[20]; // Attributes section
3 double money;
4 Person *spouse;
5 void type() { // capabilities section
6 cout << "\nMy name is " << name;
7 cout << "\nMy phone number is " << phone << " and I have RM" << money << "\n\n";
8 }
9 void acceptMoney(float m) { // accept input from other objects
10 money += m;
}
Run
oac.cpp
Output
Input
1 struct Circle {
Attributes
2 double radius;
3 double centerX, centerY;
capabilities
4 double a() { return radius*radius*PI; }
5 double area() { return a(); }
6 void setR(double r) { radius = (r >=0)? r : 0; }
7 };
L05: Object-based Programming 11
Another example
In this example, we define a new type, Circle.
The sections of attributes and capabilities are highlighted.
The input to and output from this type of struct variable are
also highlighted.
In the attribute section, we define the most important
ingredients of a circle, its radius and center.
We also provide some of the important operations that a circle
should perform, e.g. compute its area.
1 struct Circle {
Attributes
2 double radius;
3 double centerX, centerY;
capabilities
4 double a() { return radius*radius*PI; }
5 double area() { return a(); }
6 void setR(double r) { radius = (r >=0)? r : 0; }
7 };
L05: Object-based Programming 12
Using the new type definition
1 struct Circle {
2 double radius;
3 double centerX, centerY;
4 double a() { return radius*radius*PI; }
5 double area() { return a(); }
6 void setR(double r) { radius = (r >=0)? r : 0; }
7 };
8 void main() {
9 Circle c1;
10 c1.setR(5);
11 cout << "Area = " << c1.area() << endl;
12 }
Circle.h 1
Has-a relationship app.cpp has included Circle.h
#include "Circle.h" #include "Circle.h"
Circle.cpp 2
app.cpp 3
L06: Object based programming (2) 16
#include "Circle.h"
#include "Circle.h"
#include "Circle.h"
#include "Circle.h"
12 #endif
L06: Object based programming (2) 19
File 2: Circle.cpp source code
to be hidden
1 #include "Circle.h“ // saved as Circle.cpp
2 double Circle::a() { return radius*radius*PI; }
3 double Circle::area() { return a(); }
4 void Circle::setR(double r) { radius = (r >=0)? r : 0; }
5 void Circle::add2Circles(Circle c2) { radius += c2.radius; }
Circle.cpp app.cpp
User code
Circle.cpp app.cpp
User code
Compile
Circle.o app.o
Linking
app.exe
Circle.cpp app.cpp
User code
Compile
Circle.o app.o
Linking
app.exe
Circle.cpp app.cpp
User code
Compile
Circle.o app.o
Linking
app.exe
User: Most users just want to use Circle. Their concerns are
How expensive to get Circle.h and Circle.o?
How to use?
Circle.h should have contained enough information for
users to gain understanding of this type and its usage.
It is your job to provide these information in Circle.h and
any other additional files such as user manual.
L06: Object based programming (2) 25
Lecture 3: OBP
Run files in
folder “hide”
Circle.h 1
Has-a relationship app.cpp has included Circle.h
#include "Circle.h" #include "Circle.h"
Circle.cpp 2
app.cpp 3
Function Overloading
Overloading allows one name to represent two
functions or more.
Constructor Function
A constructor function is used to initialize the field of a
struct variable after it is allocated a piece of memory.