0% found this document useful (0 votes)
11 views

Lecture 19

Uploaded by

itachiisgenius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 19

Uploaded by

itachiisgenius
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

CS 101:

Computer Programming and


Utilization

Puru
with
CS101 TAs and Staff

Course webpage: https://www.cse.iitb.ac.in/~cs101/

Lecture 19: more Structures, Classes and Objects

Autumn 2019 CS101@CSE IIT Bombay


Object Oriented Programming

A methodology for designing programs

Autumn 2019 CS101@CSE IIT Bombay 2


On Design
• Whenever you design something complex, it is useful to have a
plan
• Example:
Plan for designing a building:
− Understand the requirements
− Understand the constraints: budget, land area
− Plan how many floors to have
− What should be on each floor

• A plan/methodology is also useful when designing (large) projects


and similarly while designing programs

Autumn 2019 CS101@CSE IIT Bombay 3


Object Oriented Programming
• Understand what is required and write clear specifications (needed
in all methodologies)
• Identify the entities involved in the problem
E.g., in a library management program: books, patrons
• Identify the information associated with each entity
− Fixed information: name of the book
− Variable information (state): who has borrowed the book at
present

• Organize code so that the entities and their actions/inter


relationships are explicitly represented in the code
− Information associated with entities: structure variables
− Relationships/actions of entities: functions

Autumn 2019 CS101@CSE IIT Bombay 4


The C++ structure
• Member variables
− Basic facility provided in C++ to conveniently gather together
information associated with an entity
− Inherited from the C language

• Member functions
− New feature introduced in C++
− Actions/operations that effect the entity

− User defined data type with variables and functions

Autumn 2019 CS101@CSE IIT Bombay 5


Defining a structure type
General form
struct structure-type{
member1-type member1-name;
member2-type member2-name;
...
}; // Don’t forget the semicolon!

Example
struct Book{
char title[50];
double price;
};

A structure-type is a user-defined data type, just as int, char, double


are primitive data types
Structure-type name and member names can be any identifiers

Autumn 2019 CS101@CSE IIT Bombay 6


Nested structures (structure is a data type!)

struct Point{
double x,y;
};

struct Disk{
Point center; // contains Point
double radius;
};

Disk d;
d.radius = 10;
d.center = {15, 20};
// sets the x {member of center member of d

Autumn 2019 CS101@CSE IIT Bombay 7


Parameter Passing by Value
struct Point{double x, y;};

Point midpoint(Point a, Point b){


Point mp;
mp.x = (a.x + b.x)/2;
mp.y = (a.y + b.y)/2;
return mp;
}

int main(){
Point p={10,20}, q={50,60};
Point r = midpoint(p,q);
cout << r.x << endl;
cout << midpoint(p,q).x << endl;
}

Autumn 2019 CS101@CSE IIT Bombay 8


Parameter Passing by Reference
struct Point{double x, y;};

Point midpoint( const Point &a, const Point &b){


Point mp;
mp.x = (a.x + b.x)/2;
mp.y = (a.y + b.y)/2;
return mp;
}

int main(){
Point p={10,20}, q={50,60};
Point r = midpoint(p,q);
cout << r.x << endl;
}

Autumn 2019 CS101@CSE IIT Bombay 9


Using struct V3
V3 sum(const V3 &a, const V3 &b){
V3 v;
v.x = a.x + b.x; v.y = a.y + b.y; v.z = a.z + b.z;
return v;
}

V3 scale(const V3 &a, double f){


V3 v;
v.x = a.x * f; v.y = a.y * f; v.z = a.z * f;
return v;
}

double length(const V3 &v){


return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}

Autumn 2019 CS101@CSE IIT Bombay 10


Member functions
• It is not enough to just define a struct to hold vectors, usually we
will also define functions which work on structures/entiries

• In C++, you can make the functions a part of the struct definition
itself.
Such functions are called member functions.

• By collecting together relevant functions into the definition of the


struct, the code becomes better organized (object oriented!)

Autumn 2019 CS101@CSE IIT Bombay 11


The Complete Definition of V3
struct V3{
double x, y, z;
double length const(){
return sqrt(x*x + y*y + z*z);
}
V3 sum const(V3 b){
V3 v;
v.x = x+b.x; v.y=y+b.y; v.z=z+b.z;
return v;
}
V3 scale const(double f){
V3 v;
v.x = x*f; v.y = y*f; v.z = z*f;
return v;
}
}

Autumn 2019 CS101@CSE IIT Bombay 12


One More Example: Taxi Dispatch
• Problem statement: Clients arrive and have to be assigned to
(earliest waiting) taxies

• An important part of the solution was a blackboard on which we


wrote down the ids of the waiting taxies

• How would we implement this using OOP?


– Create a struct to represent each entity:
– customer, taxi, blackboard?

Autumn 2019 CS101@CSE IIT Bombay 13


The Queue structure
const int N=100;

struct Queue{
int elements[N], nwaiting,front;
bool insert(int v){

}

bool remove(int &v){



}
};

Autumn 2019 CS101@CSE IIT Bombay 14


Member Function Insert
• A value can be inserted only if the queue has space
• The value must be inserted into the next empty index in the queue
• The number of waiting elements in the queue is updated
• Return value indicates whether operation was successful

Autumn 2019 CS101@CSE IIT Bombay 15


Member Function Insert
• A value can be inserted only if the queue has space
• The value must be inserted into the next empty index in the queue
• The number of waiting elements in the queue is updated
• Return value indicates whether operation was successful

struct Queue{

bool insert(int v){
if(nWaiting >= N) return false;
elements[(front + nWaiting)%N] = v; nWaiting++;
return true;
}
};

Autumn 2019 CS101@CSE IIT Bombay 16


Member Function remove
• A value can be removed only if the queue is non-empty
• The value must be removed from the front of the queue. The front
should be incremented mod N
• The number of waiting elements in the queue is decremented by 1
• Return value indicates whether operation was successful

struct Queue{

bool remove(int &v){
if(nWaiting < 1) return false;
v=elements[front]; front=(front+1)%N; nWaiting--;
return true;
}
};

Autumn 2019 CS101@CSE IIT Bombay 17


Main Program
int main(){
Queue q;
q.front = q.nWaiting = 0;
while(true){
char c; cin >> c;
if(c == ‘d’){
int driver; cin >> driver;
if(!q.insert(driver)) cout <<“Q is full\n”;
}
else if(c == ‘c’){
int driver;
if(!q.remove(driver)) cout <<“No taxiavailable”;
else cout <<“Assigning <<driver<< endl;
}
}

Autumn 2019 CS101@CSE IIT Bombay 18


Remarks

• The member functions only contain the logic of how to


manage the queue

• The main program only contains the logic of dealing with


taxis and customers

• The new program has become simpler compared to the earlier


version, where the above two were mixed up together

Autumn 2019 CS101@CSE IIT Bombay 19


Structures vs Arrays
• User defined data type, collection • Collection of variables of single
of variables of multiple types data type
• Members can be accessed using • Members can be accessed using
the “.” operator the “[ ]” operator
• Structure name denotes the super • Array name denotes pointer to
variable consisting of the entire first element of array
collection of contained variables • Array elements need to be
• Structures can be copied using explicity copied
assignments. Also copied when • Array elements can be accessed
passed by value, or returned from by an expression whose value can
a function be computed at run time
• Member functions can be written
to represent actions of the entities
represented by the structure

Autumn 2019 CS101@CSE IIT Bombay 20


Objects As Software Components

• A software component can be built around a struct

• Just as a hardware component is useful for building big hardware


systems,
so is a software component for building large software systems

• A software component must be convenient to use, and also safe,


i.e., help in preventing programming errors

Autumn 2019 CS101@CSE IIT Bombay 21


Packaged software components
• Hardware devices that you buy from the market are packaged,
and made safe to use
– Fridge, television : no danger of getting an electric shock.
– A “control panel” is provided on the device. A user does not
have to change capacitor values to change the channel on a
television

Autumn 2019 CS101@CSE IIT Bombay 22


Packaged software components
• Analogous idea for software:
– Make functionality associated with a struct available to the
user only through member functions (control panel)
– Do not allow the user to directly access the data members
inside a struct. (Just as a user cannot touch the circuitry)
The user does not need to know what goes on inside
• If you build a better fridge but keep the control panel the same as
the previous model, the user does not need to relearn how to use
the new fridge
– If you build a better version of the struct, but keep the
member functions the same, the programs that use the
struct need not change
– Reusable, modular, abstract!
Autumn 2019 CS101@CSE IIT Bombay 23
The modern version of a struct

• Can behave like a packaged component


• Designer of the struct provides member functions
• Designer of the struct decides what happens during execution of
standard operations
• Once structs are designed in this manner, using them becomes
convenient and less error-prone
• Structs endowed with above features are more commonly called
objects

Autumn 2019 CS101@CSE IIT Bombay 24


The modern version of a struct

• Designer of the struct decides what happens during execution of


standard operations such as:
– Creation of the object
– Assignment
– Passing the object to a function
– Returning the object from a function
– Destroying the object when it is not needed

Autumn 2019 CS101@CSE IIT Bombay 25


Structures, Classes and Objects
• Constructors
• Copy Constructors
• Destructors
• Operator overloading
• Overloading the assignment operator
• Access control
• Classes
• Graphics and input/output classes

Autumn 2019 CS101@CSE IIT Bombay 26


The Queue Struct in Taxi Dispatch

const int N=100; • Once the queue is created,


we expect it to be used only
struct queue{
through the member
int elements[N], functions, insert and
nWaiting,front; remove
bool insert(int v){
• Ideally, we do not

expect/want elements,
} nWaiting, front to be
book remove(int &v){ directly accessed

}
};
Autumn 2019 CS101@CSE IIT Bombay 27
Main Program Using Queue
int main(){ • Main program does use q
Queue q;
through operations insert
q.front = q.nWaiting = 0;
and remove
while(true){
char c; cin >> c;
if(c == ‘d’){ • However, at the beginning,
int driver; cin >> driver; q.front and q.nWaiting are
if(!q.insert(driver)) directly manipulated
cout <<“Q is full\n”;
}
else if(c == ‘c’){ • Against the philosophy of
int driver; software packaging!
if(!q.remove(driver))
. . .

Autumn 2019 CS101@CSE IIT Bombay 28


The Constructor member function
• In C++, the programmer can define a special member function
called a constructor which will always be called when an instance
of the struct is created
• A constructor has the same name as the struct, and has no
return type

• Why useful?

Autumn 2019 CS101@CSE IIT Bombay 29


The Constructor member function
struct Queue{
• When q is created in the int elements[N], front,
main program, the nWaiting;
constructor is called Queue(){ // constructor
automatically nWaiting = 0;
front = 0;
}
// other member functions
};
int main(){
Queue q;
// no need to set
// q.nWaiting, q.front
// to 0.
}
Autumn 2019 CS101@CSE IIT Bombay 30
Constructors In General
struct A{ • Constructor can take arguments
… • The creation of the object x in
main can be thought of as
A(parameters){
happening in two steps

– Memory is allocated for x
}
– The constructor is called on x
}; with the given arguments

int main(){ • Many constructors possible,


A x(arguments); provided they have different
} signatures

Autumn 2019 CS101@CSE IIT Bombay 31


Another example: Constructor for V3
struct V3{ • When defining v1, an argument is
double x,y,z; given
V3(){ • So the constructor taking a single
x = y = z = 0;
argument is called. Thus each
}
component of v1 is set to 5
V3(double a){
x = y = z = a;
• When defining v2, no argument is
}
};
given.
• So the constructor taking no
int main(); arguments gets called. Thus each
V3 v1(5), v2; component of v2 is set to 0
}

Autumn 2019 CS101@CSE IIT Bombay 32


Remarks
• If and only if programmer does not define a constructor, will C++
define a constructor which takes no arguments, and does nothing
– If a constructor taking arguments is defined, you implicitly tell
C++ that you want programmers to give arguments.
– if some programmer does not give arguments, C++ will flag it as
an error
– If you want both kinds of initialization, define both kinds of
constructor
• A constructor that does not take arguments (defined programmer
or by C++) is called a default constructor
• If you define an array of struct, each element is initialized using
the default constructor

Autumn 2019 CS101@CSE IIT Bombay 33


The Copy Constructor
• Suppose an object is passed by value to a function
– It must be copied to the variable denoted by the parameter

• Suppose an object is returned by a function


– The value returned must be copied to a temporary variable in
the calling program

• By default the copying operations are implemented by copying


each member of one object to the corresponding member of the
other object
– this default behaviour can be changed by defining a copy
constructor

Autumn 2019 CS101@CSE IIT Bombay 34


Example
struct Queue{
int elements[N], nWaiting, front;

Queue(const Queue &source){ // Copy constructor


front = source.front;
nWaiting = source.nWaiting;

for(int i=front, j=0; j<nWaiting; j++){


elements[i] = source.elements[i];
i = (i+1) % N;
}

};

Autumn 2019 CS101@CSE IIT Bombay 35


Copy Constructor in the Example
• The copy constructor must take a single reference argument:
the object which is to be copied
• Note that the argument to the copy constructor must be a
reference, otherwise the copy constructor will have to be called to
copy the argument!
• This is will result in an unending recursion
• Member elements are not copied fully. Only the useful part of it is
copied struct Queue{
– More efficient int elements[N], nWaiting, front;
Queue(const Queue &source){
• More interesting use later // Copy constructor
……
}
};

Autumn 2019 CS101@CSE IIT Bombay 36


Tracking the use of constructors
struct Queue{
int copyID; Queue updateQueue(Queue q){
Queue(){ q.copyId = 3;
cout << "copyId=" << copyId; return q;
} }
Queue(const Queue &source){
int main(){
copyId = source.copyId;
Queue q;
cout << "copyId=" << copyId;
} q.copyId = 1;
};
Queue r(q);
What will be printed? r.copyId = 2;

Queue z=updateQueue(r);
}

Autumn 2019 CS101@CSE IIT Bombay 37


Destructors

• When control goes out of a block in which a variable is defined,

that variable is destroyed

– Memory allocated for that variable is reclaimed

• You can define a destructor function, which will get executed

before the memory is reclaimed

Autumn 2019 CS101@CSE IIT Bombay 38


Destructor Example
• If a queue that you have defined goes out of scope, it will be
destroyed
• If the queue contains elements at the time of destruction, it is
likely an error
• So you may want to print a message warning the user
• It is usually an error to call the destructor explicitly. It will be called
automatically when an object is to be destroyed. It should not get
called twice.
• More interesting uses of the destructor will be considered in later
chapters.

Autumn 2019 CS101@CSE IIT Bombay 39


Destructor Example
struct Queue{
int elements[N], nWaiting, front;
. . .

~Queue(){ //Destructor
if(nWaiting>0) cout << “Warning:”
<<“ non-empty queue being destroyed.”
<< endl;
}

};

Autumn 2019 CS101@CSE IIT Bombay 40


Operator Overloading
• In Mathematics, arithmetic operators are used with numbers, but
also other objects such as

Autumn 2019 CS101@CSE IIT Bombay 41


Operator Overloading
• In Mathematics, arithmetic operators are used with numbers, but
also other objects such as vectors
• Something like this is also possible in C++!

• An expression such as x @ y where @ is any “infix” operator is


considered by C++ to be equivalent to
x.operator@(y) in which operator@ is a member function

• If the member function operator@ is defined, then that is called to


execute x @ y

Autumn 2019 CS101@CSE IIT Bombay 42


Example: Arithmetic on V3 objects
struct V3{
double x, y, z;
V3(double a, double b, double c){
x=a; y=b; z=c;
}

V3 operator+(V3 b){ // adding two V3s


return V3(x+b.x, y+b.y, z+b.z); // constructor call
}

V3 operator*(double f){ // multiplying a V3 by f


return V3(x*f, y*f, z*f); // constructor call
}
};

Autumn 2019 CS101@CSE IIT Bombay 43


Using V3 Arithmetic

int main(){
V3 u(1,2,3), a(4,5,6), s;
double t=10;
s = u*t + a*t*t*0.5;
cout << s.x <<‘ ‘<< s.y <<‘ ‘
<< s.z << endl;
}

Autumn 2019 CS101@CSE IIT Bombay 44


Remarks

• Expression involving vectors can be made to look very much like


what you studied in Physics
• Other operators can also be overloaded, including unary operators
(see the book)
• Overload operators only if they have a natural interpretation for
the struct in question
• Otherwise you will confuse the reader of your program

Autumn 2019 CS101@CSE IIT Bombay 45


Pointers to Structures
• Disk d1={{2,3},4}, *dptr;

• *dptr is defined to have type Disk, so dptr is a pointer to a variable


of type Disk

• Normal pointer operations are allowed on structure pointers

• dptr = &d1;
• (*dptr).radius = 5; //changes the radius of d1

• Operator ->
– (*x).y is same as x->y
• dptr->radius = 5; // same effect as above

Autumn 2019 CS101@CSE IIT Bombay 46


Pointers as Structure Members

struct Disk2{
double radius;
Point *centerptr;
}

Point p={10,20};
Disk2 d;
d.centerptr = &p;
cout << d.centerptr->x << endl; // will print 10.

Autumn 2019 CS101@CSE IIT Bombay 47


The this Pointer
• So far, we have not provided a way to refer to the receiver itself inside the
definition of a member function.

• Within the body of a member function, the keyword this points to the receiver
i.e., the struct on which the member function has been invoked.

• Trivial use: write this->member instead of member directly

struct V3{
double x, y, z;
double length(){
return sqrt(this->x * this->x
+ this->y * this->y
+ this->z * this->z);
}
}

• More interesting use later.

Autumn 2019 CS101@CSE IIT Bombay 48


Overloading The Assignment Operator
• Normally if you assign one struct to another, each member of the
rhs is copied to the corresponding member of the lhs

• You can change this behaviour by defining member function


operator= for the struct

• A return type must be defined if you wish to allow chained


assignments, i.e., v1 = v2 = v3; which means v1 = (v2 = v3);
– The operation must return a reference to the left hand side
object

Autumn 2019 CS101@CSE IIT Bombay 49


Example
struct Queue{
...

Queue& operator=(Queue &rhs){


front = rhs.front;
nWaiting = rhs.nWaiting;

for(int i=0; i<nWaiting; i++){


elements[i] = rhs.elements[i];
i = (i+1) % N;
}
return *this;
}

};
// only the relevant elements are copied

Autumn 2019 CS101@CSE IIT Bombay 50


Access Control

• It is possible to restrict access to members or member functions of


a struct
• Members declared public: no restriction
• Members declared private: Can be accessed only inside the
definition of the struct
• Typical strategy:
Declare all data members to be private, and
some subset of function members to be public

Autumn 2019 CS101@CSE IIT Bombay 51


Access Control Example
struct Queue{
private:
int elements[N], nWaiting, front;

public:
Queue(){ … }
bool insert(int v){
..
}
bool remove(int &v){
..
}
};

Autumn 2019 CS101@CSE IIT Bombay 52


Remarks
• public:, private: : access specifiers

• An access specifier applies to all members defined following it,


until another specifier is given

• Thus elements, nWaiting, front are private, while Queue(), insert,


remove are public

Autumn 2019 CS101@CSE IIT Bombay 53


Remarks
• The default versions of the constructor, copy constructor,
destructor, assignment operator are public

• If you specify any of these as private, then they cannot be invoked


outside of the struct definition

• Thus if you make the copy constructor of a struct X private, then


you will get an error if you try to pass a struct of type X by value

• Thus, as a designer of a struct, you can exercise great control over


how the struct gets used

Autumn 2019 CS101@CSE IIT Bombay 54


Classes
• A class is essentially the same as a struct, except:
– Any members/member functions in a struct are public by
default
– Any members/member functions in a class are private by
default

Autumn 2019 CS101@CSE IIT Bombay 55


Classes
• Example: a Queue class:

class Queue{
int elements[N], nWaiting, front;
public:
Queue(){…}
bool remove(int &v){…}
bool insert(int v){…}
};

• The members - elements, nWaiting and front will be private.

Autumn 2019 CS101@CSE IIT Bombay 56


Example
struct V3{ struct V3{
double x,y,z; double x,y,z;
V3(double v){ V3(double v);
x = y = z = v; double X();
} };
double X(){
return x; //implementations
} V3::V3(double v){
}; x = y = z = v;
}
double V3::X(){
return x;
}

Autumn 2019 CS101@CSE IIT Bombay 57


Concluding Remarks
• The notion of a packaged software component is important.
• Making data members private: hiding the implementation from the
user
• Making some member functions public: providing an interface
using which the object can be used
• Separation of the concerns of the developer and the user
• Idea similar to what we discussed in connection with ordinary
functions
– The specification of the function must be clearly written down
(analogous to interface)
– The user should not worry about how the function does its
work (analogous to hiding data members)

Autumn 2019 CS101@CSE IIT Bombay 58


Input Output Classes
• cin, cout : objects of class istream, ostream resp. predefined in C++
• <<, >> : operators defined for the objects of these classes
• ifstream: another class like istream
• You create an object of class ifstream and associate it with a file on
your computer
• Now you can read from that file by invoking the >> operator!
• ofstream: a class like ostream, to be used for writing to files
• Must include header file <fstream> to uses ifstream and ofstream

Autumn 2019 CS101@CSE IIT Bombay 59


Example of file i/o
#include <fstream>
#include <simplecpp>
int main(){
ifstream infile(“f1.txt”);
// constructor call. object infile is created and associated
// with f1.txt, which must be present in the current directory
ofstream outfile(“f2.txt”);
// constructor call. Object outfile is created and associated
// with f2.txt, which will get created in the current directory

Autumn 2019 CS101@CSE IIT Bombay 60


Example of file i/o

repeat(10){
int v;
infile >> v;
outfile << v;
}
// f1.txt must begin with 10 numbers. These will be read and
// written to file f2.txt
}

Autumn 2019 CS101@CSE IIT Bombay 61


“String theory”

• Iterative computations are demonstrated well on


arrays
• strings … luckily the system manages the array
space for us
• Can assign and append to strings
• Can read a position: cout << message[px]
• Can write a position: message[px] = ‘q’
• That’s all we need for now

Autumn 2019 CS101@CSE IIT Bombay 62


Printing a string in reverse

string message;
getline(cin, message);
int mx = message.size()-1;
while (mx >= 0) { Character at
position mx in
cout << message[mx]; string
--mx; message
}
• mx updated in a completely predictable way
• Ideal candidate to write as for loop
Autumn 2019 CS101@CSE IIT Bombay 63
Finding needles in a haystack

• Given two strings, needles and haystack


• needles has no repeated characters
• haystack may repeat characters
• How many characters in needles appear in
haystack at least once?
• needles = “bat”, haystack = “tabla” à 3
• needles = “tab”, haystack = “bottle” à 2

Autumn 2019 CS101@CSE IIT Bombay 64


One needle in a haystack
• Subproblem: given one character ch and a string
find if ch appears in string at least once

char ch; // suitably initialized


string haystack; // suitably initialized
int ans = 0; // will change to 1 if found
for (int hx = 0; hx < haystack.size(); ++hx) {
if (ch == haystack[hx]) {
++ans;
break; // quit on first match
}
}

Autumn 2019 CS101@CSE IIT Bombay 65


Many needles: nested loop
main() {
string needles, haystack;
getline(cin, needles); getline(cin, haystack);
int ans = 0;
for (int nx=0; nx < needles.size(); ++nx) {
char ch = needles[nx];
for (int hx = 0; hx < haystack.size(); ++hx) {
if (ch == haystack[hx]) {
++ans;
break; // quit on first match
} Generalize to work in
} // ends haystack loop case needles can also
} // ends needles loop have repeated
} characters
Autumn 2019 CS101@CSE IIT Bombay 66
Duplicate needles

• needles = “bat”, haystack = “tabla” à 3


• needles = “tab”, haystack = “bottle” à 2
• needles = “bata”, haystack = “tabla” à 3
• Two approaches
– Dedup needles before executing earlier code (reducing
to known problem)
– Dedup Exercise:
needles “on
If the
the fly” (inside
input thehave
strings nx loop)
n and h
characters, at most how much time does
the needle-in-haystack search code take?

Autumn 2019 CS101@CSE IIT Bombay 67


Generalize to arbitrary lengths

• “Hello” < “Help” but “Hello” > “Hell”


• Scan both strings from the beginning
• If differing character found, same as before
• If a string ends, it is “less” than the other
int ans=0, ax=0, bx=0, an=as.size(), bn=bs.size();
for (; ans==0 && ax < an && bx < bn; ++ax, ++bx) {
if ( (ans = as[ax] – bs[bx]) != 0) break;
}
This results in an arbitrary integer as
if (ans == 0) { the return value in case of unequal
ans = an – bn; input string lengths, but the sign of
} ans is what is important
Autumn 2019 CS101@CSE IIT Bombay 68
break

while (true) {
ans += base/fac;
base *= x;
fac *= (++ix);
if (base/fac < epsilon) {
break; Terminates
} immediately
cout << (base/fac) << endl;enclosing
while loop
}
Autumn 2019 CS101@CSE IIT Bombay 69

You might also like