Professional Documents
Culture Documents
W3L1 Merged
W3L1 Merged
System Modeling
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
State
Initial State
Transition
Self transition
Chattering: the heater would turn on and off rapidly when the
temperature is close to the set-point temperature
Solution: hysteresis strategy, dwell time
where,
States is a finite set of states
Inputs is a set of input valuations
Outputs is a set of output valuations
update : States × Inputs → States × Outputs is an update
function, mapping a state and an input valuation to a next
state and an output valuation
initialState is the initial state
States = {0, 1, . . . , M}
Inputs = ({up, down} → {present, absent})
Outputs = ({count} → {0, 1, . . . , M, absent})
initialState = 0
Update function:
( (s + 1, s + 1) if s < M ∧ up ∧ ¬down
update(s, i) = (s − 1, s − 1) if s > 0 ∧ ¬up ∧ down
(s, absent) otherwise
All states that can be reached from the initial state on some
input sequence
Traffic Light:
Total number of states = 244
Total number of reachable states = 189
Indranil Saha
Indranil Saha
Indranil Saha
Indranil Saha
CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/6
Friend Function
A friend function of a class is defined outside that class’ scope but it has
the right to access all private and protected members of the class
Even though the prototypes for friend functions appear in the class
definition, friends are not member functions
A friend can be a class, in which case the entire class and all of its
members are friends
CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/6
Friend Function
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/6
Example: Friend Function
Friend function for the Box class
class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/6
Example: Friend Class
CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/6
Object-Oriented Programming Using C++
Friend Function
Indranil Saha
CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/6
Object-Oriented Programming Using C++
This Pointer and Pointer to a Class
Indranil Saha
CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/9
This Pointer
Friend functions do not have a this pointer, because friends are not
members of a class
CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/9
Example 1: This Pointer
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/9
Example 2: This Pointer
This pointer for the Box class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() > box.Volume();
}
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
} else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/9
Example 2: This Pointer
Output
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/9
Pointer to a Class
As with all pointers, you must initialize the pointer before using it
CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/9
Example: Pointer to a Class
Pointer to the Box class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
}
double Volume() {
return length * breadth * height;
}
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 7/9
Example: Pointer to a Class
Output
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102
CS253: Software Development and Operations Object-Oriented Programming Using C++ 8/9
Object-Oriented Programming Using C++
This Pointer and Pointer to a Class
Indranil Saha
CS253: Software Development and Operations Object-Oriented Programming Using C++ 9/9
Object-Oriented Programming Using C++
Static Class Members
Indranil Saha
CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/8
Static Member
All static data is initialized to zero when the first object is created, if no
other initialization is present
We can’t put it in the class definition but it can be initialized outside the
class by redeclaring the static variable, using the scope resolution
operator :: to identify which class it belongs to
CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/8
Example: Static Member
Destructor for the Line class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
// Increase every time object is created
objectCount++;
}
double Volume() {
return length * breadth * height;
}
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects.
cout << "Total objects: " << Box::objectCount << endl;
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/8
Example: Static Member
Output
Constructor called.
Constructor called.
Total objects: 2
CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/8
Static Member Function
The static functions are accessed using only the class name and the
scope resolution operator ::.
CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/8
Example: Static Member Function
Static Member Function for the Box class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
objectCount++;
}
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
};
int Box::objectCount = 0;
int main(void) {
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects after creating object.
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/8
Example: Static Member Function
Output
Initial Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2
CS253: Software Development and Operations Object-Oriented Programming Using C++ 7/8
Object-Oriented Programming Using C++
Static Class Members
Indranil Saha
CS253: Software Development and Operations Object-Oriented Programming Using C++ 8/8